Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -74,23 +74,41 @@ def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
|
74 |
yield chunk.choices[0].delta.content
|
75 |
|
76 |
def search_web(query):
|
|
|
77 |
try:
|
78 |
search_url = f"https://www.google.com/search?q={query}"
|
79 |
response = requests.get(search_url)
|
80 |
if response.status_code == 200:
|
81 |
soup = BeautifulSoup(response.text, 'html.parser')
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
86 |
url = result.find('a')['href']
|
87 |
snippet = result.find('span', class_='aCOpRe').text
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
else:
|
91 |
-
|
92 |
except Exception as e:
|
93 |
-
|
|
|
94 |
|
95 |
full_response = None # Initialize full_response to None
|
96 |
|
@@ -105,7 +123,7 @@ if prompt := st.chat_input("Enter your prompt here..."):
|
|
105 |
query = prompt.lower().replace("search for", "").strip()
|
106 |
search_results = search_web(query)
|
107 |
formatted_results = "\n\n".join([f"Title: {result['title']}\nURL: {result['url']}\nSnippet: {result['snippet']}" for result in search_results])
|
108 |
-
|
109 |
with st.chat_message("assistant", avatar="🤖"):
|
110 |
full_response = formatted_results
|
111 |
else:
|
|
|
74 |
yield chunk.choices[0].delta.content
|
75 |
|
76 |
def search_web(query):
|
77 |
+
result = {"query": query, "data": {}}
|
78 |
try:
|
79 |
search_url = f"https://www.google.com/search?q={query}"
|
80 |
response = requests.get(search_url)
|
81 |
if response.status_code == 200:
|
82 |
soup = BeautifulSoup(response.text, 'html.parser')
|
83 |
+
|
84 |
+
# Scrape organic search results
|
85 |
+
result["data"]["organic"] = []
|
86 |
+
for result in soup.find_all('div', class_='g'):
|
87 |
+
title = result.find('a')['title']
|
88 |
url = result.find('a')['href']
|
89 |
snippet = result.find('span', class_='aCOpRe').text
|
90 |
+
item = {"title": title, "url": url, "snippet": snippet}
|
91 |
+
result["data"]["organic"].append(item)
|
92 |
+
|
93 |
+
# Scrape knowledge panel
|
94 |
+
result["data"]["knowledge_panel"] = {}
|
95 |
+
if soup.find('div', id='knowledge-kp'):
|
96 |
+
result["data"]["knowledge_panel"]["title"] = soup.find('div', id='knowledge-kp').find('h3').text
|
97 |
+
result["data"]["knowledge_panel"]["content"] = soup.find('div', id='knowledge-kp').find('div', class_='VwiC3b').text
|
98 |
+
|
99 |
+
# Scrape images
|
100 |
+
result["data"]["images"] = []
|
101 |
+
for result in soup.find_all('div', class_='hdtb-mitem hdtb-msel'):
|
102 |
+
title = result.find('a')['title']
|
103 |
+
url = result.find('a')['href']
|
104 |
+
snippet = ""
|
105 |
+
item = {"title": title, "url": url, "snippet": snippet}
|
106 |
+
result["data"]["images"].append(item)
|
107 |
else:
|
108 |
+
result["error"] = "Failed to retrieve search results"
|
109 |
except Exception as e:
|
110 |
+
result["error"] = f"An error occurred: {e}"
|
111 |
+
return result
|
112 |
|
113 |
full_response = None # Initialize full_response to None
|
114 |
|
|
|
123 |
query = prompt.lower().replace("search for", "").strip()
|
124 |
search_results = search_web(query)
|
125 |
formatted_results = "\n\n".join([f"Title: {result['title']}\nURL: {result['url']}\nSnippet: {result['snippet']}" for result in search_results])
|
126 |
+
st.session_state.messages.append({"role": "assistant", "content": formatted_results})
|
127 |
with st.chat_message("assistant", avatar="🤖"):
|
128 |
full_response = formatted_results
|
129 |
else:
|