Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,37 +4,32 @@ import requests
|
|
4 |
import gradio as gr
|
5 |
from transformers import pipeline
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
response
|
31 |
-
|
32 |
-
|
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 |
-
|
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)
|