Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "deepset/roberta-base-squad2"
|
7 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Setup the pipeline
|
11 |
+
nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
|
12 |
+
|
13 |
+
def answer_question(context, question):
|
14 |
+
"""
|
15 |
+
Takes a context and a question, and returns the answer based on the context.
|
16 |
+
"""
|
17 |
+
result = nlp(question=question, context=context)
|
18 |
+
return result['answer']
|
19 |
+
|
20 |
+
# Define the Gradio interface
|
21 |
+
iface = gr.Interface(fn=answer_question,
|
22 |
+
inputs=[gr.inputs.Textbox(label="Context", placeholder="Enter the text here..."),
|
23 |
+
gr.inputs.Textbox(label="Question", placeholder="Enter your question here...")],
|
24 |
+
outputs=gr.outputs.Textbox(label="Answer"),
|
25 |
+
title="Question and Answer Assistant",
|
26 |
+
description="Provide a context and ask a question based on that context. The assistant will find the answer for you.")
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
iface.launch()
|