Spaces:
Runtime error
Runtime error
Shea
commited on
Commit
•
511e1bc
1
Parent(s):
5986780
update
Browse files- app.ipynb +253 -0
- app.py +70 -40
- v2ga_w_embeddings_half.parquet +3 -0
app.ipynb
ADDED
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import numpy as np\n",
|
10 |
+
"import pandas as pd\n",
|
11 |
+
"import pyarrow"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 2,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"df = pd.read_parquet('v2ga_w_embeddings_half.parquet')"
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": 3,
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"def cosine_similarity(v1, v2):\n",
|
30 |
+
" dot_product = np.dot(v1, v2)\n",
|
31 |
+
" v1_norm = np.linalg.norm(v1)\n",
|
32 |
+
" v2_norm = np.linalg.norm(v2)\n",
|
33 |
+
" if v1_norm == 0.0 or v2_norm == 0.0:\n",
|
34 |
+
" return np.nan\n",
|
35 |
+
" else:\n",
|
36 |
+
" similarity = dot_product / (v1_norm * v2_norm)\n",
|
37 |
+
" return similarity"
|
38 |
+
]
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "code",
|
42 |
+
"execution_count": 25,
|
43 |
+
"metadata": {},
|
44 |
+
"outputs": [],
|
45 |
+
"source": [
|
46 |
+
"def relevance_scores(query_embed,df,embeddings):\n",
|
47 |
+
" scores = [cosine_similarity(query_embed, v2) for v2 in df[embeddings]]\n",
|
48 |
+
" scores = pd.Series(scores)\n",
|
49 |
+
" # sort scores in descending order\n",
|
50 |
+
" scores = scores.sort_values(ascending=False)\n",
|
51 |
+
" # set first score to 0\n",
|
52 |
+
" scores.iloc[0] = 0\n",
|
53 |
+
" return(scores)"
|
54 |
+
]
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"cell_type": "code",
|
58 |
+
"execution_count": 46,
|
59 |
+
"metadata": {},
|
60 |
+
"outputs": [],
|
61 |
+
"source": [
|
62 |
+
"def semantic_search(artist, title):\n",
|
63 |
+
"\n",
|
64 |
+
" chosen_song = df[(df['artist'] == artist) & (df['title'] == title)]\n",
|
65 |
+
"\n",
|
66 |
+
" scores_glove = relevance_scores(chosen_song[\"embedding_glove\"].values[0],df,\"embedding_glove\")\n",
|
67 |
+
" index_glove = scores_glove.idxmax()\n",
|
68 |
+
" result_glove = df.iloc[index_glove][['title', 'artist', 'lyrics']]\n",
|
69 |
+
" result_glove['lyrics'] = result_glove['lyrics'].replace('\\n', '. ')\n",
|
70 |
+
"\n",
|
71 |
+
" scores_minilm = relevance_scores(chosen_song[\"embedding_minilm\"].values[0],df,\"embedding_minilm\")\n",
|
72 |
+
" index_minilm = scores_minilm.idxmax()\n",
|
73 |
+
" result_minilm = df.iloc[index_minilm][['title', 'artist', 'lyrics']]\n",
|
74 |
+
" result_minilm['lyrics'] = result_minilm['lyrics'].replace('\\n', '. ')\n",
|
75 |
+
"\n",
|
76 |
+
" scores_roberta = relevance_scores(chosen_song[\"embedding_roberta\"].values[0],df,\"embedding_roberta\")\n",
|
77 |
+
" index_roberta = scores_roberta.idxmax()\n",
|
78 |
+
" result_roberta = df.iloc[index_roberta][['title', 'artist', 'lyrics']]\n",
|
79 |
+
" result_roberta['lyrics'] = result_roberta['lyrics'].replace('\\n', '. ')\n",
|
80 |
+
"\n",
|
81 |
+
" scores_gpt = relevance_scores(chosen_song[\"embedding_gpt\"].values[0],df,\"embedding_gpt\")\n",
|
82 |
+
" index_gpt = scores_gpt.idxmax()\n",
|
83 |
+
" result_gpt = df.iloc[index_gpt][['title', 'artist', 'lyrics']]\n",
|
84 |
+
" result_gpt['lyrics'] = result_gpt['lyrics'].replace('\\n', '. ')\n",
|
85 |
+
"\n",
|
86 |
+
" chosen_song = chosen_song[['title', 'artist', 'lyrics']].iloc[0]\n",
|
87 |
+
" chosen_song['lyrics'] = chosen_song['lyrics'].replace('\\n', '. ')\n",
|
88 |
+
"\n",
|
89 |
+
" results = {\n",
|
90 |
+
" 'chosen_song': chosen_song.to_dict(),\n",
|
91 |
+
" 'glove': result_glove.to_dict(),\n",
|
92 |
+
" 'minilm': result_minilm.to_dict(),\n",
|
93 |
+
" 'roberta': result_roberta.to_dict(),\n",
|
94 |
+
" 'gpt': result_gpt.to_dict()\n",
|
95 |
+
" }\n",
|
96 |
+
"\n",
|
97 |
+
" return results"
|
98 |
+
]
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"cell_type": "code",
|
102 |
+
"execution_count": 47,
|
103 |
+
"metadata": {},
|
104 |
+
"outputs": [
|
105 |
+
{
|
106 |
+
"data": {
|
107 |
+
"text/plain": [
|
108 |
+
"{'chosen_song': {'title': 'Century City',\n",
|
109 |
+
" 'artist': 'Tom Petty',\n",
|
110 |
+
" 'lyrics': \"Tom Petty\\nMiscellaneous\\nCentury City\\nSometimes I wanna leave here\\nSometimes I wanna go right back where I came from\\nBack where I belong\\nBut it never lasts for too long, it always goes away\\nAnd I still don't look for reasons\\nThat's much too hard these days\\nWhy worry 'bout the rain?\\nWhy worry 'bout the thunder?\\nCentury City's got everything covered\\nWell, your mama gave you lovin'\\nMama held you near\\nNow mama can't do nothin'\\nBaby, mama just ain't here\\nAnd you can pretend all you want to do\\nBut that won't work no more\\nAnd you can't run back to daddy\\nYou tried that once before\\nBut why worry 'bout your daddy?\\nWhy worry 'bout your mother?\\nCentury City's got everything covered\\nWe're gonna live in Century City\\nGo ahead and give in (Century City) like modern men\\nAnd modern girls, we're gonna live in the modern world\\nSometimes I get discouraged\\nSometimes I feel so down\\nSometimes I get so worried\\nAnd I don't know what about\\nBut it works out in the long run\\nIt always goes away\\nI've come now to accept it\\nAs a reoccurring phase\\nWhy worry 'bout the rain?\\nWhy worry 'bout the thunder?\\nCentury City's got everything covered\"},\n",
|
111 |
+
" 'glove': {'title': 'Visit',\n",
|
112 |
+
" 'artist': '311',\n",
|
113 |
+
" 'lyrics': \"He wouldn't say he cared at all if you asked him. You're heading for a fall brother it goes right past him. In another world he's in another place. You now the need for speed. Is just another form of greed. But when you jones'n your brains frozen. You're not thinkin' bout the choices you makin' you're just rollin'. On and on it's anybody's guess 'cause no one's at the wheel at the front of. That mess yes. . The sun's goin' down for me it's goin' down for me. He and she are what I need yes they're what I need. But I'm gonna buy a ticket I'm not gonna even pick it. I heard it he said it I heard it. But it we but it went something like. . Chorus :. Visit. I wanna visit the world. So now I visit the world. With my time on this world. Because livin' when you're hungry is a dog in an alley. Now I mind my business 'cause I'm rhymin' down in Cali. I ain't playin' when sayin' kings of the ghetto feel they losing ground. Vato's goin' loco because everyone's brought down. This is a visit then we dead fade to dust strickin'. I'm wearing my Doc Martens 'cause I'm always down for kickin'. This my friend the city pity everywhere the enemy. Ready or not homey stompin' everyone in front of me. . Jump up and down cus that's the 311 style. Cruise on by the frowners float follow me now with a smile. Looking at the ocean I say there's plenty. Looking at the river I say there's plenty. Chorus. . Jump up and down 'cause that's the 311 style. Cruise on by the frowners float follow me now with a smile. I got a golden ticket I'm not gonna even pick it. I heard it he said it I heard it. But it went but it went something like. Chorus. Chorus\"},\n",
|
114 |
+
" 'minilm': {'title': 'Leaving Town Blues take 5',\n",
|
115 |
+
" 'artist': 'Fleetwood Mac',\n",
|
116 |
+
" 'lyrics': \"Written by Peter Green. . Now, when I leave this town. Won't be - back no more. Yes, when I leave this town, mama. Won't be back no more. I've got the blues so bad. Standing 'round my door. . Yes, I got the blues so bad. Why won't you let me be. I have the blues so bad, mama. Why won't you let me be. Now, when I go to Chicago. Blues don't you follow me. . Break:. . I'm asking mama, mama. Please don't cry no tears. Yes, I say mama, mama. Please don't cry no tears. Because I'll always love you. Through all my days and years\"},\n",
|
117 |
+
" 'roberta': {'title': 'Someday Baby - Alternate Version Modern Times',\n",
|
118 |
+
" 'artist': 'Bob Dylan',\n",
|
119 |
+
" 'lyrics': \"I don't care what you do, don't care what you say. Don't care where you go, or how long you stay. Someday baby, you ain't gonna worry po' me any more. . You take my money and you turn me out. You fill me up with self-doubt. Someday baby, you ain't gonna worry po' me any more. . You made me eat a ton of dust. You're potentially dangerous and not worthy of trust. Someday baby, you ain't gonna worry po' me any more. . Little by little, bit by bit. Every day I'm becomin' more of a hypocrite. Someday baby, you ain't gonna worry po' me any more. . You've got my mind tied up in knots. I just keep recyclin' the same old thoughts. Someday baby, you ain't gonna worry po' me any more. . When I heard you was cold, I bought you a coat and hat. I think you must have forgotten 'bout that. Someday baby, you ain't gonna worry po' me any more. Gonna blow out your mind and make it pure. I've taken about as much of this as I can endure. Someday baby, you ain't gonna worry po' me any more. . You put me down from a haver creak. That's all right, to you i turn the other cheek. Someday baby, you ain't gonna worry po' me any more. . You say you need me, how would I know?. You say you love me, but it can't be so. Someday baby, you ain't gonna worry po' me any more. . I don't wanna brag, but I'll wring your neck. When all else fails, I'll make it a matter of self-respect. Someday baby, you ain't gonna worry po' me any more. . Livin' this way ain't a natural thing to do. Why was I born to love you?. Someday baby, you ain't gonna worry po' me any more\"},\n",
|
120 |
+
" 'gpt': {'title': 'Long Time Sunshine Reprise',\n",
|
121 |
+
" 'artist': 'Weezer',\n",
|
122 |
+
" 'lyrics': \"Sometimes I wanna pack it all up, get on a bus and move to Vermont. Or Maine, or any of those states back east that I remember. Sometimes I wanna go back to school, an east coast college with some history. I'd be satisfied, I know, in the simple things. Longtime sunshine. Longtime sunshine upon me. Sometime I wanna build a house with a wood stove or a fire place. In the middle of the living room an old piano. Sometimes it don't seem so bad to settle down with a good woman;. Leave this lonely life behind forever, and ever. Longtime sunshine. Longtime sunshine upon me. Goodbye friends, goodbye my girl, close my eyes as you fly away;. Keep on going 'til you get some place where you can truly rest. Longtime sunshine. Longtime sunshine upon me. Long time sunshine, long time sunshine upon me. Why bother? It's gonna hurt me, it's gonna kill when you desert me. He is in my eyes, he is in my ears, he is in my blood, he is in my tears. No, there is no other one, no, there is no other one, can't have any other one, even though I ???. Blast off! Up to the stars we go, and leave behind everything I use to know. Somebody's giving me a whole lot of money to do what I think I want to. So why am I still feeling blue? Oh Wuan and Dondo\"}}"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
"execution_count": 47,
|
126 |
+
"metadata": {},
|
127 |
+
"output_type": "execute_result"
|
128 |
+
}
|
129 |
+
],
|
130 |
+
"source": [
|
131 |
+
"semantic_search(\"Tom Petty\", \"Century City\")"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 49,
|
137 |
+
"metadata": {},
|
138 |
+
"outputs": [
|
139 |
+
{
|
140 |
+
"data": {
|
141 |
+
"text/plain": [
|
142 |
+
"title Century City\n",
|
143 |
+
"artist Tom Petty\n",
|
144 |
+
"lyrics Tom Petty. Miscellaneous. Century City. Someti...\n",
|
145 |
+
"Name: 3833, dtype: object"
|
146 |
+
]
|
147 |
+
},
|
148 |
+
"execution_count": 49,
|
149 |
+
"metadata": {},
|
150 |
+
"output_type": "execute_result"
|
151 |
+
}
|
152 |
+
],
|
153 |
+
"source": [
|
154 |
+
"artist = \"Tom Petty\"\n",
|
155 |
+
"title = \"Century City\"\n",
|
156 |
+
"\n",
|
157 |
+
"chosen_song = df[(df['artist'] == artist) & (df['title'] == title)]\n",
|
158 |
+
"chosen_song = chosen_song[['title', 'artist', 'lyrics']].iloc[0]\n",
|
159 |
+
"chosen_song['lyrics'] = chosen_song['lyrics'].replace('\\n', '. ')\n",
|
160 |
+
"\n",
|
161 |
+
"chosen_song"
|
162 |
+
]
|
163 |
+
},
|
164 |
+
{
|
165 |
+
"cell_type": "code",
|
166 |
+
"execution_count": 35,
|
167 |
+
"metadata": {},
|
168 |
+
"outputs": [
|
169 |
+
{
|
170 |
+
"data": {
|
171 |
+
"text/html": [
|
172 |
+
"<div>\n",
|
173 |
+
"<style scoped>\n",
|
174 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
175 |
+
" vertical-align: middle;\n",
|
176 |
+
" }\n",
|
177 |
+
"\n",
|
178 |
+
" .dataframe tbody tr th {\n",
|
179 |
+
" vertical-align: top;\n",
|
180 |
+
" }\n",
|
181 |
+
"\n",
|
182 |
+
" .dataframe thead th {\n",
|
183 |
+
" text-align: right;\n",
|
184 |
+
" }\n",
|
185 |
+
"</style>\n",
|
186 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
187 |
+
" <thead>\n",
|
188 |
+
" <tr style=\"text-align: right;\">\n",
|
189 |
+
" <th></th>\n",
|
190 |
+
" <th>title</th>\n",
|
191 |
+
" <th>artist</th>\n",
|
192 |
+
" <th>lyrics</th>\n",
|
193 |
+
" </tr>\n",
|
194 |
+
" </thead>\n",
|
195 |
+
" <tbody>\n",
|
196 |
+
" <tr>\n",
|
197 |
+
" <th>3833</th>\n",
|
198 |
+
" <td>Century City</td>\n",
|
199 |
+
" <td>Tom Petty</td>\n",
|
200 |
+
" <td>Tom Petty\\nMiscellaneous\\nCentury City\\nSometi...</td>\n",
|
201 |
+
" </tr>\n",
|
202 |
+
" </tbody>\n",
|
203 |
+
"</table>\n",
|
204 |
+
"</div>"
|
205 |
+
],
|
206 |
+
"text/plain": [
|
207 |
+
" title artist \n",
|
208 |
+
"3833 Century City Tom Petty \\\n",
|
209 |
+
"\n",
|
210 |
+
" lyrics \n",
|
211 |
+
"3833 Tom Petty\\nMiscellaneous\\nCentury City\\nSometi... "
|
212 |
+
]
|
213 |
+
},
|
214 |
+
"execution_count": 35,
|
215 |
+
"metadata": {},
|
216 |
+
"output_type": "execute_result"
|
217 |
+
}
|
218 |
+
],
|
219 |
+
"source": [
|
220 |
+
"chosen_song"
|
221 |
+
]
|
222 |
+
},
|
223 |
+
{
|
224 |
+
"cell_type": "code",
|
225 |
+
"execution_count": null,
|
226 |
+
"metadata": {},
|
227 |
+
"outputs": [],
|
228 |
+
"source": []
|
229 |
+
}
|
230 |
+
],
|
231 |
+
"metadata": {
|
232 |
+
"kernelspec": {
|
233 |
+
"display_name": "Python 3",
|
234 |
+
"language": "python",
|
235 |
+
"name": "python3"
|
236 |
+
},
|
237 |
+
"language_info": {
|
238 |
+
"codemirror_mode": {
|
239 |
+
"name": "ipython",
|
240 |
+
"version": 3
|
241 |
+
},
|
242 |
+
"file_extension": ".py",
|
243 |
+
"mimetype": "text/x-python",
|
244 |
+
"name": "python",
|
245 |
+
"nbconvert_exporter": "python",
|
246 |
+
"pygments_lexer": "ipython3",
|
247 |
+
"version": "3.11.2"
|
248 |
+
},
|
249 |
+
"orig_nbformat": 4
|
250 |
+
},
|
251 |
+
"nbformat": 4,
|
252 |
+
"nbformat_minor": 2
|
253 |
+
}
|
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import pyarrow
|
5 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
6 |
|
7 |
import os
|
8 |
import requests
|
@@ -16,59 +16,89 @@ with open(filename, 'wb') as file:
|
|
16 |
if chunk:
|
17 |
file.write(chunk)
|
18 |
|
19 |
-
print(f"File '{filename}'
|
20 |
-
|
21 |
-
|
22 |
-
pwd = os.getcwd()
|
23 |
-
print("Present Working Directory:", pwd)
|
24 |
-
|
25 |
-
contents = os.listdir(pwd)
|
26 |
-
print("Contents of the Directory:")
|
27 |
-
for item in contents:
|
28 |
-
print(item)
|
29 |
|
30 |
df = pd.read_parquet('v2ga_w_embeddings_half.parquet')
|
31 |
|
32 |
-
def
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
return results
|
52 |
|
53 |
-
|
54 |
-
titles = sorted(df[df['artist'] == artist]['title'].unique())
|
55 |
-
return titles
|
56 |
|
57 |
artists = sorted(df['artist'].unique())
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
-
output_interface = gr.
|
63 |
|
64 |
iface = gr.Interface(
|
65 |
-
fn=
|
66 |
inputs=[artist_dropdown, title_dropdown],
|
67 |
outputs=output_interface,
|
68 |
-
examples=
|
69 |
-
title="
|
70 |
-
description="Find
|
71 |
-
update=update_titles_dropdown
|
72 |
)
|
73 |
|
74 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio import components
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
import pyarrow
|
|
|
6 |
|
7 |
import os
|
8 |
import requests
|
|
|
16 |
if chunk:
|
17 |
file.write(chunk)
|
18 |
|
19 |
+
print(f"File '{filename}' download complete.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
df = pd.read_parquet('v2ga_w_embeddings_half.parquet')
|
22 |
|
23 |
+
def cosine_similarity(v1, v2):
|
24 |
+
dot_product = np.dot(v1, v2)
|
25 |
+
v1_norm = np.linalg.norm(v1)
|
26 |
+
v2_norm = np.linalg.norm(v2)
|
27 |
+
if v1_norm == 0.0 or v2_norm == 0.0:
|
28 |
+
return np.nan
|
29 |
+
else:
|
30 |
+
similarity = dot_product / (v1_norm * v2_norm)
|
31 |
+
return similarity
|
32 |
+
|
33 |
+
def relevance_scores(query_embed,df,embeddings):
|
34 |
+
scores = [cosine_similarity(query_embed, v2) for v2 in df[embeddings]]
|
35 |
+
scores = pd.Series(scores)
|
36 |
+
# sort scores in descending order
|
37 |
+
scores = scores.sort_values(ascending=False)
|
38 |
+
# set first score to 0
|
39 |
+
scores.iloc[0] = 0
|
40 |
+
return(scores)
|
41 |
+
|
42 |
+
def semantic_search(artist, title):
|
43 |
+
|
44 |
+
chosen_song = df[(df['artist'] == artist) & (df['title'] == title)]
|
45 |
+
|
46 |
+
scores_glove = relevance_scores(chosen_song["embedding_glove"].values[0],df,"embedding_glove")
|
47 |
+
index_glove = scores_glove.idxmax()
|
48 |
+
result_glove = df.iloc[index_glove][['title', 'artist', 'lyrics']]
|
49 |
+
result_glove['lyrics'] = result_glove['lyrics'].replace('\n', '. ')
|
50 |
+
|
51 |
+
scores_minilm = relevance_scores(chosen_song["embedding_minilm"].values[0],df,"embedding_minilm")
|
52 |
+
index_minilm = scores_minilm.idxmax()
|
53 |
+
result_minilm = df.iloc[index_minilm][['title', 'artist', 'lyrics']]
|
54 |
+
result_minilm['lyrics'] = result_minilm['lyrics'].replace('\n', '. ')
|
55 |
+
|
56 |
+
scores_roberta = relevance_scores(chosen_song["embedding_roberta"].values[0],df,"embedding_roberta")
|
57 |
+
index_roberta = scores_roberta.idxmax()
|
58 |
+
result_roberta = df.iloc[index_roberta][['title', 'artist', 'lyrics']]
|
59 |
+
result_roberta['lyrics'] = result_roberta['lyrics'].replace('\n', '. ')
|
60 |
+
|
61 |
+
scores_gpt = relevance_scores(chosen_song["embedding_gpt"].values[0],df,"embedding_gpt")
|
62 |
+
index_gpt = scores_gpt.idxmax()
|
63 |
+
result_gpt = df.iloc[index_gpt][['title', 'artist', 'lyrics']]
|
64 |
+
result_gpt['lyrics'] = result_gpt['lyrics'].replace('\n', '. ')
|
65 |
+
|
66 |
+
chosen_song = chosen_song[['title', 'artist', 'lyrics']].iloc[0]
|
67 |
+
chosen_song['lyrics'] = chosen_song['lyrics'].replace('\n', '. ')
|
68 |
+
|
69 |
+
results = {
|
70 |
+
'chosen_song': chosen_song.to_dict(),
|
71 |
+
'glove': result_glove.to_dict(),
|
72 |
+
'minilm': result_minilm.to_dict(),
|
73 |
+
'roberta': result_roberta.to_dict(),
|
74 |
+
'gpt': result_gpt.to_dict()
|
75 |
+
}
|
76 |
|
77 |
return results
|
78 |
|
79 |
+
from gradio.components import Dropdown
|
|
|
|
|
80 |
|
81 |
artists = sorted(df['artist'].unique())
|
82 |
+
titles = sorted(df['title'].unique())
|
83 |
+
|
84 |
+
artist_dropdown = Dropdown(artists, label="Artist")
|
85 |
+
title_dropdown = Dropdown(titles, label="Title")
|
86 |
|
87 |
+
# 100 random examples
|
88 |
+
df_sample = df.sample(100)
|
89 |
+
sample_artists = df_sample['artist'].tolist()
|
90 |
+
sample_titles = df_sample['title'].tolist()
|
91 |
+
artist_title_sample = [[artist, titles] for artist, titles in zip(sample_artists, sample_titles)]
|
92 |
|
93 |
+
output_interface = gr.components.JSON(label="Similar Songs")
|
94 |
|
95 |
iface = gr.Interface(
|
96 |
+
fn=semantic_search,
|
97 |
inputs=[artist_dropdown, title_dropdown],
|
98 |
outputs=output_interface,
|
99 |
+
examples=artist_title_sample,
|
100 |
+
title="Similar Song Finder",
|
101 |
+
description="Find four similar songs to the selected song based on different embeddings (GloVe, MiniLM, RoBERTa, GPT)."
|
|
|
102 |
)
|
103 |
|
104 |
iface.launch()
|
v2ga_w_embeddings_half.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:405f8b1651052cc1b974530d76e767578f6d7d957f931d433446dc89096a7ef4
|
3 |
+
size 133353339
|