π€ Punteggiatura Automatica Multilingua
Aggiungi automaticamente la punteggiatura al tuo testo usando AI avanzata
Supporta italiano, inglese, spagnolo, francese, tedesco e molte altre lingue
import gradio as gr from transformers import AutoTokenizer, AutoModelForTokenClassification import torch import logging import time # Configura il logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Carica il modello e tokenizer una sola volta logger.info("Caricamento modello e tokenizer...") start_time = time.time() try: tokenizer = AutoTokenizer.from_pretrained("oliverguhr/fullstop-punctuation-multilang-large") model = AutoModelForTokenClassification.from_pretrained("oliverguhr/fullstop-punctuation-multilang-large") logger.info(f"Modello caricato in {time.time() - start_time:.2f} secondi") except Exception as e: logger.error(f"Errore nel caricamento del modello: {str(e)}") raise RuntimeError("Impossibile caricare il modello") from e # Mappatura delle etichette di punteggiatura PUNCTUATION_MAP = { 0: "", # Nessuna punteggiatura 1: ".", # Punto 2: ",", # Virgola 3: "?", # Punto interrogativo 4: "-", # Trattino 5: ":" # Due punti } def restore_punctuation(text: str) -> str: """Ripristina la punteggiatura in un testo""" if not text or not text.strip(): return text try: logger.info(f"Elaborazione testo: '{text}'") # Tokenizza il testo inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512) # Esegui la predizione with torch.no_grad(): outputs = model(**inputs) # Ottieni le predizioni predictions = torch.argmax(outputs.logits, dim=2) # Converti i token tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) labels = predictions[0].tolist() # Ricostruisci il testo parola per parola words = [] current_word = "" word_labels = [] for token, label in zip(tokens, labels): # Salta i token speciali if token in [tokenizer.cls_token, tokenizer.sep_token, tokenizer.pad_token]: continue if token.startswith("β"): # Carattere di inizio parola # Processa la parola precedente if current_word: process_word(words, current_word, word_labels) current_word = "" word_labels = [] # Inizia nuova parola current_word = token[1:] word_labels.append(label) else: # Continua la parola corrente current_word += token word_labels.append(label) # Processa l'ultima parola if current_word: process_word(words, current_word, word_labels) result = " ".join(words) logger.info(f"Risultato: '{result}'") return result except Exception as e: logger.error(f"Errore durante la punteggiatura: {str(e)}") return f"Errore nell'elaborazione: {str(e)}" def process_word(words: list, word: str, labels: list): """Aggiunge la punteggiatura appropriata a una parola""" # Trova la label di punteggiatura piΓΉ comune (escludendo nessuna punteggiatura) non_zero_labels = [l for l in labels if l != 0] if non_zero_labels: # Prendi la label piΓΉ frequente label_counts = {label: non_zero_labels.count(label) for label in set(non_zero_labels)} most_common_label = max(label_counts, key=label_counts.get) punctuation = PUNCTUATION_MAP.get(most_common_label, "") words.append(word + punctuation) else: words.append(word) # Crea l'interfaccia usando Gradio 5 def create_gradio5_interface(): """Crea l'interfaccia ottimizzata per Gradio 5""" # Esempi predefiniti examples = [ "ciao come stai", "buongiorno a tutti", "mi chiamo marco piacere di conoscerti", "que horas son en madrid", "bonjour comment allez vous aujourdhui", "hello how are you today", "what time is it now", "ich liebe diese musik sehr" ] # Crea l'interfaccia principale with gr.Blocks( title="π€ Punteggiatura Automatica Multilingua", theme=gr.themes.Soft(), css=""" .container { max-width: 1200px; margin: 0 auto; } .main-header { text-align: center; margin-bottom: 2rem; } .example-box { background: #f8f9fa; border-radius: 10px; padding: 1rem; margin: 1rem 0; } """ ) as demo: # Header gr.HTML("""
Aggiungi automaticamente la punteggiatura al tuo testo usando AI avanzata
Supporta italiano, inglese, spagnolo, francese, tedesco e molte altre lingue
π€ Powered by: oliverguhr/fullstop-punctuation-multilang-large
Modello di AI specializzato nel ripristino della punteggiatura multilingua