chanyaphas commited on
Commit
196b817
·
1 Parent(s): 9bbd4d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -1,19 +1,37 @@
1
- # ใช้โมเดลสำหรับ Classification อารมณ์ในข้อความ
 
 
 
 
2
  emotion_model_name = "alexandrainst/da-emotion-classification-base"
3
  emotion_tokenizer = AutoTokenizer.from_pretrained(emotion_model_name)
4
  emotion_model = AutoModelForSequenceClassification.from_pretrained(emotion_model_name)
5
 
6
- # ตัวอย่างการใช้โมเดลสำหรับ Classification อารมณ์ในข้อความ
7
- text = "I am feeling happy and excited today."
8
- inputs = emotion_tokenizer(text, return_tensors="pt")
9
- outputs = emotion_model(**inputs)
10
- logits = outputs.logits
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # คำนวณคะแนนความน่าจะเป็นของแต่ละอารมณ์
13
- probabilities = logits.softmax(dim=1)
14
 
15
- # หาอารมณ์ที่มีความน่าจะเป็นสูงสุด
16
- predicted_emotion = torch.argmax(probabilities, dim=1).item()
 
 
 
17
 
18
- # แสดงอารมณ์ที่ทำนาย
19
- print(f"Predicted Emotion: {predicted_emotion}")
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # โหลด tokenizer และโมเดล
6
  emotion_model_name = "alexandrainst/da-emotion-classification-base"
7
  emotion_tokenizer = AutoTokenizer.from_pretrained(emotion_model_name)
8
  emotion_model = AutoModelForSequenceClassification.from_pretrained(emotion_model_name)
9
 
10
+ def main():
11
+ st.title("Emotion Classification App")
12
+
13
+ # สร้าง text area สำหรับป้อนข้อความ
14
+ text = st.text_area('Enter the text for emotion classification:', '')
15
+
16
+ # คลิกปุ่ม "Classify Emotion" เพื่อจำแนกอารมณ์ในข้อความ
17
+ if st.button('Classify Emotion'):
18
+ if text:
19
+ # ใช้โมเดลเพื่อจำแนกอารมณ์ในข้อความ
20
+ inputs = emotion_tokenizer(text, return_tensors="pt")
21
+ outputs = emotion_model(**inputs)
22
+ logits = outputs.logits
23
+
24
+ # คำนวณคะแนนความน่าจะเป็นของแต่ละอารมณ์
25
+ probabilities = logits.softmax(dim=1)
26
 
27
+ # หาอารมณ์ที่มีความน่าจะเป็นสูงสุด
28
+ predicted_emotion = torch.argmax(probabilities, dim=1).item()
29
 
30
+ # แสดงอารมณ์ที่ทำนาย
31
+ st.subheader("Predicted Emotion:")
32
+ st.write(predicted_emotion)
33
+ else:
34
+ st.warning("Please enter some text for emotion classification.")
35
 
36
+ if __name__ == "__main__":
37
+ main()