Shreyas094 commited on
Commit
a6c785f
1 Parent(s): 711bfec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -133,7 +133,7 @@ def get_response_from_pdf(query):
133
  if os.path.exists("faiss_database"):
134
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
135
  else:
136
- return "No documents available. Please upload PDF documents to answer questions.", ""
137
 
138
  retriever = database.as_retriever()
139
  relevant_docs = retriever.get_relevant_documents(query)
@@ -142,20 +142,15 @@ def get_response_from_pdf(query):
142
  prompt = f"""<s>[INST] Using the following context from the PDF documents:
143
  {context_str}
144
  Write a detailed and complete response that answers the following user question: '{query}'
145
- After writing the response, please provide a list of sources used (document names) in your answer. [/INST]"""
146
 
147
  generated_text = generate_chunked_response(prompt)
148
 
149
- # Remove all instruction-related content
150
  clean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)
151
  clean_text = clean_text.replace("Using the following context from the PDF documents:", "").strip()
152
 
153
- # Split the content and sources
154
- parts = clean_text.split("Sources:", 1)
155
- main_content = parts[0].strip()
156
- sources = parts[1].strip() if len(parts) > 1 else ""
157
-
158
- return main_content, sources
159
 
160
  def get_response_with_search(query):
161
  search_results = duckduckgo_search(query)
@@ -169,7 +164,7 @@ After writing the document, please provide a list of sources used in your respon
169
 
170
  generated_text = generate_chunked_response(prompt)
171
 
172
- # Remove all instruction-related content
173
  clean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)
174
  clean_text = clean_text.replace("Using the following context:", "").strip()
175
 
@@ -183,10 +178,11 @@ After writing the document, please provide a list of sources used in your respon
183
  def chatbot_interface(message, history, use_web_search):
184
  if use_web_search:
185
  main_content, sources = get_response_with_search(message)
 
186
  else:
187
- main_content, sources = get_response_from_pdf(message)
188
-
189
- formatted_response = f"{main_content}\n\nSources:\n{sources}"
190
  history.append((message, formatted_response))
191
  return history
192
 
 
133
  if os.path.exists("faiss_database"):
134
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
135
  else:
136
+ return "No documents available. Please upload PDF documents to answer questions."
137
 
138
  retriever = database.as_retriever()
139
  relevant_docs = retriever.get_relevant_documents(query)
 
142
  prompt = f"""<s>[INST] Using the following context from the PDF documents:
143
  {context_str}
144
  Write a detailed and complete response that answers the following user question: '{query}'
145
+ Do not include a list of sources in your response. [/INST]"""
146
 
147
  generated_text = generate_chunked_response(prompt)
148
 
149
+ # Clean the response
150
  clean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)
151
  clean_text = clean_text.replace("Using the following context from the PDF documents:", "").strip()
152
 
153
+ return clean_text
 
 
 
 
 
154
 
155
  def get_response_with_search(query):
156
  search_results = duckduckgo_search(query)
 
164
 
165
  generated_text = generate_chunked_response(prompt)
166
 
167
+ # Clean the response
168
  clean_text = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', generated_text, flags=re.DOTALL)
169
  clean_text = clean_text.replace("Using the following context:", "").strip()
170
 
 
178
  def chatbot_interface(message, history, use_web_search):
179
  if use_web_search:
180
  main_content, sources = get_response_with_search(message)
181
+ formatted_response = f"{main_content}\n\nSources:\n{sources}"
182
  else:
183
+ response = get_response_from_pdf(message)
184
+ formatted_response = response # No sources for PDF responses
185
+
186
  history.append((message, formatted_response))
187
  return history
188