ritampatra commited on
Commit
caa7c9a
·
verified ·
1 Parent(s): e0b9cc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
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
- faiss_index = None
66
- document_chunks = None
 
67
 
68
- # Function to handle document upload
69
- def upload_file(file):
70
- nonlocal faiss_index, document_chunks
71
- faiss_index, document_chunks = process_document(file.name)
72
- return "Document uploaded and indexed. You can now ask questions."
73
 
74
- # Function to handle user queries
75
- def ask_question(query):
76
- if faiss_index and document_chunks:
77
- return query_document(query, faiss_index, document_chunks)
78
- return "Please upload a document first."
79
 
80
- # Gradio UI
81
- upload = gr.File(label="Upload a PDF document")
82
- question = gr.Textbox(label="Ask a question about the document")
83
- answer = gr.Textbox(label="Answer", interactive=False) # Updated to interactive=False
84
 
85
- # Gradio app layout
86
- with gr.Blocks() as demo:
87
  gr.Markdown("# Document Chatbot")
88
- with gr.Row():
89
- upload.render()
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