Update app.py
Browse files84 unmodified lines8586878889909135 unmodified lines1271281291301311321331341351369 unmodified lines1461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761 unmodified line17817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222384 unmodified linesdef duckduckgo_search(query):with DDGS() as ddgs:results = ddgs.text(query, max_results=5)return resultsclass CitingSources(BaseModel):35 unmodified lineslogging.info(f"System Prompt: {system_prompt}")try:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''yield responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."9 unmodified linesreturn FAISS.from_documents(documents, embed)def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):search_results = duckduckgo_search(query)if use_embeddings:web_search_database = create_web_search_vectors(search_results)if not web_search_database:yield "No web search results available. Please try again.", ""returnretriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])else:context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])prompt = f"""Using the following context from web search results:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources with their URLs used in your response."""# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)# Calculate input tokens (this is an approximation, you might need a more accurate method)input_tokens = len(prompt.split()) // 41 unmodified linemodel_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default to 8192 if model not found# Calculate max_new_tokensmax_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap at 4096 to be safemain_content = ""for i in range(num_calls):try:response = client.chat_completion(messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": prompt}],max_tokens=max_new_tokens,temperature=temperature,stream=False,top_p=0.8,)# Log the raw response for debugginglogging.info(f"Raw API response: {response}")# Check if the response is a string (which might be an error message)if isinstance(response, str):logging.error(f"API returned an unexpected string response: {response}")yield f"An error occurred: {response}", ""return# If it's not a string, assume it's the expected object structureif hasattr(response, 'choices') and response.choices:for choice in response.choices:if hasattr(choice, 'message') and hasattr(choice.message, 'content'):chunk = choice.message.contentmain_content += chunkyield main_content, "" # Yield partial main content without sourceselse:logging.error(f"Unexpected response structure: {response}")yield "An unexpected error occurred. Please try again.", ""except Exception as e:logging.error(f"Error in API call: {str(e)}")yield f"An error occurred: {str(e)}", ""returndef vote(data: gr.LikeData):if data.liked:84 unmodified lines8586878889909135 unmodified lines1271281291301311321331349 unmodified lines1441451461471481491501511521531541551561571581 unmodified line16016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921084 unmodified linesdef duckduckgo_search(query):with DDGS() as ddgs:results = list(ddgs.text(query, max_results=5))return resultsclass CitingSources(BaseModel):35 unmodified lineslogging.info(f"System Prompt: {system_prompt}")try:for main_content, _ in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):yield main_contentexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."9 unmodified linesreturn FAISS.from_documents(documents, embed)def summarize_article(article, model, system_prompt, user_query, client, temperature=0.2):prompt = f"""Using the following article:Title: {article['title']}Content: {article['body']}URL: {article['href']}Write a concise summary that addresses the following user query: '{user_query}'"""# Calculate input tokens (this is an approximation, you might need a more accurate method)input_tokens = len(prompt.split()) // 41 unmodified linemodel_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default to 8192 if model not found# Calculate max_new_tokensmax_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap at 6500 to be safetry:response = client.chat_completion(messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": prompt}],max_tokens=max_new_tokens,temperature=temperature,stream=False,top_p=0.8,)if hasattr(response, 'choices') and response.choices:for choice in response.choices:if hasattr(choice, 'message') and hasattr(choice.message, 'content'):return choice.message.content.strip()except Exception as e:logging.error(f"Error summarizing article: {str(e)}")return f"Error summarizing article: {str(e)}"return "Unable to generate summary."def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):search_results = duckduckgo_search(query)client = InferenceClient(model, token=huggingface_token)summaries = []for result in search_results:summary = summarize_article(result, model, system_prompt, query, client, temperature)summaries.append({"title": result['title'],"url": result['href'],"summary": summary})yield format_output(summaries), ""def format_output(summaries):output = "Here are the summarized search results:\n\n"for item in summaries:output += f"News Title: {item['title']}\n"output += f"URL: {item['url']}\n"output += f"Summary: {item['summary']}\n\n"return outputdef vote(data: gr.LikeData):if data.liked:
app.py+55-68