Commit ·
834fe53
1
Parent(s): a066e91
not complete gradio_app
Browse files- gradio_app.py +129 -0
gradio_app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# api_key = "4e45e5b0"
|
| 8 |
+
|
| 9 |
+
# A function that takes a movie name and returns its poster image as a numpy array
|
| 10 |
+
def get_poster(movie):
|
| 11 |
+
api_key = "4e45e5b0"
|
| 12 |
+
base_url = "http://www.omdbapi.com/"
|
| 13 |
+
|
| 14 |
+
params = {"apikey": api_key , "t": movie}
|
| 15 |
+
response = requests.get(base_url, params=params)
|
| 16 |
+
data = response.json()
|
| 17 |
+
|
| 18 |
+
if data['Response'] == 'True': # Check if the response is successful
|
| 19 |
+
# Open the image from the url
|
| 20 |
+
poster_image = Image.open(requests.get(data['Poster'], stream=True).raw)
|
| 21 |
+
# Convert the image to a numpy array
|
| 22 |
+
poster_array = np.array(poster_image)
|
| 23 |
+
return poster_array
|
| 24 |
+
|
| 25 |
+
else:
|
| 26 |
+
return np.zeros((500, 500, 3))
|
| 27 |
+
|
| 28 |
+
# A function that takes a movie name and returns its meta data
|
| 29 |
+
def get_data(movie):
|
| 30 |
+
api_key = "4e45e5b0"
|
| 31 |
+
base_url = "http://www.omdbapi.com/"
|
| 32 |
+
|
| 33 |
+
params = {"apikey": api_key , "t": movie}
|
| 34 |
+
response = requests.get(base_url, params=params)
|
| 35 |
+
data = response.json()
|
| 36 |
+
|
| 37 |
+
if data['Response'] == 'True': # Check if the response is successful
|
| 38 |
+
poster = data["Poster"]
|
| 39 |
+
title = data["Title"]
|
| 40 |
+
director = data["Director"]
|
| 41 |
+
cast = data["Actors"]
|
| 42 |
+
genres = data["Genre"]
|
| 43 |
+
rating = data["imdbRating"]
|
| 44 |
+
# Return a dictionary with the information
|
| 45 |
+
return {
|
| 46 |
+
|
| 47 |
+
"poster": poster,
|
| 48 |
+
"title": title,
|
| 49 |
+
"director": director,
|
| 50 |
+
"cast": cast,
|
| 51 |
+
"genres": genres,
|
| 52 |
+
"rating": rating
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
def get_recommendations(input_list):
|
| 56 |
+
movie_names = ["The Matrix", "The Shawshank Redemption", "The Godfather", "The Dark Knight", "Inception"]
|
| 57 |
+
movies_data = [get_data(movie) for movie in movie_names]
|
| 58 |
+
|
| 59 |
+
movie_posters = [get_poster(movie) for movie in movie_names]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
return movie_names, movie_posters
|
| 63 |
+
# HTML table for recommendation section
|
| 64 |
+
def generate_table(movies, posters):
|
| 65 |
+
html_code = ""
|
| 66 |
+
# Add the table tag and style attributes
|
| 67 |
+
html_code += "<table style='width:100%; border: 1px solid black; text-align: center;'>"
|
| 68 |
+
|
| 69 |
+
for i in range(len(movies)):
|
| 70 |
+
movie_name = movies[i]
|
| 71 |
+
poster_array = posters[i]
|
| 72 |
+
movie_data = get_data(movie_name)
|
| 73 |
+
|
| 74 |
+
# Extract the information from the dictionary
|
| 75 |
+
poster_url = movie_data["poster"]
|
| 76 |
+
title = movie_data["title"]
|
| 77 |
+
director = movie_data["director"]
|
| 78 |
+
cast = movie_data["cast"]
|
| 79 |
+
genres = movie_data["genres"]
|
| 80 |
+
rating = movie_data["rating"]
|
| 81 |
+
|
| 82 |
+
# Add a table row tag for each movie
|
| 83 |
+
html_code += "<tr>"
|
| 84 |
+
# Add a table cell tag with the poster image as an img tag
|
| 85 |
+
html_code += f"<td><img src='{poster_url}' height='400' width='300'></td>"
|
| 86 |
+
# Add a table cell tag with the movie information as a paragraph tag
|
| 87 |
+
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>"
|
| 88 |
+
# Close the table row tag
|
| 89 |
+
html_code += "</tr>"
|
| 90 |
+
|
| 91 |
+
# Close the table tag
|
| 92 |
+
html_code += "</table>"
|
| 93 |
+
|
| 94 |
+
return html_cod
|
| 95 |
+
|
| 96 |
+
user_input = {}
|
| 97 |
+
|
| 98 |
+
def display_movie(movie, rating):
|
| 99 |
+
|
| 100 |
+
global user_input
|
| 101 |
+
user_input[movie] = rating
|
| 102 |
+
poster = get_poster(movie)
|
| 103 |
+
|
| 104 |
+
if len(user_input) > 5:
|
| 105 |
+
# Get the recommended movies from the input
|
| 106 |
+
r_movies, r_posters = get_recommendations(user_input)
|
| 107 |
+
|
| 108 |
+
# Create a list with a list of HTML strings with information
|
| 109 |
+
html_code = generate_table(r_movies, r_posters)
|
| 110 |
+
|
| 111 |
+
user_input = {}
|
| 112 |
+
# Return the output
|
| 113 |
+
return f"Your movies are ready!\nPlease check the recommendations below.", np.zeros((500, 500, 3)), html_code
|
| 114 |
+
|
| 115 |
+
else:
|
| 116 |
+
|
| 117 |
+
# Return the input movie name and poster
|
| 118 |
+
return f"You entered {movie} with rating {rating}", poster, ""
|
| 119 |
+
|
| 120 |
+
iface = gr.Interface(
|
| 121 |
+
|
| 122 |
+
fn= display_movie,
|
| 123 |
+
inputs= [gr.Textbox(label="Enter a movie name"), gr.Slider(minimum=0, maximum=5, step=1, label="Rate the movie")],
|
| 124 |
+
outputs= [gr.Textbox(label="Output", min_width=200), gr.components.Image(label="Poster", height=400, width=300), gr.components.HTML(label="Recommendations", height=400)],
|
| 125 |
+
|
| 126 |
+
live= False
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
iface.launch()
|