|
import gradio as gr |
|
import requests |
|
import numpy as np |
|
import pandas as pd |
|
from PIL import Image |
|
|
|
|
|
|
|
|
|
def get_poster(movie): |
|
api_key = "a4ed408" |
|
base_url = "http://www.omdbapi.com/" |
|
|
|
params = {"apikey": api_key , "t": movie} |
|
response = requests.get(base_url, params=params) |
|
data = response.json() |
|
|
|
if data['Response'] == 'True': |
|
|
|
poster_image = Image.open(requests.get(data['Poster'], stream=True).raw) |
|
|
|
poster_array = np.array(poster_image) |
|
return poster_array |
|
|
|
else: |
|
return np.zeros((500, 500, 3)) |
|
|
|
|
|
def get_data(movie): |
|
api_key = "4e45e5b0" |
|
base_url = "http://www.omdbapi.com/" |
|
|
|
params = {"apikey": api_key , "t": movie} |
|
response = requests.get(base_url, params=params) |
|
data = response.json() |
|
|
|
if data['Response'] == 'True': |
|
status = data['Response'] |
|
poster = data["Poster"] |
|
if not isinstance(poster, str): |
|
poster = "https://t2.gstatic.com/licensed-image?q=tbn:ANd9GcRav8An8wQSdccA_9fjnx0W_SH3xunzQXaitfpWmZpUKiLfKpMNRY_kZf5-6EQk2ZSi" |
|
title = data["Title"] |
|
year = data["Year"] |
|
director = data["Director"] |
|
cast = data["Actors"] |
|
genres = data["Genre"] |
|
rating = data["imdbRating"] |
|
|
|
return { |
|
"status": status, |
|
"poster": poster, |
|
"title": title, |
|
"director": director, |
|
"cast": cast, |
|
"genres": genres, |
|
"rating": rating, |
|
"year" : year |
|
} |
|
|
|
|
|
|
|
from core import output_list |
|
def get_recommendations(input_list): |
|
movie_names = output_list(input_list) |
|
|
|
|
|
movie_posters = [get_poster(movie) for movie in movie_names] |
|
|
|
|
|
return movie_names, movie_posters |
|
|
|
|
|
|
|
def generate_table(movies, posters): |
|
html_code = "" |
|
|
|
html_code += "<table style='width:100%; border: 1px solid black; text-align: center;'>" |
|
|
|
for i in range(len(movies)): |
|
movie_name = movies[i] |
|
poster_array = posters[i] |
|
movie_data = get_data(movie_name) |
|
|
|
|
|
poster_url = movie_data["poster"] |
|
title = f"{movie_data['title']} ({movie_data['year']})" |
|
director = movie_data["director"] |
|
cast = movie_data["cast"] |
|
genres = movie_data["genres"] |
|
rating = movie_data["rating"] |
|
|
|
|
|
html_code += "<tr>" |
|
|
|
html_code += f"<td><img src='{poster_url}' height='400' width='300'></td>" |
|
|
|
html_code += f"<td><p><b>Title:</b> {title}</p><p><b>Director:</b> {director}</p><p><b>Cast:</b> {cast}</p><p><b>Genres:</b> {genres}</p><p><b>Rating:</b> {rating}</p></td>" |
|
|
|
html_code += "</tr>" |
|
|
|
|
|
html_code += "</table>" |
|
|
|
return html_code |
|
|
|
|
|
user_input = {} |
|
def display_movie(movie, rating): |
|
|
|
global user_input |
|
data = get_data(movie) |
|
if data['status'] == 'True': |
|
user_input[f"{data['title']} ({data['year']})"] = rating |
|
poster = get_poster(data['title']) |
|
|
|
if len(user_input) == 5: |
|
|
|
r_movies, r_posters = get_recommendations(user_input) |
|
|
|
|
|
html_code = generate_table(r_movies, r_posters) |
|
|
|
user_input = {} |
|
|
|
return f"Your movies are ready!\nPlease check the recommendations below.", np.zeros((500, 500, 3)), html_code |
|
|
|
elif data['status'] == 'True': |
|
|
|
|
|
return f"You entered {movie} with rating {rating}", poster, "" |
|
else: |
|
return f"we can't find {movie} please try again", np.zeros((500, 500, 3)), "" |
|
|
|
|
|
iface = gr.Interface( |
|
|
|
fn= display_movie, |
|
inputs= [gr.Textbox(label="Enter a movie name (five movie in total!)"), gr.Slider(minimum=0, maximum=5, step=1, label="Rate the movie")], |
|
outputs= [gr.Textbox(label="Output", min_width=200), gr.components.Image(label="Poster", height=400, width=300), gr.components.HTML(label="Recommendations")], |
|
live= False, |
|
|
|
title = "Movie Recommender" |
|
|
|
) |
|
|
|
iface.launch() |
|
|