CoolFace
Apppublic

mehreen2712/Migration_project

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
app.py98 linesDownload Raw Back to root
1import gradio as gr2import tensorflow as tf3import numpy as np4import matplotlib.pyplot as plt5 6# 1. Loading the ANN model7try:8    model = tf.keras.models.load_model('migration_model.h5', compile=False)9    model.compile(optimizer='adam', loss='mse')10except Exception as e:11    print(f"Model Load Error: {e}")12 13def create_analytics_plots(prediction):14    # Professional Data Visualization15    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))16    17    # Chart 1: Impact Analysis18    categories = ['Predicted Impact', 'Global Median']19    values = [float(prediction), 0.40]20    ax1.bar(categories, values, color=['#0f172a', '#cbd5e1'], width=0.4)21    ax1.set_title("Movement Impact Analysis", fontsize=14, fontweight='bold', pad=20)22    ax1.set_ylim(0, 1)23 24    # Chart 2: Strategic Drivers25    labels = ['Economic', 'Social', 'Regional']26    val = float(prediction)27    sizes = [max(10, val*60), 25, 15] 28    ax2.pie(sizes, labels=labels, autopct='%1.1f%%', colors=['#1e40af', '#3b82f6', '#93c5fd'], startangle=140)29    ax2.set_title("Strategic Drivers Distribution", fontsize=14, fontweight='bold', pad=20)30 31    plt.tight_layout(pad=5.0) 32    return fig33 34def run_strategic_analysis(gdp, unemployment, distance):35    try:36        # Preparing input37        features = np.array([[float(gdp), float(unemployment), float(distance)]], dtype=np.float32)38        39        # --- FIX: Extracting the scalar value from the model's array output ---40        prediction_array = model.predict(features)41        prediction = float(np.squeeze(prediction_array)) # This removes extra dimensions42        43        # Classification44        if prediction < 0.35:45            status = "STABLE / LOW VOLATILITY"46        elif prediction < 0.65:47            status = "MODERATE TRANSITION"48        else:49            status = "CRITICAL / HIGH TURNOVER"50        51        # Generate Visuals52        fig = create_analytics_plots(prediction)53        score_display = f"{prediction:.2%}"54        55        return score_display, status, fig56    except Exception as e:57        # If something goes wrong, show the error clearly58        return "Error", f"Details: {str(e)}", None59 60# --- Professional Vertical Interface Design ---61with gr.Blocks(theme=gr.themes.Soft(primary_hue="slate")) as demo:62    63    gr.Markdown("# ๐ŸŒ Global Population Dynamics & Strategic Insights")64    gr.Markdown("### Advanced Decision Intelligence Framework powered by ANN")65    66    # SECTION 1: INPUTS67    with gr.Row(variant="panel"):68        with gr.Column():69            gr.Markdown("### โš™๏ธ Step 1: Configure Socio-Economic Variables")70            gdp_input = gr.Slider(0, 1, step=0.01, value=0.5, label="Economic Growth Index")71            unemp_input = gr.Slider(0, 1, step=0.01, value=0.2, label="Market Stability Index")72            dist_input = gr.Slider(0, 1, step=0.01, value=0.3, label="Geographical Proximity")73            submit_btn = gr.Button("๐Ÿš€ RUN STRATEGIC ANALYSIS", variant="primary", size="lg")74 75    gr.HTML("<hr style='border: 1px solid #e2e8f0;'>")76 77    # SECTION 2: OUTPUTS (Vertical Layout)78    with gr.Column():79        gr.Markdown("### ๐Ÿ“ˆ Step 2: Intelligent Analytics Dashboard")80        with gr.Row():81            res_score = gr.Textbox(label="Movement Probability Index", interactive=False)82            res_status = gr.Textbox(label="Current Security Posture", interactive=False)83        84        with gr.Row():85            res_plot = gr.Plot(label="Visual Data Intelligence Dashboard", show_label=False)86 87    # Linking Button88    submit_btn.click(89        fn=run_strategic_analysis, 90        inputs=[gdp_input, unemp_input, dist_input], 91        outputs=[res_score, res_status, res_plot]92    )93 94    gr.Markdown("---")95    gr.Markdown("ยฉ 2024 Strategic Migration Hub | Enterprise-Level Analytical Tool")96 97if __name__ == "__main__":98    demo.launch()