Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,49 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from transformers import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ResearchPaperQAModel:
|
| 7 |
+
"""Class to load the model and answer questions based on abstract and text of reserach paper.
|
| 8 |
+
"""
|
| 9 |
+
def __init__(self, model_name):
|
| 10 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
self.model = TFAutoModelForQuestionAnswering.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
def answer_question(self, question, context):
|
| 14 |
+
# Tokenize input question and context
|
| 15 |
+
inputs = self.tokenizer(question, context, return_tensors="tf")
|
| 16 |
+
|
| 17 |
+
# Get the start and end logits for the answer
|
| 18 |
+
outputs = self.model(**inputs)
|
| 19 |
+
start_logits, end_logits = outputs.start_logits[0].numpy(), outputs.end_logits[0].numpy()
|
| 20 |
+
|
| 21 |
+
# Find the tokens with the highest probability for start and end positions
|
| 22 |
+
start_index = tf.argmax(start_logits, axis=-1).numpy()
|
| 23 |
+
end_index = tf.argmax(end_logits, axis=-1).numpy()
|
| 24 |
+
|
| 25 |
+
# Convert token indices to actual tokens
|
| 26 |
+
tokens = self.tokenizer.convert_ids_to_tokens(inputs["input_ids"].numpy().squeeze())
|
| 27 |
+
answer_tokens = tokens[start_index : end_index + 1]
|
| 28 |
+
|
| 29 |
+
# Convert answer tokens back to a string
|
| 30 |
+
answer = self.tokenizer.convert_tokens_to_string(answer_tokens)
|
| 31 |
+
|
| 32 |
+
return answer
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
model = "bert-large-uncased-whole-word-masking-finetuned-squad" # Model name
|
| 36 |
+
paper_model = ResearchPaperQAModel(model) #Create an instance of the model
|
| 37 |
+
|
| 38 |
+
# Create a Gradio interface
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=paper_model.answer_question,
|
| 41 |
+
inputs=["text", "text"],
|
| 42 |
+
outputs="text",
|
| 43 |
+
live=True,
|
| 44 |
+
title="Ask question to research paper",
|
| 45 |
+
description="Enter title of research paper, abstract, and list of questions to get answers."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Launch the Gradio interface
|
| 49 |
+
iface.launch(share=True)
|