import base64 import gradio as gr import pandas as pd from datasets import load_dataset from datasets import Dataset import os from huggingface_hub import login # Access the secret token hf_token = os.getenv("WRITE_TOKEN") # Authenticate using the secret token login(token=hf_token) # Load the dataset from Hugging Face Datasets data = load_dataset('moizmoizmoizmoiz/MovieRatingDB', split='train') # Convert dataset to a pandas DataFrame for easier manipulation data_df = data.to_pandas() # Add a default column for ratings if not already present for profile in ['moiz', 'udisha', 'musab']: if profile not in data_df.columns: data_df[profile] = "" # Default rating is empty # Save the current index and selected profile current_index = [0] current_profile = ["moiz"] # Default profile def encode_image(image_path): with open(image_path, "rb") as img_file: return f"data:image/png;base64,{base64.b64encode(img_file.read()).decode('utf-8')}" # Encode images as base64 imdb_logo = encode_image("assets/imdblogo.png") rotten_logo = encode_image("assets/rotten.png") metacritic_logo = encode_image("assets/metacritic.png") def display_movie(action, profile): # Update the current profile current_profile[0] = profile # Update the index based on action if action == "next" and current_index[0] < len(data_df) - 1: current_index[0] += 1 elif action == "prev" and current_index[0] > 0: current_index[0] -= 1 # Extract movie details movie = data_df.iloc[current_index[0]] # Get the IMDb ID and Poster URL movie_id = movie.get('id', 'Unknown') # Use 'Unknown' if 'id' doesn't exist poster_url = movie.get("Poster", None) # Default to None if Poster is missing # Use default text if no poster URL is available poster_content = ( f'Poster' if poster_url else "Poster not available" ) details = { "title": f'{movie["Title"]}', "poster_content": poster_content, "ratings": f"""
IMDb

{movie['IMDb']}

Rotten Tomatoes

{movie['Rotten Tomatoes']}

Metascore

{movie['Metascore']}

""", "details": f""" **Year:** {movie['Year']} **Rated:** {movie['Rated']} **Runtime:** {movie['Runtime']} **Genre:** {movie['Genre1']}, {movie['Genre2']}, {movie['Genre3']} **Director:** {movie['Director']} **Writer:** {movie['Writer']} **Plot:** {movie['Plot']} **Awards:** {movie['Awards']} **Box Office:** {movie['BoxOffice']} """, "current_rating": f"Your Rating: {movie[profile]}", "index_display": f'{current_index[0] + 1}' } return details["title"], details["poster_content"], details["ratings"], details["details"], details["current_rating"], details["index_display"] def submit_rating(rating): # Update the rating for the current profile and movie movie_index = current_index[0] profile = current_profile[0] data_df.at[movie_index, profile] = rating # Save the changes to the dataset updated_data = Dataset.from_pandas(data_df) updated_data.push_to_hub("moizmoizmoizmoiz/MovieRatingDB") return display_movie("stay", profile) def not_watched(): # Mark the movie as "N/W" for the current profile movie_index = current_index[0] profile = current_profile[0] data_df.at[movie_index, profile] = 99 # Save the changes to the dataset updated_data = Dataset.from_pandas(data_df) updated_data.push_to_hub("moizmoizmoizmoiz/MovieRatingDB") return display_movie("stay", profile) def jump_to_index(index, profile): # Ensure the input index is valid try: index = int(index) - 1 # Convert to zero-based index except ValueError: index = current_index[0] # If invalid, keep the current index # Update the index only if within range if 0 <= index < len(data_df): current_index[0] = index # Display the movie details for the selected index return display_movie("stay", profile) # Define Gradio interface with external CSS file with gr.Blocks(css_paths="styles.css") as app: gr.Markdown("## 🎬 Movie Database Viewer 🎬") with gr.Row(): # Profile selector and movie index movie_index_input = gr.Textbox( value="1", label="Go to Movie Index", interactive=True, elem_id="movie-index-input" ) blank_column_1 = gr.Column(scale=2, elem_id="blank-column-1") blank_column_2 = gr.Column(scale=1, elem_id="blank-column-2") profile_selector = gr.Dropdown( choices=['moiz', 'udisha', 'musab'], value='moiz', label="Profile", interactive=True ) with gr.Row(): with gr.Column(scale=1): poster = gr.Markdown("🎥 Movie Poster Placeholder 🎥", elem_id="poster") with gr.Column(scale=2): title = gr.Markdown("Title Placeholder", elem_id="title") ratings = gr.HTML("
Ratings Placeholder
", elem_id="ratings") movie_details = gr.Markdown("Details will appear here.", elem_id="details") user_rating = gr.Markdown("Your Rating: 0", elem_id="current-rating") with gr.Row(): # Slider in its own row rating_slider = gr.Slider(0, 10, step=0.25, label="Rate this movie:", elem_id="rating-slider", interactive=True, scale=2) with gr.Row(): not_watched_button = gr.Button("🚫 Not Watched") blank_column_1 = gr.Column(scale=1, elem_id="blank-column-1") submit_button = gr.Button("✅ Submit Rating") with gr.Row(): prev_button = gr.Button("⬅️ Previous", scale=1) blank_column_1 = gr.Column(scale=2, elem_id="blank-column-1") blank_column_2 = gr.Column(scale=2, elem_id="blank-column-2") next_button = gr.Button("Next ➡️", scale=1) # Interactivity for buttons profile_selector.change( display_movie, inputs=[gr.Text(value="stay", visible=False), profile_selector], outputs=[title, poster, ratings, movie_details, user_rating, movie_index_input] ) prev_button.click( display_movie, inputs=[gr.Text(value="prev", visible=False), profile_selector], outputs=[title, poster, ratings, movie_details, user_rating, movie_index_input] ) next_button.click( display_movie, inputs=[gr.Text(value="next", visible=False), profile_selector], outputs=[title, poster, ratings, movie_details, user_rating, movie_index_input] ) submit_button.click( submit_rating, inputs=rating_slider, outputs=[title, poster, ratings, movie_details, user_rating, movie_index_input] ) not_watched_button.click( not_watched, outputs=[title, poster, ratings, movie_details, user_rating, movie_index_input] ) movie_index_input.submit( jump_to_index, inputs=[movie_index_input, profile_selector], outputs=[title, poster, ratings, movie_details, user_rating, movie_index_input] ) app.launch()