File size: 1,572 Bytes
b67003e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
import tensorflow as tf
import pandas as pd
from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering

# Load the pre-trained model and tokenizer
# Import required libraries
# Load the tokenizer and model
model_name = "distilbert-base-uncased-distilled-squad"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = TFAutoModelForQuestionAnswering.from_pretrained(model_name)

def read_text_file(file):
    with open(file.name) as f:
        content = f.read()
    return content


# Define a function to perform the question answering
def answer_question(doc, question):
    # context = doc.read().decode('utf-8')
    context = read_text_file(doc)

    # Tokenize the context and question
    inputs = tokenizer(question, context, return_tensors="tf")

    # Get the answer span
    start_scores = model(inputs)[0]
    end_scores = model(inputs)[1]
    answer_start = tf.argmax(start_scores, axis=1).numpy()[0]
    answer_end = tf.argmax(end_scores, axis=1).numpy()[0] + 1
    answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end]))
    return answer

# Create a Gradio interface
interface = gr.Interface(
    fn=answer_question,
    inputs=[
        gr.inputs.File(label="doc"),
        gr.inputs.Textbox(label="question")
    ],
    outputs=gr.outputs.Textbox(label="answer"),
    title="Document Question Answering",
    description="Upload a document and ask a question about its contents.",
    theme="default"
)

# Launch the interface
interface.launch()