Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from catboost import CatBoostRegressor
|
4 |
+
|
5 |
+
# Load the saved CatBoost model
|
6 |
+
model = CatBoostRegressor()
|
7 |
+
model.load_model("catboost_yield_model.cbm")
|
8 |
+
|
9 |
+
# Define the unique values for dropdown inputs
|
10 |
+
unique_soil_types = ['Sandy', 'Clay', 'Loam', 'Silt', 'Peaty', 'Chalky']
|
11 |
+
unique_crops = ['Cotton', 'Rice', 'Barley', 'Soybean', 'Wheat', 'Maize']
|
12 |
+
unique_irrigation_used = [True, False]
|
13 |
+
unique_fertilizer_used = [True, False]
|
14 |
+
|
15 |
+
# Prediction function
|
16 |
+
def predict_yield(soil_type, crop, rainfall, temperature, fertilizer_used, irrigation_used):
|
17 |
+
input_data = pd.DataFrame({
|
18 |
+
'Soil_Type': [soil_type],
|
19 |
+
'Crop': [crop],
|
20 |
+
'Rainfall_mm': [float(rainfall)],
|
21 |
+
'Temperature_Celsius': [float(temperature)],
|
22 |
+
'Fertilizer_Used': [fertilizer_used],
|
23 |
+
'Irrigation_Used': [irrigation_used]
|
24 |
+
})
|
25 |
+
prediction = model.predict(input_data)
|
26 |
+
return f"Predicted Yield (tons per hectare): {prediction[0]:.2f}"
|
27 |
+
|
28 |
+
# Create the Gradio interface
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("# ๐พ Crop Yield Prediction App ๐ฆ๏ธ")
|
31 |
+
gr.Markdown("Provide the following details to predict the crop yield (tons per hectare):")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
soil_type = gr.Dropdown(choices=unique_soil_types, label="Soil Type", value="Sandy")
|
35 |
+
crop = gr.Dropdown(choices=unique_crops, label="Type of Crop", value="Cotton")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
rainfall = gr.Textbox(label="Average Rainfall (mm)", value="897.077239")
|
39 |
+
temperature = gr.Textbox(label="Average Temperature (Celsius)", value="27.676966")
|
40 |
+
|
41 |
+
with gr.Row():
|
42 |
+
fertilizer_used = gr.Dropdown(choices=unique_fertilizer_used, label="Fertilizer Used?", value=False)
|
43 |
+
irrigation_used = gr.Dropdown(choices=unique_irrigation_used, label="Irrigation Used?", value=True)
|
44 |
+
|
45 |
+
predict_button = gr.Button("๐ฎ Predict Yield")
|
46 |
+
output = gr.Textbox(label="Prediction Output")
|
47 |
+
|
48 |
+
predict_button.click(
|
49 |
+
predict_yield,
|
50 |
+
inputs=[soil_type, crop, rainfall, temperature, fertilizer_used, irrigation_used],
|
51 |
+
outputs=output
|
52 |
+
)
|
53 |
+
|
54 |
+
gr.Examples(
|
55 |
+
examples=[
|
56 |
+
["Sandy", "Cotton", "897.077239", "27.676966", False, True],
|
57 |
+
["Clay", "Rice", "1200", "30", True, False],
|
58 |
+
],
|
59 |
+
inputs=[soil_type, crop, rainfall, temperature, fertilizer_used, irrigation_used]
|
60 |
+
)
|
61 |
+
|
62 |
+
gr.Markdown("### ๐ Thank you for using the Crop Yield Prediction App! ๐ฑ")
|
63 |
+
|
64 |
+
# Launch the app
|
65 |
+
demo.launch()
|