Spaces:
Sleeping
Sleeping
Download app.py from DakuSir/yieldpredictor: direct link, hf CLI and curl.
- Browser
- Download file 3.98 kB
-
https://huggingface.co/spaces/DakuSir/yieldpredictor/resolve/main/app.py
- Command line
-
hf download hf://spaces/DakuSir/yieldpredictor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DakuSir/yieldpredictor/resolve/main/app.py
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) | |