Upload 4 files
Browse files- app.py +58 -0
- best_model_75.pth +3 -0
- model.py +22 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
from model import SentimentAnalysisModel
|
5 |
+
|
6 |
+
# Load the pre-trained sentiment analysis model
|
7 |
+
model = SentimentAnalysisModel(bert_model_name="SamLowe/roberta-base-go_emotions", num_labels=7)
|
8 |
+
model.load_state_dict(torch.load("best_model_0.75.pth", map_location=torch.device('cpu')))
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Mapping from predicted class to emoji
|
12 |
+
emoji_to_emotion = {
|
13 |
+
0: 'joy 😆',
|
14 |
+
1: 'fear 😱',
|
15 |
+
2: 'anger 😡',
|
16 |
+
3: 'sadness 😭',
|
17 |
+
4: 'disgust 🤮',
|
18 |
+
5: 'shame 😳',
|
19 |
+
6: 'guilt 😞'
|
20 |
+
}
|
21 |
+
|
22 |
+
# Function to make predictions
|
23 |
+
def predict_sentiment(text):
|
24 |
+
inputs = model.tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
25 |
+
input_ids = inputs["input_ids"]
|
26 |
+
attention_mask = inputs["attention_mask"]
|
27 |
+
|
28 |
+
with torch.no_grad():
|
29 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
30 |
+
|
31 |
+
logits = outputs.logits
|
32 |
+
_, predicted_class = torch.max(logits, dim=1)
|
33 |
+
|
34 |
+
# Map predicted class to emoji
|
35 |
+
result = emoji_to_emotion[predicted_class.item()]
|
36 |
+
|
37 |
+
return result
|
38 |
+
|
39 |
+
# Create title, description and article strings
|
40 |
+
title = "Emoji-aware Sentiment Analysis using Roberta Model"
|
41 |
+
description = "Explore the power of sentiment analysis with our Emotion Detector! Simply input a sentence or text, and let our model predict the underlying emotion. Discover the magic of AI in understanding human sentiments."
|
42 |
+
article = "Sentiment Analysis, also known as opinion mining, is a branch of Natural Language Processing (NLP) that involves determining the emotional tone behind a piece of text. This powerful tool allows us to uncover the underlying feelings, attitudes, and opinions expressed in written communication."
|
43 |
+
|
44 |
+
|
45 |
+
# Interface for Gradio
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=predict_sentiment,
|
48 |
+
inputs="text",
|
49 |
+
outputs="text",
|
50 |
+
live=True,
|
51 |
+
theme="huggingface",
|
52 |
+
interpretation="default",
|
53 |
+
title=title,
|
54 |
+
description=description,
|
55 |
+
article=article)
|
56 |
+
|
57 |
+
# Launch the Gradio interface
|
58 |
+
iface.launch()
|
best_model_75.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:05496d2e0ba1ddb147181357c259ece6f7bcced221e259c48acdf503d1c1207a
|
3 |
+
size 1496074346
|
model.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
+
|
5 |
+
class SentimentAnalysisModel(torch.nn.Module):
|
6 |
+
def __init__(self, bert_model_name, num_labels=7, dropout_rate=0.4):
|
7 |
+
super(SentimentAnalysisModel, self).__init__()
|
8 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(bert_model_name)
|
9 |
+
self.model.classifier.out_proj = torch.nn.Linear(self.model.config.hidden_size, num_labels)
|
10 |
+
self.model.classifier.dropout = torch.nn.Dropout(p=dropout_rate, inplace=False)
|
11 |
+
# ปรับ dropout ในทุก RobertaLayer
|
12 |
+
for layer in self.model.roberta.encoder.layer:
|
13 |
+
layer.attention.self.dropout = torch.nn.Dropout(p=dropout_rate)
|
14 |
+
layer.attention.output.dropout = torch.nn.Dropout(p=dropout_rate)
|
15 |
+
layer.intermediate.dropout = torch.nn.Dropout(p=dropout_rate)
|
16 |
+
layer.output.dropout = torch.nn.Dropout(p=dropout_rate)
|
17 |
+
|
18 |
+
self.tokenizer = AutoTokenizer.from_pretrained(bert_model_name)
|
19 |
+
|
20 |
+
def forward(self, input_ids, attention_mask, labels=None):
|
21 |
+
outputs = self.model(input_ids, attention_mask=attention_mask, labels=labels)
|
22 |
+
return outputs
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.10.0
|
2 |
+
transformers==4.11.3
|
3 |
+
gradio==2.4.0
|