""" app.py Gradio interface for the crop yield predictor. Run: python app.py Then open → http://localhost:7860 """ import gradio as gr from predictor import predict # ── Options ─────────────────────────────────────────────────────────────────── CROPS = ["Wheat", "Rice", "Maize", "Soybean", "Cotton"] SOILS = ["Loamy", "Clayey", "Sandy", "Black", "Red"] SEASONS = ["Kharif", "Rabi", "Zaid"] DISEASE = ["None", "Mild", "Moderate", "Severe"] IRRIGATE = ["Rainfed", "Canal", "Drip", "Sprinkler"] def run_prediction(crop, soil, season, disease, irrigation, rainfall, temperature, fertilizer, area, msp): result = predict( crop=crop, soil=soil, season=season, disease=disease, irrigation=irrigation, rainfall=rainfall, temperature=temperature, fertilizer=fertilizer, area=area, ) revenue_low = result["low"] * area * msp revenue_mid = result["total"] * msp revenue_high = result["high"] * area * msp summary = f""" ## 🌾 Yield Prediction Results | Metric | Value | |---|---| | **Low estimate** | {result['low']:,} kg/acre | | **Expected yield** | {result['mid']:,} kg/acre | | **High estimate** | {result['high']:,} kg/acre | | **Confidence band** | ±{result['band']}% | ### 📦 Total Farm Output ({area:.0f} acres) | Scenario | Yield (kg) | Revenue (₹) | |---|---|---| | Pessimistic | {int(result['low'] * area):,} | ₹{int(revenue_low):,} | | Expected | {result['total']:,} | ₹{int(revenue_mid):,} | | Optimistic | {int(result['high'] * area):,} | ₹{int(revenue_high):,} | > **Range:** `{result['low']:,} – {result['high']:,} kg/acre` > Disease severity **{disease}** → ±{result['band']}% confidence band applied. """ return summary.strip() # ── UI layout ───────────────────────────────────────────────────────────────── with gr.Blocks(title="Crop Yield Predictor", theme=gr.themes.Soft()) as demo: gr.Markdown("# 🌾 Crop Yield Predictor\nAI-powered range estimation for Indian farmers") with gr.Row(): with gr.Column(scale=1): gr.Markdown("### Crop & Field Info") crop = gr.Dropdown(CROPS, label="Crop type", value="Wheat") soil = gr.Dropdown(SOILS, label="Soil type", value="Loamy") season = gr.Dropdown(SEASONS, label="Season", value="Rabi") disease = gr.Dropdown(DISEASE, label="Disease severity", value="None") irrigation = gr.Dropdown(IRRIGATE, label="Irrigation", value="Rainfed") with gr.Column(scale=1): gr.Markdown("### Weather & Inputs") rainfall = gr.Slider(100, 1200, value=600, step=10, label="Rainfall (mm)") temperature = gr.Slider(10, 45, value=28, step=0.5, label="Temperature (°C)") fertilizer = gr.Slider(0, 150, value=60, step=5, label="Fertilizer (kg/acre)") area = gr.Slider(1, 50, value=5, step=1, label="Farm area (acres)") msp = gr.Slider(10, 100, value=22, step=1, label="MSP / kg (₹)") predict_btn = gr.Button("🔍 Predict Yield Range", variant="primary") output = gr.Markdown(label="Prediction") predict_btn.click( fn=run_prediction, inputs=[crop, soil, season, disease, irrigation, rainfall, temperature, fertilizer, area, msp], outputs=output, ) gr.Markdown(""" --- **How it works:** Random Forest (200 trees, R²≈0.93) trained on 5,000 synthetic Indian farm records. Confidence band widens with disease severity: None ±8% → Severe ±30%. """) if __name__ == "__main__": demo.launch(share=True)