Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,55 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def question_answering(context, question):
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
return output['answer'], str(output['score'] * 100)
|
| 14 |
|
| 15 |
iface = gr.Interface(
|
| 16 |
fn = question_answering,
|
|
@@ -24,4 +63,4 @@ iface = gr.Interface(
|
|
| 24 |
]
|
| 25 |
)
|
| 26 |
|
| 27 |
-
iface.launch(
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Method 1
|
| 4 |
+
# from transformers import pipeline
|
| 5 |
+
# question_answer = pipeline('question-answering',model = 'distilbert/distilbert-base-cased-distilled-squad')
|
| 6 |
+
|
| 7 |
+
# def question_answering(context, question):
|
| 8 |
+
|
| 9 |
+
# output = question_answer({
|
| 10 |
+
# 'context': context,
|
| 11 |
+
# 'question': question
|
| 12 |
+
# })
|
| 13 |
+
|
| 14 |
+
# return output['answer'], str(output['score'] * 100)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Method 2
|
| 18 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
| 19 |
+
import torch
|
| 20 |
+
import torch.nn.functional as F
|
| 21 |
+
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-cased-distilled-squad")
|
| 23 |
+
model = AutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-cased-distilled-squad")
|
| 24 |
|
| 25 |
def question_answering(context, question):
|
| 26 |
|
| 27 |
+
inputs = tokenizer.encode_plus(question, context, return_tensors="pt")
|
| 28 |
+
|
| 29 |
+
# Get input IDs and attention mask
|
| 30 |
+
input_ids = inputs["input_ids"].tolist()[0]
|
| 31 |
+
|
| 32 |
+
# Perform inference to get the start and end logits
|
| 33 |
+
outputs = model(**inputs)
|
| 34 |
+
start_logits = outputs.start_logits
|
| 35 |
+
end_logits = outputs.end_logits
|
| 36 |
+
|
| 37 |
+
# Get the most likely beginning and end of answer with the argmax of the logits
|
| 38 |
+
start_index = torch.argmax(start_logits)
|
| 39 |
+
end_index = torch.argmax(end_logits) + 1
|
| 40 |
+
|
| 41 |
+
# Apply softmax to get probabilities
|
| 42 |
+
start_probs = F.softmax(start_logits, dim=-1)
|
| 43 |
+
end_probs = F.softmax(end_logits, dim=-1)
|
| 44 |
+
|
| 45 |
+
# Convert token IDs of the answer span back to text
|
| 46 |
+
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[start_index:end_index]))
|
| 47 |
+
|
| 48 |
+
# Calculate the confidence score
|
| 49 |
+
confidence_score = start_probs[0][start_index].item() * end_probs[0][end_index-1].item()
|
| 50 |
+
|
| 51 |
+
return answer, str(confidence_score * 100)
|
| 52 |
|
|
|
|
| 53 |
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn = question_answering,
|
|
|
|
| 63 |
]
|
| 64 |
)
|
| 65 |
|
| 66 |
+
iface.launch()
|