DocQnA / app.py
humbleakh's picture
Update app.py
b99b2b6 verified
raw
history blame
1.23 kB
import gradio as gr
from transformers import pipeline
import chardet
# Load the model for QnA
question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
# Function to detect encoding and read the file
def read_file_content(file_obj):
with open(file_obj.name, 'rb') as f: # Read in binary mode
raw_data = f.read()
detected_encoding = chardet.detect(raw_data)["encoding"] # Detect encoding
with open(file_obj.name, 'r', encoding=detected_encoding) as f: # Open with correct encoding
return f.read()
# Function to answer questions from the document
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)}"
# Gradio Interface
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)