victor HF staff commited on
Commit
4da1980
1 Parent(s): e8c7350

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -4,37 +4,32 @@ import requests
4
  import gradio as gr
5
  from transformers import pipeline
6
 
7
- class WikipediaBot:
8
- SEARCH_TEMPLATE = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json"
9
- CONTENT_TEMPLATE = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s"
10
-
11
- def __init__(self):
12
- self.summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
13
-
14
- @spaces.GPU
15
- def search_wikipedia(self, query):
16
- query = urllib.parse.quote_plus(query)
17
- data = requests.get(self.SEARCH_TEMPLATE % query).json()
18
- if data and data[1]:
19
- page = urllib.parse.quote_plus(data[1][0])
20
- content = requests.get(self.CONTENT_TEMPLATE % page).json()
21
- content = list(content["query"]["pages"].values())[0]["extract"]
22
- summary = self.summarizer(content, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
23
- source = data[3][0]
24
- return summary, source
25
- else:
26
- return "No results found.", ""
27
-
28
- def chatbot_response(self, message, history):
29
- summary, source = self.search_wikipedia(message)
30
- response = f"Here's a summary of the top Wikipedia result:\n\n{summary}"
31
- if source:
32
- response += f"\n\nSource: {source}"
33
- history.append((message, response))
34
- return history
35
-
36
- def create_chatbot():
37
- return WikipediaBot()
38
 
39
  demo = gr.Blocks()
40
 
@@ -46,9 +41,7 @@ with demo:
46
  msg = gr.Textbox(label="Enter your query")
47
  clear = gr.Button("Clear")
48
 
49
- bot = create_chatbot()
50
-
51
- msg.submit(bot.chatbot_response, [msg, chatbot], chatbot).then(
52
  lambda: gr.update(value=""), None, [msg], queue=False
53
  )
54
  clear.click(lambda: None, None, chatbot, queue=False)
 
4
  import gradio as gr
5
  from transformers import pipeline
6
 
7
+ SEARCH_TEMPLATE = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json"
8
+ CONTENT_TEMPLATE = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s"
9
+
10
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
11
+
12
+ @spaces.GPU
13
+ def search_wikipedia(query):
14
+ query = urllib.parse.quote_plus(query)
15
+ data = requests.get(SEARCH_TEMPLATE % query).json()
16
+ if data and data[1]:
17
+ page = urllib.parse.quote_plus(data[1][0])
18
+ content = requests.get(CONTENT_TEMPLATE % page).json()
19
+ content = list(content["query"]["pages"].values())[0]["extract"]
20
+ summary = summarizer(content, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
21
+ source = data[3][0]
22
+ return summary, source
23
+ else:
24
+ return "No results found.", ""
25
+
26
+ def chatbot_response(message, history):
27
+ summary, source = search_wikipedia(message)
28
+ response = f"Here's a summary of the top Wikipedia result:\n\n{summary}"
29
+ if source:
30
+ response += f"\n\nSource: {source}"
31
+ history.append((message, response))
32
+ return history
 
 
 
 
 
33
 
34
  demo = gr.Blocks()
35
 
 
41
  msg = gr.Textbox(label="Enter your query")
42
  clear = gr.Button("Clear")
43
 
44
+ msg.submit(chatbot_response, [msg, chatbot], chatbot).then(
 
 
45
  lambda: gr.update(value=""), None, [msg], queue=False
46
  )
47
  clear.click(lambda: None, None, chatbot, queue=False)