Spaces:
Sleeping
Sleeping
Annalyn Ng
commited on
Commit
•
c129047
1
Parent(s):
dfaaad7
update app to chinese sentence grading
Browse files
app.py
CHANGED
@@ -1,15 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
-
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
gr.Interface(
|
11 |
-
|
12 |
-
inputs=
|
13 |
-
outputs=
|
14 |
-
title="
|
15 |
-
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
4 |
|
|
|
5 |
|
6 |
+
model_checkpoint = "xlm-roberta-base"
|
7 |
+
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
9 |
+
model = AutoModelForMaskedLM.from_pretrained(model_checkpoint)
|
10 |
+
mask_token = tokenizer.mask_token
|
11 |
+
|
12 |
+
text = f"雨天,我整个人就便{mask_token}了,不想出外,甚至不想去上课。"
|
13 |
+
target_word = "懒惰"
|
14 |
+
|
15 |
+
def eval_prob(target_word, text):
|
16 |
+
# Get index of target_word
|
17 |
+
idx = tokenizer.encode(target_word)[2]
|
18 |
+
|
19 |
+
# Get logits
|
20 |
+
inputs = tokenizer(text, return_tensors="pt")
|
21 |
+
token_logits = model(**inputs).logits
|
22 |
+
|
23 |
+
# Find the location of the MASK and extract its logits
|
24 |
+
mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
|
25 |
+
mask_token_logits = token_logits[0, mask_token_index, :]
|
26 |
+
|
27 |
+
# Convert logits to softmax probability
|
28 |
+
logits = mask_token_logits[0].tolist()
|
29 |
+
probs = torch.nn.functional.softmax(torch.tensor([logits]), dim=1)[0]
|
30 |
+
|
31 |
+
# Get probability of target word filling the MASK
|
32 |
+
result = float(probs[idx])
|
33 |
+
|
34 |
+
return round(result, 5)
|
35 |
|
36 |
gr.Interface(
|
37 |
+
fn=eval_prob,
|
38 |
+
inputs="text",
|
39 |
+
outputs="text",
|
40 |
+
title="Chinese Sentence Grading",
|
41 |
+
).launch(share=True)
|
42 |
+
|
43 |
+
# Plot bar chart of probs x target_words to find optimal cutoff
|
44 |
+
|
45 |
+
# pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
46 |
+
|
47 |
+
# def predict(image):
|
48 |
+
# predictions = pipeline(image)
|
49 |
+
# return {p["label"]: p["score"] for p in predictions}
|
50 |
+
|
51 |
+
# gr.Interface(
|
52 |
+
# predict,
|
53 |
+
# inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
|
54 |
+
# outputs=gr.outputs.Label(num_top_classes=2),
|
55 |
+
# title="Hot Dog? Or Not?",
|
56 |
+
# ).launch()
|