Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
-
import pickle
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
-
# Load the trained model
|
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 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
+
import joblib
|
4 |
|
5 |
+
# Load the pre-trained crop yield prediction model
|
6 |
+
crop_yield_model = joblib.load('voting_yield.sav')
|
7 |
+
|
8 |
+
# Define states, crops, and seasons for dropdown menus
|
9 |
+
states = [
|
10 |
+
'Andaman and Nicobar Islands', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh',
|
11 |
+
'Chhattisgarh', 'Dadra and Nagar Haveli', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh',
|
12 |
+
'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur',
|
13 |
+
'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu',
|
14 |
+
'Telangana', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal'
|
15 |
+
]
|
16 |
+
|
17 |
+
crops = [
|
18 |
+
'Arecanut', 'Barley', 'Banana', 'Blackpepper', 'Brinjal', 'Cabbage', 'Cardamom', 'Cashewnuts', 'Cauliflower',
|
19 |
+
'Coriander', 'Cotton', 'Garlic', 'Grapes', 'Horsegram', 'Jowar', 'Jute', 'Ladyfinger', 'Maize',
|
20 |
+
'Mango', 'Moong', 'Onion', 'Orange', 'Papaya', 'Pineapple', 'Potato', 'Rapeseed', 'Ragi', 'Rice',
|
21 |
+
'Sesamum', 'Soyabean', 'Sunflower', 'Sweetpotato', 'Tapioca', 'Tomato', 'Turmeric', 'Wheat'
|
22 |
+
]
|
23 |
+
|
24 |
+
seasons = ['Kharif', 'Rabi', 'Summer', 'Whole Year']
|
25 |
+
|
26 |
+
def predict_yield(state, crop, season, pH, rainfall, temperature, area, production):
|
27 |
+
"""Predict crop yield based on user inputs."""
|
28 |
+
state_encoded = [1 if s == state else 0 for s in states]
|
29 |
+
crop_encoded = [1 if c == crop else 0 for c in crops]
|
30 |
+
season_encoded = [1 if s == season else 0 for s in seasons]
|
31 |
+
|
32 |
+
input_features = np.array(state_encoded + crop_encoded + season_encoded + [pH, rainfall, temperature, area, production])
|
33 |
+
input_features = input_features.reshape(1, -1)
|
34 |
+
|
35 |
+
try:
|
36 |
+
predicted_yield = crop_yield_model.predict(input_features)[0]
|
37 |
+
return f"The predicted yield is {predicted_yield:.2f} tons per hectare."
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error: {e}"
|
40 |
+
|
41 |
+
# Gradio interface
|
42 |
+
def interface():
|
43 |
+
"""Define the Gradio interface."""
|
44 |
+
inputs = [
|
45 |
+
gr.Dropdown(choices=states, label="Select State"),
|
46 |
+
gr.Dropdown(choices=crops, label="Select Crop"),
|
47 |
+
gr.Dropdown(choices=seasons, label="Select Season"),
|
48 |
+
gr.Number(label="Soil pH Value", value=7.0, precision=2),
|
49 |
+
gr.Number(label="Rainfall (mm)", value=100.0, precision=1),
|
50 |
+
gr.Number(label="Temperature (°C)", value=25.0, precision=1),
|
51 |
+
gr.Number(label="Area (hectares)", value=1.0, precision=2),
|
52 |
+
gr.Number(label="Production (tons)", value=2.0, precision=2)
|
53 |
+
]
|
54 |
+
|
55 |
+
outputs = gr.Textbox(label="Predicted Yield")
|
56 |
+
|
57 |
+
gr.Interface(
|
58 |
+
fn=predict_yield,
|
59 |
+
inputs=inputs,
|
60 |
+
outputs=outputs,
|
61 |
+
title="Crop Yield Prediction",
|
62 |
+
description="Enter the details below to predict crop yield using the trained model."
|
63 |
+
).launch()
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
interface()
|