Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
|
6 |
+
dfp_matrice_association = pd.read_csv("./data/association.csv", index_col=0)
|
7 |
+
dfp_matrice_distance_from_encoding = pd.read_csv("./data/ed_from_encoding.csv", index_col=0)
|
8 |
+
dfp_matrice_distance_from_association = pd.read_csv("./data/ed_from_association.csv", index_col=0)
|
9 |
+
dfp_cards = pd.read_csv("./data/cards.csv")
|
10 |
+
dfp_cards.sort_values("cname", inplace=True)
|
11 |
+
|
12 |
+
mapping_cname_cid = dfp_cards[["cname", "cid"]].reset_index()
|
13 |
+
mapping_cname_cid = mapping_cname_cid.set_index("cname").to_dict(orient="index")
|
14 |
+
|
15 |
+
dfp_cards.set_index(["cid"], inplace=True)
|
16 |
+
|
17 |
+
def clean_recommendations(cid, recommendations):
|
18 |
+
if cid in recommendations:
|
19 |
+
recommendations.remove(cid)
|
20 |
+
if str(cid) in recommendations:
|
21 |
+
recommendations.remove(str(cid))
|
22 |
+
return recommendations
|
23 |
+
|
24 |
+
def get_recommendations(cid, dfp_, ascending=False, k=5):
|
25 |
+
recommendations = dfp_.loc[cid].sort_values(ascending=ascending).index.tolist()
|
26 |
+
clean_recommendations(cid, recommendations)
|
27 |
+
return recommendations[:k]
|
28 |
+
|
29 |
+
def display_recommendations(cname, use_euclidian_distance, use_description_embedding, k):
|
30 |
+
|
31 |
+
cid = int(mapping_cname_cid[cname]["cid"])
|
32 |
+
# Collect the recommendations
|
33 |
+
dfp_dist = dfp_matrice_distance_from_association if use_euclidian_distance else dfp_matrice_association
|
34 |
+
|
35 |
+
if (use_description_embedding == False):
|
36 |
+
recommendations = get_recommendations(cid, dfp_dist, use_euclidian_distance, k=25)
|
37 |
+
else:
|
38 |
+
closest_card = get_recommendations(cid, dfp_matrice_distance_from_encoding, True, k=1)[0]
|
39 |
+
recommendations = get_recommendations(int(closest_card), dfp_dist, use_euclidian_distance, k=25)
|
40 |
+
|
41 |
+
recommendations = recommendations[:k]
|
42 |
+
recommendations_string = [dfp_cards.loc[int(cid_r)]["cname"] for cid_r in recommendations]
|
43 |
+
recommendations_image = [Image.open(requests.get(dfp_cards.loc[int(cid_r)]["art"], stream=True).raw).resize((240,300)) for cid_r in recommendations]
|
44 |
+
|
45 |
+
block_text = "\n"
|
46 |
+
for idx, cid_r in enumerate(recommendations):
|
47 |
+
block_text += f"{idx+1}){dfp_cards.loc[int(cid_r)]['cname']} : {dfp_cards.loc[int(cid_r)]['ability']}\n"
|
48 |
+
# block_text += f"{idx+1})\n"
|
49 |
+
|
50 |
+
text_output = f"""
|
51 |
+
Recommended cards:{block_text}
|
52 |
+
"""
|
53 |
+
return text_output, recommendations_image
|
54 |
+
demo = gr.Interface(
|
55 |
+
fn=display_recommendations,
|
56 |
+
inputs=[gr.inputs.Dropdown(dfp_cards["cname"].tolist()),
|
57 |
+
"checkbox",
|
58 |
+
"checkbox",
|
59 |
+
gr.Slider(minimum=1, maximum=10, ste=1, value=5)],
|
60 |
+
outputs=["text", gr.Gallery(label="Recommendations")],
|
61 |
+
)
|
62 |
+
demo.launch()
|