Threatthriver commited on
Commit
960cefd
1 Parent(s): 51241b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -17
app.py CHANGED
@@ -2,37 +2,67 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from bs4 import BeautifulSoup
4
  import requests
5
- from typing import List, Tuple
6
 
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) -> Tuple[str, str]:
11
  """
12
- Scrapes Yahoo search results for the given query and returns the top result's snippet and URL.
13
 
14
  Args:
15
  query (str): The search query.
 
16
 
17
  Returns:
18
- Tuple[str, str]: The snippet and URL of the top search result.
19
  """
20
  search_url = f"https://search.yahoo.com/search?p={query}"
 
 
 
 
21
  try:
22
- response = requests.get(search_url)
23
  response.raise_for_status()
24
  soup = BeautifulSoup(response.content, 'html.parser')
25
- # Find the top search result snippet and URL
26
- result = soup.find('div', {'class': 'dd algo algo-sr Sr'})
27
- if result:
28
  snippet = result.find('div', {'class': 'compText aAbs'}).get_text(strip=True)
29
  url = result.find('a')['href']
30
- return snippet, url
 
 
 
 
31
  else:
32
  return "No results found.", search_url
 
33
  except Exception as e:
34
  return f"An error occurred while scraping Yahoo: {str(e)}", search_url
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def respond(
37
  message: str,
38
  history: List[Tuple[str, str]],
@@ -58,14 +88,12 @@ def respond(
58
 
59
  # Check for trigger word and activate search feature if present
60
  trigger_word = "search"
61
- if trigger_word in message.lower():
62
- # Extract the query from the message
63
- query = message.lower().split(trigger_word, 1)[-1].strip()
64
- if query:
65
- snippet, url = scrape_yahoo_search(query)
66
- message = f"{message}\n\nWeb Content:\n{snippet}\nSource: {url}"
67
- else:
68
- message = "Please provide a search query after the trigger word."
69
 
70
  # Prepare the conversation history for the API call
71
  messages = [{"role": "system", "content": system_message}]
 
2
  from huggingface_hub import InferenceClient
3
  from bs4 import BeautifulSoup
4
  import requests
5
+ from typing import List, Tuple, Optional
6
 
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 = {
23
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
24
+ }
25
+
26
  try:
27
+ response = requests.get(search_url, headers=headers)
28
  response.raise_for_status()
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
+ """
48
+ Extracts the search query from the message based on the trigger word.
49
+
50
+ Args:
51
+ message (str): The user's input message.
52
+ trigger_word (str): The word that activates the search feature.
53
+
54
+ Returns:
55
+ Optional[str]: The extracted search query if found, otherwise None.
56
+ """
57
+ lower_message = message.lower()
58
+ if trigger_word in lower_message:
59
+ parts = lower_message.split(trigger_word, 1)
60
+ if len(parts) > 1:
61
+ query = parts[1].strip()
62
+ if query:
63
+ return query
64
+ return None
65
+
66
  def respond(
67
  message: str,
68
  history: List[Tuple[str, str]],
 
88
 
89
  # Check for trigger word and activate search feature if present
90
  trigger_word = "search"
91
+ query = extract_search_query(message, trigger_word)
92
+ if query:
93
+ snippet, url = scrape_yahoo_search(query, num_results=3) # Retrieve top 3 results
94
+ message = f"{message}\n\nWeb Content:\n{snippet}\nSource: {url}"
95
+ elif query is None:
96
+ message = "Please provide a search query after the trigger word."
 
 
97
 
98
  # Prepare the conversation history for the API call
99
  messages = [{"role": "system", "content": system_message}]