Shreyas094 commited on
Commit
50a6cc0
·
verified ·
1 Parent(s): a813e93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -35
app.py CHANGED
@@ -76,21 +76,22 @@ Answer:"""
76
  except Exception as e:
77
  return f"Error processing PDF: {str(e)}"
78
 
79
- def generate_response(self, query):
80
  try:
81
- if not query.strip():
82
- return "Please enter a question."
83
-
84
  if self.vector_store is None:
85
  return "Please upload and process a PDF first."
86
 
 
 
 
 
87
  # Search for relevant chunks
88
  relevant_chunks = self.vector_store.similarity_search(query, k=3)
89
  context = "\n\n".join([doc.page_content for doc in relevant_chunks])
90
 
91
  # Format conversation history
92
  conversation_history = "\n".join([
93
- f"Q: {q}\nA: {a}" for q, a in self.conversation_history[-3:] # Keep last 3 exchanges
94
  ])
95
 
96
  # Create prompt with system prompt, context, and conversation history
@@ -113,9 +114,6 @@ Answer:"""
113
  ):
114
  response += message.choices[0].delta.content
115
 
116
- # Update conversation history
117
- self.conversation_history.append((query, response))
118
-
119
  return response
120
  except Exception as e:
121
  return f"Error generating response: {str(e)}"
@@ -129,40 +127,45 @@ def create_gradio_interface():
129
  gr.Markdown("# PDF Question Answering System")
130
 
131
  with gr.Row():
132
- with gr.Column():
133
- pdf_input = gr.File(
134
- label="Upload PDF",
135
- file_types=[".pdf"],
136
- type="filepath"
137
- )
138
- process_button = gr.Button("Process PDF")
139
- status_output = gr.Textbox(label="Status", interactive=False)
140
-
141
- with gr.Row():
142
- with gr.Column():
143
- query_input = gr.Textbox(
144
- label="Ask a question about the PDF",
145
- placeholder="Enter your question here..."
146
- )
147
- submit_button = gr.Button("Submit")
148
- answer_output = gr.Textbox(
149
- label="Answer",
150
- interactive=False,
151
- lines=5
152
- )
153
-
154
  process_button.click(
155
  fn=rag.process_pdf,
156
  inputs=[pdf_input],
157
  outputs=[status_output]
158
  )
159
-
160
- submit_button.click(
161
  fn=rag.generate_response,
162
- inputs=[query_input],
163
- outputs=[answer_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  )
165
-
166
  return demo
167
 
168
  if __name__ == "__main__":
 
76
  except Exception as e:
77
  return f"Error processing PDF: {str(e)}"
78
 
79
+ def generate_response(self, message, history):
80
  try:
 
 
 
81
  if self.vector_store is None:
82
  return "Please upload and process a PDF first."
83
 
84
+ query = message.strip()
85
+ if not query:
86
+ return "Please enter a question."
87
+
88
  # Search for relevant chunks
89
  relevant_chunks = self.vector_store.similarity_search(query, k=3)
90
  context = "\n\n".join([doc.page_content for doc in relevant_chunks])
91
 
92
  # Format conversation history
93
  conversation_history = "\n".join([
94
+ f"Q: {q}\nA: {a}" for q, a in history[-3:] if q and a # Keep last 3 exchanges
95
  ])
96
 
97
  # Create prompt with system prompt, context, and conversation history
 
114
  ):
115
  response += message.choices[0].delta.content
116
 
 
 
 
117
  return response
118
  except Exception as e:
119
  return f"Error generating response: {str(e)}"
 
127
  gr.Markdown("# PDF Question Answering System")
128
 
129
  with gr.Row():
130
+ pdf_input = gr.File(
131
+ label="Upload PDF",
132
+ file_types=[".pdf"],
133
+ type="filepath"
134
+ )
135
+ process_button = gr.Button("Process PDF")
136
+ status_output = gr.Textbox(label="Status", interactive=False)
137
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  process_button.click(
139
  fn=rag.process_pdf,
140
  inputs=[pdf_input],
141
  outputs=[status_output]
142
  )
143
+
144
+ chatbot = gr.ChatInterface(
145
  fn=rag.generate_response,
146
+ chatbot=gr.Chatbot(
147
+ label="Chat History",
148
+ height=400,
149
+ show_label=True,
150
+ ),
151
+ textbox=gr.Textbox(
152
+ placeholder="Ask a question about the PDF...",
153
+ container=False,
154
+ scale=7,
155
+ ),
156
+ title="Chat with your PDF",
157
+ description="Upload a PDF and ask questions about its contents.",
158
+ theme="soft",
159
+ examples=[
160
+ "What is the main topic of this document?",
161
+ "Can you summarize the key points?",
162
+ "What are the main conclusions?",
163
+ ],
164
+ retry_btn="Retry",
165
+ undo_btn="Undo",
166
+ clear_btn="Clear",
167
  )
168
+
169
  return demo
170
 
171
  if __name__ == "__main__":