File size: 4,439 Bytes
2ff9250
3a24a7b
 
 
 
2ff9250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a24a7b
 
 
 
 
 
 
 
 
 
 
4bdde62
3a24a7b
 
 
 
4bdde62
3a24a7b
 
 
 
b9c5837
3a24a7b
4bdde62
3a24a7b
 
 
 
 
 
 
 
 
 
 
 
 
4bdde62
3a24a7b
 
4bdde62
 
 
b9c5837
4bdde62
 
 
 
 
 
 
 
 
3a24a7b
 
 
 
4bdde62
3a24a7b
 
 
ef72586
3a24a7b
 
 
ef72586
3a24a7b
 
 
 
 
b9c5837
3a24a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 = "<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