File size: 1,011 Bytes
c367360 83e6f21 |
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 |
import torch
import gradio as gr
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("question-answering", model="deepset/roberta-base-squad2")
def read_file_content(file_obj):
try:
with open(file_obj.name, "r", encoding="utf-8") as f:
content = f.read()
return content
except Exception as e:
return f"An error occurred: {e}"
def get_answer(file, question):
context= read_file_content(file)
answer = pipe(question=question, context = context)
return (answer['answer'])
demo = gr.Interface(fn=get_answer,
inputs= [gr.File(label="Upload the file for context"), gr.Textbox(label='Ask the question')],
outputs=[gr.Textbox(label='Here is the answer to your question')],
title='Get the Answers to your question',
description='This application gives the answers to your questions'
)
demo.launch(share=True) |