AmirShabani commited on
Commit
4ed4320
1 Parent(s): 4ba0b82

Create app.py version 01

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Recommendation Function
55
+ from core import output_list
56
+ def get_recommendations(input_list):
57
+ movie_names = output_list(input_list)
58
+ movies_data = [get_data(movie) for movie in movie_names]
59
+
60
+ movie_posters = [get_poster(movie) for movie in movie_names]
61
+
62
+
63
+ return movie_names, movie_posters
64
+
65
+
66
+ # HTML table
67
+ def generate_table(movies, posters):
68
+ html_code = ""
69
+ # Add the table tag and style attributes
70
+ html_code += "<table style='width:100%; border: 1px solid black; text-align: center;'>"
71
+
72
+ for i in range(len(movies)):
73
+ movie_name = movies[i]
74
+ poster_array = posters[i]
75
+ movie_data = get_data(movie_name)
76
+
77
+ # Extract the information from the dictionary
78
+ poster_url = movie_data["poster"]
79
+ title = movie_data["title"]
80
+ director = movie_data["director"]
81
+ cast = movie_data["cast"]
82
+ genres = movie_data["genres"]
83
+ rating = movie_data["rating"]
84
+
85
+ # Add a table row tag for each movie
86
+ html_code += "<tr>"
87
+ # Add a table cell tag with the poster image as an img tag
88
+ html_code += f"<td><img src='{poster_url}' height='400' width='300'></td>"
89
+ # Add a table cell tag with the movie information as a paragraph tag
90
+ 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>"
91
+ # Close the table row tag
92
+ html_code += "</tr>"
93
+
94
+ # Close the table tag
95
+ html_code += "</table>"
96
+
97
+ return html_code
98
+
99
+ # Display Function
100
+ user_input = {}
101
+ def display_movie(movie, rating):
102
+
103
+ global user_input
104
+ user_input[movie] = rating
105
+ poster = get_poster(movie)
106
+
107
+ if len(user_input) == 5:
108
+ # Get the recommended movies from the input
109
+ r_movies, r_posters = get_recommendations(user_input)
110
+
111
+ # Create a list with a list of HTML strings with information
112
+ html_code = generate_table(r_movies, r_posters)
113
+
114
+ user_input = {}
115
+ # Return the output
116
+ return f"Your movies are ready!\nPlease check the recommendations below.", np.zeros((500, 500, 3)), html_code
117
+
118
+ else:
119
+
120
+ # Return the input movie name and poster
121
+ return f"You entered {movie} with rating {rating}", poster, ""
122
+
123
+ # Interface
124
+ iface = gr.Interface(
125
+
126
+ fn= display_movie,
127
+ inputs= [gr.Textbox(label="Enter a movie name (five movie in total!)"), gr.Slider(minimum=0, maximum=5, step=1, label="Rate the movie")],
128
+ outputs= [gr.Textbox(label="Output", min_width=200), gr.components.Image(label="Poster", height=400, width=300), gr.components.HTML(label="Recommendations", height=400)],
129
+ live= False,
130
+ examples=[["The Matrix"], ["The Lion King"], ["Titanic"], ['Fight Club'], ["Inception"]],
131
+ title = "Movie Recommender",
132
+
133
+ )
134
+
135
+ iface.launch()
136
+