EdZ543
commited on
Commit
•
3beaecc
1
Parent(s):
154c5fa
Update requirements
Browse files- app.py +13 -9
- requirements.txt +1 -2
app.py
CHANGED
@@ -7,9 +7,6 @@ import requests
|
|
7 |
import torch
|
8 |
import torch.nn as nn
|
9 |
from urllib import parse
|
10 |
-
from dotenv import load_dotenv
|
11 |
-
|
12 |
-
load_dotenv()
|
13 |
|
14 |
MAL_CLIENT_ID = os.getenv("MAL_CLIENT_ID")
|
15 |
|
@@ -25,8 +22,11 @@ def fetch_anime_image(anime):
|
|
25 |
headers = {"X-MAL-CLIENT-ID": MAL_CLIENT_ID}
|
26 |
query_response = requests.get(query_url, headers=headers)
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
def recommend(anime):
|
@@ -34,13 +34,17 @@ def recommend(anime):
|
|
34 |
anime_embedding = anime_embeddings[anime_index][None]
|
35 |
|
36 |
embedding_distances = nn.CosineSimilarity(dim=1)(anime_embeddings, anime_embedding)
|
37 |
-
recommendation_indexes = embedding_distances.argsort(descending=True)[1:
|
38 |
|
39 |
recommendations = []
|
40 |
for recommendation_index in recommendation_indexes:
|
41 |
recommendation_anime = anime_indexes.iloc[recommendation_index]["Anime"]
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
return recommendations
|
46 |
|
@@ -65,7 +69,7 @@ with gr.Blocks(css=css) as space:
|
|
65 |
dropdown = gr.Dropdown(container=False, choices=animes)
|
66 |
selection_image = gr.Image(show_label=False, width=225, visible=False)
|
67 |
|
68 |
-
gallery = gr.Gallery(label="Recommendations",
|
69 |
|
70 |
def submit(anime):
|
71 |
if anime is None:
|
|
|
7 |
import torch
|
8 |
import torch.nn as nn
|
9 |
from urllib import parse
|
|
|
|
|
|
|
10 |
|
11 |
MAL_CLIENT_ID = os.getenv("MAL_CLIENT_ID")
|
12 |
|
|
|
22 |
headers = {"X-MAL-CLIENT-ID": MAL_CLIENT_ID}
|
23 |
query_response = requests.get(query_url, headers=headers)
|
24 |
|
25 |
+
try:
|
26 |
+
image_url = query_response.json()["data"][0]["node"]["main_picture"]["large"]
|
27 |
+
return image_url
|
28 |
+
except:
|
29 |
+
return None
|
30 |
|
31 |
|
32 |
def recommend(anime):
|
|
|
34 |
anime_embedding = anime_embeddings[anime_index][None]
|
35 |
|
36 |
embedding_distances = nn.CosineSimilarity(dim=1)(anime_embeddings, anime_embedding)
|
37 |
+
recommendation_indexes = embedding_distances.argsort(descending=True)[1:].tolist()
|
38 |
|
39 |
recommendations = []
|
40 |
for recommendation_index in recommendation_indexes:
|
41 |
recommendation_anime = anime_indexes.iloc[recommendation_index]["Anime"]
|
42 |
+
recommendation_image = fetch_anime_image(recommendation_anime)
|
43 |
+
if recommendation_image is not None:
|
44 |
+
recommendations.append((recommendation_image, recommendation_anime))
|
45 |
+
|
46 |
+
if len(recommendations) == 5:
|
47 |
+
break
|
48 |
|
49 |
return recommendations
|
50 |
|
|
|
69 |
dropdown = gr.Dropdown(container=False, choices=animes)
|
70 |
selection_image = gr.Image(show_label=False, width=225, visible=False)
|
71 |
|
72 |
+
gallery = gr.Gallery(label="Recommendations", columns=[2, 2, 3, 3, 4, 5])
|
73 |
|
74 |
def submit(anime):
|
75 |
if anime is None:
|
requirements.txt
CHANGED
@@ -1,3 +1,2 @@
|
|
1 |
pandas
|
2 |
-
torch
|
3 |
-
dotenv
|
|
|
1 |
pandas
|
2 |
+
torch
|
|