hendri commited on
Commit
ca7f3cc
·
verified ·
1 Parent(s): f90aa64

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/sentiment")
9
+
10
+ labels = ["LABEL_0", "LABEL_1", "LABEL_2"]
11
+
12
+ # Map these to your actual labels:
13
+ label_mapping = {
14
+ "LABEL_0": "positive",
15
+ "LABEL_1": "neutral",
16
+ "LABEL_2": "negative"
17
+ }
18
+ # Define a function to process user input and return predictions
19
+ def classify_emotion(text):
20
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
21
+ with torch.no_grad():
22
+ outputs = model(**inputs)
23
+ logits = outputs.logits
24
+ probabilities = F.softmax(logits, dim=-1)
25
+ predictions = {label_mapping[labels[i]]: round(float(prob), 4) for i, prob in enumerate(probabilities[0])}
26
+ return predictions
27
+
28
+ # Create the Gradio interface
29
+ interface = gr.Interface(
30
+ fn=classify_emotion,
31
+ inputs=gr.Textbox(label="Enter Text for Sentiment Analysis"),
32
+ outputs=gr.Label(label="Predicted Sentiment"),
33
+ title="Sentiment Analysis",
34
+ description="This application uses an IndoBERT model fine-tuned for sentiment analysis. Enter a sentence (bahasa Indonesia) to see the predicted sentiment and their probabilities."
35
+ )
36
+
37
+ # Launch the Gradio interface
38
+ interface.launch()