Spaces:
Runtime error
Runtime error
Download app.py from PiJul/MovieRevenuePrediction: direct link, hf CLI and curl.
- Browser
- Download file 5.96 kB
-
https://huggingface.co/spaces/PiJul/MovieRevenuePrediction/resolve/main/app.py
- Command line
-
hf download hf://spaces/PiJul/MovieRevenuePrediction/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PiJul/MovieRevenuePrediction/resolve/main/app.py
5.96 kB
| import gradio as gr | |
| import os | |
| import numpy as np | |
| import datetime as dt | |
| import hopsworks | |
| import pandas as pd | |
| import utils | |
| import random | |
| from PIL import Image | |
| from io import BytesIO | |
| import requests | |
| import pickle | |
| MAX_MOVIES = 3 | |
| def fetch_new_predictions(): | |
| project = hopsworks.login() | |
| fs = project.get_feature_store(name='igijul63_featurestore') | |
| fg = fs.get_feature_group('movie_predictions', version=1) | |
| query = fg.select_all() | |
| new_predictions_df = query.read() | |
| hopsworks.logout() | |
| ids = new_predictions_df['id'].tolist() | |
| print(f"Predicted movies are {ids}") | |
| details = [] | |
| titles = [] | |
| for i, movie_id in enumerate(ids): | |
| movie_details = utils.get_movie_details(movie_id) | |
| if movie_details['budget'] > 0 : | |
| # movie_details['predicted_revenue'] = new_predictions[new_predictions['id'] == movie_id]['revenue'].values[0] | |
| movie_details['predicted_revenue'] = new_predictions_df.iloc[i,:]['revenue'] | |
| # movie_details['release_date'] = dt.datetime.strptime(movie_details['release_date'], '%Y-%m-%d').date() | |
| details.append(movie_details) | |
| details = pd.DataFrame(details) | |
| details.sort_values(by = 'release_date', ascending = False, inplace = True) | |
| # details = details[:MAX_MOVIES] | |
| idxs = random.sample(range(len(details)), 3) | |
| details = details.iloc[idxs, :] | |
| new_titles = [f"### {title}" for title in details['title']] | |
| new_revenues = [f"#### Predicted revenue is {int(revenue)}\$" for revenue in details['predicted_revenue']] | |
| get_img = lambda poster_path : Image.open(BytesIO(requests.get(f"https://image.tmdb.org/t/p/w500{poster_path}", stream=True).content)) | |
| new_images = [get_img(poster_path) for poster_path in details['poster_path']] | |
| return new_titles[0], new_titles[1], new_titles[2], new_revenues[0], new_revenues[1], new_revenues[2], new_images[0], new_images[1], new_images[2] | |
| def fetch_prediction_for_title(title): | |
| movie_id = utils.get_movie_by_title(title)['results'][0]['id'] | |
| project = hopsworks.login() | |
| fs = project.get_feature_store(name='igijul63_featurestore') | |
| fg = fs.get_feature_group('movie_predictions', version=1) | |
| query = fg.select_all() | |
| new_predictions_df = query.read() | |
| print(f"Got predictions df") | |
| movie_details = utils.get_movie_details(movie_id) | |
| if movie_id not in new_predictions_df['id'].tolist(): | |
| features = utils.extract_features(movie_id, latest=False) | |
| print(features) | |
| features_df = pd.DataFrame([features]) | |
| features_df = features_df[['top_cast_popularity', 'budget', 'crew_popularity', 'similar_revenues']] | |
| features_df[['budget','similar_revenues']] /= 1000000 | |
| model = utils.load_model() | |
| output = model.predict(features_df) | |
| new_revenue = output * 1000000 | |
| # new_revenue = random.randint(200 * 1e6, 1000 * 1e6) | |
| else: | |
| movie_prediction = new_predictions_df[new_predictions_df['id'] == movie_id] | |
| new_revenue = movie_prediction['revenue'] | |
| movie_details['predicted_revenue'] = new_revenue # movie_prediction['revenue'] | |
| new_title = f"### {movie_details['title']}" | |
| new_revenue = f"#### Predicted revenue is {int(movie_details['predicted_revenue'])}\$" | |
| get_img = lambda poster_path : Image.open(BytesIO(requests.get(f"https://image.tmdb.org/t/p/w500{poster_path}", stream=True).content)) | |
| new_image = get_img(movie_details['poster_path']) | |
| budget = f"#### Movie with budget of {movie_details['budget']} \$" | |
| overview = movie_details['overview'] | |
| hopsworks.logout() | |
| return new_revenue, new_image, budget, overview | |
| new_data = fetch_new_predictions() | |
| search_data = fetch_prediction_for_title(new_data[0][4:]) | |
| with gr.Blocks("soft") as demo: | |
| live = True | |
| gr.Markdown("## Movie revenue prediction") | |
| with gr.Tabs(): | |
| with gr.TabItem("New movies"): | |
| gr.Markdown("# Prediction for recently released movies") | |
| gr.Markdown("Press the button to see other new predictions!") | |
| reload_button = gr.Button(value="Refresh") | |
| with gr.Row(): | |
| title_0 = gr.Markdown(new_data[0]) | |
| title_1 = gr.Markdown(new_data[1]) | |
| title_2 = gr.Markdown(new_data[2]) | |
| with gr.Row(): | |
| image_0 = gr.Image(new_data[6]) | |
| image_1 = gr.Image(new_data[7]) | |
| image_2 = gr.Image(new_data[8]) | |
| with gr.Row(): | |
| prediction_0 = gr.Markdown(new_data[3]) | |
| prediction_1 = gr.Markdown(new_data[4]) | |
| prediction_2 = gr.Markdown(new_data[5]) | |
| with gr.TabItem("Search a movie"): | |
| gr.Markdown("## Predict a revenue for a selected movie! You can type the title you want to search for and we'll see if we can predict how much it will earn.") | |
| with gr.Row(): | |
| with gr.Column(scale = 3): | |
| input_title = gr.Textbox(value = new_data[0][4:]) | |
| with gr.Column(scale = 1): | |
| search_button = gr.Button(value="Search") | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| with gr.Row(): | |
| prediction_s = gr.Markdown(search_data[0]) | |
| with gr.Row(): | |
| budget = gr.Markdown(search_data[2]) | |
| with gr.Row(): | |
| overview = gr.Markdown(search_data[3]) | |
| with gr.Column(scale=1): | |
| image_search = gr.Image(search_data[1]) | |
| search_button.click(fn = fetch_prediction_for_title, inputs = [input_title], outputs = [prediction_s, image_search, budget, overview]) | |
| reload_button.click(fn = fetch_new_predictions, inputs=[], outputs=[title_0, title_1, title_2, prediction_0, prediction_1, prediction_2, image_0, image_1, image_2]) | |
| demo.launch() | |
| # iface.launch() | |