Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 4 |
-
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Check for TensorFlow
|
| 9 |
try:
|
| 10 |
import tensorflow as tf
|
| 11 |
IS_TF_AVAILABLE = True
|
| 12 |
except ImportError:
|
| 13 |
IS_TF_AVAILABLE = False
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
model = None
|
| 17 |
processor = None
|
| 18 |
is_pytorch_model = True
|
| 19 |
-
|
| 20 |
model_name_or_path = "ravi86/mood_detector"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
| 31 |
-
is_pytorch_model = False
|
| 32 |
except:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
if model is None or processor is None:
|
| 38 |
-
raise RuntimeError("
|
| 39 |
|
| 40 |
-
if is_pytorch_model:
|
| 41 |
model.eval()
|
| 42 |
|
| 43 |
-
# ---
|
| 44 |
emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
| 45 |
-
|
| 46 |
spotify_playlist_mapping = {
|
| 47 |
"Angry": "https://open.spotify.com/playlist/37i9dQZF1DX2LTjeP1y0aR",
|
| 48 |
"Disgust": "https://open.spotify.com/playlist/37i9dQZF1DXcK3k3gJ6usM",
|
|
@@ -53,56 +90,55 @@ spotify_playlist_mapping = {
|
|
| 53 |
"Neutral": "https://open.spotify.com/playlist/37i9dQZF1DXasMvN3R0sVw"
|
| 54 |
}
|
| 55 |
|
| 56 |
-
# ---
|
| 57 |
-
def classify_expression_and_suggest_music(image_input):
|
| 58 |
if image_input is None:
|
| 59 |
-
return "No
|
| 60 |
|
| 61 |
image = Image.fromarray(image_input).convert("L").resize((48, 48))
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
inputs_for_model = tf.expand_dims(tf.convert_to_tensor(pixel_values_np), 0)
|
| 69 |
-
|
| 70 |
-
with torch.no_grad():
|
| 71 |
-
if is_pytorch_model:
|
| 72 |
-
outputs = model(inputs_for_model)
|
| 73 |
-
logits = outputs.logits
|
| 74 |
-
else:
|
| 75 |
-
outputs = model(inputs_for_model)
|
| 76 |
-
logits = outputs
|
| 77 |
-
|
| 78 |
if isinstance(logits, tf.Tensor):
|
| 79 |
-
logits =
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
emotion = emotions[idx]
|
| 87 |
-
confidence = probs[0, idx].item() * 100
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
# --- Gradio
|
| 96 |
iface = gr.Interface(
|
| 97 |
fn=classify_expression_and_suggest_music,
|
| 98 |
-
inputs=gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
outputs=[
|
| 100 |
-
gr.Textbox(label="Emotion
|
| 101 |
-
gr.Markdown(label="Suggested
|
| 102 |
],
|
| 103 |
live=True,
|
| 104 |
-
title="π MoodTune:
|
| 105 |
-
description="
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
import os
|
| 5 |
+
import warnings
|
| 6 |
+
|
| 7 |
+
warnings.filterwarnings("ignore")
|
| 8 |
+
|
| 9 |
+
# --- Optional torch and tf loading ---
|
| 10 |
+
try:
|
| 11 |
+
import torch
|
| 12 |
+
IS_TORCH_AVAILABLE = True
|
| 13 |
+
except ImportError:
|
| 14 |
+
IS_TORCH_AVAILABLE = False
|
| 15 |
|
|
|
|
| 16 |
try:
|
| 17 |
import tensorflow as tf
|
| 18 |
IS_TF_AVAILABLE = True
|
| 19 |
except ImportError:
|
| 20 |
IS_TF_AVAILABLE = False
|
| 21 |
|
| 22 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 23 |
+
|
| 24 |
+
# --- Model loading ---
|
| 25 |
model = None
|
| 26 |
processor = None
|
| 27 |
is_pytorch_model = True
|
|
|
|
| 28 |
model_name_or_path = "ravi86/mood_detector"
|
| 29 |
+
local_model_dir = "./model"
|
| 30 |
+
local_h5_path = os.path.join(local_model_dir, "my_model.h5")
|
| 31 |
+
|
| 32 |
+
# Try Hugging Face PyTorch
|
| 33 |
+
if IS_TORCH_AVAILABLE:
|
| 34 |
+
try:
|
| 35 |
+
model = AutoModelForImageClassification.from_pretrained(model_name_or_path)
|
| 36 |
+
processor = AutoImageProcessor.from_pretrained(model_name_or_path)
|
| 37 |
+
is_pytorch_model = True
|
| 38 |
+
print("Loaded PyTorch model from Hugging Face.")
|
| 39 |
+
except:
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
# Try Hugging Face TensorFlow
|
| 43 |
+
if model is None and IS_TF_AVAILABLE:
|
| 44 |
+
try:
|
| 45 |
+
model = AutoModelForImageClassification.from_pretrained(model_name_or_path, from_tf=True)
|
| 46 |
+
processor = AutoImageProcessor.from_pretrained(model_name_or_path)
|
| 47 |
+
is_pytorch_model = False
|
| 48 |
+
print("Loaded TensorFlow model from Hugging Face.")
|
| 49 |
+
except:
|
| 50 |
+
pass
|
| 51 |
+
|
| 52 |
+
# Try local Transformers model
|
| 53 |
+
if model is None:
|
| 54 |
+
try:
|
| 55 |
+
model = AutoModelForImageClassification.from_pretrained(local_model_dir)
|
| 56 |
+
processor = AutoImageProcessor.from_pretrained(local_model_dir)
|
| 57 |
+
is_pytorch_model = hasattr(model, 'parameters')
|
| 58 |
+
print("Loaded local Transformers model.")
|
| 59 |
+
except:
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
# Try raw .h5
|
| 63 |
+
if model is None and IS_TF_AVAILABLE and os.path.exists(local_h5_path):
|
| 64 |
+
try:
|
| 65 |
+
model = tf.keras.models.load_model(local_h5_path)
|
| 66 |
try:
|
| 67 |
+
processor = AutoImageProcessor.from_pretrained(local_model_dir)
|
|
|
|
|
|
|
| 68 |
except:
|
| 69 |
+
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
| 70 |
+
is_pytorch_model = False
|
| 71 |
+
print("Loaded local Keras .h5 model.")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
raise RuntimeError(f"Failed to load model: {e}")
|
| 74 |
|
| 75 |
if model is None or processor is None:
|
| 76 |
+
raise RuntimeError("Failed to load model and processor.")
|
| 77 |
|
| 78 |
+
if is_pytorch_model and IS_TORCH_AVAILABLE:
|
| 79 |
model.eval()
|
| 80 |
|
| 81 |
+
# --- Emotion & Spotify Map ---
|
| 82 |
emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
|
|
|
| 83 |
spotify_playlist_mapping = {
|
| 84 |
"Angry": "https://open.spotify.com/playlist/37i9dQZF1DX2LTjeP1y0aR",
|
| 85 |
"Disgust": "https://open.spotify.com/playlist/37i9dQZF1DXcK3k3gJ6usM",
|
|
|
|
| 90 |
"Neutral": "https://open.spotify.com/playlist/37i9dQZF1DXasMvN3R0sVw"
|
| 91 |
}
|
| 92 |
|
| 93 |
+
# --- Inference Function ---
|
| 94 |
+
def classify_expression_and_suggest_music(image_input: np.ndarray):
|
| 95 |
if image_input is None:
|
| 96 |
+
return "No image detected. Please enable your webcam.", ""
|
| 97 |
|
| 98 |
image = Image.fromarray(image_input).convert("L").resize((48, 48))
|
| 99 |
+
inputs = processor(images=image, return_tensors="pt" if is_pytorch_model else "tf")
|
| 100 |
|
| 101 |
+
if not is_pytorch_model:
|
| 102 |
+
pixel_values = inputs['pixel_values'].numpy()
|
| 103 |
+
tf_tensor = tf.convert_to_tensor(pixel_values)
|
| 104 |
+
outputs = model(tf_tensor)
|
| 105 |
+
logits = outputs if isinstance(outputs, (np.ndarray, tf.Tensor)) else outputs[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
if isinstance(logits, tf.Tensor):
|
| 107 |
+
logits = logits.numpy()
|
| 108 |
+
probs = tf.nn.softmax(logits).numpy()
|
| 109 |
+
else:
|
| 110 |
+
with torch.no_grad():
|
| 111 |
+
outputs = model(inputs['pixel_values'])
|
| 112 |
+
logits = outputs.logits
|
| 113 |
+
probs = torch.softmax(logits, dim=-1).numpy()
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
predicted_class = int(np.argmax(probs))
|
| 116 |
+
confidence = float(np.max(probs)) * 100
|
| 117 |
+
emotion = emotions[predicted_class]
|
| 118 |
|
| 119 |
+
spotify_link = spotify_playlist_mapping.get(emotion, spotify_playlist_mapping["Neutral"])
|
| 120 |
+
return (
|
| 121 |
+
f"Detected Emotion: **{emotion}** (Confidence: {confidence:.2f}%)",
|
| 122 |
+
f"**Listen on Spotify:** <a href='{spotify_link}' target='_blank'>π§ {emotion} Vibes</a>"
|
| 123 |
+
)
|
| 124 |
|
| 125 |
+
# --- Gradio Interface ---
|
| 126 |
iface = gr.Interface(
|
| 127 |
fn=classify_expression_and_suggest_music,
|
| 128 |
+
inputs=gr.Image(
|
| 129 |
+
type="numpy",
|
| 130 |
+
source="webcam",
|
| 131 |
+
streaming=True,
|
| 132 |
+
label="Webcam Input"
|
| 133 |
+
),
|
| 134 |
outputs=[
|
| 135 |
+
gr.Textbox(label="Detected Emotion"),
|
| 136 |
+
gr.Markdown(label="Suggested Spotify Playlist")
|
| 137 |
],
|
| 138 |
live=True,
|
| 139 |
+
title="π MoodTune: Emotion-Based Music Recommender",
|
| 140 |
+
description="This app detects your mood from your face and plays music to match it! Allow webcam access to begin."
|
| 141 |
)
|
| 142 |
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
iface.launch()
|