hendri commited on
Commit
bffcedb
·
verified ·
1 Parent(s): 4288bf2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BertTokenizer, BertForSequenceClassification
3
+ import torch
4
+ import torch.nn.functional as F
5
+
6
+ # Load the tokenizer and model
7
+ tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-large-p1')
8
+ model = BertForSequenceClassification.from_pretrained("hendri/emotion")
9
+
10
+ labels = ["LABEL_0", "LABEL_1", "LABEL_2", "LABEL_3", "LABEL_4"]
11
+
12
+ # Map these to your actual labels:
13
+ label_mapping = {
14
+ "LABEL_0": "sadness",
15
+ "LABEL_1": "anger",
16
+ "LABEL_2": "love",
17
+ "LABEL_3": "fear",
18
+ "LABEL_4": "happy"
19
+ }
20
+
21
+ # Define a function to process user input and return predictions
22
+ def classify_emotion(text):
23
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
24
+ with torch.no_grad():
25
+ outputs = model(**inputs)
26
+ logits = outputs.logits
27
+ probabilities = F.softmax(logits, dim=-1)
28
+ predictions = {label_mapping[labels[i]]: round(float(prob), 4) for i, prob in enumerate(probabilities[0])}
29
+ return predictions
30
+
31
+ # Create the Gradio interface
32
+ interface = gr.Interface(
33
+ fn=classify_emotion,
34
+ inputs=gr.Textbox(label="Enter Text for Emotion Classification"),
35
+ outputs=gr.Label(label="Predicted Emotions"),
36
+ title="Emotion Classification",
37
+ description="This application uses an IndoBERT model fine-tuned for emotion classification. Enter a sentence (bahasa Indonesia) to see the predicted emotions and their probabilities."
38
+ )
39
+
40
+ # Launch the Gradio interface
41
+ interface.launch()