import gradio as gr from model_utils import ( get_parent_categories, get_sub_categories, get_countries, get_currencies, get_months, load_lgbm_model, prediction_pipeline, load_llm_model, generate_blurb_pipeline ) # --- 1. Load Both Models at Startup --- lgbm_model = load_lgbm_model() llm = load_llm_model() # --- 2. Define Initial UI State --- parent_categories = get_parent_categories() default_parent_category = parent_categories[0] if parent_categories else None initial_sub_categories = get_sub_categories(default_parent_category) if default_parent_category else [] countries = get_countries() currencies = get_currencies() months = get_months() # --- 3. Define UI Interaction Logic --- def update_sub_category_choices(parent_category): sub_cats = get_sub_categories(parent_category) return gr.Dropdown(label="Sub-Category", choices=sub_cats, value=sub_cats[0] if sub_cats else None, interactive=True) # --- Custom CSS for button styling --- custom_css = """ #dark-mode-btn { max-width: 80px; flex-grow: 0; align-self: center; } """ # --- 4. Build the Gradio Interface --- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), css=custom_css) as app: # --- Title Bar with Dark Mode Toggle --- with gr.Row(): gr.Markdown("# Kickstarter Campaign Success Predictor - Client Edition") dark_mode_btn = gr.Button("Toggle Theme ☀️/🌙", elem_id="dark-mode-btn") # --- Section for Predictive Analysis --- gr.Markdown("## Success Prediction Analysis") gr.Markdown("Fill in your project details to estimate its probability of success and see how different factors impact the outcome.") with gr.Row(): with gr.Column(scale=1): with gr.Accordion("Project Features", open=True): parent_category_input = gr.Dropdown(label="Parent Category", choices=parent_categories, value=default_parent_category) sub_category_input = gr.Dropdown(label="Sub-Category", choices=initial_sub_categories, value=initial_sub_categories[0] if initial_sub_categories else None) country_input = gr.Dropdown(label="Country", choices=countries, value=countries[0] if countries else None) currency_input = gr.Dropdown(label="Currency", choices=currencies, value=currencies[0] if currencies else None) month_input = gr.Dropdown(label="Launch Month", choices=months, value=months[0] if months else None) goal_input = gr.Slider(minimum=1000, maximum=250000, value=50000, step=1000, label="Goal", info="Select the funding amount required") previous_success_input = gr.Checkbox(label="I have had a successful Kickstarter before", value=False) predict_button = gr.Button("Analyze Project", variant="primary") with gr.Column(scale=2): probability_output = gr.HTML(label="Result") with gr.Tabs(): with gr.TabItem("Goal vs. Probability"): goal_plot_output = gr.Plot() with gr.TabItem("Month vs. Probability"): month_plot_output = gr.Plot() with gr.TabItem("Sub-Category Comparison"): subcategory_plot_output = gr.Plot() # --- Section for Generative AI Blurb --- gr.Markdown("