Spaces:
Build error
Build error
import gradio as gr | |
import pandas as pd | |
from PIL import Image | |
import requests | |
dfp_matrice_association = pd.read_csv("./data/association.csv", index_col=0) | |
dfp_matrice_distance_from_encoding = pd.read_csv("./data/ed_from_encoding.csv", index_col=0) | |
dfp_matrice_distance_from_association = pd.read_csv("./data/ed_from_association.csv", index_col=0) | |
dfp_cards = pd.read_csv("./data/cards.csv") | |
dfp_cards.sort_values("cname", inplace=True) | |
mapping_cname_cid = dfp_cards[["cname", "cid"]].reset_index() | |
mapping_cname_cid = mapping_cname_cid.set_index("cname").to_dict(orient="index") | |
dfp_cards.set_index(["cid"], inplace=True) | |
def clean_recommendations(cid, recommendations): | |
if cid in recommendations: | |
recommendations.remove(cid) | |
if str(cid) in recommendations: | |
recommendations.remove(str(cid)) | |
return recommendations | |
def get_recommendations(cid, dfp_, ascending=False, k=5): | |
recommendations = dfp_.loc[cid].sort_values(ascending=ascending).index.tolist() | |
clean_recommendations(cid, recommendations) | |
return recommendations[:k] | |
def display_recommendations(cname, use_euclidian_distance, use_description_embedding, k): | |
cid = int(mapping_cname_cid[cname]["cid"]) | |
# Collect the recommendations | |
dfp_dist = dfp_matrice_distance_from_association if use_euclidian_distance else dfp_matrice_association | |
if (use_description_embedding == False): | |
recommendations = get_recommendations(cid, dfp_dist, use_euclidian_distance, k=25) | |
else: | |
closest_card = get_recommendations(cid, dfp_matrice_distance_from_encoding, True, k=1)[0] | |
recommendations = get_recommendations(int(closest_card), dfp_dist, use_euclidian_distance, k=25) | |
recommendations = recommendations[:k] | |
recommendations_string = [dfp_cards.loc[int(cid_r)]["cname"] for cid_r in recommendations] | |
recommendations_image = [Image.open(requests.get(dfp_cards.loc[int(cid_r)]["art"], stream=True).raw).resize((240,300)) for cid_r in recommendations] | |
block_text = "\n" | |
for idx, cid_r in enumerate(recommendations): | |
block_text += f"{idx+1}){dfp_cards.loc[int(cid_r)]['cname']} : {dfp_cards.loc[int(cid_r)]['ability']}\n" | |
# block_text += f"{idx+1})\n" | |
text_output = f""" | |
Recommended cards:{block_text} | |
""" | |
return text_output, recommendations_image | |
title = "Marvel Snap deck starter" | |
description = """Gradio demo for Marvel Snap deck starter \n | |
To use it, simply select the card in the dropdown (cname) that you want to use to kickstart the deck and after use the type of recommender system:\n | |
1) use_euclidian_distance: Use the euclidian distance or the normalized association \n | |
2) use_description_embedding: Use the description (name + stats + ability X transformers encoder) as first filtering before the model in 1)\n | |
More details in this article https://www.the-odd-dataguy.com/2023/01/15/marvel_snap_cold_start/ | |
""" | |
article = "<div style='text-align: center;'>Marvel Snap deck starter by <a href='https://www.linkedin.com/in/jeanmicheldaignan/' target='_blank'>Jean-Michel D</a> | <a href='https://www.the-odd-dataguy.com/2023/01/15/marvel_snap_cold_start/' target='_blank'>Article</a>" | |
demo = gr.Interface( | |
fn=display_recommendations, | |
inputs=[gr.inputs.Dropdown(dfp_cards["cname"].tolist()), | |
"checkbox", | |
"checkbox", | |
gr.Slider(minimum=1, maximum=20, step=1, value=5)], | |
outputs=["text", gr.Gallery(label="Recommendations")], | |
title=title, | |
description=description, | |
article=article, | |
) | |
demo.launch() |