Spaces:
Sleeping
Sleeping
Commit
·
ce147cb
1
Parent(s):
7ac9813
Delete app.py
Browse files
app.py
DELETED
@@ -1,102 +0,0 @@
|
|
1 |
-
__all__ = ['is_fantasy', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
|
2 |
-
|
3 |
-
from fastai.vision.all import *
|
4 |
-
|
5 |
-
import pandas as pd
|
6 |
-
import gradio as gr
|
7 |
-
|
8 |
-
class CollabNN(Module):
|
9 |
-
def __init__(self, user_sz, item_sz, y_range=(0,11), n_act=100):
|
10 |
-
self.user_factors = Embedding(*user_sz)
|
11 |
-
self.item_factors = Embedding(*item_sz)
|
12 |
-
self.layers = nn.Sequential(
|
13 |
-
nn.Linear(user_sz[1]+item_sz[1], n_act),
|
14 |
-
nn.ReLU(),
|
15 |
-
nn.Linear(n_act, 1))
|
16 |
-
self.y_range = y_range
|
17 |
-
|
18 |
-
def forward(self, x):
|
19 |
-
embs = self.user_factors(x[:,0]),self.item_factors(x[:,1])
|
20 |
-
x = self.layers(torch.cat(embs, dim=1))
|
21 |
-
return sigmoid_range(x, *self.y_range)
|
22 |
-
|
23 |
-
|
24 |
-
def load_animes(filename):
|
25 |
-
# Carrega o arquivo CSV de animes usando a biblioteca pandas
|
26 |
-
try:
|
27 |
-
animes_df = pd.read_csv(filename, encoding='latin-1', delimiter=';')
|
28 |
-
return animes_df
|
29 |
-
except FileNotFoundError:
|
30 |
-
print("Arquivo não encontrado!")
|
31 |
-
return None
|
32 |
-
|
33 |
-
def recommend_animes(anime1, anime2, anime3, animes_df): # Adiciona animes_df como parâmetro
|
34 |
-
# Verifica se os três animes foram selecionados
|
35 |
-
if not anime1 or not anime2 or not anime3:
|
36 |
-
return "Selecione três animes!"
|
37 |
-
|
38 |
-
# Carregar o modelo de filtro colaborativo
|
39 |
-
model = load_learner('model.pkl')
|
40 |
-
|
41 |
-
# Obter os títulos dos animes selecionados
|
42 |
-
anime_titles = [anime1, anime2, anime3]
|
43 |
-
|
44 |
-
# Verificar se os animes selecionados estão presentes no dataframe
|
45 |
-
if not all(title in animes_df["anime-Title"].values for title in anime_titles):
|
46 |
-
return "Um ou mais animes selecionados não estão disponíveis."
|
47 |
-
|
48 |
-
# Obter as recomendações com base nos animes selecionados
|
49 |
-
recommendations = collaborative_filtering_recommendations(anime_titles, model, animes_df) # Adiciona animes_df como parâmetro
|
50 |
-
|
51 |
-
# Cria a string de recomendações
|
52 |
-
recommendations_str = f"Já que gostou do anime {anime1}, {anime2} e {anime3}, você pode gostar desses:\n"
|
53 |
-
recommendations_str += "\n".join(f"• {anime}" for anime in recommendations)
|
54 |
-
|
55 |
-
return recommendations_str
|
56 |
-
|
57 |
-
def collaborative_filtering_recommendations(anime_titles, model, animes_df): # Adiciona animes_df como parâmetro
|
58 |
-
# Preprocess anime titles (convert to indices or required format)
|
59 |
-
preprocessed_titles = preprocess_titles(anime_titles)
|
60 |
-
|
61 |
-
# Pass preprocessed data through the model to get predictions
|
62 |
-
predictions = model.predict(preprocessed_titles)
|
63 |
-
|
64 |
-
# Sort predictions and select top recommendations
|
65 |
-
top_indices = predictions.argsort()[5:][::1] # Select top 5 recommendations (adjust as needed)
|
66 |
-
|
67 |
-
# Map indices back to anime titles
|
68 |
-
recommendations = [animes_df.iloc[index]["anime-Title"] for index in top_indices]
|
69 |
-
|
70 |
-
return recommendations
|
71 |
-
|
72 |
-
def preprocess_titles(anime_titles):
|
73 |
-
# Get the user index for all animes (assuming a single user for recommendations)
|
74 |
-
user_indices = [0] * len(anime_titles)
|
75 |
-
|
76 |
-
# Get the item indices for the anime titles
|
77 |
-
item_indices = [animes_df.loc[animes_df["anime-Title"] == title].index.item() for title in anime_titles]
|
78 |
-
|
79 |
-
# Combine user and item indices into tuples
|
80 |
-
indices = list(zip(user_indices, item_indices))
|
81 |
-
|
82 |
-
return indices
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# Define a função de interface
|
88 |
-
def interface(anime1, anime2, anime3):
|
89 |
-
recommendations = recommend_animes(anime1, anime2, anime3, animes_df) # Passa animes_df como argumento
|
90 |
-
return recommendations
|
91 |
-
|
92 |
-
# Cria a interface usando a biblioteca Gradio
|
93 |
-
iface = gr.Interface(
|
94 |
-
fn=interface,
|
95 |
-
inputs=["text", "text", "text"],
|
96 |
-
outputs="text",
|
97 |
-
title="Recomendações de animes",
|
98 |
-
description="Selecione três animes e obtenha recomendações baseadas em filtro colaborativo."
|
99 |
-
)
|
100 |
-
|
101 |
-
# Executa a interface
|
102 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|