abhilashnl2006 commited on
Commit
c784bd4
1 Parent(s): 61bba61
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,38 +1,43 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
- def wikipedia_search(query):
5
- url = "https://en.wikipedia.org/w/api.php"
6
- params = {
7
- "action": "query",
8
- "format": "json",
9
- "prop": "extracts",
10
- "exintro": True,
11
- "explaintext": True,
12
- "titles": query,
13
  }
14
- response = requests.get(url, params=params)
 
15
  data = response.json()
16
- page = next(iter(data["query"]["pages"].values()))
17
- return page.get("extract", "Sorry, I couldn't find any information on that topic.")
 
 
 
18
 
19
  def chatbot_response(message, history):
20
- response = wikipedia_search(message)
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 about a topic...", container=False, scale=7),
28
- title="Wikipedia Search Chatbot",
29
- description="This chatbot searches Wikipedia for information on the topic you ask about.",
30
  theme="soft",
31
  examples=[
32
- "Python programming language",
33
- "Artificial Intelligence",
34
- "Gradio library",
35
- "Machine Learning",
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,