tyang's picture
Create app.py
d512ec9
raw
history blame
2.18 kB
from transformers import pipeline
import wikipedia
import random
import gradio as gr
model_name = "deepset/electra-base-squad2"
nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
def get_wiki_article(topic):
topic=topic
try:
search = wikipedia.search(topic, results = 1)[0]
except wikipedia.DisambiguationError as e:
choices = [x for x in e.options if ('disambiguation' not in x) and ('All pages' not in x) and (x!=topic)]
search = random.choice(choices)
try:
p = wikipedia.page(search)
except wikipedia.exceptions.DisambiguationError as e:
choices = [x for x in e.options if ('disambiguation' not in x) and ('All pages' not in x) and (x!=topic)]
s = random.choice(choices)
p = wikipedia.page(s)
return p.content, p.url
def get_answer(topic, question):
w_art, w_url=get_wiki_article(topic)
qa = {'question': question, 'context': w_art}
res = nlp(qa)
return res['answer'], w_url, {'confidence':res['score']}
inputs = [
gr.inputs.Textbox(lines=5, label="Topic"),
gr.inputs.Textbox(lines=5, label="Question")
]
outputs = [
gr.outputs.Textbox(type='str',label="Answer"),
gr.outputs.Textbox(type='str',label="Wikipedia Reference Article"),
gr.outputs.Label(type="confidences",label="Confidence in answer (assuming the correct wikipedia article)"),
]
title = "Question Answering with ELECTRA and Wikipedia"
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).'
article = ''
examples = [
["Unabomber","What radicalized him?"],
["George H. Bush","Did he pursue higher education?"],
["Jayson Tatum","How was he percieved coming out of high school?"],
]
gr.Interface(get_answer, inputs, outputs, title=title, description=description, article=article,
theme="darkdefault", examples=examples, flagging_options=["strongly related","related", "neutral", "unrelated", "stongly unrelated"]).launch(share=True,enable_queue=False)