FadQ commited on
Commit
3536c33
·
1 Parent(s): 7c0ca90

Add application file

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn.functional as F
4
+ from transformers import RobertaTokenizerFast, RobertaForSequenceClassification
5
+ from sklearn.preprocessing import LabelEncoder
6
+ import numpy as np
7
+
8
+ # Load tokenizer dan model
9
+ # Load model directly
10
+ tokenizer = RobertaTokenizerFast.from_pretrained("FadQ/results")
11
+ model = RobertaForSequenceClassification.from_pretrained("FadQ/results")
12
+ model.eval()
13
+
14
+ # Label Ekman (urutan harus cocok dengan urutan training)
15
+ ekman_labels = ['anger', 'disgust', 'fear', 'joy', 'sadness', 'surprise', 'neutral']
16
+ label_encoder = LabelEncoder()
17
+ label_encoder.fit(ekman_labels)
18
+
19
+ # Fungsi prediksi rata-rata emosi per baris
20
+ def predict_emotion_distribution(text):
21
+ lines = [line.strip() for line in text.split('\n') if line.strip()]
22
+ all_probs = []
23
+
24
+ for line in lines:
25
+ inputs = tokenizer(line, return_tensors="pt", truncation=True, padding=True, max_length=128)
26
+ with torch.no_grad():
27
+ logits = model(**inputs).logits
28
+ probs = F.softmax(logits, dim=-1).squeeze().cpu().numpy()
29
+ all_probs.append(probs)
30
+
31
+ if not all_probs:
32
+ return {label: 0.0 for label in ekman_labels}
33
+
34
+ avg_probs = np.mean(all_probs, axis=0)
35
+ result = {label: float(np.round(prob, 4)) for label, prob in zip(ekman_labels, avg_probs)}
36
+ return result
37
+
38
+ # Gradio Interface
39
+ interface = gr.Interface(
40
+ fn=predict_emotion_distribution,
41
+ inputs=gr.Textbox(lines=10, placeholder="Tulis diary-mu. Setiap baris = 1 kalimat...", label="Catatan Harian"),
42
+ outputs=gr.Label(num_top_classes=7, label="Distribusi Emosi"),
43
+ title="Prediksi Emosi dari Diary Harian",
44
+ description="Model mendeteksi emosi (7 label Ekman) dari teks harian. Input dipisah per baris. Output adalah rata-rata probabilitas per emosi."
45
+ )
46
+
47
+ interface.launch()