File size: 6,412 Bytes
39c71c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import gradio as gr
import logging
from app.document_handling import upload_and_index, list_indexed_files, delete_file_from_database
from app.functions.database_handling import (
    create_database, 
    modify_database, 
    delete_database, 
    list_databases
)

class ManagementTabs:
    def __init__(self, update_all_dropdowns):
        self.update_all_dropdowns = update_all_dropdowns
        self.databases = list_databases()
        
    def create_tabs(self):
        with gr.Tabs():
            self._create_db_management_tab()
            self._create_document_management_tab()
            
    def _create_db_management_tab(self):
        with gr.Tab("Gestione Database"):
            gr.Markdown("## Operazioni sui Database")
            
            with gr.Row():
                # Creazione Database
                with gr.Column():
                    gr.Markdown("### Crea Database")
                    self.db_name_input = gr.Textbox(label="Nome Nuovo Database")
                    self.create_db_button = gr.Button("Crea Database")
                    self.create_output = gr.Textbox(label="Stato Creazione")
                
                # Modifica Database
                with gr.Column():
                    gr.Markdown("### Rinomina Database")
                    self.modify_db_old_name = gr.Dropdown(
                        choices=self.databases, 
                        label="Database da Rinominare"
                    )
                    self.modify_db_new_name = gr.Textbox(label="Nuovo Nome")
                    self.modify_db_button = gr.Button("Rinomina Database")
                    self.modify_output = gr.Textbox(label="Stato Modifica")
                
                # Eliminazione Database
                with gr.Column():
                    gr.Markdown("### Elimina Database")
                    self.delete_db_dropdown = gr.Dropdown(
                        choices=self.databases, 
                        label="Database da Eliminare"
                    )
                    self.delete_db_button = gr.Button("Elimina Database")
                    self.delete_output = gr.Textbox(label="Stato Eliminazione")
            
            self._setup_db_events()
    
    def _create_document_management_tab(self):
        with gr.Tab("Gestione Documenti"):
            with gr.Column():
                # Upload Documenti
                gr.Markdown("### Carica Documenti")
                with gr.Row():
                    self.file_input = gr.File(
                        label="Carica i tuoi documenti",
                        file_types=[".txt", ".pdf", ".docx"],
                        file_count="multiple"
                    )
                    self.db_name_upload = gr.Dropdown(
                        choices=self.databases,
                        label="Seleziona Database",
                        value="default_db"
                    )
                
                with gr.Row():
                    self.title_input = gr.Textbox(label="Titolo del documento")
                    self.author_input = gr.Textbox(label="Autore")
                
                self.upload_button = gr.Button("Indicizza Documenti")
                self.upload_output = gr.Textbox(label="Stato Upload")
                
                # Gestione File
                self._setup_file_management()
            
            self._setup_document_events()
    
    def _setup_file_management(self):
        gr.Markdown("### Gestione File")
        with gr.Row():
            self.db_name_list = gr.Dropdown(
                choices=self.databases,
                label="Database",
                value="default_db"
            )
            self.list_button = gr.Button("Lista File")
        self.list_output = gr.Textbox(label="File nel Database")
        
        with gr.Row():
            self.delete_file_input = gr.Textbox(label="Nome File da Eliminare")
            self.delete_file_button = gr.Button("Elimina File")
        self.delete_file_output = gr.Textbox(label="Stato Eliminazione")
    
    def _setup_db_events(self):
        # Eventi per la gestione del database
        self.create_db_button.click(
            fn=create_database,
            inputs=self.db_name_input,
            outputs=self.create_output
        ).then(fn=self.update_all_dropdowns)
        
        self.modify_db_button.click(
            fn=modify_database,
            inputs=[self.modify_db_old_name, self.modify_db_new_name],
            outputs=self.modify_output
        ).then(fn=self.update_all_dropdowns)
        
        self.delete_db_button.click(
            fn=delete_database,
            inputs=self.delete_db_dropdown,
            outputs=self.delete_output
        ).then(fn=self.update_all_dropdowns)
    
    def _setup_document_events(self):
        # Eventi per la gestione dei documenti
        self.upload_button.click(
            fn=self._upload_and_index_callback,
            inputs=[self.file_input, self.title_input, self.author_input, self.db_name_upload],
            outputs=self.upload_output
        ).then(fn=self.update_all_dropdowns).then(
            fn=self._list_files_callback,
            inputs=[self.db_name_list],
            outputs=self.list_output
        )
        
        self.list_button.click(
            fn=self._list_files_callback,
            inputs=[self.db_name_list],
            outputs=self.list_output
        )
        
        self.delete_file_button.click(
            fn=self._delete_file_callback,
            inputs=[self.delete_file_input, self.db_name_list],
            outputs=self.delete_file_output
        ).then(fn=self.update_all_dropdowns)
    
    def _upload_and_index_callback(self, files, title, author, db_name):
        try:
            status = upload_and_index(files, title, author, db_name)
            logging.info(f"Upload completato: {status}")
            return status
        except Exception as e:
            logging.error(f"Errore durante l'upload: {str(e)}")
            return f"Errore: {str(e)}"
    
    def _list_files_callback(self, db_name):
        return list_indexed_files(db_name)
    
    def _delete_file_callback(self, file_name, db_name):
        return delete_file_from_database(file_name, db_name)

def create_management_tabs(update_all_dropdowns):
    tabs = ManagementTabs(update_all_dropdowns)
    tabs.create_tabs()
    return tabs