Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
from torch.nn.functional import sigmoid
|
4 |
import torch
|
|
|
|
|
|
|
5 |
|
6 |
# Load text emotion model
|
7 |
model_name = "SamLowe/roberta-base-go_emotions"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
|
11 |
-
# Load image emotion
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Emotion label to icon mapping (subset)
|
15 |
emotion_icons = {
|
@@ -55,14 +84,6 @@ def get_emotions(text, threshold):
|
|
55 |
|
56 |
return ", ".join(icons) if icons else "No strong emotion detected."
|
57 |
|
58 |
-
# Analyze image emotion
|
59 |
-
def analyze_image_emotion(image):
|
60 |
-
if image is None:
|
61 |
-
return "No image provided."
|
62 |
-
results = image_emotion_pipeline(image)
|
63 |
-
top = results[0]
|
64 |
-
return f"{top['label']} ({top['score']:.2f})"
|
65 |
-
|
66 |
# Combined analysis
|
67 |
def analyze_combined(text, threshold, image):
|
68 |
text_result = get_emotions(text, threshold)
|
@@ -117,4 +138,4 @@ demo = gr.Interface(
|
|
117 |
css=custom_css
|
118 |
)
|
119 |
|
120 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
from torch.nn.functional import sigmoid
|
4 |
import torch
|
5 |
+
from PIL import Image
|
6 |
+
from torchvision import transforms
|
7 |
+
import requests
|
8 |
|
9 |
# Load text emotion model
|
10 |
model_name = "SamLowe/roberta-base-go_emotions"
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
13 |
|
14 |
+
# Load image emotion model (fine-tuned ResNet-50)
|
15 |
+
image_model_name = "Celal11/resnet-50-finetuned-FER2013CKPlus-0.003"
|
16 |
+
image_emotion_model = AutoModelForSequenceClassification.from_pretrained(image_model_name)
|
17 |
+
image_tokenizer = AutoTokenizer.from_pretrained("microsoft/resnet-50")
|
18 |
+
|
19 |
+
# Transform for image preprocessing
|
20 |
+
image_transform = transforms.Compose([
|
21 |
+
transforms.Resize((224, 224)),
|
22 |
+
transforms.ToTensor(),
|
23 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
24 |
+
])
|
25 |
+
|
26 |
+
# FER labels
|
27 |
+
image_labels = [
|
28 |
+
"Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral", "Contempt"
|
29 |
+
]
|
30 |
+
|
31 |
+
# Analyze image emotion
|
32 |
+
def analyze_image_emotion(image_path):
|
33 |
+
if image_path is None:
|
34 |
+
return "No image provided."
|
35 |
+
image = Image.open(image_path).convert("RGB")
|
36 |
+
img_tensor = image_transform(image).unsqueeze(0)
|
37 |
+
with torch.no_grad():
|
38 |
+
output = image_emotion_model(img_tensor)
|
39 |
+
probs = sigmoid(output.logits)[0]
|
40 |
+
top_idx = torch.argmax(probs).item()
|
41 |
+
return f"{image_labels[top_idx]} ({probs[top_idx]:.2f})"
|
42 |
|
43 |
# Emotion label to icon mapping (subset)
|
44 |
emotion_icons = {
|
|
|
84 |
|
85 |
return ", ".join(icons) if icons else "No strong emotion detected."
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
# Combined analysis
|
88 |
def analyze_combined(text, threshold, image):
|
89 |
text_result = get_emotions(text, threshold)
|
|
|
138 |
css=custom_css
|
139 |
)
|
140 |
|
141 |
+
demo.launch()
|