ZachZeke2004 commited on
Commit
00da74f
ยท
verified ยท
1 Parent(s): 42232c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -84
app.py CHANGED
@@ -1,84 +1,103 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()