Threatthriver commited on
Commit
16cb777
1 Parent(s): 960cefd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -7,16 +7,16 @@ from typing import List, Tuple, Optional
7
  # Initialize the InferenceClient with the model ID from Hugging Face
8
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
9
 
10
- def scrape_yahoo_search(query: str, num_results: int = 1) -> Tuple[str, str]:
11
  """
12
- Scrapes Yahoo search results for the given query and returns the top results' snippets and URLs.
13
 
14
  Args:
15
  query (str): The search query.
16
  num_results (int): Number of results to retrieve.
17
 
18
  Returns:
19
- Tuple[str, str]: The snippets and URLs of the top search results.
20
  """
21
  search_url = f"https://search.yahoo.com/search?p={query}"
22
  headers = {
@@ -29,19 +29,26 @@ def scrape_yahoo_search(query: str, num_results: int = 1) -> Tuple[str, str]:
29
  soup = BeautifulSoup(response.content, 'html.parser')
30
 
31
  results = []
32
- for result in soup.find_all('div', {'class': 'dd algo algo-sr Sr'}, limit=num_results):
33
- snippet = result.find('div', {'class': 'compText aAbs'}).get_text(strip=True)
34
- url = result.find('a')['href']
35
- results.append((snippet, url))
36
 
37
- if results:
38
- snippets = "\n\n".join(f"Snippet: {snippet}\nSource: {url}" for snippet, url in results)
39
- return snippets, search_url
 
 
 
 
 
 
 
 
40
  else:
41
  return "No results found.", search_url
42
 
 
 
43
  except Exception as e:
44
- return f"An error occurred while scraping Yahoo: {str(e)}", search_url
45
 
46
  def extract_search_query(message: str, trigger_word: str) -> Optional[str]:
47
  """
 
7
  # Initialize the InferenceClient with the model ID from Hugging Face
8
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
9
 
10
+ def scrape_yahoo_search(query: str, num_results: int = 3) -> Tuple[str, str]:
11
  """
12
+ Scrapes Yahoo search results for the given query and returns detailed snippets and URLs for the top results.
13
 
14
  Args:
15
  query (str): The search query.
16
  num_results (int): Number of results to retrieve.
17
 
18
  Returns:
19
+ Tuple[str, str]: The formatted snippets and URLs of the top search results.
20
  """
21
  search_url = f"https://search.yahoo.com/search?p={query}"
22
  headers = {
 
29
  soup = BeautifulSoup(response.content, 'html.parser')
30
 
31
  results = []
32
+ result_elements = soup.find_all('div', {'class': 'dd algo algo-sr Sr'}, limit=num_results)
 
 
 
33
 
34
+ if result_elements:
35
+ for result in result_elements:
36
+ title = result.find('h3').get_text(strip=True) if result.find('h3') else "No title"
37
+ snippet = result.find('div', {'class': 'compText aAbs'}).get_text(strip=True)
38
+ url = result.find('a')['href']
39
+ results.append((title, snippet, url))
40
+
41
+ formatted_results = "\n\n".join(
42
+ f"Title: {title}\nSnippet: {snippet}\nURL: {url}" for title, snippet, url in results
43
+ )
44
+ return formatted_results, search_url
45
  else:
46
  return "No results found.", search_url
47
 
48
+ except requests.RequestException as e:
49
+ return f"An error occurred while making the request: {str(e)}", search_url
50
  except Exception as e:
51
+ return f"An error occurred while processing the results: {str(e)}", search_url
52
 
53
  def extract_search_query(message: str, trigger_word: str) -> Optional[str]:
54
  """