Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,129 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
],
|
| 10 |
-
"movie_url": [
|
| 11 |
-
"https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" # Open source test video
|
| 12 |
-
],
|
| 13 |
-
"genre": ["Animation"],
|
| 14 |
-
"rating": [4.5]
|
| 15 |
-
}
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
selected_idx = evt.index
|
| 29 |
if selected_idx >= len(content_df):
|
| 30 |
-
return None, None
|
| 31 |
|
| 32 |
selected_movie = content_df.iloc[selected_idx]
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
def rate_movie(movie_title, rating):
|
|
|
|
| 36 |
if not movie_title:
|
| 37 |
return "Please select a movie first!"
|
| 38 |
|
| 39 |
content_df.loc[content_df["title"] == movie_title, "rating"] = rating
|
| 40 |
-
return f"Thank you for rating {movie_title}! New rating: {rating}"
|
| 41 |
|
| 42 |
# Gradio UI
|
| 43 |
-
with gr.Blocks() as movie_app:
|
| 44 |
gr.Markdown("# 🎬 Movie Streaming App")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
genre_dropdown = gr.Dropdown(
|
| 48 |
-
choices=
|
| 49 |
value="All",
|
| 50 |
label="Filter by Genre"
|
| 51 |
)
|
|
@@ -55,18 +133,23 @@ with gr.Blocks() as movie_app:
|
|
| 55 |
value=content_df["poster_url"].tolist(),
|
| 56 |
label="Movie Gallery",
|
| 57 |
show_label=True,
|
| 58 |
-
columns=[
|
| 59 |
-
height="auto"
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
video_player = gr.Video(label="
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Event handlers
|
| 72 |
genre_dropdown.change(
|
|
@@ -77,7 +160,7 @@ with gr.Blocks() as movie_app:
|
|
| 77 |
|
| 78 |
gallery.select(
|
| 79 |
fn=get_movie_info,
|
| 80 |
-
outputs=[video_player, selected_movie_title]
|
| 81 |
)
|
| 82 |
|
| 83 |
rate_button.click(
|
|
@@ -87,4 +170,5 @@ with gr.Blocks() as movie_app:
|
|
| 87 |
)
|
| 88 |
|
| 89 |
# Launch app
|
| 90 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# TMDB API Configuration (Environment Variables)
|
| 8 |
+
TMDB_API_KEY = os.environ.get("TMDB_API_KEY", "YOUR_API_KEY")
|
| 9 |
+
TMDB_API_TOKEN = os.environ.get("TMDB_API_TOKEN", "YOUR_API_TOKEN")
|
| 10 |
+
TMDB_BASE_URL = "https://api.themoviedb.org/3"
|
| 11 |
+
TMDB_IMAGE_BASE_URL = "https://image.tmdb.org/t/p/w500"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def fetch_genres() -> List[Dict[str, Any]]:
|
| 14 |
+
"""Fetch movie genres from TMDB"""
|
| 15 |
+
headers = {
|
| 16 |
+
"Authorization": f"Bearer {TMDB_API_TOKEN}",
|
| 17 |
+
"accept": "application/json"
|
| 18 |
+
}
|
| 19 |
+
response = requests.get(f"{TMDB_BASE_URL}/genre/movie/list", headers=headers)
|
| 20 |
+
return response.json()["genres"]
|
| 21 |
|
| 22 |
+
def fetch_movies_by_genre(genre_id: str = None) -> pd.DataFrame:
|
| 23 |
+
"""Fetch movies from TMDB, optionally filtered by genre"""
|
| 24 |
+
headers = {
|
| 25 |
+
"Authorization": f"Bearer {TMDB_API_TOKEN}",
|
| 26 |
+
"accept": "application/json"
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
params = {
|
| 30 |
+
"include_adult": False,
|
| 31 |
+
"include_video": True,
|
| 32 |
+
"language": "en-US",
|
| 33 |
+
"page": 1,
|
| 34 |
+
"sort_by": "popularity.desc"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
if genre_id and genre_id != "All":
|
| 38 |
+
params["with_genres"] = genre_id
|
| 39 |
+
|
| 40 |
+
response = requests.get(f"{TMDB_BASE_URL}/discover/movie", headers=headers, params=params)
|
| 41 |
+
movies = response.json()["results"]
|
| 42 |
+
|
| 43 |
+
# Convert to DataFrame
|
| 44 |
+
movie_data = {
|
| 45 |
+
"title": [],
|
| 46 |
+
"poster_url": [],
|
| 47 |
+
"overview": [],
|
| 48 |
+
"rating": [],
|
| 49 |
+
"id": []
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
for movie in movies:
|
| 53 |
+
movie_data["title"].append(movie["title"])
|
| 54 |
+
movie_data["poster_url"].append(
|
| 55 |
+
f"{TMDB_IMAGE_BASE_URL}{movie['poster_path']}" if movie['poster_path'] else None
|
| 56 |
+
)
|
| 57 |
+
movie_data["overview"].append(movie["overview"])
|
| 58 |
+
movie_data["rating"].append(movie["vote_average"])
|
| 59 |
+
movie_data["id"].append(movie["id"])
|
| 60 |
+
|
| 61 |
+
return pd.DataFrame(movie_data)
|
| 62 |
+
|
| 63 |
+
def get_movie_trailer(movie_id: int) -> str:
|
| 64 |
+
"""Fetch movie trailer URL from TMDB"""
|
| 65 |
+
headers = {
|
| 66 |
+
"Authorization": f"Bearer {TMDB_API_TOKEN}",
|
| 67 |
+
"accept": "application/json"
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
response = requests.get(
|
| 71 |
+
f"{TMDB_BASE_URL}/movie/{movie_id}/videos",
|
| 72 |
+
headers=headers
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
videos = response.json().get("results", [])
|
| 76 |
+
trailer = next(
|
| 77 |
+
(v for v in videos if v["type"] == "Trailer" and v["site"] == "YouTube"),
|
| 78 |
+
None
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if trailer:
|
| 82 |
+
return f"https://www.youtube.com/watch?v={trailer['key']}"
|
| 83 |
+
return None
|
| 84 |
|
| 85 |
+
# Initialize global variables
|
| 86 |
+
genres = fetch_genres()
|
| 87 |
+
genre_map = {str(g["id"]): g["name"] for g in genres}
|
| 88 |
+
genre_map["All"] = "All"
|
| 89 |
+
content_df = fetch_movies_by_genre()
|
| 90 |
+
|
| 91 |
+
def list_movies(selected_genre: str) -> List[str]:
|
| 92 |
+
"""Update movie list based on selected genre"""
|
| 93 |
+
global content_df
|
| 94 |
+
content_df = fetch_movies_by_genre(selected_genre if selected_genre != "All" else None)
|
| 95 |
+
return content_df["poster_url"].tolist()
|
| 96 |
+
|
| 97 |
+
def get_movie_info(evt: gr.SelectData) -> tuple:
|
| 98 |
+
"""Get selected movie information"""
|
| 99 |
selected_idx = evt.index
|
| 100 |
if selected_idx >= len(content_df):
|
| 101 |
+
return None, None, None
|
| 102 |
|
| 103 |
selected_movie = content_df.iloc[selected_idx]
|
| 104 |
+
trailer_url = get_movie_trailer(selected_movie["id"])
|
| 105 |
+
|
| 106 |
+
return (
|
| 107 |
+
trailer_url,
|
| 108 |
+
selected_movie["title"],
|
| 109 |
+
f"{selected_movie['title']}\n\nRating: {selected_movie['rating']}/10\n\n{selected_movie['overview']}"
|
| 110 |
+
)
|
| 111 |
|
| 112 |
+
def rate_movie(movie_title: str, rating: float) -> str:
|
| 113 |
+
"""Handle movie rating submission"""
|
| 114 |
if not movie_title:
|
| 115 |
return "Please select a movie first!"
|
| 116 |
|
| 117 |
content_df.loc[content_df["title"] == movie_title, "rating"] = rating
|
| 118 |
+
return f"Thank you for rating {movie_title}! New rating: {rating}/5"
|
| 119 |
|
| 120 |
# Gradio UI
|
| 121 |
+
with gr.Blocks(theme=gr.themes.Soft()) as movie_app:
|
| 122 |
gr.Markdown("# 🎬 Movie Streaming App")
|
| 123 |
|
| 124 |
with gr.Row():
|
| 125 |
genre_dropdown = gr.Dropdown(
|
| 126 |
+
choices=list(genre_map.items()),
|
| 127 |
value="All",
|
| 128 |
label="Filter by Genre"
|
| 129 |
)
|
|
|
|
| 133 |
value=content_df["poster_url"].tolist(),
|
| 134 |
label="Movie Gallery",
|
| 135 |
show_label=True,
|
| 136 |
+
columns=[4],
|
| 137 |
+
height="auto",
|
| 138 |
+
allow_preview=False
|
| 139 |
)
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
+
video_player = gr.Video(label="Trailer")
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
+
with gr.Column():
|
| 146 |
+
selected_movie_title = gr.Textbox(label="Selected Movie", interactive=False)
|
| 147 |
+
movie_info = gr.TextArea(label="Movie Information", interactive=False)
|
| 148 |
+
|
| 149 |
+
with gr.Column():
|
| 150 |
+
rating_input = gr.Slider(minimum=1, maximum=5, step=0.5, label="Rate This Movie")
|
| 151 |
+
rate_button = gr.Button("Submit Rating")
|
| 152 |
+
rating_output = gr.Textbox(label="Rating Status")
|
| 153 |
|
| 154 |
# Event handlers
|
| 155 |
genre_dropdown.change(
|
|
|
|
| 160 |
|
| 161 |
gallery.select(
|
| 162 |
fn=get_movie_info,
|
| 163 |
+
outputs=[video_player, selected_movie_title, movie_info]
|
| 164 |
)
|
| 165 |
|
| 166 |
rate_button.click(
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
# Launch app
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
movie_app.launch(share=True)
|