Raptor1038 commited on
Commit
b67003e
1 Parent(s): 63d8f6f

Initial commit

Browse files
Files changed (1) hide show
  1. test_app.py +49 -0
test_app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import pandas as pd
4
+ from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
5
+
6
+ # Load the pre-trained model and tokenizer
7
+ # Import required libraries
8
+ # Load the tokenizer and model
9
+ model_name = "distilbert-base-uncased-distilled-squad"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = TFAutoModelForQuestionAnswering.from_pretrained(model_name)
12
+
13
+ def read_text_file(file):
14
+ with open(file.name) as f:
15
+ content = f.read()
16
+ return content
17
+
18
+
19
+ # Define a function to perform the question answering
20
+ def answer_question(doc, question):
21
+ # context = doc.read().decode('utf-8')
22
+ context = read_text_file(doc)
23
+
24
+ # Tokenize the context and question
25
+ inputs = tokenizer(question, context, return_tensors="tf")
26
+
27
+ # Get the answer span
28
+ start_scores = model(inputs)[0]
29
+ end_scores = model(inputs)[1]
30
+ answer_start = tf.argmax(start_scores, axis=1).numpy()[0]
31
+ answer_end = tf.argmax(end_scores, axis=1).numpy()[0] + 1
32
+ answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end]))
33
+ return answer
34
+
35
+ # Create a Gradio interface
36
+ interface = gr.Interface(
37
+ fn=answer_question,
38
+ inputs=[
39
+ gr.inputs.File(label="doc"),
40
+ gr.inputs.Textbox(label="question")
41
+ ],
42
+ outputs=gr.outputs.Textbox(label="answer"),
43
+ title="Document Question Answering",
44
+ description="Upload a document and ask a question about its contents.",
45
+ theme="default"
46
+ )
47
+
48
+ # Launch the interface
49
+ interface.launch()