Spaces:
Build error
Build error
upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""DeployColab.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1CxHPmRuxSJrXJfa4tdmtTfPvE4tmfOlY
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import numpy as np
|
| 12 |
+
import ast
|
| 13 |
+
|
| 14 |
+
!pip install gradio
|
| 15 |
+
!pip install scikit-surprise
|
| 16 |
+
|
| 17 |
+
from google.colab import drive
|
| 18 |
+
drive.mount('/content/drive')
|
| 19 |
+
|
| 20 |
+
dfmerge = pd.read_csv('/content/drive/MyDrive/merged_data6.csv')
|
| 21 |
+
|
| 22 |
+
dfmerge.head()
|
| 23 |
+
|
| 24 |
+
dfmerge = pd.read_csv('/content/drive/MyDrive/merged_data6.csv')
|
| 25 |
+
|
| 26 |
+
dfmerge.head()
|
| 27 |
+
|
| 28 |
+
import gradio as gr
|
| 29 |
+
import pandas as pd
|
| 30 |
+
import pickle
|
| 31 |
+
from surprise import SVD
|
| 32 |
+
|
| 33 |
+
def generar_recomendacion(svd_model, user_id, df, genres, top=5):
|
| 34 |
+
# Filtrar las películas que correspondan a los géneros de interés
|
| 35 |
+
df_filtered = df[df[genres].any(axis=1)]
|
| 36 |
+
|
| 37 |
+
# Crear un mapeo de id de película a título de película para una búsqueda más eficiente
|
| 38 |
+
id_to_title = df_filtered.set_index('id')['title'].to_dict()
|
| 39 |
+
|
| 40 |
+
# Obtener las recomendaciones utilizando la función `apply` de pandas
|
| 41 |
+
predicted_ratings = df_filtered['id'].apply(lambda movie_id: svd_model.predict(int(user_id), movie_id).est)
|
| 42 |
+
|
| 43 |
+
# Ordenar las películas según su predicción de rating
|
| 44 |
+
movie_rating = list(zip(df_filtered['id'], predicted_ratings))
|
| 45 |
+
movie_rating.sort(key=lambda x: x[1], reverse=True)
|
| 46 |
+
|
| 47 |
+
# Obtener los títulos de las películas recomendadas
|
| 48 |
+
recommended_movies = movie_rating[:top]
|
| 49 |
+
recommended_titles = [id_to_title[movie_id] for movie_id, _ in recommended_movies]
|
| 50 |
+
|
| 51 |
+
# Devolver la lista de títulos como una cadena
|
| 52 |
+
return ', '.join(recommended_titles)
|
| 53 |
+
|
| 54 |
+
# Leer los datos
|
| 55 |
+
dfmerge = pd.read_csv('/content/drive/MyDrive/merged_data6.csv')
|
| 56 |
+
|
| 57 |
+
# Cargar el modelo
|
| 58 |
+
with open('/content/drive/MyDrive/fc_model_svd_v1.pkl', 'rb') as file:
|
| 59 |
+
svd_model = pickle.load(file)
|
| 60 |
+
|
| 61 |
+
def wrap_generar_recomendacion(user_id, drama, comedy, horror, romance, top=5):
|
| 62 |
+
# Crear la lista de géneros de interés a partir de las casillas de verificación
|
| 63 |
+
genres = []
|
| 64 |
+
if drama: genres.append('Drama')
|
| 65 |
+
if comedy: genres.append('Comedy')
|
| 66 |
+
if horror: genres.append('Horror')
|
| 67 |
+
if romance: genres.append('Romance')
|
| 68 |
+
|
| 69 |
+
# Llamar a la función de recomendación y devolver los resultados como una cadena
|
| 70 |
+
return generar_recomendacion(svd_model, user_id, dfmerge, genres, int(top))
|
| 71 |
+
|
| 72 |
+
# Definir la interfaz de Gradio
|
| 73 |
+
demo = gr.Interface(
|
| 74 |
+
fn=wrap_generar_recomendacion,
|
| 75 |
+
inputs=["text", gr.inputs.Checkbox(label="Drama"), gr.inputs.Checkbox(label="Comedy"), gr.inputs.Checkbox(label="Horror"), gr.inputs.Checkbox(label="Romance"), "text"],
|
| 76 |
+
outputs="text",
|
| 77 |
+
title="Sistema de recomendación de películas basado en filtro colaborativo",
|
| 78 |
+
description="Ingresa el ID del usuario (user_id), selecciona los géneros de interés y la cantidad de recomendaciones que te gustaría generar. Te mostraremos algunas películas que le pueden gustar.",
|
| 79 |
+
allow_flagging='auto'
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Lanzar la interfaz
|
| 83 |
+
demo.launch()
|