GaetanoParente's picture
fix bug visivo in seguito a cambio del modulo visualizzato
ef72586
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.
"""
# 1. NORMALIZZAZIONE INPUT (Il Fix Cruciale)
if isinstance(file_data, list):
if not file_data:
return [gr.update() for _ in range(5)]
file_path = file_data[0]
else:
# Se è None o stringa vuota
if not file_data:
return [gr.update() for _ in range(5)]
file_path = file_data # È già la stringa del percorso
ext = os.path.splitext(file_path)[1].lower()
# Valori di default (non cambiare nulla)
bpo_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', '.csv', '.log', '.md', '.json']:
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 Exception as e:
print(f"Errore lettura file: {e}")
return bpo_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 (più belli e moderni)
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 = "<div style='line-height: 2.2; font-family: sans-serif; font-size: 16px; color: #334155; background-color: #1e293b'>"
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"""
<span style='background-color: {c}20; border: 1px solid {c}; border-radius: 6px; padding: 2px 6px; margin: 0 2px; white-space: nowrap;'>
<span style='font-size: 0.75em; font-weight: 700; color: {c}; text-transform: uppercase;'>{label}</span>
<span style='font-weight: 600; color: white; margin-left: 6px;'>{text}</span>
</span>
"""
else:
# Testo normale
html += text.replace("\n", "<br>") # Gestisce a capo
html += "</div>"
return html