Spaces:
Runtime error
Runtime error
Threatthriver
commited on
Commit
•
1ba4597
1
Parent(s):
16cb777
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,11 @@ from bs4 import BeautifulSoup
|
|
4 |
import requests
|
5 |
from typing import List, Tuple, Optional
|
6 |
|
7 |
-
# Initialize the InferenceClient with the model
|
8 |
-
client = InferenceClient(
|
|
|
|
|
|
|
9 |
|
10 |
def scrape_yahoo_search(query: str, num_results: int = 3) -> Tuple[str, str]:
|
11 |
"""
|
@@ -34,8 +37,8 @@ def scrape_yahoo_search(query: str, num_results: int = 3) -> Tuple[str, str]:
|
|
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(
|
@@ -46,9 +49,9 @@ def scrape_yahoo_search(query: str, num_results: int = 3) -> Tuple[str, str]:
|
|
46 |
return "No results found.", search_url
|
47 |
|
48 |
except requests.RequestException as e:
|
49 |
-
return f"
|
50 |
except Exception as e:
|
51 |
-
return f"
|
52 |
|
53 |
def extract_search_query(message: str, trigger_word: str) -> Optional[str]:
|
54 |
"""
|
@@ -66,8 +69,7 @@ def extract_search_query(message: str, trigger_word: str) -> Optional[str]:
|
|
66 |
parts = lower_message.split(trigger_word, 1)
|
67 |
if len(parts) > 1:
|
68 |
query = parts[1].strip()
|
69 |
-
if query
|
70 |
-
return query
|
71 |
return None
|
72 |
|
73 |
def respond(
|
@@ -96,9 +98,10 @@ def respond(
|
|
96 |
# Check for trigger word and activate search feature if present
|
97 |
trigger_word = "search"
|
98 |
query = extract_search_query(message, trigger_word)
|
|
|
99 |
if query:
|
100 |
-
snippet, url = scrape_yahoo_search(query, num_results=3)
|
101 |
-
message
|
102 |
elif query is None:
|
103 |
message = "Please provide a search query after the trigger word."
|
104 |
|
@@ -129,11 +132,10 @@ def respond(
|
|
129 |
token = response_chunk.choices[0].delta.content
|
130 |
response += token
|
131 |
except Exception as e:
|
132 |
-
return f"
|
133 |
|
134 |
return response
|
135 |
|
136 |
-
|
137 |
# Define the ChatInterface with additional input components for user customization
|
138 |
demo = gr.ChatInterface(
|
139 |
fn=respond,
|
|
|
4 |
import requests
|
5 |
from typing import List, Tuple, Optional
|
6 |
|
7 |
+
# Initialize the InferenceClient with the specified model and API key
|
8 |
+
client = InferenceClient(
|
9 |
+
model="meta-llama/Meta-Llama-3.1-405B-Instruct",
|
10 |
+
token="" # Replace with your API key
|
11 |
+
)
|
12 |
|
13 |
def scrape_yahoo_search(query: str, num_results: int = 3) -> Tuple[str, str]:
|
14 |
"""
|
|
|
37 |
if result_elements:
|
38 |
for result in result_elements:
|
39 |
title = result.find('h3').get_text(strip=True) if result.find('h3') else "No title"
|
40 |
+
snippet = result.find('div', {'class': 'compText aAbs'}).get_text(strip=True) if result.find('div', {'class': 'compText aAbs'}) else "No snippet"
|
41 |
+
url = result.find('a')['href'] if result.find('a') else "No URL"
|
42 |
results.append((title, snippet, url))
|
43 |
|
44 |
formatted_results = "\n\n".join(
|
|
|
49 |
return "No results found.", search_url
|
50 |
|
51 |
except requests.RequestException as e:
|
52 |
+
return f"Request error: {str(e)}", search_url
|
53 |
except Exception as e:
|
54 |
+
return f"Processing error: {str(e)}", search_url
|
55 |
|
56 |
def extract_search_query(message: str, trigger_word: str) -> Optional[str]:
|
57 |
"""
|
|
|
69 |
parts = lower_message.split(trigger_word, 1)
|
70 |
if len(parts) > 1:
|
71 |
query = parts[1].strip()
|
72 |
+
return query if query else None
|
|
|
73 |
return None
|
74 |
|
75 |
def respond(
|
|
|
98 |
# Check for trigger word and activate search feature if present
|
99 |
trigger_word = "search"
|
100 |
query = extract_search_query(message, trigger_word)
|
101 |
+
|
102 |
if query:
|
103 |
+
snippet, url = scrape_yahoo_search(query, num_results=3)
|
104 |
+
message += f"\n\nWeb Content:\n{snippet}\nSource: {url}"
|
105 |
elif query is None:
|
106 |
message = "Please provide a search query after the trigger word."
|
107 |
|
|
|
132 |
token = response_chunk.choices[0].delta.content
|
133 |
response += token
|
134 |
except Exception as e:
|
135 |
+
return f"AI model error: {str(e)}"
|
136 |
|
137 |
return response
|
138 |
|
|
|
139 |
# Define the ChatInterface with additional input components for user customization
|
140 |
demo = gr.ChatInterface(
|
141 |
fn=respond,
|