File size: 7,956 Bytes
6275709
2369a18
e32be66
db97852
d68addc
11566b6
 
 
 
 
 
 
 
 
20fd1c8
db97852
20fd1c8
 
 
b302c0d
ebe8012
 
20fd1c8
 
ebe8012
 
6275709
ebe8012
2369a18
6275709
 
 
b302c0d
6275709
 
 
 
 
ebe8012
 
 
 
6275709
20fd1c8
6275709
 
 
ebe8012
6275709
20fd1c8
ebe8012
230d12a
ebe8012
230d12a
ebe8012
230d12a
 
 
 
 
 
d3ad890
6275709
ebe8012
230d12a
6275709
ebe8012
 
 
6275709
 
ebe8012
 
6275709
 
ebe8012
 
6275709
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe8012
 
3f6b95e
6275709
230d12a
ebe8012
 
 
 
 
20fd1c8
6275709
20fd1c8
 
 
ebe8012
 
 
 
 
 
 
2c523f3
ebe8012
20fd1c8
 
 
ebe8012
 
 
3f6b95e
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe8012
 
6275709
ebe8012
 
3f6b95e
 
 
 
ebe8012
7024b2a
 
ebe8012
 
 
 
 
 
 
 
6275709
 
 
 
ebe8012
6275709
 
ebe8012
 
 
 
 
 
 
 
 
 
6275709
 
ebe8012
 
 
 
6275709
 
ebe8012
 
 
3f6b95e
ebe8012
 
6275709
 
ebe8012
3f6b95e
6275709
b302c0d
6275709
ebe8012
3f6b95e
ebe8012
 
 
 
 
3f6b95e
ebe8012
 
 
 
3f6b95e
 
 
 
 
 
 
b302c0d
c6a3e36
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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'<img src="{poster_url}" alt="Poster" style="width: 100%; max-width: 500px; height: auto; border-radius: 10px;"/>'
        if poster_url
        else "Poster not available"
    )

    details = {
        "title": f'<a href="https://www.imdb.com/title/{movie_id}/" target="_blank" style="font-size: 36px; text-decoration: none; color: inherit; font-family: Arial, sans-serif;">{movie["Title"]}</a>',
        "poster_content": poster_content,
        "ratings": f"""
        <div style="display: flex; gap: 15px; align-items: center; text-align: center;">
            <div>
                <img src="{imdb_logo}" alt="IMDb" style="width: 30px; height: auto;"/>
                <p>{movie['IMDb']}</p>
            </div>
            <div>
                <img src="{rotten_logo}" alt="Rotten Tomatoes" style="width: 30px; height: auto;"/>
                <p>{movie['Rotten Tomatoes']}</p>
            </div>
            <div>
                <img src="{metacritic_logo}" alt="Metascore" style="width: 30px; height: auto;"/>
                <p>{movie['Metascore']}</p>
            </div>
        </div>
        """,
        "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("<div style='text-align: center;'>Ratings Placeholder</div>", 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()