|
import gradio as gr |
|
from transformers import pipeline |
|
import chardet |
|
|
|
|
|
question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2") |
|
|
|
|
|
def read_file_content(file_obj): |
|
with open(file_obj.name, 'rb') as f: |
|
raw_data = f.read() |
|
detected_encoding = chardet.detect(raw_data)["encoding"] |
|
with open(file_obj.name, 'r', encoding=detected_encoding) as f: |
|
return f.read() |
|
|
|
|
|
def get_answer(file_obj, question): |
|
try: |
|
context = read_file_content(file_obj) |
|
answer = question_answer(question=question, context=context) |
|
return answer.get("answer", "No answer found.") |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=get_answer, |
|
inputs=[gr.File(label="Upload your File"), gr.Textbox(label="Input the Question", lines=1)], |
|
outputs=[gr.Textbox(label="Answer Text", lines=1)], |
|
title="Document QnA", |
|
theme="soft", |
|
description="Get answers to your questions from a document!" |
|
) |
|
|
|
demo.launch(share=False) |