Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,103 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
-
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
css = """
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
background-
|
| 20 |
-
background-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
.
|
| 28 |
-
background:
|
| 29 |
-
border:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
font-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
gr.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
</
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import warnings
|
| 5 |
+
|
| 6 |
+
# Suppress warnings
|
| 7 |
+
warnings.filterwarnings("ignore")
|
| 8 |
+
|
| 9 |
+
# Load model and encoders
|
| 10 |
+
emotion_model = joblib.load("emotion_model.pkl")
|
| 11 |
+
song_encoder = joblib.load("song_encoder.pkl")
|
| 12 |
+
emotion_decoder = joblib.load("emotion_decoder.pkl")
|
| 13 |
+
|
| 14 |
+
# CSS for funky retro aesthetic
|
| 15 |
+
css = """
|
| 16 |
+
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
|
| 17 |
+
|
| 18 |
+
body {
|
| 19 |
+
background-image: url('https://i.postimg.cc/tRnqqyLn/retrowave.gif');
|
| 20 |
+
background-size: cover;
|
| 21 |
+
background-repeat: no-repeat;
|
| 22 |
+
background-attachment: fixed;
|
| 23 |
+
font-family: 'Press Start 2P', cursive;
|
| 24 |
+
color: #00ffff;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
.gradio-container, .gradio-interface, .gradio-box, .title-box {
|
| 28 |
+
background-color: rgba(0, 0, 0, 0.75) !important;
|
| 29 |
+
border-radius: 15px !important;
|
| 30 |
+
padding: 25px !important;
|
| 31 |
+
box-shadow: 0 0 20px #00ffff;
|
| 32 |
+
font-family: 'Press Start 2P', cursive !important;
|
| 33 |
+
color: #00ffff;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
input, textarea, .gr-textbox, .gr-input {
|
| 37 |
+
background-color: #111 !important;
|
| 38 |
+
color: #00ffff !important;
|
| 39 |
+
border: 2px solid #00ffff !important;
|
| 40 |
+
font-family: 'Press Start 2P', cursive !important;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
.track-btn {
|
| 44 |
+
background: linear-gradient(135deg, #ff00cc, #3333ff) !important;
|
| 45 |
+
border: none !important;
|
| 46 |
+
color: white !important;
|
| 47 |
+
font-weight: bold !important;
|
| 48 |
+
border-radius: 10px !important;
|
| 49 |
+
padding: 10px 25px !important;
|
| 50 |
+
font-size: 12px !important;
|
| 51 |
+
font-family: 'Press Start 2P', cursive !important;
|
| 52 |
+
box-shadow: 0 0 10px #ff00cc;
|
| 53 |
+
transition: all 0.3s ease !important;
|
| 54 |
+
margin-top: 20px !important;
|
| 55 |
+
}
|
| 56 |
+
.track-btn:hover {
|
| 57 |
+
transform: scale(1.05);
|
| 58 |
+
background: linear-gradient(135deg, #3333ff, #ff00cc) !important;
|
| 59 |
+
}
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
def predict_emotion(song_name):
|
| 63 |
+
try:
|
| 64 |
+
encoded_song = song_encoder.transform([song_name])
|
| 65 |
+
emotion_label = emotion_model.predict(np.array(encoded_song).reshape(1, -1))[0]
|
| 66 |
+
emotion = emotion_decoder.inverse_transform([emotion_label])[0]
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"<p style='color: #ff4444;'>Error: {str(e)}</p>", gr.update(visible=False)
|
| 69 |
+
|
| 70 |
+
return f"""
|
| 71 |
+
<div style='text-align: center; font-family: "Press Start 2P", cursive;'>
|
| 72 |
+
<h2 style="color: #ff00cc;">๐ฝ Mood Tracker Results ๐ฝ</h2>
|
| 73 |
+
<p>Your current pixel-powered emotion is:</p>
|
| 74 |
+
<h1 style='font-size: 2.2em; color: #00ffff;'>{emotion.upper()}</h1>
|
| 75 |
+
</div>
|
| 76 |
+
""", gr.update(visible=True)
|
| 77 |
+
|
| 78 |
+
def clear_form():
|
| 79 |
+
return "", "", gr.update(visible=False)
|
| 80 |
+
|
| 81 |
+
# Gradio interface
|
| 82 |
+
with gr.Blocks(title="๐ฎ 8-Bit Song2Mood Tracker", theme=gr.themes.Soft(), css=css) as app:
|
| 83 |
+
with gr.Column(elem_classes="title-box"):
|
| 84 |
+
gr.Markdown("""
|
| 85 |
+
<div style="text-align: center;">
|
| 86 |
+
<h1 style="color: #ff00cc;">๐ฎ SONG2MOOD TRACKER ๐ง</h1>
|
| 87 |
+
<p>Type a song. Get the vibe. Unlock the emotion.</p>
|
| 88 |
+
</div>
|
| 89 |
+
""")
|
| 90 |
+
|
| 91 |
+
song_input = gr.Textbox(
|
| 92 |
+
label="๐ต Type your song below:",
|
| 93 |
+
placeholder="e.g., Blinding Lights by The Weeknd"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
submit_btn = gr.Button("๐๏ธ Decode My Mood", elem_classes="track-btn")
|
| 97 |
+
output_html = gr.HTML()
|
| 98 |
+
clear_btn = gr.Button("๐ Try Another Track", visible=False, elem_classes="track-btn")
|
| 99 |
+
|
| 100 |
+
submit_btn.click(predict_emotion, inputs=song_input, outputs=[output_html, clear_btn])
|
| 101 |
+
clear_btn.click(clear_form, inputs=None, outputs=[output_html, song_input, clear_btn])
|
| 102 |
+
|
| 103 |
+
app.launch()
|