IshmamF commited on
Commit
88ec395
1 Parent(s): e6caf54

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from spotify_api import SpotifyClient
3
+ from sentiment_analysis import SentimentAnalyzer
4
+ from song_matching import SongMatcher
5
+
6
+ # Spotify API credentials - ensure these are securely stored or use environment variables
7
+ CLIENT_ID = "your_spotify_client_id"
8
+ CLIENT_SECRET = "your_spotify_client_secret"
9
+
10
+ # Initialize SpotifyClient, SentimentAnalyzer, and SongMatcher
11
+ spotify_client = SpotifyClient(CLIENT_ID, CLIENT_SECRET)
12
+ sentiment_analyzer = SentimentAnalyzer()
13
+ song_matcher = SongMatcher('./app/music_mental_health.csv')
14
+
15
+ # Streamlit app layout
16
+ st.set_page_config(page_title="MODUS MUSIC", layout="wide") # New: Setting page title and layout
17
+ #st.markdown("<h1 style='text-align: center; font-weight: bold;'>MODUS MUSIC</h1>", unsafe_allow_html=True)
18
+
19
+ # Custom gradient background using CSS
20
+ st.markdown("""
21
+ <style>
22
+ .stApp {
23
+ background: rgb(0,0,0);
24
+ background-size: cover;
25
+ }
26
+ </style>
27
+ """, unsafe_allow_html=True)
28
+
29
+ st.title('Music Suggestion Based on Your Feeling') # Existing Title
30
+
31
+ # New: Introduction Section
32
+ with st.container():
33
+ st.markdown("""
34
+ <style>
35
+ .intro {
36
+ font-size:18px;
37
+ }
38
+ </style>
39
+ <div class='intro'>
40
+ Welcome to Modus Music! Share your vibe, and let's find the perfect songs to match your mood.
41
+ Just type in your thoughts, and we'll do the rest.
42
+ </div>
43
+ """, unsafe_allow_html=True)
44
+
45
+ # User input text area
46
+ with st.container():
47
+ user_input = st.text_area("What's your vibe? Tell me about it:", key="123", height=150, max_chars=500)
48
+ m = st.markdown("""
49
+ <style>
50
+ div.stButton > button:first-child {
51
+ background-color: rgb(204, 49, 49);
52
+
53
+ }
54
+ </style>""", unsafe_allow_html=True)
55
+ # Use the custom style for the button
56
+ submit_button = st.button("Generate music")
57
+
58
+ # Processing and Displaying Results
59
+ if submit_button and user_input.strip():
60
+ sentiment_label, sentiment_score = sentiment_analyzer.analyze_sentiment(user_input)
61
+ st.write(f"Sentiment: {sentiment_label}, Score: {sentiment_score:.2f}")
62
+
63
+ suggested_songs = song_matcher.match_songs_with_sentiment(sentiment_label, sentiment_score, user_input)
64
+
65
+ with st.container():
66
+ st.markdown("<div class='song-list'>", unsafe_allow_html=True)
67
+ st.write("Based on your mood, you might like these songs:")
68
+ for index, row in suggested_songs.iterrows():
69
+ song = row['song']
70
+ artist = row['artist']
71
+ track_id = spotify_client.get_track_id(song)
72
+ if track_id:
73
+ preview_url = spotify_client.get_track_preview_url(track_id)
74
+ st.write(f"{song} by {artist}")
75
+ with st.expander(f"Show Lyrics for {song} by {artist}", expanded=False):
76
+ st.write(f"Lyrics: {row['seq']}")
77
+ if preview_url:
78
+ st.audio(preview_url)
79
+ else:
80
+ st.write("No Preview Available")
81
+ else:
82
+ st.write(f"Unable to find {song} by {artist} on Spotify.")
83
+
84
+ st.markdown("</div>", unsafe_allow_html=True)
85
+ elif submit_button and not user_input.strip():
86
+ st.warning("Please provide a longer response with 5 words or more.")
87
+ st.rerun()