import gradio as gr from fastai.learner import load_learner import pandas as pd import numpy as np # Modelo learn = load_learner('anime_rec.pkl') # Ids para usar id_data = pd.read_csv('dataset.csv') id_data = id_data.sort_values('id_user', ascending=True) id_list = list(map(str, id_data['id_user'].unique().tolist())) # Dados originais data = pd.read_csv('dataset.csv') def get_anime_names_by_ids(id_animes): # Filtrar as linhas correspondentes aos id_animes anime_names = data.loc[data['id_anime'].isin(id_animes), ['id_anime', 'name']] # Remover duplicatas anime_names = anime_names.drop_duplicates(subset='id_anime') return anime_names def preds(id_user): id_user = int(id_user) itens = pd.Series(learn.dls.classes['id_anime']).unique() itens_series = pd.Series(itens) classifications = data.loc[(data['id_user'] == id_user) & (data['Overall Rating'] > 0), 'id_anime'] # Transformar itens em uma Series do Pandas no_classifications = itens_series[~itens_series.isin(classifications)] df = pd.DataFrame({'id_user': [id_user] * len(no_classifications), 'id_anime': no_classifications}) preds, _ = learn.get_preds(dl=learn.dls.test_dl(df)) df[['prediction']] = preds.numpy() top_5 = df.nlargest(5, 'prediction') # Obter os nomes dos animes correspondentes aos id_anime result = get_anime_names_by_ids(top_5['id_anime'].astype(int).tolist()) result_str = result.apply(lambda x: f"{x['id_anime']}: {x['name']}", axis=1).tolist() return '\n'.join(result_str) iface = gr.Interface( fn=preds, inputs=gr.Dropdown(choices=id_list), outputs="text", title="Recomendador de Animes", description="Esse modelo é capaz de recomendar animes através de um Id de usuário", ) iface.launch(share=True)