Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,11 +13,24 @@ if not os.path.exists(model_path):
|
|
13 |
with open(model_path, "rb") as file:
|
14 |
model = pickle.load(file)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
# Prediction Function
|
17 |
-
def predict_yield(
|
18 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Prepare input
|
20 |
-
features = np.array([[
|
21 |
prediction = model.predict(features)
|
22 |
return f"Predicted Crop Yield: {float(prediction[0]):.2f}"
|
23 |
except Exception as e:
|
@@ -27,13 +40,14 @@ def predict_yield(feature1, feature2, feature3):
|
|
27 |
demo = gr.Interface(
|
28 |
fn=predict_yield,
|
29 |
inputs=[
|
30 |
-
gr.Number(label="
|
31 |
-
gr.
|
32 |
-
gr.
|
|
|
33 |
],
|
34 |
outputs=gr.Textbox(label="Prediction Result"),
|
35 |
title="Crop Yield Prediction",
|
36 |
-
description="Enter the
|
37 |
)
|
38 |
|
39 |
if __name__ == "__main__":
|
|
|
13 |
with open(model_path, "rb") as file:
|
14 |
model = pickle.load(file)
|
15 |
|
16 |
+
# Example mapping for categorical variables (if encoded during training)
|
17 |
+
season_mapping = {"Kharif": 0, "Rabi": 1, "Zaid": 2}
|
18 |
+
district_mapping = {"District A": 0, "District B": 1, "District C": 2}
|
19 |
+
crop_mapping = {"Wheat": 0, "Rice": 1, "Maize": 2, "Sugarcane": 3}
|
20 |
+
|
21 |
# Prediction Function
|
22 |
+
def predict_yield(area, season, district, crop):
|
23 |
try:
|
24 |
+
# Convert categorical inputs to numerical if necessary
|
25 |
+
season_encoded = season_mapping.get(season, -1)
|
26 |
+
district_encoded = district_mapping.get(district, -1)
|
27 |
+
crop_encoded = crop_mapping.get(crop, -1)
|
28 |
+
|
29 |
+
if -1 in [season_encoded, district_encoded, crop_encoded]:
|
30 |
+
return "Error: Invalid categorical input value."
|
31 |
+
|
32 |
# Prepare input
|
33 |
+
features = np.array([[area, season_encoded, district_encoded, crop_encoded]])
|
34 |
prediction = model.predict(features)
|
35 |
return f"Predicted Crop Yield: {float(prediction[0]):.2f}"
|
36 |
except Exception as e:
|
|
|
40 |
demo = gr.Interface(
|
41 |
fn=predict_yield,
|
42 |
inputs=[
|
43 |
+
gr.Number(label="Area (in acres)"),
|
44 |
+
gr.Dropdown(["Kharif", "Rabi", "Zaid"], label="Season"),
|
45 |
+
gr.Dropdown(["District A", "District B", "District C"], label="District"),
|
46 |
+
gr.Dropdown(["Wheat", "Rice", "Maize", "Sugarcane"], label="Crop"),
|
47 |
],
|
48 |
outputs=gr.Textbox(label="Prediction Result"),
|
49 |
title="Crop Yield Prediction",
|
50 |
+
description="Enter the details to predict crop yield.",
|
51 |
)
|
52 |
|
53 |
if __name__ == "__main__":
|