Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ def update_scores(winner, loser, k_factor=100):
|
|
| 11 |
loser -= score_difference
|
| 12 |
return winner, loser
|
| 13 |
|
| 14 |
-
def vote_startup(songs_df
|
| 15 |
try:
|
| 16 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 17 |
except:
|
|
@@ -31,7 +31,7 @@ def vote_startup(songs_df, previews_df):
|
|
| 31 |
first_string = first_df["song_title"].tolist()[0]+" - "+first_df["artist"].tolist()[0]
|
| 32 |
second_df = comparison_song
|
| 33 |
second_string = second_df["song_title"]+" - "+second_df["artist"]
|
| 34 |
-
return f"Do you like '{artist} - {song_title}' better than '{comparison_song['artist']} - {comparison_song['song_title']}'?", first_string, second_string, display_rankings(songs_df)
|
| 35 |
else:
|
| 36 |
return "Add some songs to start voting!", "", "", display_rankings(songs_df)
|
| 37 |
|
|
@@ -55,7 +55,7 @@ def add_and_compare(artist, song_title, songs_df):
|
|
| 55 |
return "", "", display_rankings(songs_df)
|
| 56 |
|
| 57 |
# Function to update Elo ratings based on user's choice
|
| 58 |
-
def update_ratings_pos(first_string, second_string, songs_df
|
| 59 |
try:
|
| 60 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 61 |
except:
|
|
@@ -87,12 +87,12 @@ def update_ratings_pos(first_string, second_string, songs_df, previews_df):
|
|
| 87 |
first_string = first_df["song_title"].tolist()[0]+" - "+first_df["artist"].tolist()[0]
|
| 88 |
second_df = comparison_song
|
| 89 |
second_string = second_df["song_title"]+" - "+second_df["artist"]
|
| 90 |
-
return f"Do you like '{artist} - {song_title}' better than '{comparison_song['artist']} - {comparison_song['song_title']}'?", first_string, second_string, display_rankings(songs_df)
|
| 91 |
else:
|
| 92 |
return "Add some songs to start voting!", "", "", display_rankings(songs_df)
|
| 93 |
|
| 94 |
# Function to update Elo ratings based on user's choice
|
| 95 |
-
def update_ratings_neg(first_string, second_string, songs_df
|
| 96 |
try:
|
| 97 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 98 |
except:
|
|
@@ -125,7 +125,7 @@ def update_ratings_neg(first_string, second_string, songs_df, previews_df):
|
|
| 125 |
first_string = first_df["song_title"].tolist()[0]+" - "+first_df["artist"].tolist()[0]
|
| 126 |
second_df = comparison_song
|
| 127 |
second_string = second_df["song_title"]+" - "+second_df["artist"]
|
| 128 |
-
return f"Do you like '{artist} - {song_title}' better than '{comparison_song['artist']} - {comparison_song['song_title']}'?", first_string, second_string, display_rankings(songs_df)
|
| 129 |
else:
|
| 130 |
return "Add some songs to start voting!", "", "", display_rankings(songs_df)
|
| 131 |
|
|
@@ -135,31 +135,25 @@ def display_rankings(songs_df=pd.DataFrame(columns=['elo_score', 'artist', 'song
|
|
| 135 |
songs_df.to_csv("songs_df.csv")
|
| 136 |
return songs_df
|
| 137 |
|
| 138 |
-
def export_csv(songs_df
|
| 139 |
# Function to export DataFrame to CSV
|
| 140 |
save_df = songs_df
|
| 141 |
-
save_df["preview_url"] = [previews_df[previews_df["artist"]==artist][previews_df["song_title"]==song_title].iloc[0]["preview_url"] for artist, song_title in zip(songs_df["artist"].tolist(),songs_df["song_title"].tolist())]
|
| 142 |
save_df.to_csv("songs_df.csv")
|
| 143 |
return "songs_df.csv"
|
| 144 |
|
| 145 |
-
def import_csv(file, songs_df
|
| 146 |
if file is not None:
|
| 147 |
#file_content = file.decode('utf-8')
|
| 148 |
new_df = pd.read_csv(file)
|
| 149 |
try:
|
| 150 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 151 |
-
previews_df = previews_df[["elo_score", "artist", "song_title", "preview_url"]]
|
| 152 |
except:
|
| 153 |
songs_df = pd.DataFrame(columns=['elo_score', 'artist', 'song_title'])
|
| 154 |
-
previews_df = pd.DataFrame(columns=["elo_score", "artist", "song_title", "preview_url"])
|
| 155 |
-
new_df = new_df[["elo_score", "artist", "song_title", "preview_url"]]
|
| 156 |
-
previews_df = pd.concat([previews_df,new_df])
|
| 157 |
new_df = new_df[["elo_score", "artist", "song_title"]]
|
| 158 |
songs_df = pd.concat([songs_df,new_df])
|
| 159 |
|
| 160 |
songs_df = songs_df.drop_duplicates(subset=['artist', 'song_title'])
|
| 161 |
-
|
| 162 |
-
return songs_df, previews_df
|
| 163 |
|
| 164 |
|
| 165 |
# Function to remove a song
|
|
@@ -250,13 +244,13 @@ with gr.Blocks(theme=theme) as app:
|
|
| 250 |
|
| 251 |
# Import CSV file to replace the existing DataFrame
|
| 252 |
import_button = gr.File(label="Import CSV", file_count="single")
|
| 253 |
-
import_button.change(fn=import_csv, inputs=[import_button, rankings
|
| 254 |
|
| 255 |
with gr.Column():
|
| 256 |
# Export button to download the DataFrame as CSV
|
| 257 |
export_link = gr.File(label="Download CSV", file_count="single")
|
| 258 |
export_button = gr.Button("Export as CSV")
|
| 259 |
-
export_button.click(fn=export_csv, inputs=[rankings
|
| 260 |
|
| 261 |
gr.Markdown("### Reset Data")
|
| 262 |
with gr.Row():
|
|
@@ -266,8 +260,8 @@ with gr.Blocks(theme=theme) as app:
|
|
| 266 |
clear_button.click(clear_rankings, inputs=[rankings], outputs=rankings)
|
| 267 |
|
| 268 |
# add_button.click(add_and_compare, inputs=[artist_input, song_title_input, rankings], outputs=[artist_input, song_title_input, rankings])
|
| 269 |
-
yes_button.click(update_ratings_pos, inputs=[compare_index_1, compare_index_2, rankings
|
| 270 |
-
no_button.click(update_ratings_neg, inputs=[compare_index_1, compare_index_2, rankings
|
| 271 |
-
new_vote.click(vote_startup, inputs=[rankings, previews],outputs=[compare_output, compare_index_1, compare_index_2, rankings
|
| 272 |
|
| 273 |
app.launch(share=False)
|
|
|
|
| 11 |
loser -= score_difference
|
| 12 |
return winner, loser
|
| 13 |
|
| 14 |
+
def vote_startup(songs_df):
|
| 15 |
try:
|
| 16 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 17 |
except:
|
|
|
|
| 31 |
first_string = first_df["song_title"].tolist()[0]+" - "+first_df["artist"].tolist()[0]
|
| 32 |
second_df = comparison_song
|
| 33 |
second_string = second_df["song_title"]+" - "+second_df["artist"]
|
| 34 |
+
return f"Do you like '{artist} - {song_title}' better than '{comparison_song['artist']} - {comparison_song['song_title']}'?", first_string, second_string, display_rankings(songs_df)
|
| 35 |
else:
|
| 36 |
return "Add some songs to start voting!", "", "", display_rankings(songs_df)
|
| 37 |
|
|
|
|
| 55 |
return "", "", display_rankings(songs_df)
|
| 56 |
|
| 57 |
# Function to update Elo ratings based on user's choice
|
| 58 |
+
def update_ratings_pos(first_string, second_string, songs_df):
|
| 59 |
try:
|
| 60 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 61 |
except:
|
|
|
|
| 87 |
first_string = first_df["song_title"].tolist()[0]+" - "+first_df["artist"].tolist()[0]
|
| 88 |
second_df = comparison_song
|
| 89 |
second_string = second_df["song_title"]+" - "+second_df["artist"]
|
| 90 |
+
return f"Do you like '{artist} - {song_title}' better than '{comparison_song['artist']} - {comparison_song['song_title']}'?", first_string, second_string, display_rankings(songs_df)
|
| 91 |
else:
|
| 92 |
return "Add some songs to start voting!", "", "", display_rankings(songs_df)
|
| 93 |
|
| 94 |
# Function to update Elo ratings based on user's choice
|
| 95 |
+
def update_ratings_neg(first_string, second_string, songs_df):
|
| 96 |
try:
|
| 97 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
| 98 |
except:
|
|
|
|
| 125 |
first_string = first_df["song_title"].tolist()[0]+" - "+first_df["artist"].tolist()[0]
|
| 126 |
second_df = comparison_song
|
| 127 |
second_string = second_df["song_title"]+" - "+second_df["artist"]
|
| 128 |
+
return f"Do you like '{artist} - {song_title}' better than '{comparison_song['artist']} - {comparison_song['song_title']}'?", first_string, second_string, display_rankings(songs_df)
|
| 129 |
else:
|
| 130 |
return "Add some songs to start voting!", "", "", display_rankings(songs_df)
|
| 131 |
|
|
|
|
| 135 |
songs_df.to_csv("songs_df.csv")
|
| 136 |
return songs_df
|
| 137 |
|
| 138 |
+
def export_csv(songs_df):
|
| 139 |
# Function to export DataFrame to CSV
|
| 140 |
save_df = songs_df
|
|
|
|
| 141 |
save_df.to_csv("songs_df.csv")
|
| 142 |
return "songs_df.csv"
|
| 143 |
|
| 144 |
+
def import_csv(file, songs_df):
|
| 145 |
if file is not None:
|
| 146 |
#file_content = file.decode('utf-8')
|
| 147 |
new_df = pd.read_csv(file)
|
| 148 |
try:
|
| 149 |
songs_df = songs_df[["elo_score", "artist", "song_title"]]
|
|
|
|
| 150 |
except:
|
| 151 |
songs_df = pd.DataFrame(columns=['elo_score', 'artist', 'song_title'])
|
|
|
|
|
|
|
|
|
|
| 152 |
new_df = new_df[["elo_score", "artist", "song_title"]]
|
| 153 |
songs_df = pd.concat([songs_df,new_df])
|
| 154 |
|
| 155 |
songs_df = songs_df.drop_duplicates(subset=['artist', 'song_title'])
|
| 156 |
+
return songs_df
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
# Function to remove a song
|
|
|
|
| 244 |
|
| 245 |
# Import CSV file to replace the existing DataFrame
|
| 246 |
import_button = gr.File(label="Import CSV", file_count="single")
|
| 247 |
+
import_button.change(fn=import_csv, inputs=[import_button, rankings], outputs=[rankings])
|
| 248 |
|
| 249 |
with gr.Column():
|
| 250 |
# Export button to download the DataFrame as CSV
|
| 251 |
export_link = gr.File(label="Download CSV", file_count="single")
|
| 252 |
export_button = gr.Button("Export as CSV")
|
| 253 |
+
export_button.click(fn=export_csv, inputs=[rankings], outputs=export_link)
|
| 254 |
|
| 255 |
gr.Markdown("### Reset Data")
|
| 256 |
with gr.Row():
|
|
|
|
| 260 |
clear_button.click(clear_rankings, inputs=[rankings], outputs=rankings)
|
| 261 |
|
| 262 |
# add_button.click(add_and_compare, inputs=[artist_input, song_title_input, rankings], outputs=[artist_input, song_title_input, rankings])
|
| 263 |
+
yes_button.click(update_ratings_pos, inputs=[compare_index_1, compare_index_2, rankings], outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
| 264 |
+
no_button.click(update_ratings_neg, inputs=[compare_index_1, compare_index_2, rankings], outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
| 265 |
+
new_vote.click(vote_startup, inputs=[rankings, previews],outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
| 266 |
|
| 267 |
app.launch(share=False)
|