Spaces:
Runtime error
Runtime error
hongkokokok
commited on
Commit
·
a86eb67
1
Parent(s):
8434626
Upload app (5).py
Browse files- app (5).py +37 -0
app (5).py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|