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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -63,25 +63,25 @@ def query_document(query, faiss_index, document_chunks, model_name="sentence-tra
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")
 
63
  # Gradio interface
64
  def chatbot_interface():
65
  with gr.Blocks() as demo:
66
+ state = gr.State() # This state will store the FAISS index and document chunks
 
67
 
68
  # Function to handle document upload
69
  def upload_file(file):
 
70
  faiss_index, document_chunks = process_document(file.name)
71
+ state.value = (faiss_index, document_chunks)
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 state.value is not None:
77
+ faiss_index, document_chunks = state.value
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)
85
 
86
  # Layout for Gradio app
87
  gr.Markdown("# Document Chatbot")