Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -240,18 +240,14 @@ def summarize_content(content, model):
|
|
240 |
if content is None:
|
241 |
return "No content available to summarize."
|
242 |
|
243 |
-
# Approximate the token limit using character count
|
244 |
-
# Assuming an average of 4 characters per token
|
245 |
-
max_chars = 7000 * 4 # Leave some room for the prompt
|
246 |
-
if len(content) > max_chars:
|
247 |
-
content = content[:max_chars] + "..."
|
248 |
-
|
249 |
summary_prompt = f"""
|
250 |
-
Summarize the following
|
251 |
-
|
|
|
|
|
252 |
Summary:
|
253 |
"""
|
254 |
-
summary = generate_chunked_response(model, summary_prompt, max_tokens=
|
255 |
return summary
|
256 |
|
257 |
def rank_search_results(titles, summaries, model):
|
@@ -309,7 +305,6 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
309 |
summary = summarize_content(result["text"], model)
|
310 |
processed_results.append({
|
311 |
"title": result.get("title", f"Result {index}"),
|
312 |
-
"content": result["text"],
|
313 |
"summary": summary,
|
314 |
"index": index
|
315 |
})
|
@@ -323,24 +318,16 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
323 |
|
324 |
print(f"Number of processed results: {len(processed_results)}")
|
325 |
|
326 |
-
#
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
print(f"Error in ranking results: {str(e)}. Using default ranking.")
|
333 |
-
ranks = list(range(1, len(processed_results) + 1))
|
334 |
-
|
335 |
-
print(f"Number of ranks: {len(ranks)}")
|
336 |
|
337 |
-
#
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
# Prepare context for the question
|
342 |
-
context_str = "\n\n".join([f"Title: {r['title']}\nSummary: {r['summary']}\nRank: {ranks[i]}"
|
343 |
-
for i, r in enumerate(processed_results)])
|
344 |
|
345 |
prompt_template = """
|
346 |
Answer the question based on the following web search results:
|
@@ -348,10 +335,12 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
348 |
{context}
|
349 |
Current Question: {question}
|
350 |
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
351 |
-
Provide a concise and direct answer to the question
|
352 |
"""
|
353 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
354 |
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
|
|
|
|
355 |
else:
|
356 |
if database is None:
|
357 |
return "No documents available. Please upload documents or enable web search to answer questions."
|
@@ -368,23 +357,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
368 |
prompt_val = ChatPromptTemplate.from_template(prompt)
|
369 |
formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)
|
370 |
|
371 |
-
|
372 |
-
|
373 |
-
# Extract only the part after the last occurrence of a prompt-like sentence
|
374 |
-
answer_patterns = [
|
375 |
-
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
376 |
-
r"Provide a concise and direct answer to the question:",
|
377 |
-
r"Answer:"
|
378 |
-
]
|
379 |
-
|
380 |
-
for pattern in answer_patterns:
|
381 |
-
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
382 |
-
if len(match) > 1:
|
383 |
-
answer = match[-1].strip()
|
384 |
-
break
|
385 |
-
else:
|
386 |
-
# If no pattern is found, return the full response
|
387 |
-
answer = full_response.strip()
|
388 |
|
389 |
if not web_search:
|
390 |
memory_database[question] = answer
|
@@ -530,11 +503,19 @@ with gr.Blocks() as demo:
|
|
530 |
top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
|
531 |
repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)
|
532 |
web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False)
|
533 |
-
|
534 |
def chat(question, history, temperature, top_p, repetition_penalty, web_search):
|
535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
536 |
history.append((question, answer))
|
537 |
-
|
|
|
538 |
|
539 |
submit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox], outputs=[question_input, chatbot])
|
540 |
|
|
|
240 |
if content is None:
|
241 |
return "No content available to summarize."
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
summary_prompt = f"""
|
244 |
+
Summarize the following news article in 10-15 lines. Focus on the key points, main events, and significant details. Ensure the summary is informative and relevant to current news:
|
245 |
+
|
246 |
+
{content[:3000]} # Limit input to avoid token limits
|
247 |
+
|
248 |
Summary:
|
249 |
"""
|
250 |
+
summary = generate_chunked_response(model, summary_prompt, max_tokens=300) # Adjust max_tokens as needed
|
251 |
return summary
|
252 |
|
253 |
def rank_search_results(titles, summaries, model):
|
|
|
305 |
summary = summarize_content(result["text"], model)
|
306 |
processed_results.append({
|
307 |
"title": result.get("title", f"Result {index}"),
|
|
|
308 |
"summary": summary,
|
309 |
"index": index
|
310 |
})
|
|
|
318 |
|
319 |
print(f"Number of processed results: {len(processed_results)}")
|
320 |
|
321 |
+
# For news requests, return the summaries directly
|
322 |
+
if "news" in question.lower():
|
323 |
+
news_response = "Here are the latest news summaries on this topic:\n\n"
|
324 |
+
for result in processed_results[:5]: # Limit to top 5 results
|
325 |
+
news_response += f"Title: {result['title']}\n\nSummary: {result['summary']}\n\n---\n\n"
|
326 |
+
return news_response.strip()
|
|
|
|
|
|
|
|
|
327 |
|
328 |
+
# For other questions, use the summaries as context
|
329 |
+
context_str = "\n\n".join([f"Title: {r['title']}\nSummary: {r['summary']}"
|
330 |
+
for r in processed_results])
|
|
|
|
|
|
|
|
|
331 |
|
332 |
prompt_template = """
|
333 |
Answer the question based on the following web search results:
|
|
|
335 |
{context}
|
336 |
Current Question: {question}
|
337 |
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
338 |
+
Provide a concise and direct answer to the question:
|
339 |
"""
|
340 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
341 |
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
342 |
+
|
343 |
+
answer = generate_chunked_response(model, formatted_prompt)
|
344 |
else:
|
345 |
if database is None:
|
346 |
return "No documents available. Please upload documents or enable web search to answer questions."
|
|
|
357 |
prompt_val = ChatPromptTemplate.from_template(prompt)
|
358 |
formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)
|
359 |
|
360 |
+
answer = generate_chunked_response(model, formatted_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
|
362 |
if not web_search:
|
363 |
memory_database[question] = answer
|
|
|
503 |
top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
|
504 |
repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)
|
505 |
web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False)
|
|
|
506 |
def chat(question, history, temperature, top_p, repetition_penalty, web_search):
|
507 |
+
answer = ask_question(question, temperature, top_p, repetition_penalty, web_search)
|
508 |
+
|
509 |
+
if "news" in question.lower():
|
510 |
+
# Split the answer into individual news items
|
511 |
+
news_items = answer.split("---")
|
512 |
+
for item in news_items:
|
513 |
+
if item.strip():
|
514 |
+
history.append((question, item.strip()))
|
515 |
+
else:
|
516 |
history.append((question, answer))
|
517 |
+
|
518 |
+
return "", history
|
519 |
|
520 |
submit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox], outputs=[question_input, chatbot])
|
521 |
|