Create QA_Tensorflow
Browse files- QA_Tensorflow +19 -0
QA_Tensorflow
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
|
| 2 |
+
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
|
| 5 |
+
checkpoint = "distilbert-base-cased-distilled-squad"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 7 |
+
model = TFAutoModelForQuestionAnswering.from_pretrained(checkpoint)
|
| 8 |
+
|
| 9 |
+
def question_answering_tf(question, context):
|
| 10 |
+
inputs = tokenizer(question, context, return_tensors="tf")
|
| 11 |
+
#print(inputs["input_ids"])
|
| 12 |
+
#print(tokenizer.decode(inputs["input_ids"][0]))
|
| 13 |
+
outputs = model(inputs)
|
| 14 |
+
#print(outputs.start_logits)
|
| 15 |
+
#print(outputs.end_logits)
|
| 16 |
+
start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0])
|
| 17 |
+
end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0])
|
| 18 |
+
print(start_index, end_index)
|
| 19 |
+
return tokenizer.decode(inputs["input_ids"][0][start_index: end_index+1])
|