Spaces:
Running
Running
FoodDesert
commited on
Commit
•
a509ff9
1
Parent(s):
67acdf2
Upload 2 files
Browse files- app.py +49 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
+
import numpy as np
|
4 |
+
from joblib import load
|
5 |
+
import h5py
|
6 |
+
|
7 |
+
# Load the model and data once at startup
|
8 |
+
with h5py.File('complete_artist_data.hdf5', 'r') as f:
|
9 |
+
# Deserialize the vectorizer
|
10 |
+
vectorizer_bytes = f['vectorizer'][()].tobytes()
|
11 |
+
vectorizer_buffer = BytesIO(vectorizer_bytes)
|
12 |
+
vectorizer = load(vectorizer_buffer)
|
13 |
+
|
14 |
+
# Load X_artist
|
15 |
+
X_artist = f['X_artist'][:]
|
16 |
+
|
17 |
+
# Load artist names and decode to strings
|
18 |
+
artist_names = [name.decode() for name in f['artist_names'][:]]
|
19 |
+
|
20 |
+
def find_similar_artists(new_tags_string):
|
21 |
+
new_image_tags = [tag.strip() for tag in new_tags_string.split(",")]
|
22 |
+
unseen_tags = set(new_image_tags) - set(vectorizer.vocabulary_.keys())
|
23 |
+
unseen_tags_str = f'Unseen Tags: {", ".join(unseen_tags)}' if unseen_tags else 'No unseen tags.'
|
24 |
+
|
25 |
+
X_new_image = vectorizer.transform([','.join(new_image_tags)])
|
26 |
+
similarities = cosine_similarity(X_new_image, X_artist)[0]
|
27 |
+
|
28 |
+
top_n = 20
|
29 |
+
top_artist_indices = np.argsort(similarities)[-top_n:][::-1]
|
30 |
+
bottom_artist_indices = np.argsort(similarities)[:top_n]
|
31 |
+
|
32 |
+
top_artists = [(artist_names[i], similarities[i]) for i in top_artist_indices]
|
33 |
+
bottom_artists = [(artist_names[i], similarities[i]) for i in bottom_artist_indices]
|
34 |
+
|
35 |
+
top_artists_str = "\n".join([f"{rank+1}. {artist} - similarity score: {score:.4f}" for rank, (artist, score) in enumerate(top_artists)])
|
36 |
+
bottom_artists_str = "\n".join([f"{rank+1}. {artist} - similarity score: {score:.4f}" for rank, (artist, score) in enumerate(bottom_artists)])
|
37 |
+
|
38 |
+
output_str = f"{unseen_tags_str}\n\nTop 10 artists:\n{top_artists_str}\n\nBottom 10 artists:\n{bottom_artists_str}"
|
39 |
+
return output_str
|
40 |
+
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=find_similar_artists,
|
43 |
+
inputs="text",
|
44 |
+
outputs="text",
|
45 |
+
title="Artist Similarity Finder",
|
46 |
+
description="Enter image tags to find similar artists based on learned similarities."
|
47 |
+
)
|
48 |
+
|
49 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.41.2
|
2 |
+
numpy==1.25.1
|
3 |
+
scikit-learn==1.2.2
|
4 |
+
h5py==3.8.0
|
5 |
+
joblib==1.2.0
|
6 |
+
|