Shreyas094 commited on
Commit
34018a5
·
verified ·
1 Parent(s): 0f26a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -68
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, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, use_embeddings=use_embeddings, system_prompt=system_prompt):
131
- response = f"{main_content}\n\n{sources}"
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 get_response_with_search(query, model, num_calls=3, temperature=0.2, use_embeddings=True, system_prompt=DEFAULT_SYSTEM_PROMPT):
150
- search_results = duckduckgo_search(query)
151
-
152
- if use_embeddings:
153
- web_search_database = create_web_search_vectors(search_results)
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
- prompt = f"""Using the following context from web search results:
167
- {context}
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 4096 to be safe
182
-
183
- main_content = ""
184
- for i in range(num_calls):
185
- try:
186
- response = client.chat_completion(
187
- messages=[
188
- {"role": "system", "content": system_prompt},
189
- {"role": "user", "content": prompt}
190
- ],
191
- max_tokens=max_new_tokens,
192
- temperature=temperature,
193
- stream=False,
194
- top_p=0.8,
195
- )
196
-
197
- # Log the raw response for debugging
198
- logging.info(f"Raw API response: {response}")
199
-
200
- # Check if the response is a string (which might be an error message)
201
- if isinstance(response, str):
202
- logging.error(f"API returned an unexpected string response: {response}")
203
- yield f"An error occurred: {response}", ""
204
- return
205
-
206
- # If it's not a string, assume it's the expected object structure
207
- if hasattr(response, 'choices') and response.choices:
208
- for choice in response.choices:
209
- if hasattr(choice, 'message') and hasattr(choice.message, 'content'):
210
- chunk = choice.message.content
211
- main_content += chunk
212
- yield main_content, "" # Yield partial main content without sources
213
- else:
214
- logging.error(f"Unexpected response structure: {response}")
215
- yield "An unexpected error occurred. Please try again.", ""
216
-
217
- except Exception as e:
218
- logging.error(f"Error in API call: {str(e)}")
219
- yield f"An error occurred: {str(e)}", ""
220
- return
 
 
 
 
 
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: