|
import gradio as gr |
|
import numpy as np |
|
import joblib |
|
|
|
|
|
crop_yield_model = joblib.load('yield_model.sav') |
|
|
|
|
|
states = [ |
|
'Andaman and Nicobar Islands', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh', |
|
'Chhattisgarh', 'Dadra and Nagar Haveli', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', |
|
'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur', |
|
'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', |
|
'Telangana', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal' |
|
] |
|
|
|
crops = [ |
|
'Arecanut', 'Barley', 'Banana', 'Blackpepper', 'Brinjal', 'Cabbage', 'Cardamom', 'Cashewnuts', 'Cauliflower', |
|
'Coriander', 'Cotton', 'Garlic', 'Grapes', 'Horsegram', 'Jowar', 'Jute', 'Ladyfinger', 'Maize', |
|
'Mango', 'Moong', 'Onion', 'Orange', 'Papaya', 'Pineapple', 'Potato', 'Rapeseed', 'Ragi', 'Rice', |
|
'Sesamum', 'Soyabean', 'Sunflower', 'Sweetpotato', 'Tapioca', 'Tomato', 'Turmeric', 'Wheat' |
|
] |
|
|
|
seasons = ['Kharif', 'Rabi', 'Summer', 'Whole Year'] |
|
|
|
def predict_yield(state, crop, season, pH, rainfall, temperature, area, production): |
|
"""Predict crop yield based on user inputs.""" |
|
state_encoded = [1 if s == state else 0 for s in states] |
|
crop_encoded = [1 if c == crop else 0 for c in crops] |
|
season_encoded = [1 if s == season else 0 for s in seasons] |
|
|
|
input_features = np.array(state_encoded + crop_encoded + season_encoded + [pH, rainfall, temperature, area, production]) |
|
input_features = input_features.reshape(1, -1) |
|
|
|
try: |
|
predicted_yield = crop_yield_model.predict(input_features)[0] |
|
return f"The predicted yield is {predicted_yield:.2f} tons per hectare." |
|
except Exception as e: |
|
return f"Error: {e}" |
|
|
|
|
|
def interface(): |
|
"""Define the Gradio interface.""" |
|
inputs = [ |
|
gr.Dropdown(choices=states, label="Select State"), |
|
gr.Dropdown(choices=crops, label="Select Crop"), |
|
gr.Dropdown(choices=seasons, label="Select Season"), |
|
gr.Number(label="Soil pH Value", value=7.0, precision=2), |
|
gr.Number(label="Rainfall (mm)", value=100.0, precision=1), |
|
gr.Number(label="Temperature (°C)", value=25.0, precision=1), |
|
gr.Number(label="Area (hectares)", value=1.0, precision=2), |
|
gr.Number(label="Production (tons)", value=2.0, precision=2) |
|
] |
|
|
|
outputs = gr.Textbox(label="Predicted Yield") |
|
|
|
gr.Interface( |
|
fn=predict_yield, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="Crop Yield Prediction", |
|
description="Enter the details below to predict crop yield using the trained model." |
|
).launch() |
|
|
|
if __name__ == "__main__": |
|
interface() |
|
|