Spaces:
Sleeping
Sleeping
ristrutturazione file
Browse filesdivisione file app.py in file più piccoli per ogni funzione
- app.py +31 -333
- app_3.py +0 -275
- logging_config.py +40 -0
- ui/chatbot_tab.py +99 -0
- ui/db_management_tab.py +68 -0
- ui/document_management_tab.py +92 -0
- ui/document_view_tab.py +41 -0
- ui/new_features_tab.py +42 -0
- utils/__init__.py +0 -0
- utils/helpers.py +28 -0
app.py
CHANGED
@@ -1,342 +1,40 @@
|
|
1 |
-
|
2 |
-
import logging
|
3 |
|
4 |
-
|
5 |
-
# - list_databases(), create_database(), modify_database(), delete_database()...
|
6 |
-
# - list_indexed_files(), upload_and_index(), delete_file_from_database(), etc.
|
7 |
-
# - search_documents(), list_indexed_documents()...
|
8 |
-
#
|
9 |
-
# Se hanno nomi o posizioni diverse, adatta gli import di conseguenza
|
10 |
-
from app.document_handling import (
|
11 |
-
list_databases,
|
12 |
-
create_database,
|
13 |
-
modify_database,
|
14 |
-
delete_database,
|
15 |
-
upload_and_index,
|
16 |
-
list_indexed_files,
|
17 |
-
delete_file_from_database,
|
18 |
-
list_indexed_documents,
|
19 |
-
search_documents,
|
20 |
-
)
|
21 |
-
from app.llm_handling import answer_question
|
22 |
from app.logging_config import configure_logging
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
configure_logging()
|
25 |
|
26 |
-
def
|
27 |
-
"""
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def extract_text_from_files(files):
|
33 |
-
"""Estrae e concatena il testo da PDF, DOCX e TXT."""
|
34 |
-
text = ""
|
35 |
-
for file in files:
|
36 |
-
try:
|
37 |
-
if file.name.endswith('.pdf'):
|
38 |
-
text += extract_text_from_pdf(file.name) # Definita in document_handling
|
39 |
-
elif file.name.endswith('.docx'):
|
40 |
-
text += extract_text_from_docx(file.name) # Definita in document_handling
|
41 |
-
else:
|
42 |
-
with open(file.name, 'r', encoding='utf-8') as f:
|
43 |
-
text += f.read()
|
44 |
-
except Exception as e:
|
45 |
-
logging.error(f"Errore durante la lettura del file {file.name}: {e}")
|
46 |
-
return text
|
47 |
-
|
48 |
-
|
49 |
-
with gr.Blocks() as rag_chatbot:
|
50 |
-
gr.Markdown("# Chatbot basato su RAG")
|
51 |
-
|
52 |
databases = list_databases()
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
db_name_chat = gr.State()
|
59 |
-
db_name_new = gr.State()
|
60 |
-
modify_db_old_name = gr.State()
|
61 |
-
delete_db_dropdown = gr.State()
|
62 |
-
|
63 |
-
# =============================================
|
64 |
-
# TAB: Chatbot
|
65 |
-
# =============================================
|
66 |
-
with gr.Tab("Chatbot"):
|
67 |
-
with gr.Row():
|
68 |
-
with gr.Column(scale=2):
|
69 |
-
# Dropdown per selezionare il DB
|
70 |
-
db_name_chat = gr.Dropdown(
|
71 |
-
choices=databases,
|
72 |
-
label="Seleziona Database",
|
73 |
-
value="default_db"
|
74 |
-
)
|
75 |
-
|
76 |
-
# Chatbot component
|
77 |
-
chatbot = gr.Chatbot(label="Conversazione", type="messages")
|
78 |
-
|
79 |
-
# Input domanda
|
80 |
-
question_input = gr.Textbox(
|
81 |
-
label="Fai una domanda",
|
82 |
-
placeholder="Scrivi qui la tua domanda...",
|
83 |
-
lines=2
|
84 |
-
)
|
85 |
-
# Bottoni azione
|
86 |
-
with gr.Row():
|
87 |
-
ask_button = gr.Button("Invia")
|
88 |
-
clear_button = gr.Button("Pulisci Chat")
|
89 |
-
|
90 |
-
# File upload con dimensioni ridotte
|
91 |
-
with gr.Row():
|
92 |
-
file_input = gr.File(
|
93 |
-
label="Carica PDF/Docx/TXT per la conversazione",
|
94 |
-
file_types=[".pdf", ".docx", ".txt"],
|
95 |
-
file_count="multiple",
|
96 |
-
height="100px", # Altezza ridotta
|
97 |
-
scale=3 # Riduce la larghezza relativa
|
98 |
-
)
|
99 |
-
upload_button = gr.Button("Carica Documenti", scale=1)
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
# Stato chat
|
104 |
-
chat_state = gr.State([])
|
105 |
-
|
106 |
-
# ----------------------
|
107 |
-
# FUNZIONI DI CALLBACK
|
108 |
-
# ----------------------
|
109 |
-
def chat_upload_and_respond(files, chat_history, db_name):
|
110 |
-
# Se chat_history è None, inizializziamo
|
111 |
-
if chat_history is None:
|
112 |
-
chat_history = []
|
113 |
-
|
114 |
-
# Estrai il testo dai file
|
115 |
-
text = extract_text_from_files(files)
|
116 |
-
|
117 |
-
# Aggiungo un messaggio "assistant" che mostra il testo caricato
|
118 |
-
chat_history.append({
|
119 |
-
"role": "assistant",
|
120 |
-
"content": f"📄 Contenuto dei documenti caricati:\n{text}"
|
121 |
-
})
|
122 |
-
|
123 |
-
return chat_history
|
124 |
-
|
125 |
-
def respond(message, chat_history, db_name):
|
126 |
-
if chat_history is None:
|
127 |
-
chat_history = []
|
128 |
-
|
129 |
-
# `answer_question` restituisce due messaggi (user + assistant) in lista
|
130 |
-
new_messages = answer_question(message, db_name)
|
131 |
-
|
132 |
-
# Li aggiungiamo in coda alla history
|
133 |
-
chat_history.extend(new_messages)
|
134 |
-
|
135 |
-
# Ritorniamo l'input svuotato (per pulire il Textbox) e la nuova history
|
136 |
-
return "", chat_history
|
137 |
-
|
138 |
-
def clear_chat():
|
139 |
-
# Svuota la chat
|
140 |
-
return [], []
|
141 |
-
|
142 |
-
# ------------------
|
143 |
-
# EVENTI BOTTONE
|
144 |
-
# ------------------
|
145 |
-
upload_button.click(
|
146 |
-
fn=chat_upload_and_respond,
|
147 |
-
inputs=[file_input, chat_state, db_name_chat],
|
148 |
-
outputs=chatbot
|
149 |
-
)
|
150 |
-
|
151 |
-
ask_button.click(
|
152 |
-
fn=respond,
|
153 |
-
inputs=[question_input, chat_state, db_name_chat],
|
154 |
-
outputs=[question_input, chatbot]
|
155 |
-
)
|
156 |
-
|
157 |
-
clear_button.click(
|
158 |
-
fn=clear_chat,
|
159 |
-
outputs=[chatbot, chat_state]
|
160 |
-
)
|
161 |
-
|
162 |
-
|
163 |
-
# =============================================
|
164 |
-
# TAB: Gestione Database
|
165 |
-
# =============================================
|
166 |
-
with gr.Tab("Gestione Database"):
|
167 |
-
gr.Markdown("## Operazioni sui Database")
|
168 |
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
modify_db_old_name = gr.Dropdown(choices=databases, label="Database da Rinominare")
|
179 |
-
modify_db_new_name = gr.Textbox(label="Nuovo Nome")
|
180 |
-
modify_db_button = gr.Button("Rinomina Database")
|
181 |
-
modify_output = gr.Textbox(label="Stato Modifica")
|
182 |
-
|
183 |
-
with gr.Column():
|
184 |
-
gr.Markdown("### Elimina Database")
|
185 |
-
delete_db_dropdown = gr.Dropdown(choices=databases, label="Database da Eliminare")
|
186 |
-
delete_db_button = gr.Button("Elimina Database")
|
187 |
-
delete_output = gr.Textbox(label="Stato Eliminazione")
|
188 |
-
|
189 |
-
# Eventi per i pulsanti di gestione DB
|
190 |
-
create_db_button.click(
|
191 |
-
create_database, # funzione
|
192 |
-
inputs=db_name_input, # input
|
193 |
-
outputs=create_output # output
|
194 |
-
).then(
|
195 |
-
update_dropdowns,
|
196 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
197 |
-
)
|
198 |
-
|
199 |
-
modify_db_button.click(
|
200 |
-
modify_database,
|
201 |
-
inputs=[modify_db_old_name, modify_db_new_name],
|
202 |
-
outputs=modify_output
|
203 |
-
).then(
|
204 |
-
update_dropdowns,
|
205 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
206 |
-
)
|
207 |
-
|
208 |
-
delete_db_button.click(
|
209 |
-
delete_database,
|
210 |
-
inputs=delete_db_dropdown,
|
211 |
-
outputs=delete_output
|
212 |
-
).then(
|
213 |
-
update_dropdowns,
|
214 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
215 |
-
)
|
216 |
-
|
217 |
-
|
218 |
-
# =============================================
|
219 |
-
# TAB: Gestione Documenti
|
220 |
-
# =============================================
|
221 |
-
with gr.Tab("Gestione Documenti"):
|
222 |
-
with gr.Column():
|
223 |
-
gr.Markdown("### Carica Documenti")
|
224 |
-
with gr.Row():
|
225 |
-
file_input = gr.File(
|
226 |
-
label="Carica i tuoi documenti",
|
227 |
-
file_types=[".txt", ".pdf", ".docx"],
|
228 |
-
file_count="multiple"
|
229 |
-
)
|
230 |
-
db_name_upload = gr.Dropdown(
|
231 |
-
choices=databases,
|
232 |
-
label="Seleziona Database",
|
233 |
-
value="default_db"
|
234 |
-
)
|
235 |
-
|
236 |
-
with gr.Row():
|
237 |
-
title_input = gr.Textbox(label="Titolo del documento")
|
238 |
-
author_input = gr.Textbox(label="Autore")
|
239 |
-
|
240 |
-
upload_button = gr.Button("Indicizza Documenti")
|
241 |
-
upload_output = gr.Textbox(label="Stato Upload")
|
242 |
-
|
243 |
-
with gr.Column():
|
244 |
-
gr.Markdown("### Documenti nel Database")
|
245 |
-
db_name_list = gr.Dropdown(
|
246 |
-
choices=databases,
|
247 |
-
label="Seleziona Database",
|
248 |
-
value="default_db"
|
249 |
-
)
|
250 |
-
list_button = gr.Button("Visualizza Files")
|
251 |
-
list_output = gr.Textbox(label="Files nel Database")
|
252 |
-
delete_file_input = gr.Textbox(label="Nome file da eliminare")
|
253 |
-
delete_file_button = gr.Button("Elimina File")
|
254 |
-
delete_file_output = gr.Textbox(label="Stato Eliminazione")
|
255 |
-
|
256 |
-
# Eventi
|
257 |
-
upload_button.click(
|
258 |
-
upload_and_index,
|
259 |
-
inputs=[file_input, title_input, author_input, db_name_upload],
|
260 |
-
outputs=upload_output
|
261 |
-
).then(
|
262 |
-
list_indexed_files,
|
263 |
-
inputs=db_name_list,
|
264 |
-
outputs=list_output
|
265 |
-
)
|
266 |
-
|
267 |
-
list_button.click(
|
268 |
-
list_indexed_files,
|
269 |
-
inputs=db_name_list,
|
270 |
-
outputs=list_output
|
271 |
-
)
|
272 |
-
|
273 |
-
delete_file_button.click(
|
274 |
-
delete_file_from_database,
|
275 |
-
inputs=[delete_file_input, db_name_list],
|
276 |
-
outputs=delete_file_output
|
277 |
-
).then(
|
278 |
-
list_indexed_files,
|
279 |
-
inputs=db_name_list,
|
280 |
-
outputs=list_output
|
281 |
-
).then(
|
282 |
-
update_dropdowns,
|
283 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
284 |
-
)
|
285 |
-
|
286 |
-
|
287 |
-
# =============================================
|
288 |
-
# TAB: Visualizza Documenti Indicizzati
|
289 |
-
# =============================================
|
290 |
-
with gr.Tab("Visualizza Documenti Indicizzati"):
|
291 |
-
with gr.Column():
|
292 |
-
gr.Markdown("### Documenti nel Database")
|
293 |
-
db_name_list = gr.Dropdown(
|
294 |
-
choices=databases,
|
295 |
-
label="Seleziona Database",
|
296 |
-
value="default_db",
|
297 |
-
interactive=True
|
298 |
-
)
|
299 |
-
list_button = gr.Button("Visualizza Documenti")
|
300 |
-
list_output = gr.Textbox(
|
301 |
-
label="Elenco Documenti",
|
302 |
-
lines=10,
|
303 |
-
interactive=False,
|
304 |
-
value="Clicca 'Visualizza Documenti' per vedere l'elenco"
|
305 |
-
)
|
306 |
-
|
307 |
-
list_button.click(
|
308 |
-
fn=list_indexed_documents,
|
309 |
-
inputs=[db_name_list],
|
310 |
-
outputs=[list_output],
|
311 |
-
api_name="list_docs"
|
312 |
-
)
|
313 |
-
|
314 |
-
# =============================================
|
315 |
-
# TAB: Nuove Funzionalità
|
316 |
-
# =============================================
|
317 |
-
with gr.Tab("Nuove Funzionalità"):
|
318 |
-
gr.Markdown("## Cerca Documenti e Genera Riassunto")
|
319 |
-
|
320 |
-
db_name_new = gr.Dropdown(choices=databases, label="Seleziona Database", value="default_db")
|
321 |
-
search_input = gr.Textbox(label="Inserisci Termine di Ricerca")
|
322 |
-
search_button = gr.Button("Cerca Documenti")
|
323 |
-
search_output = gr.Textbox(label="Documenti Trovati")
|
324 |
-
|
325 |
-
summary_button = gr.Button("Genera Riassunto")
|
326 |
-
summary_output = gr.Textbox(label="Riassunto")
|
327 |
-
|
328 |
-
search_button.click(
|
329 |
-
search_documents,
|
330 |
-
inputs=[search_input, db_name_new],
|
331 |
-
outputs=search_output
|
332 |
-
)
|
333 |
-
# Esempio di eventuale generazione riassunto
|
334 |
-
# summary_button.click(
|
335 |
-
# generate_summary,
|
336 |
-
# inputs=db_name_new,
|
337 |
-
# outputs=summary_output
|
338 |
-
# )
|
339 |
|
340 |
-
# Avvio dell'app
|
341 |
if __name__ == "__main__":
|
342 |
-
|
|
|
1 |
+
# main.py
|
|
|
2 |
|
3 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from app.logging_config import configure_logging
|
5 |
+
from app.document_handling import list_databases
|
6 |
+
from ui.chatbot_tab import create_chatbot_tab
|
7 |
+
from ui.db_management_tab import create_db_management_tab
|
8 |
+
from ui.document_management_tab import create_document_management_tab
|
9 |
+
from ui.document_view_tab import create_document_view_tab
|
10 |
+
from ui.new_features_tab import create_new_features_tab
|
11 |
+
|
12 |
+
# Configura il logging
|
13 |
configure_logging()
|
14 |
|
15 |
+
def update_all_dropdowns():
|
16 |
+
"""
|
17 |
+
Aggiorna tutti i dropdown con la lista aggiornata dei database.
|
18 |
+
Questo potrebbe essere esteso per aggiornare tutti i dropdown necessari.
|
19 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
databases = list_databases()
|
21 |
+
updates = [gr.update(choices=databases) for _ in range(6)]
|
22 |
+
return updates
|
23 |
|
24 |
+
def main():
|
25 |
+
"""Funzione principale che crea e lancia l'app Gradio."""
|
26 |
+
with gr.Blocks() as rag_chatbot:
|
27 |
+
gr.Markdown("# Chatbot basato su RAG")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Crea i vari tab dell'interfaccia
|
30 |
+
create_chatbot_tab()
|
31 |
+
create_db_management_tab(update_all_dropdowns)
|
32 |
+
create_document_management_tab(update_all_dropdowns)
|
33 |
+
create_document_view_tab()
|
34 |
+
create_new_features_tab()
|
35 |
+
|
36 |
+
# Avvia l'app
|
37 |
+
rag_chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
39 |
if __name__ == "__main__":
|
40 |
+
main()
|
app_3.py
DELETED
@@ -1,275 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from app.document_handling import *
|
3 |
-
from app.llm_handling import answer_question
|
4 |
-
from app.logging_config import configure_logging
|
5 |
-
|
6 |
-
configure_logging()
|
7 |
-
|
8 |
-
def update_dropdowns():
|
9 |
-
"""Aggiorna tutti i dropdown con la lista aggiornata dei database"""
|
10 |
-
databases = list_databases()
|
11 |
-
return [gr.update(choices=databases) for _ in range(6)]
|
12 |
-
|
13 |
-
def extract_text_from_files(files):
|
14 |
-
text = ""
|
15 |
-
for file in files:
|
16 |
-
try:
|
17 |
-
if file.name.endswith('.pdf'):
|
18 |
-
text += extract_text_from_pdf(file.name)
|
19 |
-
elif file.name.endswith('.docx'):
|
20 |
-
text += extract_text_from_docx(file.name)
|
21 |
-
else:
|
22 |
-
with open(file.name, 'r', encoding='utf-8') as f:
|
23 |
-
text += f.read()
|
24 |
-
except Exception as e:
|
25 |
-
logging.error(f"Errore durante la lettura del file {file.name}: {e}")
|
26 |
-
return text
|
27 |
-
|
28 |
-
with gr.Blocks() as rag_chatbot:
|
29 |
-
gr.Markdown("# Chatbot basato su RAG")
|
30 |
-
|
31 |
-
databases = list_databases()
|
32 |
-
|
33 |
-
# Definizione dei dropdown prima del loro utilizzo
|
34 |
-
db_name_upload = gr.State()
|
35 |
-
db_name_list = gr.State()
|
36 |
-
db_name_chat = gr.State()
|
37 |
-
db_name_new = gr.State()
|
38 |
-
modify_db_old_name = gr.State()
|
39 |
-
delete_db_dropdown = gr.State()
|
40 |
-
|
41 |
-
|
42 |
-
with gr.Tab("Chatbot"):
|
43 |
-
with gr.Row():
|
44 |
-
with gr.Column(scale=2):
|
45 |
-
db_name_chat = gr.Dropdown(choices=databases, label="Seleziona Database", value="default_db")
|
46 |
-
# Aggiornato il tipo del chatbot
|
47 |
-
chatbot = gr.Chatbot(label="Conversazione", type="messages")
|
48 |
-
|
49 |
-
with gr.Row():
|
50 |
-
# Aggiunta upload file direttamente nella chat
|
51 |
-
file_input = gr.File(
|
52 |
-
label="Carica PDF per la conversazione",
|
53 |
-
file_types=[".pdf", ".docx", ".txt"],
|
54 |
-
file_count="multiple"
|
55 |
-
)
|
56 |
-
upload_button = gr.Button("Carica Documenti")
|
57 |
-
|
58 |
-
question_input = gr.Textbox(
|
59 |
-
label="Fai una domanda",
|
60 |
-
placeholder="Scrivi qui la tua domanda...",
|
61 |
-
lines=2
|
62 |
-
)
|
63 |
-
|
64 |
-
with gr.Row():
|
65 |
-
ask_button = gr.Button("Invia")
|
66 |
-
clear_button = gr.Button("Pulisci Chat")
|
67 |
-
|
68 |
-
chat_state = gr.State([])
|
69 |
-
|
70 |
-
def chat_upload_and_respond(files, chat_history, db_name):
|
71 |
-
# Estrai il testo dai file
|
72 |
-
text = extract_text_from_files(files)
|
73 |
-
|
74 |
-
# Aggiungi il testo alla chat come messaggio dell'utente
|
75 |
-
chat_history.append((None, "📄 Contenuto dei documenti caricati:"))
|
76 |
-
chat_history.append((None, text))
|
77 |
-
|
78 |
-
return chat_history
|
79 |
-
|
80 |
-
def respond(message, chat_history, db_name):
|
81 |
-
bot_message = answer_question(message, db_name)
|
82 |
-
chat_history.append((message, bot_message))
|
83 |
-
return "", chat_history
|
84 |
-
|
85 |
-
def clear_chat():
|
86 |
-
return [], []
|
87 |
-
|
88 |
-
# Eventi
|
89 |
-
upload_button.click(
|
90 |
-
chat_upload_and_respond,
|
91 |
-
inputs=[file_input, chat_state, db_name_chat],
|
92 |
-
outputs=[chatbot]
|
93 |
-
)
|
94 |
-
|
95 |
-
ask_button.click(
|
96 |
-
respond,
|
97 |
-
inputs=[question_input, chat_state, db_name_chat],
|
98 |
-
outputs=[question_input, chatbot]
|
99 |
-
)
|
100 |
-
|
101 |
-
clear_button.click(
|
102 |
-
clear_chat,
|
103 |
-
outputs=[chatbot, chat_state]
|
104 |
-
)
|
105 |
-
|
106 |
-
with gr.Tab("Gestione Database"):
|
107 |
-
gr.Markdown("## Operazioni sui Database")
|
108 |
-
|
109 |
-
with gr.Row():
|
110 |
-
with gr.Column():
|
111 |
-
gr.Markdown("### Crea Database")
|
112 |
-
db_name_input = gr.Textbox(label="Nome Nuovo Database")
|
113 |
-
create_db_button = gr.Button("Crea Database")
|
114 |
-
create_output = gr.Textbox(label="Stato Creazione")
|
115 |
-
|
116 |
-
with gr.Column():
|
117 |
-
gr.Markdown("### Rinomina Database")
|
118 |
-
modify_db_old_name = gr.Dropdown(choices=databases, label="Database da Rinominare")
|
119 |
-
modify_db_new_name = gr.Textbox(label="Nuovo Nome")
|
120 |
-
modify_db_button = gr.Button("Rinomina Database")
|
121 |
-
modify_output = gr.Textbox(label="Stato Modifica")
|
122 |
-
|
123 |
-
with gr.Column():
|
124 |
-
gr.Markdown("### Elimina Database")
|
125 |
-
delete_db_dropdown = gr.Dropdown(choices=databases, label="Database da Eliminare")
|
126 |
-
delete_db_button = gr.Button("Elimina Database")
|
127 |
-
delete_output = gr.Textbox(label="Stato Eliminazione")
|
128 |
-
|
129 |
-
# Eventi per i pulsanti di gestione database
|
130 |
-
create_db_button.click(
|
131 |
-
create_database,
|
132 |
-
inputs=db_name_input,
|
133 |
-
outputs=create_output
|
134 |
-
).then(
|
135 |
-
update_dropdowns,
|
136 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
137 |
-
)
|
138 |
-
|
139 |
-
modify_db_button.click(
|
140 |
-
modify_database,
|
141 |
-
inputs=[modify_db_old_name, modify_db_new_name],
|
142 |
-
outputs=modify_output
|
143 |
-
).then(
|
144 |
-
update_dropdowns,
|
145 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
146 |
-
)
|
147 |
-
|
148 |
-
delete_db_button.click(
|
149 |
-
delete_database,
|
150 |
-
inputs=delete_db_dropdown,
|
151 |
-
outputs=delete_output
|
152 |
-
).then(
|
153 |
-
update_dropdowns,
|
154 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
155 |
-
)
|
156 |
-
|
157 |
-
with gr.Tab("Gestione Documenti"):
|
158 |
-
with gr.Column():
|
159 |
-
gr.Markdown("### Carica Documenti")
|
160 |
-
with gr.Row():
|
161 |
-
file_input = gr.File(
|
162 |
-
label="Carica i tuoi documenti",
|
163 |
-
file_types=[".txt", ".pdf", ".docx"],
|
164 |
-
file_count="multiple"
|
165 |
-
)
|
166 |
-
db_name_upload = gr.Dropdown(
|
167 |
-
choices=databases,
|
168 |
-
label="Seleziona Database",
|
169 |
-
value="default_db"
|
170 |
-
)
|
171 |
-
|
172 |
-
with gr.Row():
|
173 |
-
title_input = gr.Textbox(label="Titolo del documento")
|
174 |
-
author_input = gr.Textbox(label="Autore")
|
175 |
-
|
176 |
-
upload_button = gr.Button("Indicizza Documenti")
|
177 |
-
upload_output = gr.Textbox(label="Stato Upload")
|
178 |
-
|
179 |
-
with gr.Column():
|
180 |
-
gr.Markdown("### Documenti nel Database")
|
181 |
-
db_name_list = gr.Dropdown(
|
182 |
-
choices=databases,
|
183 |
-
label="Seleziona Database",
|
184 |
-
value="default_db"
|
185 |
-
)
|
186 |
-
list_button = gr.Button("Visualizza Files")
|
187 |
-
list_output = gr.Textbox(label="Files nel Database")
|
188 |
-
delete_file_input = gr.Textbox(label="Nome file da eliminare")
|
189 |
-
delete_file_button = gr.Button("Elimina File")
|
190 |
-
delete_file_output = gr.Textbox(label="Stato Eliminazione")
|
191 |
-
|
192 |
-
# Eventi modificati
|
193 |
-
upload_button.click(
|
194 |
-
upload_and_index,
|
195 |
-
inputs=[file_input, title_input, author_input, db_name_upload],
|
196 |
-
outputs=upload_output
|
197 |
-
).then(
|
198 |
-
list_indexed_files,
|
199 |
-
inputs=db_name_list,
|
200 |
-
outputs=list_output
|
201 |
-
)
|
202 |
-
|
203 |
-
list_button.click(
|
204 |
-
list_indexed_files,
|
205 |
-
inputs=db_name_list,
|
206 |
-
outputs=list_output
|
207 |
-
)
|
208 |
-
|
209 |
-
delete_file_button.click(
|
210 |
-
delete_file_from_database,
|
211 |
-
inputs=[delete_file_input, db_name_list],
|
212 |
-
outputs=delete_file_output
|
213 |
-
).then(
|
214 |
-
list_indexed_files,
|
215 |
-
inputs=db_name_list,
|
216 |
-
outputs=list_output
|
217 |
-
).then(
|
218 |
-
update_dropdowns,
|
219 |
-
outputs=[db_name_upload, db_name_list, db_name_chat, db_name_new, modify_db_old_name, delete_db_dropdown]
|
220 |
-
)
|
221 |
-
|
222 |
-
with gr.Tab("Visualizza Documenti Indicizzati"):
|
223 |
-
with gr.Column():
|
224 |
-
gr.Markdown("### Documenti nel Database")
|
225 |
-
db_name_list = gr.Dropdown(
|
226 |
-
choices=databases,
|
227 |
-
label="Seleziona Database",
|
228 |
-
value="default_db",
|
229 |
-
interactive=True
|
230 |
-
)
|
231 |
-
list_button = gr.Button("Visualizza Documenti")
|
232 |
-
list_output = gr.Textbox(
|
233 |
-
label="Elenco Documenti",
|
234 |
-
lines=10,
|
235 |
-
interactive=False,
|
236 |
-
value="Clicca 'Visualizza Documenti' per vedere l'elenco"
|
237 |
-
)
|
238 |
-
|
239 |
-
# Evento click con aggiornamento
|
240 |
-
list_button.click(
|
241 |
-
fn=list_indexed_documents,
|
242 |
-
inputs=[db_name_list],
|
243 |
-
outputs=[list_output],
|
244 |
-
api_name="list_docs"
|
245 |
-
)
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
# Adding a new tab for new functionalities
|
250 |
-
with gr.Tab("Nuove Funzionalità"):
|
251 |
-
gr.Markdown("## Cerca Documenti e Genera Riassunto")
|
252 |
-
|
253 |
-
db_name_new = gr.Dropdown(choices=databases, label="Seleziona Database", value="default_db")
|
254 |
-
search_input = gr.Textbox(label="Inserisci Termine di Ricerca")
|
255 |
-
search_button = gr.Button("Cerca Documenti")
|
256 |
-
search_output = gr.Textbox(label="Documenti Trovati")
|
257 |
-
|
258 |
-
summary_button = gr.Button("Genera Riassunto")
|
259 |
-
summary_output = gr.Textbox(label="Riassunto")
|
260 |
-
|
261 |
-
search_button.click(
|
262 |
-
search_documents,
|
263 |
-
inputs=[search_input, db_name_new],
|
264 |
-
outputs=search_output
|
265 |
-
)
|
266 |
-
|
267 |
-
# summary_button.click(
|
268 |
-
# generate_summary,
|
269 |
-
# inputs=db_name_new,
|
270 |
-
# outputs=summary_output
|
271 |
-
# )
|
272 |
-
|
273 |
-
# Avvio dell'app
|
274 |
-
if __name__ == "__main__":
|
275 |
-
rag_chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging_config.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
from logging.handlers import RotatingFileHandler
|
4 |
+
|
5 |
+
def configure_logging():
|
6 |
+
"""Configura le impostazioni di logging dell'applicazione."""
|
7 |
+
# Crea la directory dei log se non esiste
|
8 |
+
log_directory = "logs"
|
9 |
+
if not os.path.exists(log_directory):
|
10 |
+
os.makedirs(log_directory)
|
11 |
+
|
12 |
+
# Percorso completo del file di log
|
13 |
+
log_file = os.path.join(log_directory, "app.log")
|
14 |
+
|
15 |
+
# Configura il logger root
|
16 |
+
logger = logging.getLogger()
|
17 |
+
logger.setLevel(logging.INFO)
|
18 |
+
|
19 |
+
# Formattazione del log
|
20 |
+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
21 |
+
|
22 |
+
# Handler per il file con rotazione
|
23 |
+
file_handler = RotatingFileHandler(log_file, maxBytes=1024*1024, backupCount=5)
|
24 |
+
file_handler.setFormatter(formatter)
|
25 |
+
file_handler.setLevel(logging.INFO)
|
26 |
+
|
27 |
+
# Handler per la console
|
28 |
+
console_handler = logging.StreamHandler()
|
29 |
+
console_handler.setFormatter(formatter)
|
30 |
+
console_handler.setLevel(logging.INFO)
|
31 |
+
|
32 |
+
# Rimuovi eventuali handler esistenti
|
33 |
+
logger.handlers.clear()
|
34 |
+
|
35 |
+
# Aggiungi i nuovi handler
|
36 |
+
logger.addHandler(file_handler)
|
37 |
+
logger.addHandler(console_handler)
|
38 |
+
|
39 |
+
# Log di test per verifica
|
40 |
+
logging.info("Logging configurato con successo")
|
ui/chatbot_tab.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ui/chatbot_tab.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from app.document_handling import list_databases
|
5 |
+
from utils.helpers import extract_text_from_files
|
6 |
+
from app.llm_handling import answer_question
|
7 |
+
|
8 |
+
def create_chatbot_tab():
|
9 |
+
"""Crea il tab 'Chatbot' dell'interfaccia Gradio."""
|
10 |
+
|
11 |
+
def chat_upload_and_respond(files, chat_history, db_name):
|
12 |
+
"""Gestisce il caricamento dei file e aggiorna la chat con il contenuto."""
|
13 |
+
if chat_history is None:
|
14 |
+
chat_history = []
|
15 |
+
|
16 |
+
text = extract_text_from_files(files)
|
17 |
+
|
18 |
+
chat_history.append({
|
19 |
+
"role": "assistant",
|
20 |
+
"content": f"📄 Contenuto dei documenti caricati:\n{text}"
|
21 |
+
})
|
22 |
+
|
23 |
+
return chat_history
|
24 |
+
|
25 |
+
def respond(message, chat_history, db_name):
|
26 |
+
"""Genera una risposta alla domanda dell'utente e aggiorna la chat."""
|
27 |
+
if chat_history is None:
|
28 |
+
chat_history = []
|
29 |
+
|
30 |
+
new_messages = answer_question(message, db_name)
|
31 |
+
chat_history.extend(new_messages)
|
32 |
+
|
33 |
+
return "", chat_history
|
34 |
+
|
35 |
+
def clear_chat():
|
36 |
+
"""Pulisce la cronologia della chat."""
|
37 |
+
return [], []
|
38 |
+
|
39 |
+
# Ottieni la lista aggiornata dei database
|
40 |
+
databases = list_databases()
|
41 |
+
|
42 |
+
with gr.Tab("Chatbot"):
|
43 |
+
with gr.Row():
|
44 |
+
with gr.Column(scale=2):
|
45 |
+
# Dropdown per selezionare il database
|
46 |
+
db_name_chat = gr.Dropdown(
|
47 |
+
choices=databases,
|
48 |
+
label="Seleziona Database",
|
49 |
+
value="default_db"
|
50 |
+
)
|
51 |
+
|
52 |
+
# Componente Chatbot
|
53 |
+
chatbot = gr.Chatbot(label="Conversazione", type="messages")
|
54 |
+
|
55 |
+
# Input per la domanda
|
56 |
+
question_input = gr.Textbox(
|
57 |
+
label="Fai una domanda",
|
58 |
+
placeholder="Scrivi qui la tua domanda...",
|
59 |
+
lines=2
|
60 |
+
)
|
61 |
+
|
62 |
+
# Bottoni per azioni
|
63 |
+
with gr.Row():
|
64 |
+
ask_button = gr.Button("Invia")
|
65 |
+
clear_button = gr.Button("Pulisci Chat")
|
66 |
+
|
67 |
+
# Upload file con dimensioni ridotte
|
68 |
+
with gr.Row():
|
69 |
+
file_input = gr.File(
|
70 |
+
label="Carica PDF/Docx/TXT per la conversazione",
|
71 |
+
file_types=[".pdf", ".docx", ".txt"],
|
72 |
+
file_count="multiple",
|
73 |
+
height="100px",
|
74 |
+
scale=3
|
75 |
+
)
|
76 |
+
upload_button = gr.Button("Carica Documenti", scale=1)
|
77 |
+
|
78 |
+
# Stato della chat
|
79 |
+
chat_state = gr.State([])
|
80 |
+
|
81 |
+
# Eventi per i bottoni
|
82 |
+
upload_button.click(
|
83 |
+
fn=chat_upload_and_respond,
|
84 |
+
inputs=[file_input, chat_state, db_name_chat],
|
85 |
+
outputs=chatbot
|
86 |
+
)
|
87 |
+
|
88 |
+
ask_button.click(
|
89 |
+
fn=respond,
|
90 |
+
inputs=[question_input, chat_state, db_name_chat],
|
91 |
+
outputs=[question_input, chatbot]
|
92 |
+
)
|
93 |
+
|
94 |
+
clear_button.click(
|
95 |
+
fn=clear_chat,
|
96 |
+
outputs=[chatbot, chat_state]
|
97 |
+
)
|
98 |
+
|
99 |
+
return
|
ui/db_management_tab.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ui/db_management_tab.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from app.document_handling import create_database, modify_database, delete_database, list_databases
|
5 |
+
|
6 |
+
def create_db_management_tab(update_dropdowns):
|
7 |
+
"""Crea il tab 'Gestione Database' dell'interfaccia Gradio."""
|
8 |
+
|
9 |
+
def update_dropdowns_callback():
|
10 |
+
"""Aggiorna tutti i dropdown con la lista aggiornata dei database."""
|
11 |
+
databases = list_databases()
|
12 |
+
return [gr.update(choices=databases) for _ in range(6)]
|
13 |
+
|
14 |
+
# Ottieni la lista iniziale dei database
|
15 |
+
databases = list_databases()
|
16 |
+
|
17 |
+
with gr.Tab("Gestione Database"):
|
18 |
+
gr.Markdown("## Operazioni sui Database")
|
19 |
+
|
20 |
+
with gr.Row():
|
21 |
+
with gr.Column():
|
22 |
+
gr.Markdown("### Crea Database")
|
23 |
+
db_name_input = gr.Textbox(label="Nome Nuovo Database")
|
24 |
+
create_db_button = gr.Button("Crea Database")
|
25 |
+
create_output = gr.Textbox(label="Stato Creazione")
|
26 |
+
|
27 |
+
with gr.Column():
|
28 |
+
gr.Markdown("### Rinomina Database")
|
29 |
+
modify_db_old_name = gr.Dropdown(choices=databases, label="Database da Rinominare")
|
30 |
+
modify_db_new_name = gr.Textbox(label="Nuovo Nome")
|
31 |
+
modify_db_button = gr.Button("Rinomina Database")
|
32 |
+
modify_output = gr.Textbox(label="Stato Modifica")
|
33 |
+
|
34 |
+
with gr.Column():
|
35 |
+
gr.Markdown("### Elimina Database")
|
36 |
+
delete_db_dropdown = gr.Dropdown(choices=databases, label="Database da Eliminare")
|
37 |
+
delete_db_button = gr.Button("Elimina Database")
|
38 |
+
delete_output = gr.Textbox(label="Stato Eliminazione")
|
39 |
+
|
40 |
+
# Eventi per i bottoni di gestione DB
|
41 |
+
create_db_button.click(
|
42 |
+
create_database,
|
43 |
+
inputs=db_name_input,
|
44 |
+
outputs=create_output
|
45 |
+
).then(
|
46 |
+
update_dropdowns,
|
47 |
+
outputs=[modify_db_old_name, delete_db_dropdown]
|
48 |
+
)
|
49 |
+
|
50 |
+
modify_db_button.click(
|
51 |
+
modify_database,
|
52 |
+
inputs=[modify_db_old_name, modify_db_new_name],
|
53 |
+
outputs=modify_output
|
54 |
+
).then(
|
55 |
+
update_dropdowns,
|
56 |
+
outputs=[modify_db_old_name, delete_db_dropdown]
|
57 |
+
)
|
58 |
+
|
59 |
+
delete_db_button.click(
|
60 |
+
delete_database,
|
61 |
+
inputs=delete_db_dropdown,
|
62 |
+
outputs=delete_output
|
63 |
+
).then(
|
64 |
+
update_dropdowns,
|
65 |
+
outputs=[modify_db_old_name, delete_db_dropdown]
|
66 |
+
)
|
67 |
+
|
68 |
+
return
|
ui/document_management_tab.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ui/document_management_tab.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from app.document_handling import upload_and_index, list_indexed_files, delete_file_from_database, list_databases
|
5 |
+
|
6 |
+
def create_document_management_tab(update_dropdowns):
|
7 |
+
"""Crea il tab 'Gestione Documenti' dell'interfaccia Gradio."""
|
8 |
+
|
9 |
+
def upload_and_index_callback(files, title, author, db_name):
|
10 |
+
"""Carica e indicizza i documenti, quindi aggiorna la lista dei file."""
|
11 |
+
status = upload_and_index(files, title, author, db_name)
|
12 |
+
return status
|
13 |
+
|
14 |
+
def list_files_callback(db_name):
|
15 |
+
"""Elenca i file indicizzati nel database specificato."""
|
16 |
+
files = list_indexed_files(db_name)
|
17 |
+
return "\n".join(files)
|
18 |
+
|
19 |
+
def delete_file_callback(file_name, db_name):
|
20 |
+
"""Elimina un file dal database e aggiorna la lista."""
|
21 |
+
status = delete_file_from_database(file_name, db_name)
|
22 |
+
return status
|
23 |
+
|
24 |
+
# Ottieni la lista aggiornata dei database
|
25 |
+
databases = list_databases()
|
26 |
+
|
27 |
+
with gr.Tab("Gestione Documenti"):
|
28 |
+
with gr.Column():
|
29 |
+
gr.Markdown("### Carica Documenti")
|
30 |
+
with gr.Row():
|
31 |
+
file_input = gr.File(
|
32 |
+
label="Carica i tuoi documenti",
|
33 |
+
file_types=[".txt", ".pdf", ".docx"],
|
34 |
+
file_count="multiple"
|
35 |
+
)
|
36 |
+
db_name_upload = gr.Dropdown(
|
37 |
+
choices=databases,
|
38 |
+
label="Seleziona Database",
|
39 |
+
value="default_db"
|
40 |
+
)
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
title_input = gr.Textbox(label="Titolo del documento")
|
44 |
+
author_input = gr.Textbox(label="Autore")
|
45 |
+
|
46 |
+
upload_button = gr.Button("Indicizza Documenti")
|
47 |
+
upload_output = gr.Textbox(label="Stato Upload")
|
48 |
+
|
49 |
+
with gr.Column():
|
50 |
+
gr.Markdown("### Documenti nel Database")
|
51 |
+
db_name_list = gr.Dropdown(
|
52 |
+
choices=databases,
|
53 |
+
label="Seleziona Database",
|
54 |
+
value="default_db"
|
55 |
+
)
|
56 |
+
list_button = gr.Button("Visualizza Files")
|
57 |
+
list_output = gr.Textbox(label="Files nel Database")
|
58 |
+
delete_file_input = gr.Textbox(label="Nome file da eliminare")
|
59 |
+
delete_file_button = gr.Button("Elimina File")
|
60 |
+
delete_file_output = gr.Textbox(label="Stato Eliminazione")
|
61 |
+
|
62 |
+
# Eventi per i bottoni
|
63 |
+
upload_button.click(
|
64 |
+
upload_and_index_callback,
|
65 |
+
inputs=[file_input, title_input, author_input, db_name_upload],
|
66 |
+
outputs=upload_output
|
67 |
+
).then(
|
68 |
+
lambda db: list_indexed_files(db),
|
69 |
+
inputs=db_name_list,
|
70 |
+
outputs=list_output
|
71 |
+
)
|
72 |
+
|
73 |
+
list_button.click(
|
74 |
+
list_indexed_files,
|
75 |
+
inputs=db_name_list,
|
76 |
+
outputs=list_output
|
77 |
+
)
|
78 |
+
|
79 |
+
delete_file_button.click(
|
80 |
+
delete_file_callback,
|
81 |
+
inputs=[delete_file_input, db_name_list],
|
82 |
+
outputs=delete_file_output
|
83 |
+
).then(
|
84 |
+
list_indexed_files,
|
85 |
+
inputs=db_name_list,
|
86 |
+
outputs=list_output
|
87 |
+
).then(
|
88 |
+
update_dropdowns,
|
89 |
+
outputs=[db_name_upload, db_name_list]
|
90 |
+
)
|
91 |
+
|
92 |
+
return
|
ui/document_view_tab.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ui/document_view_tab.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from app.document_handling import list_indexed_documents, list_databases
|
5 |
+
|
6 |
+
def create_document_view_tab():
|
7 |
+
"""Crea il tab 'Visualizza Documenti Indicizzati' dell'interfaccia Gradio."""
|
8 |
+
|
9 |
+
def list_docs_callback(db_name):
|
10 |
+
"""Elenca i documenti indicizzati nel database specificato."""
|
11 |
+
documents = list_indexed_documents(db_name)
|
12 |
+
return "\n".join(documents)
|
13 |
+
|
14 |
+
# Ottieni la lista dei database
|
15 |
+
databases = list_databases()
|
16 |
+
|
17 |
+
with gr.Tab("Visualizza Documenti Indicizzati"):
|
18 |
+
with gr.Column():
|
19 |
+
gr.Markdown("### Documenti nel Database")
|
20 |
+
db_name_list = gr.Dropdown(
|
21 |
+
choices=databases,
|
22 |
+
label="Seleziona Database",
|
23 |
+
value="default_db",
|
24 |
+
interactive=True
|
25 |
+
)
|
26 |
+
list_button = gr.Button("Visualizza Documenti")
|
27 |
+
list_output = gr.Textbox(
|
28 |
+
label="Elenco Documenti",
|
29 |
+
lines=10,
|
30 |
+
interactive=False,
|
31 |
+
value="Clicca 'Visualizza Documenti' per vedere l'elenco"
|
32 |
+
)
|
33 |
+
|
34 |
+
list_button.click(
|
35 |
+
fn=list_docs_callback,
|
36 |
+
inputs=[db_name_list],
|
37 |
+
outputs=[list_output],
|
38 |
+
api_name="list_docs"
|
39 |
+
)
|
40 |
+
|
41 |
+
return
|
ui/new_features_tab.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ui/new_features_tab.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from app.document_handling import search_documents, list_databases
|
5 |
+
|
6 |
+
def create_new_features_tab():
|
7 |
+
"""Crea il tab 'Nuove Funzionalità' dell'interfaccia Gradio."""
|
8 |
+
|
9 |
+
def search_documents_callback(query, db_name):
|
10 |
+
"""Cerca documenti nel database in base alla query."""
|
11 |
+
results = search_documents(query, db_name)
|
12 |
+
return "\n".join(results)
|
13 |
+
|
14 |
+
# Ottieni la lista dei database
|
15 |
+
databases = list_databases()
|
16 |
+
|
17 |
+
with gr.Tab("Nuove Funzionalità"):
|
18 |
+
gr.Markdown("## Cerca Documenti e Genera Riassunto")
|
19 |
+
|
20 |
+
db_name_new = gr.Dropdown(choices=databases, label="Seleziona Database", value="default_db")
|
21 |
+
search_input = gr.Textbox(label="Inserisci Termini di Ricerca")
|
22 |
+
search_button = gr.Button("Cerca Documenti")
|
23 |
+
search_output = gr.Textbox(label="Documenti Trovati")
|
24 |
+
|
25 |
+
summary_button = gr.Button("Genera Riassunto")
|
26 |
+
summary_output = gr.Textbox(label="Riassunto")
|
27 |
+
|
28 |
+
# Evento per il bottone di ricerca
|
29 |
+
search_button.click(
|
30 |
+
search_documents_callback,
|
31 |
+
inputs=[search_input, db_name_new],
|
32 |
+
outputs=search_output
|
33 |
+
)
|
34 |
+
|
35 |
+
# Evento per il bottone di generazione riassunto (implementare generate_summary se necessario)
|
36 |
+
# summary_button.click(
|
37 |
+
# generate_summary,
|
38 |
+
# inputs=db_name_new,
|
39 |
+
# outputs=summary_output
|
40 |
+
# )
|
41 |
+
|
42 |
+
return
|
utils/__init__.py
ADDED
File without changes
|
utils/helpers.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# utils/helpers.py
|
2 |
+
|
3 |
+
import logging
|
4 |
+
from app.document_handling import extract_text_from_pdf, extract_text_from_docx
|
5 |
+
|
6 |
+
def extract_text_from_files(files):
|
7 |
+
"""
|
8 |
+
Estrae e concatena il testo da file PDF, DOCX e TXT.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
files (list): Lista di file caricati.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: Testo concatenato estratto dai file.
|
15 |
+
"""
|
16 |
+
text = ""
|
17 |
+
for file in files:
|
18 |
+
try:
|
19 |
+
if file.name.endswith('.pdf'):
|
20 |
+
text += extract_text_from_pdf(file.name)
|
21 |
+
elif file.name.endswith('.docx'):
|
22 |
+
text += extract_text_from_docx(file.name)
|
23 |
+
else:
|
24 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
25 |
+
text += f.read()
|
26 |
+
except Exception as e:
|
27 |
+
logging.error(f"Errore durante la lettura del file {file.name}: {e}")
|
28 |
+
return text
|