File size: 5,199 Bytes
4ed4320 d316fc7 4ed4320 d316fc7 4ed4320 966b550 4ed4320 a684e88 4ed4320 d316fc7 4ed4320 966b550 4ed4320 d316fc7 45d4c99 a684e88 d316fc7 4ed4320 d316fc7 4ed4320 d316fc7 4ed4320 14282a4 308787f 966b550 474d0e7 4ed4320 474d0e7 966b550 4ed4320 34ffe42 4ed4320 bf5ecf9 4ed4320 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import gradio as gr
import requests
import numpy as np
import pandas as pd
from PIL import Image
# api_key = "a4ed408" _ added to get_poster and get_data Function!
# A function that takes a movie name and returns its poster image as a numpy array
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': # Check if the response is successful
# Open the image from the url
poster_image = Image.open(requests.get(data['Poster'], stream=True).raw)
# Convert the image to a numpy array
poster_array = np.array(poster_image)
return poster_array
else:
return np.zeros((500, 500, 3))
# A function that takes a movie name and returns its meta data
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': # Check if the response is successful
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 a dictionary with the information
return {
"status": status,
"poster": poster,
"title": title,
"director": director,
"cast": cast,
"genres": genres,
"rating": rating,
"year" : year
}
# Recommendation Function
from core import output_list
def get_recommendations(input_list):
movie_names = output_list(input_list)
#movies_data = [get_data(movie) for movie in movie_names]
movie_posters = [get_poster(movie) for movie in movie_names]
return movie_names, movie_posters
# HTML table
def generate_table(movies, posters):
html_code = ""
# Add the table tag and style attributes
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)
# Extract the information from the dictionary
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"]
# Add a table row tag for each movie
html_code += "<tr>"
# Add a table cell tag with the poster image as an img tag
html_code += f"<td><img src='{poster_url}' height='400' width='300'></td>"
# Add a table cell tag with the movie information as a paragraph tag
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>"
# Close the table row tag
html_code += "</tr>"
# Close the table tag
html_code += "</table>"
return html_code
# Display Function
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:
# Get the recommended movies from the input
r_movies, r_posters = get_recommendations(user_input)
# Create a list with a list of HTML strings with information
html_code = generate_table(r_movies, r_posters)
user_input = {}
# Return the output
return f"Your movies are ready!\nPlease check the recommendations below.", np.zeros((500, 500, 3)), html_code
elif data['status'] == 'True':
# Return the input movie name and poster
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)), ""
# Interface
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,
#examples=[["The Matrix"], ["The Lion King"], ["Titanic"], ['Fight Club'], ["Inception"], ["Pulp Fiction"], ["Forrest Gump"], ["Schindler’s List"]],
title = "Movie Recommender"
)
iface.launch()
|