Spaces:
Runtime error
Runtime error
abhilashnl2006
commited on
Commit
•
c784bd4
1
Parent(s):
61bba61
Serper
Browse files
app.py
CHANGED
@@ -1,38 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
"
|
12 |
-
"
|
13 |
}
|
14 |
-
|
|
|
15 |
data = response.json()
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
def chatbot_response(message, history):
|
20 |
-
response =
|
21 |
# Truncate the response if it's too long
|
22 |
return response[:500] + "..." if len(response) > 500 else response
|
23 |
|
24 |
iface = gr.ChatInterface(
|
25 |
chatbot_response,
|
26 |
chatbot=gr.Chatbot(height=400),
|
27 |
-
textbox=gr.Textbox(placeholder="Ask
|
28 |
-
title="
|
29 |
-
description="This chatbot
|
30 |
theme="soft",
|
31 |
examples=[
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
],
|
37 |
cache_examples=True,
|
38 |
retry_btn=None,
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import os
|
4 |
|
5 |
+
# You should set your Serper API key as an environment variable
|
6 |
+
SERPER_API_KEY = "37228930df7532489158d04f41333979853b1422"
|
7 |
+
|
8 |
+
def serper_search(query):
|
9 |
+
url = "https://google.serper.dev/search"
|
10 |
+
payload = {"q": query}
|
11 |
+
headers = {
|
12 |
+
"X-API-KEY": SERPER_API_KEY,
|
13 |
+
"Content-Type": "application/json"
|
14 |
}
|
15 |
+
|
16 |
+
response = requests.post(url, json=payload, headers=headers)
|
17 |
data = response.json()
|
18 |
+
|
19 |
+
if 'organic' in data and len(data['organic']) > 0:
|
20 |
+
return data['organic'][0]['snippet']
|
21 |
+
else:
|
22 |
+
return "Sorry, I couldn't find any information on that topic."
|
23 |
|
24 |
def chatbot_response(message, history):
|
25 |
+
response = serper_search(message)
|
26 |
# Truncate the response if it's too long
|
27 |
return response[:500] + "..." if len(response) > 500 else response
|
28 |
|
29 |
iface = gr.ChatInterface(
|
30 |
chatbot_response,
|
31 |
chatbot=gr.Chatbot(height=400),
|
32 |
+
textbox=gr.Textbox(placeholder="Ask a question...", container=False, scale=7),
|
33 |
+
title="Serper Search Chatbot",
|
34 |
+
description="This chatbot uses Serper API to search for information on the topic you ask about.",
|
35 |
theme="soft",
|
36 |
examples=[
|
37 |
+
"What is machine learning?",
|
38 |
+
"Latest news about AI",
|
39 |
+
"How does blockchain work?",
|
40 |
+
"Benefits of exercise",
|
41 |
],
|
42 |
cache_examples=True,
|
43 |
retry_btn=None,
|