koshin2001 commited on
Commit
6364f02
ยท
1 Parent(s): cc9b9f8

๐ŸŽ‰ Add app

Browse files
Files changed (5) hide show
  1. .gitignore +5 -0
  2. app.py +57 -0
  3. const.py +10 -0
  4. inference.py +35 -0
  5. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .env
2
+
3
+ .DS_Store
4
+
5
+ __pycache__
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import inference
3
+ import pandas as pd
4
+ import const
5
+
6
+ st.title("Japanese to Emotion classification")
7
+ st.write(
8
+ "I fine-tuned the BERT-based distillation model for classification of Japanese text."
9
+ )
10
+
11
+ if "input_text" not in st.session_state:
12
+ st.session_state.input_text = ""
13
+
14
+ input_text = st.text_area(
15
+ "Japanese text", value=st.session_state.input_text, max_chars=512
16
+ )
17
+
18
+ suggestions = ["ไปŠๆ—ฅใฏๆ„›็Šฌใจๆ•ฃๆญฉใ—ใŸ", "็Œซใ‚ซใƒ•ใ‚ง่กŒใใŸใ„", "่‡ช่ปข่ปŠ็›—ใพใ‚ŒใŸ"]
19
+
20
+ COLUMNS_NUM = len(suggestions)
21
+ cols = st.columns(COLUMNS_NUM)
22
+
23
+ for i, suggestion in enumerate(suggestions):
24
+ with cols[i]:
25
+ if st.button(suggestion, use_container_width=True):
26
+ st.session_state.input_text = suggestion
27
+ st.rerun()
28
+
29
+ st.session_state.input_text = input_text
30
+
31
+ if input_text:
32
+ probs_dict = inference.exec(input_text)
33
+
34
+ label_dict = {
35
+ const.EMOTIONS[0]: "๐Ÿ˜Š Joy",
36
+ const.EMOTIONS[1]: "๐Ÿ˜ข Sadness",
37
+ const.EMOTIONS[2]: "๐Ÿ˜ฎ Anticipation",
38
+ const.EMOTIONS[3]: "๐Ÿ˜ฒ Surprise",
39
+ const.EMOTIONS[4]: "๐Ÿ˜  Anger",
40
+ const.EMOTIONS[5]: "๐Ÿ˜จ Fear",
41
+ const.EMOTIONS[6]: "๐Ÿ˜– Disgust",
42
+ const.EMOTIONS[7]: "๐Ÿ˜‰ Trust",
43
+ }
44
+
45
+ df = pd.DataFrame(
46
+ {
47
+ "Emotion": label_dict.values(),
48
+ "Probs": [probs_dict[emotion] for emotion in const.EMOTIONS],
49
+ }
50
+ )
51
+
52
+ st.bar_chart(df.set_index("Emotion"), horizontal=True)
53
+
54
+ st.write('''
55
+ - [GitHub](https://github.com/koshin01)
56
+ - [Blog](https://zenn.dev/koshin)
57
+ ''')
const.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ EMOTIONS = [
2
+ "Joy",
3
+ "Sadness",
4
+ "Anticipation",
5
+ "Surprise",
6
+ "Anger",
7
+ "Fear",
8
+ "Disgust",
9
+ "Trust",
10
+ ]
inference.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
2
+ import torch
3
+ import torch.nn.functional as F
4
+ import const
5
+
6
+
7
+ def load_model():
8
+ return AutoModelForSequenceClassification.from_pretrained(
9
+ "koshin2001/Japanese-to-emotions"
10
+ ).eval()
11
+
12
+
13
+ def load_tokenizer():
14
+ return AutoTokenizer.from_pretrained("koshin2001/Japanese-to-emotions")
15
+
16
+
17
+ def exec(text):
18
+ model = load_model()
19
+ tokenizer = load_tokenizer()
20
+
21
+ inputs = tokenizer(
22
+ text,
23
+ return_tensors="pt",
24
+ truncation=True,
25
+ return_token_type_ids=False,
26
+ max_length=512,
27
+ )
28
+ output = model(**inputs)
29
+
30
+ output_logits = torch.tensor(output.logits).clone().detach().requires_grad_(True)
31
+ probs = F.softmax(output_logits, dim=-1).tolist()[0]
32
+
33
+ emotion_probs = dict(zip(const.EMOTIONS, probs))
34
+
35
+ return emotion_probs
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ transformers == 4.44.2