Add application file
Browse files- app.py +51 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
|
7 |
+
labels = ['ๅใณ', 'ๆฒใใฟ', 'ๆๅพ
', '้ฉใ', 'ๆใ', 'ไฟก้ ผ', 'ๆฒใใฟ', 'ๅซๆช']
|
8 |
+
|
9 |
+
def np_softmax(x):
|
10 |
+
x_exp = torch.exp(torch.tensor(x) - torch.max(torch.tensor(x)))
|
11 |
+
f_x = x_exp / x_exp.sum()
|
12 |
+
return f_x
|
13 |
+
|
14 |
+
def emotion_clf(text):
|
15 |
+
model.eval()
|
16 |
+
tokens = tokenizer(text, truncation=True, return_tensors="pt")
|
17 |
+
tokens.to(model.device)
|
18 |
+
preds = model(**tokens)
|
19 |
+
prob = np_softmax(preds.logits.cpu().detach().numpy()[0])
|
20 |
+
out_dict = {n: p.item() for n, p in zip(labels, prob)}
|
21 |
+
return out_dict
|
22 |
+
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-whole-word-masking")
|
24 |
+
model = AutoModelForSequenceClassification.from_pretrained("jingwora/language-emotion-classification-ja", num_labels=8)
|
25 |
+
|
26 |
+
title = "Emotion Classification"
|
27 |
+
description = "Enter Japanese text and get the emotion probabilities."
|
28 |
+
text = gr.Textbox(placeholder="Enter japanese text", label="text", lines=3)
|
29 |
+
label = gr.Label(num_top_classes=8)
|
30 |
+
examples = [
|
31 |
+
["ๅคงๅใชๅไบบใ็ตๅฉใ็บ่กจใใใจใใๅฟใๆบใก่ถณใใๅนธใใๆใใพใใใ"],
|
32 |
+
["ๆใใ็ฅๆฏใไบกใใชใฃใใจใใ่ธใซๆทฑใๆฒใใฟใๅบใใใพใใใ"],
|
33 |
+
["ๆฐใใไปไบใๅงใใๅๅคใๆๆฅใใใฎๆช็ฅใฎไธ็ใซ่ธใ้ซ้ณดใใพใใใ"],
|
34 |
+
["ๅฑฑ้ใๆญฉใใฆใใใใ็พใใๆปใ็พใใๆใใๆฏใ้ฃฒใ้ฉใใๆใใพใใใ"],
|
35 |
+
["ๅ
ฌๅนณใใๆฌ ใใๆฑบๅฎใๅใใใจใใๆคใใๆฑใใฆๆใใๆนงใไธใใใพใใใ"],
|
36 |
+
["้ฃใใๅฑ้ขใงไปฒ้ใใใฃใใใจๆฏใใฆใใใฆใๅฟใใใฎไฟก้ ผใๆใใพใใใ"],
|
37 |
+
["ๅคฑๆใใใจใใๅฟใซ้ใๆฒใใฟใๅบใใใไฝใๆใซใคใใชใ็ถๆณใงใใใ"],
|
38 |
+
["ๆฑใใๅ ดๆใง้ทๆ้้ใใใใจใใไธๅฟซๆใจๅซๆชๆใๅใใพใใใ"],
|
39 |
+
]
|
40 |
+
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=emotion_clf,
|
43 |
+
inputs=text,
|
44 |
+
outputs=label,
|
45 |
+
title=title,
|
46 |
+
description=description,
|
47 |
+
examples=examples,
|
48 |
+
theme="freddyaboulton/dracula_revamped",
|
49 |
+
)
|
50 |
+
|
51 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.36.1
|
2 |
+
transformers==4.30.2
|
3 |
+
torch==2.0.1
|