File size: 1,233 Bytes
f72d0b3
 
b99b2b6
f72d0b3
b99b2b6
f72d0b3
 
b99b2b6
f72d0b3
b99b2b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72d0b3
b99b2b6
 
 
 
f72d0b3
b99b2b6
 
 
 
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
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)