tyang commited on
Commit
d512ec9
1 Parent(s): b854aec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import wikipedia
3
+ import random
4
+ import gradio as gr
5
+
6
+ model_name = "deepset/electra-base-squad2"
7
+ nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
8
+
9
+
10
+ def get_wiki_article(topic):
11
+ topic=topic
12
+ try:
13
+ search = wikipedia.search(topic, results = 1)[0]
14
+ except wikipedia.DisambiguationError as e:
15
+ choices = [x for x in e.options if ('disambiguation' not in x) and ('All pages' not in x) and (x!=topic)]
16
+ search = random.choice(choices)
17
+ try:
18
+ p = wikipedia.page(search)
19
+ except wikipedia.exceptions.DisambiguationError as e:
20
+ choices = [x for x in e.options if ('disambiguation' not in x) and ('All pages' not in x) and (x!=topic)]
21
+ s = random.choice(choices)
22
+ p = wikipedia.page(s)
23
+ return p.content, p.url
24
+
25
+ def get_answer(topic, question):
26
+ w_art, w_url=get_wiki_article(topic)
27
+ qa = {'question': question, 'context': w_art}
28
+ res = nlp(qa)
29
+ return res['answer'], w_url, {'confidence':res['score']}
30
+
31
+
32
+ inputs = [
33
+ gr.inputs.Textbox(lines=5, label="Topic"),
34
+ gr.inputs.Textbox(lines=5, label="Question")
35
+ ]
36
+ outputs = [
37
+ gr.outputs.Textbox(type='str',label="Answer"),
38
+ gr.outputs.Textbox(type='str',label="Wikipedia Reference Article"),
39
+ gr.outputs.Label(type="confidences",label="Confidence in answer (assuming the correct wikipedia article)"),
40
+ ]
41
+
42
+ title = "Question Answering with ELECTRA and Wikipedia"
43
+ description = 'Please note that topics with long articles may take around a minute. If you get an error, please try double checking spelling, or try a more specific topic (e.g. George H. Bush instead of George Bush).'
44
+ article = ''
45
+ examples = [
46
+ ["Unabomber","What radicalized him?"],
47
+ ["George H. Bush","Did he pursue higher education?"],
48
+ ["Jayson Tatum","How was he percieved coming out of high school?"],
49
+
50
+ ]
51
+
52
+ gr.Interface(get_answer, inputs, outputs, title=title, description=description, article=article,
53
+ theme="darkdefault", examples=examples, flagging_options=["strongly related","related", "neutral", "unrelated", "stongly unrelated"]).launch(share=True,enable_queue=False)