File size: 4,042 Bytes
7297579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import utils

# Placeholder functions and variables for RAG components

# Function to ingest a new file into the system
class VectorData():
    def __init__(self):
        self.retriever = None
        self.ingested_files = []
        self.vectorstore = None

    def add_file(self,file):
        if file is not None:
            self.ingested_files.append(file.name.split('/')[-1])
            self.retriever, self.vectorstore = utils.add_doc(file,self.vectorstore)
        return [[name] for name in self.ingested_files]

    def delete_file_by_name(self,file_name):
        if file_name in self.ingested_files:
            self.retriever, self.vectorstore = utils.delete_doc(file_name,self.vectorstore)
            self.ingested_files.remove(file_name)
        return [[name] for name in self.ingested_files]

    def delete_all_files(self):
        self.ingested_files.clear()
        self.retriever, self.vectorstore = utils.delete_all_doc(self.vectorstore)
        return []

# Function to handle question answering
def answer_question(question):
    if question.strip():
        return f"Generated answer for the question: '{question}'"
    return "Please enter a question."

data_obj = VectorData()

# Define the Gradio interface
with gr.Blocks() as rag_interface:
    # Title and Description
    gr.Markdown("# RAG Interface")
    gr.Markdown("Manage documents and ask questions with a Retrieval-Augmented Generation (RAG) system.")

    with gr.Row():
        # Left Column: File Management
        with gr.Column():
            gr.Markdown("### File Management")

            # File upload and ingest
            file_input = gr.File(label="Upload File to Ingest")
            add_file_button = gr.Button("Ingest File")

            # Scrollable list for ingested files
            ingested_files_box = gr.Dataframe(
                headers=["Files"], 
                datatype="str",
                row_count=4,  # Limits the visible rows to create a scrollable view
                interactive=False
            )

            # Radio buttons to choose delete option
            delete_option = gr.Radio(choices=["Delete by File Name", "Delete All Files"], label="Delete Option")
            file_name_input = gr.Textbox(label="Enter File Name to Delete", visible=False)
            delete_button = gr.Button("Delete Selected")

            # Show or hide file name input based on delete option selection
            def toggle_file_input(option):
                return gr.update(visible=(option == "Delete by File Name"))

            delete_option.change(fn=toggle_file_input, inputs=delete_option, outputs=file_name_input)

            # Handle file ingestion
            add_file_button.click(
                fn=data_obj.add_file,
                inputs=file_input,
                outputs=ingested_files_box
            )

            # Handle delete based on selected option
            def delete_action(delete_option, file_name):
                if delete_option == "Delete by File Name" and file_name:
                    return data_obj.delete_file_by_name(file_name)
                elif delete_option == "Delete All Files":
                    return data_obj.delete_all_files()
                else:
                    return [[name] for name in data_obj.ingested_files]

            delete_button.click(
                fn=delete_action,
                inputs=[delete_option, file_name_input],
                outputs=ingested_files_box
            )

        # Right Column: Question Answering
        with gr.Column():
            gr.Markdown("### Ask a Question")

            # Question input
            question_input = gr.Textbox(label="Enter your question")

            # Get answer button and answer output
            ask_button = gr.Button("Get Answer")
            answer_output = gr.Textbox(label="Answer", interactive=False)

            ask_button.click(fn=answer_question, inputs=question_input, outputs=answer_output)

# Launch the Gradio interface
rag_interface.launch()