yieldpredictor / app.py
3v324v23's picture
Initial push
50d94cb
Raw History Blame Contribute Delete
3.98 kB
"""
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)