Update app.py
Browse files239 unmodified lines24024124224324424524624724824325025124424524624724824925425025125225351 unmodified lines3053063073123083093107 unmodified lines3183193203263273283293303313323333343353213223233243253263273373383393403413423433283293303313323331 unmodified line33533633735133833934034134234334434534610 unmodified lines357358359371372373374375376377378379380381382383384385386387360361362363139 unmodified lines503504505533506535507508509510511512513514515516517537518519520521239 unmodified lines if content is None: return "No content available to summarize."
# Approximate the token limit using character count # Assuming an average of 4 characters per token max_chars = 7000 * 4 # Leave some room for the prompt if len(content) > max_chars: content = content[:max_chars] + "..." summary_prompt = f""" Summarize the following content concisely: {content} 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:
{content[:3000]} # Limit input to avoid token limits
Summary: """ summary = generate_chunked_response(model, summary_prompt, max_tokens=200) summary = generate_chunked_response(model, summary_prompt, max_tokens=300) # Adjust max_tokens as needed return summary
def rank_search_results(titles, summaries, model):51 unmodified lines summary = summarize_content(result["text"], model) processed_results.append({ "title": result.get("title", f"Result {index}"), "content": result["text"], "summary": summary, "index": index })7 unmodified lines
print(f"Number of processed results: {len(processed_results)}")
# Rank the results titles = [r["title"] for r in processed_results] summaries = [r["summary"] for r in processed_results] try: ranks = rank_search_results(titles, summaries, model) except Exception as e: print(f"Error in ranking results: {str(e)}. Using default ranking.") ranks = list(range(1, len(processed_results) + 1))
print(f"Number of ranks: {len(ranks)}") # For news requests, return the summaries directly if "news" in question.lower(): news_response = "Here are the latest news summaries on this topic:\n\n" for result in processed_results[:5]: # Limit to top 5 results news_response += f"Title: {result['title']}\n\nSummary: {result['summary']}\n\n---\n\n" return news_response.strip()
# Update Vector DB current_date = datetime.now().strftime("%Y-%m-%d") update_vector_db_with_search_results(processed_results, ranks, current_date) # Prepare context for the question context_str = "\n\n".join([f"Title: {r['title']}\nSummary: {r['summary']}\nRank: {ranks[i]}" for i, r in enumerate(processed_results)]) # For other questions, use the summaries as context context_str = "\n\n".join([f"Title: {r['title']}\nSummary: {r['summary']}" for r in processed_results]) prompt_template = """ Answer the question based on the following web search results:1 unmodified line {context} Current Question: {question} If the web search results don't contain relevant information, state that the information is not available in the search results. Provide a concise and direct answer to the question without mentioning the web search or these instructions: Provide a concise and direct answer to the question: """ prompt_val = ChatPromptTemplate.from_template(prompt_template) formatted_prompt = prompt_val.format(context=context_str, question=question) answer = generate_chunked_response(model, formatted_prompt) else: if database is None: return "No documents available. Please upload documents or enable web search to answer questions."10 unmodified lines prompt_val = ChatPromptTemplate.from_template(prompt) formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)
full_response = generate_chunked_response(model, formatted_prompt) # Extract only the part after the last occurrence of a prompt-like sentence answer_patterns = [ r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:", r"Provide a concise and direct answer to the question:", r"Answer:" ] for pattern in answer_patterns: match = re.split(pattern, full_response, flags=re.IGNORECASE) if len(match) > 1: answer = match[-1].strip() break else: # If no pattern is found, return the full response answer = full_response.strip() answer = generate_chunked_response(model, formatted_prompt)
if not web_search: memory_database[question] = answer139 unmodified lines top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1) repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1) web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False) def chat(question, history, temperature, top_p, repetition_penalty, web_search): answer = ask_question(question, temperature, top_p, repetition_penalty, web_search) answer = ask_question(question, temperature, top_p, repetition_penalty, web_search) if "news" in question.lower(): # Split the answer into individual news items news_items = answer.split("---") for item in news_items: if item.strip(): history.append((question, item.strip())) else: history.append((question, answer)) return "", history return "", history submit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox], outputs=[question_input, chatbot]) app.py+29-48