davidmezzetti commited on
Commit
79c2d9a
1 Parent(s): 545d424

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Application that queries Wikipedia API and summarizes the top result.
3
+ """
4
+
5
+ import os
6
+ import urllib.parse
7
+
8
+ import requests
9
+ import streamlit as st
10
+
11
+ from txtai.pipeline import Summary
12
+
13
+
14
+ class Application:
15
+ """
16
+ Main application.
17
+ """
18
+
19
+ SEARCH_TEMPLATE = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json"
20
+ CONTENT_TEMPLATE = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s"
21
+
22
+ def __init__(self):
23
+ """
24
+ Creates a new application.
25
+ """
26
+
27
+ self.summary = Summary("sshleifer/distilbart-cnn-12-6")
28
+
29
+ def run(self):
30
+ """
31
+ Runs a Streamlit application.
32
+ """
33
+
34
+ st.title("Wikipedia")
35
+ st.markdown("This application queries the Wikipedia API and summarizes the top result.")
36
+
37
+ query = st.text_input("Query")
38
+
39
+ if query:
40
+ query = urllib.parse.quote_plus(query)
41
+ data = requests.get(Application.SEARCH_TEMPLATE % query).json()
42
+ if data and data[1]:
43
+ page = urllib.parse.quote_plus(data[1][0])
44
+ content = requests.get(Application.CONTENT_TEMPLATE % page).json()
45
+ content = list(content["query"]["pages"].values())[0]["extract"]
46
+
47
+ st.write(self.summary(content))
48
+ st.markdown("*Source: " + data[3][0] + "*")
49
+ else:
50
+ st.markdown("*No results found*")
51
+
52
+
53
+ @st.cache(allow_output_mutation=True)
54
+ def create():
55
+ """
56
+ Creates and caches a Streamlit application.
57
+
58
+ Returns:
59
+ Application
60
+ """
61
+
62
+ return Application()
63
+
64
+
65
+ if __name__ == "__main__":
66
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
67
+
68
+ # Create and run application
69
+ app = create()
70
+ app.run()