Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder
|
| 5 |
+
import warnings
|
| 6 |
+
|
| 7 |
+
# Suppress warnings
|
| 8 |
+
warnings.filterwarnings("ignore")
|
| 9 |
+
|
| 10 |
+
# Load model and encoder
|
| 11 |
+
emotion_model = joblib.load("emotion_model.pkl")
|
| 12 |
+
song_encoder = joblib.load("song_encoder.pkl")
|
| 13 |
+
emotion_decoder = joblib.load("emotion_decoder.pkl") # For converting predicted label to text
|
| 14 |
+
|
| 15 |
+
css = """
|
| 16 |
+
body {
|
| 17 |
+
background-image: url('https://i.postimg.cc/Y0gQkvSb/music-mood-bg.jpg');
|
| 18 |
+
background-size: cover;
|
| 19 |
+
background-repeat: no-repeat;
|
| 20 |
+
background-attachment: fixed;
|
| 21 |
+
}
|
| 22 |
+
.gradio-container, .gradio-interface, .gradio-box, .title-box {
|
| 23 |
+
background-color: rgba(255, 255, 255, 0.7) !important;
|
| 24 |
+
border-radius: 10px !important;
|
| 25 |
+
padding: 20px !important;
|
| 26 |
+
}
|
| 27 |
+
.track-btn {
|
| 28 |
+
background: linear-gradient(135deg, #6a11cb, #2575fc) !important;
|
| 29 |
+
border: none !important;
|
| 30 |
+
color: white !important;
|
| 31 |
+
margin-top: 15px !important;
|
| 32 |
+
font-weight: bold !important;
|
| 33 |
+
border-radius: 25px !important;
|
| 34 |
+
padding: 10px 25px !important;
|
| 35 |
+
font-size: 16px !important;
|
| 36 |
+
box-shadow: 0 4px 8px rgba(106, 17, 203, 0.3) !important;
|
| 37 |
+
transition: all 0.3s ease !important;
|
| 38 |
+
}
|
| 39 |
+
.track-btn:hover {
|
| 40 |
+
background: linear-gradient(135deg, #2575fc, #6a11cb) !important;
|
| 41 |
+
transform: translateY(-2px) !important;
|
| 42 |
+
}
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def predict_emotion(song_name):
|
| 46 |
+
try:
|
| 47 |
+
# Encode input song
|
| 48 |
+
encoded_song = song_encoder.transform([song_name])
|
| 49 |
+
emotion_label = emotion_model.predict(np.array(encoded_song).reshape(1, -1))[0]
|
| 50 |
+
emotion = emotion_decoder.inverse_transform([emotion_label])[0]
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"<p style='color: red;'>Error: {str(e)}</p>", gr.update(visible=False)
|
| 53 |
+
|
| 54 |
+
return f"""
|
| 55 |
+
<div style='text-align: center;'>
|
| 56 |
+
<h2>🎵 Mood Detected 🎵</h2>
|
| 57 |
+
<p>Your current emotional vibe is:</p>
|
| 58 |
+
<h1 style='font-size: 2.5em; color: #6a11cb;'>{emotion}</h1>
|
| 59 |
+
</div>
|
| 60 |
+
""", gr.update(visible=True)
|
| 61 |
+
|
| 62 |
+
def clear_form():
|
| 63 |
+
return "", "", gr.update(visible=False)
|
| 64 |
+
|
| 65 |
+
# Gradio interface
|
| 66 |
+
with gr.Blocks(title="🎧 Song2Mood Tracker", theme=gr.themes.Soft(), css=css) as app:
|
| 67 |
+
with gr.Column(elem_classes="title-box"):
|
| 68 |
+
gr.Markdown("""
|
| 69 |
+
<div style="text-align: center;">
|
| 70 |
+
<h1>🎧 Song2Mood Tracker</h1>
|
| 71 |
+
<p>Type a song that reflects your vibe right now—and we'll tell you your mood.</p>
|
| 72 |
+
</div>
|
| 73 |
+
""")
|
| 74 |
+
|
| 75 |
+
song_input = gr.Textbox(label="🎵 What song are you listening to?", placeholder="e.g., Heather by Conan Gray")
|
| 76 |
+
|
| 77 |
+
submit_btn = gr.Button("Track Emotion 🎶", elem_classes="track-btn")
|
| 78 |
+
output_html = gr.HTML()
|
| 79 |
+
clear_btn = gr.Button("Try Another Song", visible=False, elem_classes="track-btn")
|
| 80 |
+
|
| 81 |
+
submit_btn.click(predict_emotion, inputs=song_input, outputs=[output_html, clear_btn])
|
| 82 |
+
clear_btn.click(clear_form, inputs=None, outputs=[output_html, song_input, clear_btn])
|
| 83 |
+
|
| 84 |
+
app.launch()
|