Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,21 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
st.title("English to German")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if clicked:
|
| 13 |
-
results = classifier([text])
|
| 14 |
-
st.json(results)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering,
|
| 3 |
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("valhalla/longformer-base-4096-finetuned-squadv1")
|
| 5 |
+
model = AutoModelForQuestionAnswering.from_pretrained("valhalla/longformer-base-4096-finetuned-squadv1")
|
|
|
|
| 6 |
|
| 7 |
+
text = "Huggingface has democratized NLP. Huge thanks to Huggingface for this."
|
| 8 |
+
question = "What has Huggingface done ?"
|
| 9 |
+
encoding = tokenizer(question, text, return_tensors="pt")
|
| 10 |
+
input_ids = encoding["input_ids"]
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# default is local attention everywhere
|
| 13 |
+
# the forward method will automatically set global attention on question tokens
|
| 14 |
+
attention_mask = encoding["attention_mask"]
|
| 15 |
+
|
| 16 |
+
start_scores, end_scores = model(input_ids, attention_mask=attention_mask)
|
| 17 |
+
all_tokens = tokenizer.convert_ids_to_tokens(input_ids[0].tolist())
|
| 18 |
+
|
| 19 |
+
answer_tokens = all_tokens[torch.argmax(start_scores) :torch.argmax(end_scores)+1]
|
| 20 |
+
answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
|
| 21 |
+
# output => democratized NLP
|