msures3 commited on
Commit
22ba023
1 Parent(s): af0d46f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer
2
+ import torch
3
+ import gradio as gr
4
+
5
+
6
+ model = AutoModelForQuestionAnswering.from_pretrained("msures3/distilbert-base-squad")
7
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
8
+
9
+ def generate_response(context, question):
10
+ inputs = tokenizer(question, context, return_tensors="pt")
11
+ with torch.no_grad():
12
+ outputs = model(**inputs)
13
+ answer_start_index = outputs.start_logits.argmax()
14
+ answer_end_index = outputs.end_logits.argmax()
15
+ predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
16
+ predicted_answers = tokenizer.decode(predict_answer_tokens)
17
+ return predicted_answers
18
+
19
+
20
+ inputs = [
21
+ gr.Textbox(label="Enter Context"),
22
+ gr.Textbox(label="Enter Question")
23
+ ]
24
+ outputs = gr.Textbox(label="Predicted Answer")
25
+
26
+
27
+ app = gr.Interface(
28
+ fn=generate_response,
29
+ inputs=inputs,
30
+ outputs=outputs,
31
+ title="Context Based Question Answering",
32
+ description="Enter a context and a question to get the predicted answer.",
33
+ examples=[
34
+ ["This is a sample context. The quick brown fox jumps over the lazy dog.", "What animal jumps over the dog?"],
35
+ ["The capital of France is Paris. It is a beautiful city with many attractions.", "What is the capital of France?"]
36
+ ]
37
+ )
38
+
39
+ app.launch()