File size: 1,275 Bytes
d80673e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import gradio as gr
from transformers import pipeline
# Load a pre-trained question answering 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.", ""
# Define the Gradio interface
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.",
)
# Launch the Gradio app
iface.launch() |