Spaces:
Runtime error
Runtime error
from models.search_model import MovieSearch | |
from models.recommendation_model import Model | |
import gradio | |
import re | |
import pandas as pd | |
df = pd.read_csv('movie_data/movie_data.csv') | |
recommender = Model(df) | |
recommender.load('movie_data/_similarity') | |
movie = [[df['id'].iloc[i], df['title'].iloc[i], df['year'].iloc[i]] for i in range(len(df))] | |
corpus = df['title'].values.tolist() | |
stopwords = 'for a of the and to in - , is'.split() | |
search_model = MovieSearch(movie, corpus, stopwords) | |
def search_movie(title): | |
search_res = re.compile(r'--res=\d+').findall(title) | |
if(len(search_res) > 0): | |
search_res = int(search_res[0].replace('--res=', '')) | |
else: | |
search_res = 10 | |
s_res = search_model.search(title, search_res) | |
s_res = [i[0] for i in s_res] | |
return(f'Search Results For "{title}"\n' + "\n".join([f"[{i[0]}] {i[1]} ({i[2]})" for i in s_res])) | |
def get_recommendation(ids): | |
id = [int(id) for id in ids.split()] | |
rec = recommender.forward(id) | |
return(f'Movies That You Might Like\n' + "\n".join([f"- {i[0]} ({int(i[1])})" for i in rec])) | |
interface = gradio.Blocks() | |
with interface: | |
gradio.Markdown('<center><h2>Movie Recommendation System</h3></center>') | |
with gradio.Row(): | |
with gradio.Column(): | |
gradio.Markdown('Find the ID of the movie that you like') | |
input = gradio.Textbox( | |
label = 'Movie Title', | |
placeholder = 'Example: batman' | |
) | |
output = gradio.Textbox( | |
label = 'Search Result', | |
lines = 15 | |
) | |
search_button = gradio.Button('Search') | |
search_button.click( | |
fn = search_movie, | |
inputs = input, | |
outputs = output | |
) | |
with gradio.Column(): | |
gradio.Markdown('Get a movie recommendation') | |
input = gradio.Textbox( | |
label = 'Movie IDs, separated by space', | |
placeholder = 'Example: 0 1 2' | |
) | |
output = gradio.Textbox( | |
label = 'Movie Recommendation', | |
lines = 15 | |
) | |
search_button = gradio.Button('Get Recommendation') | |
search_button.click( | |
fn = get_recommendation, | |
inputs = input, | |
outputs = output | |
) | |
def main(): | |
interface.launch() | |
if __name__ == '__main__': | |
main() |