Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import utils
|
| 3 |
+
|
| 4 |
+
# Placeholder functions and variables for RAG components
|
| 5 |
+
|
| 6 |
+
# Function to ingest a new file into the system
|
| 7 |
+
class VectorData():
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.retriever = None
|
| 10 |
+
self.ingested_files = []
|
| 11 |
+
self.vectorstore = None
|
| 12 |
+
|
| 13 |
+
def add_file(self,file):
|
| 14 |
+
if file is not None:
|
| 15 |
+
self.ingested_files.append(file.name.split('/')[-1])
|
| 16 |
+
self.retriever, self.vectorstore = utils.add_doc(file,self.vectorstore)
|
| 17 |
+
return [[name] for name in self.ingested_files]
|
| 18 |
+
|
| 19 |
+
def delete_file_by_name(self,file_name):
|
| 20 |
+
if file_name in self.ingested_files:
|
| 21 |
+
self.retriever, self.vectorstore = utils.delete_doc(file_name,self.vectorstore)
|
| 22 |
+
self.ingested_files.remove(file_name)
|
| 23 |
+
return [[name] for name in self.ingested_files]
|
| 24 |
+
|
| 25 |
+
def delete_all_files(self):
|
| 26 |
+
self.ingested_files.clear()
|
| 27 |
+
self.retriever, self.vectorstore = utils.delete_all_doc(self.vectorstore)
|
| 28 |
+
return []
|
| 29 |
+
|
| 30 |
+
# Function to handle question answering
|
| 31 |
+
def answer_question(question):
|
| 32 |
+
if question.strip():
|
| 33 |
+
return f"Generated answer for the question: '{question}'"
|
| 34 |
+
return "Please enter a question."
|
| 35 |
+
|
| 36 |
+
data_obj = VectorData()
|
| 37 |
+
|
| 38 |
+
# Define the Gradio interface
|
| 39 |
+
with gr.Blocks() as rag_interface:
|
| 40 |
+
# Title and Description
|
| 41 |
+
gr.Markdown("# RAG Interface")
|
| 42 |
+
gr.Markdown("Manage documents and ask questions with a Retrieval-Augmented Generation (RAG) system.")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
# Left Column: File Management
|
| 46 |
+
with gr.Column():
|
| 47 |
+
gr.Markdown("### File Management")
|
| 48 |
+
|
| 49 |
+
# File upload and ingest
|
| 50 |
+
file_input = gr.File(label="Upload File to Ingest")
|
| 51 |
+
add_file_button = gr.Button("Ingest File")
|
| 52 |
+
|
| 53 |
+
# Scrollable list for ingested files
|
| 54 |
+
ingested_files_box = gr.Dataframe(
|
| 55 |
+
headers=["Files"],
|
| 56 |
+
datatype="str",
|
| 57 |
+
row_count=4, # Limits the visible rows to create a scrollable view
|
| 58 |
+
interactive=False
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Radio buttons to choose delete option
|
| 62 |
+
delete_option = gr.Radio(choices=["Delete by File Name", "Delete All Files"], label="Delete Option")
|
| 63 |
+
file_name_input = gr.Textbox(label="Enter File Name to Delete", visible=False)
|
| 64 |
+
delete_button = gr.Button("Delete Selected")
|
| 65 |
+
|
| 66 |
+
# Show or hide file name input based on delete option selection
|
| 67 |
+
def toggle_file_input(option):
|
| 68 |
+
return gr.update(visible=(option == "Delete by File Name"))
|
| 69 |
+
|
| 70 |
+
delete_option.change(fn=toggle_file_input, inputs=delete_option, outputs=file_name_input)
|
| 71 |
+
|
| 72 |
+
# Handle file ingestion
|
| 73 |
+
add_file_button.click(
|
| 74 |
+
fn=data_obj.add_file,
|
| 75 |
+
inputs=file_input,
|
| 76 |
+
outputs=ingested_files_box
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Handle delete based on selected option
|
| 80 |
+
def delete_action(delete_option, file_name):
|
| 81 |
+
if delete_option == "Delete by File Name" and file_name:
|
| 82 |
+
return data_obj.delete_file_by_name(file_name)
|
| 83 |
+
elif delete_option == "Delete All Files":
|
| 84 |
+
return data_obj.delete_all_files()
|
| 85 |
+
else:
|
| 86 |
+
return [[name] for name in data_obj.ingested_files]
|
| 87 |
+
|
| 88 |
+
delete_button.click(
|
| 89 |
+
fn=delete_action,
|
| 90 |
+
inputs=[delete_option, file_name_input],
|
| 91 |
+
outputs=ingested_files_box
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Right Column: Question Answering
|
| 95 |
+
with gr.Column():
|
| 96 |
+
gr.Markdown("### Ask a Question")
|
| 97 |
+
|
| 98 |
+
# Question input
|
| 99 |
+
question_input = gr.Textbox(label="Enter your question")
|
| 100 |
+
|
| 101 |
+
# Get answer button and answer output
|
| 102 |
+
ask_button = gr.Button("Get Answer")
|
| 103 |
+
answer_output = gr.Textbox(label="Answer", interactive=False)
|
| 104 |
+
|
| 105 |
+
ask_button.click(fn=answer_question, inputs=question_input, outputs=answer_output)
|
| 106 |
+
|
| 107 |
+
# Launch the Gradio interface
|
| 108 |
+
rag_interface.launch()
|