damilojohn commited on
Commit
c15867b
·
1 Parent(s): d65cd6f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +138 -0
  2. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # importing libraries
2
+ import gradio as gr
3
+ import pickle
4
+ import pandas as pd
5
+ from huggingface_hub import hf_hub_download
6
+ import json
7
+ import requests
8
+
9
+ pd.options.mode.chained_assignment = None # Turn off SettingWithCopyWarning
10
+
11
+
12
+ songs_df = pd.read_csv(
13
+ hf_hub_download('damilojohn/Personal_Playlist_Generator',
14
+ repo_type='dataset',
15
+ filename='spotify_transformed.csv'))
16
+ mappings = pd.read_csv(
17
+ hf_hub_download('damilojohn/Personal_Playlist_Generator',
18
+ repo_type='dataset',
19
+ filename='song_mappings.csv'))
20
+ verses_df = pd.read_csv(
21
+ hf_hub_download('damilojohn/Personal_Playlist_Generator',
22
+ repo_type='dataset',
23
+ filename='verses.csv'))
24
+ song_embeddings = pickle.load(
25
+ open(hf_hub_download('damilojohn/Personal_Playlist_Generator',
26
+ repo_type='dataset',
27
+ filename='embeddings.pkl'), 'rb'))
28
+
29
+ verses_df.rename(columns={'0': 'verse'}, inplace=True)
30
+ mappings.rename(columns={'Unnamed: 0': 'verse', '0': 'song_name'},
31
+ inplace=True)
32
+
33
+
34
+ def generate_playlist(prompt):
35
+ payload = {'prompt': prompt}
36
+ response = requests.request('POST',
37
+ url='https://xi5j0hwh1a.execute-api.eu-west-2.amazonaws.com/test/huh',
38
+ data=json.dumps(payload)).json()
39
+ hits = response['hits']
40
+ hits = pd.DataFrame.from_dict(hits[0])
41
+ verses_match = verses_df.iloc[hits['corpus_id']]
42
+ songs_match = mappings[mappings['verse'].isin(
43
+ verses_match['verse'].values)]
44
+ songs_match = songs_df[songs_df['song_name'].isin(
45
+ songs_match['song_name'].values)]
46
+ songs_match = songs_match.sort_values('song_name')
47
+ songs_match = songs_match.drop_duplicates(subset='song_name')
48
+ songs_name = list(songs_match['song_name'][:9])
49
+ cover_art = list(songs_match['image'][:9])
50
+ images = [gr.Image.update(value=art, visible=True) for art in cover_art]
51
+ return (gr.Radio.update(label='songs', interactive=True,
52
+ choices=songs_name),
53
+ *images)
54
+
55
+
56
+ def set_example_prompt(examples):
57
+ return gr.TextArea.update(value=examples[0])
58
+
59
+
60
+ def create_frontend():
61
+ demo = gr.Blocks()
62
+ with demo:
63
+ gr.Markdown(
64
+ '''
65
+ # A Text based playlist Generator for Afrobeats
66
+
67
+ '''
68
+ )
69
+ with gr.Row():
70
+ with gr.Column():
71
+ gr.Markdown(
72
+ '''
73
+ Enter words describing your playlist
74
+ '''
75
+ )
76
+ song_prompt = gr.TextArea(
77
+ value='',
78
+ placeholder=" Enter a sentence that describes how you're feeling or what you want your playlist to be about "
79
+ )
80
+ example_prompts = gr.Dataset(
81
+ components=[song_prompt],
82
+ samples=[
83
+ ['heartbreak'],
84
+ ['love at the beach'],
85
+ ['uncertainty and bleak hopes']
86
+ ]
87
+ )
88
+ with gr.Column():
89
+ fetch_songs = gr.Button(
90
+ value='Enter to see playlist',).style(full_width=True)
91
+
92
+ with gr.Column():
93
+ song_options = gr.Radio(label='songs', interactive=True,
94
+ choices=None, type='value',
95
+ visible=True)
96
+ with gr.Column():
97
+ with gr.Row():
98
+ tile1 = gr.Image(value="/content/songs_cover.jpg",
99
+ show_label=False, visible=True)
100
+ tile2 = gr.Image(value="/content/songs_cover.jpg",
101
+ show_label=False, visible=True)
102
+ tile3 = gr.Image(value="/content/songs_cover.jpg",
103
+ show_label=False, visible=True)
104
+ with gr.Row():
105
+ tile4 = gr.Image(value="/content/songs_cover.jpg",
106
+ show_label=False, visible=True)
107
+ tile5 = gr.Image(value="/content/songs_cover.jpg",
108
+ show_label=False, visible=True)
109
+ tile6 = gr.Image(value="/content/songs_cover.jpg",
110
+ show_label=False, visible=True)
111
+ with gr.Row():
112
+ tile7 = gr.Image(value="/content/songs_cover.jpg",
113
+ show_label=False, visible=True)
114
+ tile8 = gr.Image(value='/content/songs_cover.jpg',
115
+ show_label=False, visible=True)
116
+
117
+ tiles = [tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8]
118
+
119
+ fetch_songs.click(
120
+ fn=generate_playlist,
121
+ inputs=[song_prompt],
122
+ outputs=[song_options, *tiles]
123
+ )
124
+ example_prompts.click(
125
+ fn=set_example_prompt,
126
+ inputs=[example_prompts],
127
+ outputs=example_prompts.components
128
+ )
129
+
130
+ demo.launch(debug=True)
131
+
132
+
133
+ def main():
134
+ create_frontend()
135
+
136
+
137
+ if __name__ == "__main__":
138
+ main()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ sentence-transformers
2
+ spotify
3
+ requests_html
4
+ boto3
5
+ datasets
6
+ # torch --extra-index-url https://download.pytorch.org/whl/cpu
7
+ torch
8
+ loguru
9
+ huggingface-hub
10
+ wandb