Update app.py
Browse files
app.py
CHANGED
|
@@ -85,7 +85,7 @@ def get_embeddings():
|
|
| 85 |
|
| 86 |
def duckduckgo_search(query):
|
| 87 |
with DDGS() as ddgs:
|
| 88 |
-
results = ddgs.text(query, max_results=5)
|
| 89 |
return results
|
| 90 |
|
| 91 |
class CitingSources(BaseModel):
|
|
@@ -127,10 +127,8 @@ def respond(message, history, model, temperature, num_calls, use_embeddings, sys
|
|
| 127 |
logging.info(f"System Prompt: {system_prompt}")
|
| 128 |
|
| 129 |
try:
|
| 130 |
-
for main_content,
|
| 131 |
-
|
| 132 |
-
first_line = response.split('\n')[0] if response else ''
|
| 133 |
-
yield response
|
| 134 |
except Exception as e:
|
| 135 |
logging.error(f"Error with {model}: {str(e)}")
|
| 136 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
|
@@ -146,31 +144,15 @@ def create_web_search_vectors(search_results):
|
|
| 146 |
|
| 147 |
return FAISS.from_documents(documents, embed)
|
| 148 |
|
| 149 |
-
def
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
if not web_search_database:
|
| 156 |
-
yield "No web search results available. Please try again.", ""
|
| 157 |
-
return
|
| 158 |
-
|
| 159 |
-
retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
|
| 160 |
-
relevant_docs = retriever.get_relevant_documents(query)
|
| 161 |
-
|
| 162 |
-
context = "\n".join([doc.page_content for doc in relevant_docs])
|
| 163 |
-
else:
|
| 164 |
-
context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
| 169 |
-
After writing the document, please provide a list of sources with their URLs used in your response."""
|
| 170 |
|
| 171 |
-
# Use Hugging Face API
|
| 172 |
-
client = InferenceClient(model, token=huggingface_token)
|
| 173 |
-
|
| 174 |
# Calculate input tokens (this is an approximation, you might need a more accurate method)
|
| 175 |
input_tokens = len(prompt.split()) // 4
|
| 176 |
|
|
@@ -178,46 +160,51 @@ After writing the document, please provide a list of sources with their URLs use
|
|
| 178 |
model_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default to 8192 if model not found
|
| 179 |
|
| 180 |
# Calculate max_new_tokens
|
| 181 |
-
max_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap at
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
def vote(data: gr.LikeData):
|
| 223 |
if data.liked:
|
|
|
|
| 85 |
|
| 86 |
def duckduckgo_search(query):
|
| 87 |
with DDGS() as ddgs:
|
| 88 |
+
results = list(ddgs.text(query, max_results=5))
|
| 89 |
return results
|
| 90 |
|
| 91 |
class CitingSources(BaseModel):
|
|
|
|
| 127 |
logging.info(f"System Prompt: {system_prompt}")
|
| 128 |
|
| 129 |
try:
|
| 130 |
+
for main_content, _ in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):
|
| 131 |
+
yield main_content
|
|
|
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
logging.error(f"Error with {model}: {str(e)}")
|
| 134 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
|
|
|
| 144 |
|
| 145 |
return FAISS.from_documents(documents, embed)
|
| 146 |
|
| 147 |
+
def summarize_article(article, model, system_prompt, user_query, client, temperature=0.2):
|
| 148 |
+
prompt = f"""Using the following article:
|
| 149 |
+
Title: {article['title']}
|
| 150 |
+
Content: {article['body']}
|
| 151 |
+
URL: {article['href']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
Write a concise summary that addresses the following user query: '{user_query}'
|
| 154 |
+
"""
|
|
|
|
|
|
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
# Calculate input tokens (this is an approximation, you might need a more accurate method)
|
| 157 |
input_tokens = len(prompt.split()) // 4
|
| 158 |
|
|
|
|
| 160 |
model_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default to 8192 if model not found
|
| 161 |
|
| 162 |
# Calculate max_new_tokens
|
| 163 |
+
max_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap at 6500 to be safe
|
| 164 |
+
|
| 165 |
+
try:
|
| 166 |
+
response = client.chat_completion(
|
| 167 |
+
messages=[
|
| 168 |
+
{"role": "system", "content": system_prompt},
|
| 169 |
+
{"role": "user", "content": prompt}
|
| 170 |
+
],
|
| 171 |
+
max_tokens=max_new_tokens,
|
| 172 |
+
temperature=temperature,
|
| 173 |
+
stream=False,
|
| 174 |
+
top_p=0.8,
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
if hasattr(response, 'choices') and response.choices:
|
| 178 |
+
for choice in response.choices:
|
| 179 |
+
if hasattr(choice, 'message') and hasattr(choice.message, 'content'):
|
| 180 |
+
return choice.message.content.strip()
|
| 181 |
+
except Exception as e:
|
| 182 |
+
logging.error(f"Error summarizing article: {str(e)}")
|
| 183 |
+
return f"Error summarizing article: {str(e)}"
|
| 184 |
+
|
| 185 |
+
return "Unable to generate summary."
|
| 186 |
+
|
| 187 |
+
def get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
| 188 |
+
search_results = duckduckgo_search(query)
|
| 189 |
+
client = InferenceClient(model, token=huggingface_token)
|
| 190 |
+
|
| 191 |
+
summaries = []
|
| 192 |
+
for result in search_results:
|
| 193 |
+
summary = summarize_article(result, model, system_prompt, query, client, temperature)
|
| 194 |
+
summaries.append({
|
| 195 |
+
"title": result['title'],
|
| 196 |
+
"url": result['href'],
|
| 197 |
+
"summary": summary
|
| 198 |
+
})
|
| 199 |
+
yield format_output(summaries), ""
|
| 200 |
+
|
| 201 |
+
def format_output(summaries):
|
| 202 |
+
output = "Here are the summarized search results:\n\n"
|
| 203 |
+
for item in summaries:
|
| 204 |
+
output += f"News Title: {item['title']}\n"
|
| 205 |
+
output += f"URL: {item['url']}\n"
|
| 206 |
+
output += f"Summary: {item['summary']}\n\n"
|
| 207 |
+
return output
|
| 208 |
|
| 209 |
def vote(data: gr.LikeData):
|
| 210 |
if data.liked:
|