Spaces:
Sleeping
Sleeping
| """Movie Box Office Revenue Predictor - Gradio Web Application.""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| from typing import Any | |
| import gradio as gr | |
| from components import create_input_form, get_example_data, predict_revenue_from_form | |
| from src.preprocess import load_model, parse_feature_options | |
| # Configuration | |
| MODEL_PATH = Path("model/final_model.pkl") | |
| # Global state | |
| MODEL: Any | None = None | |
| MODEL_ERROR: str | None = None | |
| FEATURE_OPTIONS: dict[str, list[str]] = {} | |
| # Initialize model | |
| try: | |
| MODEL = load_model(MODEL_PATH) | |
| FEATURE_OPTIONS = parse_feature_options(list(MODEL.feature_names_in_)) | |
| except Exception as exc: | |
| MODEL_ERROR = str(exc) | |
| def build_app() -> gr.Blocks: | |
| """Build and configure the Gradio interface.""" | |
| with gr.Blocks( | |
| title="π¬ Movie Revenue Predictor", | |
| ) as app: | |
| # Header | |
| with gr.Row(): | |
| gr.Markdown( | |
| """ | |
| # π¬ Movie Box Office Revenue Predictor | |
| Predict movie revenue using machine learning trained on historical box office data. | |
| Enter movie details below and get instant revenue predictions with profitability analysis. | |
| """, | |
| elem_classes=["header"] | |
| ) | |
| # Model status | |
| if MODEL is None: | |
| gr.Warning(f"β οΈ Model loading error: {MODEL_ERROR}") | |
| # Main content | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| # Input form | |
| input_dict, input_list = create_input_form(FEATURE_OPTIONS) | |
| # Action buttons | |
| with gr.Row(): | |
| predict_btn = gr.Button( | |
| "π― Predict Revenue", | |
| variant="primary", | |
| scale=2, | |
| size="lg" | |
| ) | |
| clear_btn = gr.ClearButton( | |
| components=input_list, | |
| value="π Clear", | |
| scale=1, | |
| size="lg" | |
| ) | |
| # Examples | |
| gr.Markdown("### π Quick Examples") | |
| gr.Examples( | |
| examples=get_example_data(FEATURE_OPTIONS), | |
| inputs=input_list, | |
| label="Click an example to auto-fill the form", | |
| ) | |
| with gr.Column(scale=2): | |
| gr.Markdown("### π Prediction Results") | |
| # Output displays | |
| prediction_output = gr.Markdown( | |
| "π‘ Fill in the form and click **Predict Revenue** to see results.", | |
| elem_classes=["output-box"] | |
| ) | |
| profitability_output = gr.Markdown( | |
| "", | |
| elem_classes=["output-box"] | |
| ) | |
| # Event handlers | |
| predict_btn.click( | |
| fn=lambda *args: predict_revenue_from_form(MODEL, *args), | |
| inputs=input_list, | |
| outputs=[prediction_output, profitability_output], | |
| ) | |
| return app | |
| def main(): | |
| """Launch the application.""" | |
| theme = gr.themes.Default( | |
| primary_hue="zinc", | |
| secondary_hue="slate", | |
| neutral_hue="slate", | |
| ) | |
| app = build_app() | |
| app.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| theme=theme, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |