import json import gradio as gr import os from keras.preprocessing.text import Tokenizer from keras.preprocessing.text import tokenizer_from_json def load_doc(filename): # open the file as read only file = open(filename, 'r') # read all text text = file.read() # close the file file.close() return text def load_tokenizer(tokenizer_path): with open(tokenizer_path) as f: data = json.load(f) tokenizer = Tokenizer() tokenizer = tokenizer_from_json(data) return tokenizer #### utilità per interfaccia Gradio def global_file_loader(file_data): """ Gestisce il caricamento file dalla Sidebar. Supporta sia input stringa (Gradio 5 single) che lista. """ if isinstance(file_data, list): if not file_data: return [gr.update() for _ in range(6)] file_path = file_data[0] else: # Se è None o stringa vuota if not file_data: return [gr.update() for _ in range(6)] file_path = file_data # È già la stringa del percorso ext = os.path.splitext(file_path)[1].lower() # Valori di default bpo_update = gr.update() forecast_update = gr.update() img_xray_update = gr.update() img_retina_update = gr.update() news_update = gr.update() sentiment_update = gr.update() try: # LOGICA PER IMMAGINI if ext in ['.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff']: # Gradio accetta il path assoluto se è in allowed_paths img_xray_update = gr.Image(value=file_path) img_retina_update = gr.Image(value=file_path) # LOGICA PER TESTI elif ext in ['.txt', '.log', '.md', '.json']: with open(file_path, "r", encoding="utf-8") as f: content = f.read() elif ext in ['.csv']: forecast_update = gr.update(value=file_path) try: with open(file_path, "r", encoding="utf-8") as f: content = f.read() bpo_update = gr.Textbox(value=content) news_update = gr.Textbox(value=content) sentiment_update = gr.Textbox(value=content) except: pass except Exception as e: print(f"Errore lettura file: {e}") return bpo_update, forecast_update, img_xray_update, img_retina_update, news_update, sentiment_update def enable_sidebar(new_root): """Mostra la sidebar e aggiorna il path (Per Computer Vision)""" return gr.update(visible=True), gr.update(root_dir=new_root, value=[], interactive=True), None, None def disable_sidebar(): """Nasconde la sidebar (Per NLP)""" return gr.update(visible=False), gr.update(value=[]), None, None def render_ner_html(entities): """ Trasforma la lista [('testo', 'LABEL'), ('testo', None)] in HTML puro. """ # Mappa colori HEX colors = { "CODICE CLIENTE": "#3b82f6", # Blue 500 "N. FATTURA": "#f97316", # Orange 500 "COD. FORNITURA": "#d946ef", # Fuchsia 500 "EMAIL": "#ef4444", # Red 500 "TELEFONO": "#06b6d4", # Cyan 500 "PERSONA": "#22c55e", # Green 500 "AZIENDA": "#8b5cf6", # Violet 500 "LUOGO": "#64748b", # Slate 500 "POSSIBILE ID": "#a8a29e" # Stone 400 } html = "
" for text, label in entities: if label: # Recupera colore o usa grigio di default c = colors.get(label, "#1e293b") # Crea lo "Chip" (Pillola colorata) # - bg-color con opacità (c + '20') # - border solido # - label piccola in grassetto accanto al testo html += f""" {label} {text} """ else: # Testo normale html += text.replace("\n", "
") # Gestisce a capo html += "
" return html