|
import gradio as gr |
|
import utils |
|
|
|
|
|
|
|
|
|
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 [] |
|
|
|
|
|
def answer_question(question): |
|
if question.strip(): |
|
return f"Generated answer for the question: '{question}'" |
|
return "Please enter a question." |
|
|
|
data_obj = VectorData() |
|
|
|
|
|
with gr.Blocks() as rag_interface: |
|
|
|
gr.Markdown("# RAG Interface") |
|
gr.Markdown("Manage documents and ask questions with a Retrieval-Augmented Generation (RAG) system.") |
|
|
|
with gr.Row(): |
|
|
|
with gr.Column(): |
|
gr.Markdown("### File Management") |
|
|
|
|
|
file_input = gr.File(label="Upload File to Ingest") |
|
add_file_button = gr.Button("Ingest File") |
|
|
|
|
|
ingested_files_box = gr.Dataframe( |
|
headers=["Files"], |
|
datatype="str", |
|
row_count=4, |
|
interactive=False |
|
) |
|
|
|
|
|
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") |
|
|
|
|
|
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) |
|
|
|
|
|
add_file_button.click( |
|
fn=data_obj.add_file, |
|
inputs=file_input, |
|
outputs=ingested_files_box |
|
) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
with gr.Column(): |
|
gr.Markdown("### Ask a Question") |
|
|
|
|
|
question_input = gr.Textbox(label="Enter your question") |
|
|
|
|
|
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) |
|
|
|
|
|
rag_interface.launch() |
|
|