Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,35 @@
|
|
1 |
import torch
|
2 |
import streamlit as st
|
3 |
-
from transformers import BertTokenizer,
|
4 |
|
5 |
-
#
|
6 |
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
7 |
-
model =
|
8 |
|
9 |
def answer_query(question, context):
|
10 |
-
# Preprocess
|
11 |
inputs = tokenizer(question, context, return_tensors="pt")
|
12 |
|
13 |
-
# Use
|
14 |
with torch.no_grad():
|
15 |
outputs = model(**inputs)
|
16 |
|
17 |
-
#
|
18 |
-
start_logits = outputs.
|
19 |
-
end_logits = outputs.
|
20 |
|
21 |
-
# Find
|
22 |
answer_start = torch.argmax(start_logits)
|
23 |
answer_end = torch.argmax(end_logits) + 1
|
24 |
|
25 |
-
# Extract
|
26 |
-
answer = tokenizer.convert_tokens_to_string(
|
|
|
|
|
27 |
|
28 |
return answer
|
29 |
|
|
|
30 |
# Streamlit app
|
31 |
st.title("Question Answering App")
|
32 |
|
|
|
1 |
import torch
|
2 |
import streamlit as st
|
3 |
+
from transformers import BertTokenizer, BertForQuestionAnswering
|
4 |
|
5 |
+
# Utilize BertForQuestionAnswering model for direct start/end logits
|
6 |
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
7 |
+
model = BertForQuestionAnswering.from_pretrained("bert-base-uncased")
|
8 |
|
9 |
def answer_query(question, context):
|
10 |
+
# Preprocess using tokenizer
|
11 |
inputs = tokenizer(question, context, return_tensors="pt")
|
12 |
|
13 |
+
# Use model for question answering
|
14 |
with torch.no_grad():
|
15 |
outputs = model(**inputs)
|
16 |
|
17 |
+
# Retrieve logits directly
|
18 |
+
start_logits = outputs.start_logits
|
19 |
+
end_logits = outputs.end_logits
|
20 |
|
21 |
+
# Find answer span
|
22 |
answer_start = torch.argmax(start_logits)
|
23 |
answer_end = torch.argmax(end_logits) + 1
|
24 |
|
25 |
+
# Extract answer from context
|
26 |
+
answer = tokenizer.convert_tokens_to_string(
|
27 |
+
tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) # Access original tokens
|
28 |
+
)[answer_start:answer_end]
|
29 |
|
30 |
return answer
|
31 |
|
32 |
+
|
33 |
# Streamlit app
|
34 |
st.title("Question Answering App")
|
35 |
|