awacke1 commited on
Commit
b281633
1 Parent(s): 9622941

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import wikipedia
3
+ import random
4
+ import gradio as gr
5
+ model_name = "deepset/electra-base-squad2"
6
+ nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
7
+
8
+ def get_wiki_article(topic):
9
+ topic=topic
10
+ try:
11
+ search = wikipedia.search(topic, results = 1)[0]
12
+ except wikipedia.DisambiguationError as e:
13
+ choices = [x for x in e.options if ('disambiguation' not in x) and ('All pages' not in x) and (x!=topic)]
14
+ search = random.choice(choices)
15
+ try:
16
+ p = wikipedia.page(search)
17
+ except wikipedia.exceptions.DisambiguationError as e:
18
+ choices = [x for x in e.options if ('disambiguation' not in x) and ('All pages' not in x) and (x!=topic)]
19
+ s = random.choice(choices)
20
+ p = wikipedia.page(s)
21
+ return p.content, p.url
22
+
23
+ def get_answer(topic, question):
24
+ w_art, w_url=get_wiki_article(topic)
25
+ qa = {'question': question, 'context': w_art}
26
+ res = nlp(qa)
27
+ return res['answer'], w_url, {'confidence':res['score']}
28
+
29
+
30
+ inputs = [
31
+ gr.inputs.Textbox(lines=5, label="Topic"),
32
+ gr.inputs.Textbox(lines=5, label="Question")
33
+ ]
34
+ outputs = [
35
+ gr.outputs.Textbox(type='str',label="Answer"),
36
+ gr.outputs.Textbox(type='str',label="Wikipedia Reference Article"),
37
+ gr.outputs.Label(type="confidences",label="Confidence in answer (assuming the correct wikipedia article)"),
38
+ ]
39
+
40
+ title = "AI Wikipedia Search"
41
+ description = 'Contextual Question and Answer'
42
+ article = ''
43
+ examples = [
44
+ ['State of Minnesota', 'What birds and mammals are in Minnesota?'],
45
+ ['Syncope', 'What are possible causes of it?'],
46
+ ['Marvel', 'Who are the artists?'],
47
+ ['DC Comics', 'Who are the artists?'],
48
+ ['Artwork', 'What are the top paintings sold at auction?']
49
+ ]
50
+
51
+ gr.Interface(get_answer, inputs, outputs, title=title, description=description, article=article, examples=examples, flagging_options=["strongly related","related", "neutral", "unrelated", "strongly unrelated"]).launch(share=False,enable_queue=False)