Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -62,38 +62,31 @@ def query_document(query, faiss_index, document_chunks, model_name="sentence-tra
|
|
62 |
|
63 |
# Gradio interface
|
64 |
def chatbot_interface():
|
65 |
-
|
66 |
-
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
with gr.Blocks() as demo:
|
87 |
gr.Markdown("# Document Chatbot")
|
88 |
-
|
89 |
-
|
90 |
-
with gr.Row():
|
91 |
-
question.render()
|
92 |
-
answer.render()
|
93 |
-
|
94 |
-
# Bind upload and question functionality
|
95 |
-
upload.upload(upload_file)
|
96 |
-
question.submit(ask_question, inputs=question, outputs=answer)
|
97 |
|
98 |
demo.launch()
|
99 |
|
|
|
62 |
|
63 |
# Gradio interface
|
64 |
def chatbot_interface():
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
faiss_index = None
|
67 |
+
document_chunks = None
|
68 |
|
69 |
+
# Function to handle document upload
|
70 |
+
def upload_file(file):
|
71 |
+
nonlocal faiss_index, document_chunks
|
72 |
+
faiss_index, document_chunks = process_document(file.name)
|
73 |
+
return "Document uploaded and indexed. You can now ask questions."
|
74 |
|
75 |
+
# Function to handle user queries
|
76 |
+
def ask_question(query):
|
77 |
+
if faiss_index and document_chunks:
|
78 |
+
return query_document(query, faiss_index, document_chunks)
|
79 |
+
return "Please upload a document first."
|
80 |
|
81 |
+
# Gradio UI
|
82 |
+
upload = gr.File(label="Upload a PDF document")
|
83 |
+
question = gr.Textbox(label="Ask a question about the document")
|
84 |
+
answer = gr.Textbox(label="Answer", interactive=False) # Updated to interactive=False
|
85 |
|
86 |
+
# Layout for Gradio app
|
|
|
87 |
gr.Markdown("# Document Chatbot")
|
88 |
+
upload.change(upload_file, inputs=upload, outputs=None) # Trigger file upload
|
89 |
+
question.submit(ask_question, inputs=question, outputs=answer) # Trigger question submission
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
demo.launch()
|
92 |
|