Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,12 +74,16 @@ def get_model(temperature, top_p, repetition_penalty):
|
|
| 74 |
def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):
|
| 75 |
full_response = ""
|
| 76 |
for i in range(max_chunks):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
full_response += chunk
|
|
|
|
|
|
|
| 81 |
break
|
| 82 |
-
full_response += chunk
|
| 83 |
return full_response.strip()
|
| 84 |
|
| 85 |
def extract_text_from_webpage(html):
|
|
@@ -209,7 +213,12 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
| 209 |
else:
|
| 210 |
database = None
|
| 211 |
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
original_query = question
|
| 214 |
rephrased_query = rephrase_for_search(original_query, model)
|
| 215 |
print(f"Original query: {original_query}")
|
|
@@ -242,6 +251,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
| 242 |
"""
|
| 243 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
| 244 |
formatted_prompt = prompt_val.format(context=context_str, original_question=question, rephrased_query=rephrased_query)
|
|
|
|
| 245 |
else:
|
| 246 |
if database is None:
|
| 247 |
return "No documents available. Please upload documents or enable web search to answer questions."
|
|
@@ -250,6 +260,11 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
| 250 |
relevant_docs = retriever.get_relevant_documents(question)
|
| 251 |
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
prompt_template = """
|
| 254 |
Answer the question based on the following context:
|
| 255 |
Context:
|
|
@@ -287,6 +302,15 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
| 287 |
|
| 288 |
return answer
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
# Gradio interface
|
| 291 |
with gr.Blocks() as demo:
|
| 292 |
gr.Markdown("# Chat with your PDF documents and Web Search")
|
|
|
|
| 74 |
def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):
|
| 75 |
full_response = ""
|
| 76 |
for i in range(max_chunks):
|
| 77 |
+
try:
|
| 78 |
+
chunk = model(prompt + full_response, max_new_tokens=max_tokens)
|
| 79 |
+
chunk = chunk.strip()
|
| 80 |
+
if chunk.endswith((".", "!", "?")):
|
| 81 |
+
full_response += chunk
|
| 82 |
+
break
|
| 83 |
full_response += chunk
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error in generate_chunked_response: {e}")
|
| 86 |
break
|
|
|
|
| 87 |
return full_response.strip()
|
| 88 |
|
| 89 |
def extract_text_from_webpage(html):
|
|
|
|
| 213 |
else:
|
| 214 |
database = None
|
| 215 |
|
| 216 |
+
max_attempts = 3
|
| 217 |
+
context_reduction_factor = 0.7
|
| 218 |
+
|
| 219 |
+
for attempt in range(max_attempts):
|
| 220 |
+
try:
|
| 221 |
+
if web_search:
|
| 222 |
original_query = question
|
| 223 |
rephrased_query = rephrase_for_search(original_query, model)
|
| 224 |
print(f"Original query: {original_query}")
|
|
|
|
| 251 |
"""
|
| 252 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
| 253 |
formatted_prompt = prompt_val.format(context=context_str, original_question=question, rephrased_query=rephrased_query)
|
| 254 |
+
|
| 255 |
else:
|
| 256 |
if database is None:
|
| 257 |
return "No documents available. Please upload documents or enable web search to answer questions."
|
|
|
|
| 260 |
relevant_docs = retriever.get_relevant_documents(question)
|
| 261 |
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
| 262 |
|
| 263 |
+
# Reduce context if we're not on the first attempt
|
| 264 |
+
if attempt > 0:
|
| 265 |
+
words = context_str.split()
|
| 266 |
+
context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
|
| 267 |
+
|
| 268 |
prompt_template = """
|
| 269 |
Answer the question based on the following context:
|
| 270 |
Context:
|
|
|
|
| 302 |
|
| 303 |
return answer
|
| 304 |
|
| 305 |
+
except Exception as e:
|
| 306 |
+
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
| 307 |
+
if "Input validation error" in str(e) and attempt < max_attempts - 1:
|
| 308 |
+
print(f"Reducing context length for next attempt")
|
| 309 |
+
elif attempt == max_attempts - 1:
|
| 310 |
+
return f"I apologize, but I'm having trouble processing your question due to its length or complexity. Could you please try rephrasing it more concisely?"
|
| 311 |
+
|
| 312 |
+
return "An unexpected error occurred. Please try again later."
|
| 313 |
+
|
| 314 |
# Gradio interface
|
| 315 |
with gr.Blocks() as demo:
|
| 316 |
gr.Markdown("# Chat with your PDF documents and Web Search")
|