Spaces:
Sleeping
Sleeping
File size: 3,619 Bytes
b490ee7 | 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 | """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()
|