File size: 2,859 Bytes
1a94b3f
 
959b774
1a94b3f
959b774
b5881ac
959b774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import numpy as np
import joblib

# Load the pre-trained crop yield prediction model
crop_yield_model = joblib.load('yield_model.sav')

# Define states, crops, and seasons for dropdown menus
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}"

# Gradio interface
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()