|
import gradio as gr |
|
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
|
|
|
|
|
question_answerer = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad") |
|
|
|
|
|
|
|
|
|
def answer_question(context, question): |
|
|
|
""" |
|
|
|
Takes context and a question as input and returns the predicted answer. |
|
|
|
""" |
|
|
|
if context and question: |
|
|
|
result = question_answerer(question=question, context=context) |
|
|
|
answer = result['answer'] |
|
|
|
confidence = f"{result['score']:.4f}" |
|
|
|
return f"Answer: {answer}", f"Confidence: {confidence}" |
|
|
|
else: |
|
|
|
return "Please provide both context and a question.", "" |
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
|
fn=answer_question, |
|
|
|
inputs=[ |
|
|
|
gr.Textbox(lines=7, placeholder="Enter the context here..."), |
|
|
|
gr.Textbox(placeholder="Ask a question about the context...") |
|
|
|
], |
|
|
|
outputs=[ |
|
|
|
gr.Textbox(label="Predicted Answer"), |
|
|
|
gr.Textbox(label="Confidence Score") |
|
|
|
], |
|
|
|
title="Simple Question Answering", |
|
|
|
description="Enter a block of text (context) and then ask a question about it. The app will try to find the answer within the text.", |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
iface.launch() |