Shreyas094 commited on
Commit
87e3f69
·
verified ·
1 Parent(s): abc7a3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -55,8 +55,32 @@ def google_search(term, num_results=5, lang="en", timeout=5, safe="active", ssl_
55
  attempts = 0
56
  while len(all_results) < num_results and attempts < max_attempts:
57
  try:
58
- # ... (Google search request remains the same) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
 
 
 
 
60
  for result in result_block:
61
  if len(all_results) >= num_results:
62
  break
@@ -187,7 +211,7 @@ def summarize_financial_news(query):
187
  Provide a detailed, coherent summary focusing on financial implications and analysis."""
188
 
189
  summary = query_llama({"inputs": prompt, "parameters": {"max_length": 1000}})
190
- if summary and 'generated_text' in summary[0]:
191
  summaries.append(summary[0]['generated_text'])
192
 
193
  # Combine summaries
@@ -200,7 +224,7 @@ Focus on the most important financial implications and analysis."""
200
 
201
  final_summary = query_llama({"inputs": final_prompt, "parameters": {"max_length": 2000}})
202
 
203
- if final_summary and 'generated_text' in final_summary[0]:
204
  return final_summary[0]['generated_text']
205
  else:
206
  return "Unable to generate summary due to an error."
 
55
  attempts = 0
56
  while len(all_results) < num_results and attempts < max_attempts:
57
  try:
58
+ # Choose a random user agent
59
+ user_agent = random.choice(_useragent_list)
60
+ headers = {'User-Agent': user_agent}
61
+
62
+ resp = session.get(
63
+ url="https://www.google.com/search",
64
+ headers=headers,
65
+ params={
66
+ "q": search_term,
67
+ "num": num_results - len(all_results),
68
+ "hl": lang,
69
+ "start": start,
70
+ "safe": safe,
71
+ },
72
+ timeout=timeout,
73
+ verify=ssl_verify,
74
+ )
75
+ resp.raise_for_status()
76
+
77
+ soup = BeautifulSoup(resp.text, "html.parser")
78
+ result_block = soup.find_all("div", attrs={"class": "g"})
79
 
80
+ if not result_block:
81
+ print("No more results found.")
82
+ break
83
+
84
  for result in result_block:
85
  if len(all_results) >= num_results:
86
  break
 
211
  Provide a detailed, coherent summary focusing on financial implications and analysis."""
212
 
213
  summary = query_llama({"inputs": prompt, "parameters": {"max_length": 1000}})
214
+ if summary and isinstance(summary, list) and 'generated_text' in summary[0]:
215
  summaries.append(summary[0]['generated_text'])
216
 
217
  # Combine summaries
 
224
 
225
  final_summary = query_llama({"inputs": final_prompt, "parameters": {"max_length": 2000}})
226
 
227
+ if final_summary and isinstance(final_summary, list) and 'generated_text' in final_summary[0]:
228
  return final_summary[0]['generated_text']
229
  else:
230
  return "Unable to generate summary due to an error."