skura commited on
Commit
edb6019
1 Parent(s): 50d558f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=2, label="Topic"),
32
+ gr.inputs.Textbox(lines=2, 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
+ ['Quantum', 'What is quanta in physics?'],
45
+ ['Cicero', 'What quotes did Marcus Tullius Cicero make?'],
46
+ ['Alzheimers', 'What causes alzheimers?'],
47
+ ['Neuropathy', 'With neuropathy and neuro-muskoskeletal issues, and what are the treatments available?'],
48
+ ['Chemotherapy', 'What are possible care options for patients in chemotherapy?'],
49
+ ['Health', 'What is mindfulness and how does it affect health?'],
50
+ ['Medicine', 'In medicine what is the Hippocratic Oath?'],
51
+ ['Insurance', 'What is Medicare?'],
52
+ ['Financial Services', 'Does Medicaid offer financial assistance?'],
53
+ ['Ontology', 'Why is an anthology different than ontology?'],
54
+ ['Taxonomy', 'What is a biology taxonomy?'],
55
+ ['Pharmacy', 'What does a pharmacist do?']
56
+ ]
57
+
58
+ 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)