File size: 3,366 Bytes
352ebdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ui/document_management_tab.py

import gradio as gr
from app.document_handling import upload_and_index, list_indexed_files, delete_file_from_database, list_databases

def create_document_management_tab(update_dropdowns):
    """Crea il tab 'Gestione Documenti' dell'interfaccia Gradio."""
    
    def upload_and_index_callback(files, title, author, db_name):
        """Carica e indicizza i documenti, quindi aggiorna la lista dei file."""
        status = upload_and_index(files, title, author, db_name)
        return status

    def list_files_callback(db_name):
        """Elenca i file indicizzati nel database specificato."""
        files = list_indexed_files(db_name)
        return "\n".join(files)

    def delete_file_callback(file_name, db_name):
        """Elimina un file dal database e aggiorna la lista."""
        status = delete_file_from_database(file_name, db_name)
        return status

    # Ottieni la lista aggiornata dei database
    databases = list_databases()

    with gr.Tab("Gestione Documenti"):
        with gr.Column():
            gr.Markdown("### Carica Documenti")
            with gr.Row():
                file_input = gr.File(
                    label="Carica i tuoi documenti", 
                    file_types=[".txt", ".pdf", ".docx"], 
                    file_count="multiple"
                )
                db_name_upload = gr.Dropdown(
                    choices=databases, 
                    label="Seleziona Database", 
                    value="default_db"
                )
            
            with gr.Row():
                title_input = gr.Textbox(label="Titolo del documento")
                author_input = gr.Textbox(label="Autore")
            
            upload_button = gr.Button("Indicizza Documenti")
            upload_output = gr.Textbox(label="Stato Upload")
            
            with gr.Column():
                gr.Markdown("### Documenti nel Database")
                db_name_list = gr.Dropdown(
                    choices=databases, 
                    label="Seleziona Database", 
                    value="default_db"
                )
                list_button = gr.Button("Visualizza Files")
                list_output = gr.Textbox(label="Files nel Database")
                delete_file_input = gr.Textbox(label="Nome file da eliminare")
                delete_file_button = gr.Button("Elimina File")
                delete_file_output = gr.Textbox(label="Stato Eliminazione")
        
        # Eventi per i bottoni
        upload_button.click(
            upload_and_index_callback,
            inputs=[file_input, title_input, author_input, db_name_upload],
            outputs=upload_output
        ).then(
            lambda db: list_indexed_files(db),
            inputs=db_name_list,
            outputs=list_output
        )

        list_button.click(
            list_indexed_files,
            inputs=db_name_list,
            outputs=list_output
        )

        delete_file_button.click(
            delete_file_callback,
            inputs=[delete_file_input, db_name_list],
            outputs=delete_file_output
        ).then(
            list_indexed_files,
            inputs=db_name_list,
            outputs=list_output
        ).then(
            update_dropdowns,
            outputs=[db_name_upload, db_name_list]
        )
    
    return