Spaces:
Runtime error
Runtime error
Commit
·
80dfbce
1
Parent(s):
64ae95d
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,27 +7,6 @@ device = torch.device('cpu')
|
|
| 7 |
|
| 8 |
NUM_CLASSES=6
|
| 9 |
|
| 10 |
-
#model=BertForMaskedLM.from_pretrained("./")
|
| 11 |
-
#tokenizer=BertTokenizer.from_pretrained("./")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def predict(text=None) -> dict:
|
| 15 |
-
print(text)
|
| 16 |
-
model.eval()
|
| 17 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 18 |
-
input_ids = inputs["input_ids"].to(device)
|
| 19 |
-
attention_mask = inputs["attention_mask"].to(device)
|
| 20 |
-
model.to(device)
|
| 21 |
-
token_logits = model(input_ids, attention_mask=attention_mask).logits
|
| 22 |
-
mask_token_index = torch.where(inputs_ex["input_ids"] == tokenizer.mask_token_id)[1]
|
| 23 |
-
mask_token_logits = token_logits[0, mask_token_index, :]
|
| 24 |
-
top_5_tokens = torch.topk(mask_token_logits, NUM_CLASSES, dim=1).indices[0].tolist()
|
| 25 |
-
score = torch.nn.functional.softmax(mask_token_logits)[0]
|
| 26 |
-
top_5_score = torch.topk(score, NUM_CLASSES).values.tolist()
|
| 27 |
-
print(top_5_tokens)
|
| 28 |
-
return {tokenizer.decode([tok]): float(score) for tok, score in zip(top_5_tokens, top_5_score)}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
def test():
|
| 32 |
return {"token": 0.57}
|
| 33 |
|
|
|
|
| 7 |
|
| 8 |
NUM_CLASSES=6
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def test():
|
| 11 |
return {"token": 0.57}
|
| 12 |
|