Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Load the trained model
|
6 |
+
model_file = 'Yield_Ridge.sav'
|
7 |
+
with open(model_file, 'rb') as file:
|
8 |
+
model = pickle.load(file)
|
9 |
+
|
10 |
+
# Define prediction function
|
11 |
+
def predict_yield(crop: str, year: int, rainfall: float, temperature: float, fertilizer: float):
|
12 |
+
"""
|
13 |
+
Predicts crop yield based on user inputs.
|
14 |
+
|
15 |
+
Parameters:
|
16 |
+
crop (str): Name of the crop.
|
17 |
+
year (int): Year of prediction.
|
18 |
+
rainfall (float): Average annual rainfall (in mm).
|
19 |
+
temperature (float): Average annual temperature (in °C).
|
20 |
+
fertilizer (float): Amount of fertilizer used (in kg/ha).
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
str: Predicted yield (in tons per hectare).
|
24 |
+
"""
|
25 |
+
# Create a feature array for prediction
|
26 |
+
features = np.array([[crop_mapping[crop], year, rainfall, temperature, fertilizer]])
|
27 |
+
prediction = model.predict(features)[0]
|
28 |
+
return f"{prediction:.2f} tons/ha"
|
29 |
+
|
30 |
+
# Crop mapping (example mapping; adjust as per your dataset)
|
31 |
+
crop_mapping = {
|
32 |
+
"Wheat": 1,
|
33 |
+
"Rice": 2,
|
34 |
+
"Maize": 3,
|
35 |
+
"Sugarcane": 4
|
36 |
+
}
|
37 |
+
|
38 |
+
# Define Gradio interface
|
39 |
+
with gr.Blocks() as yield_app:
|
40 |
+
gr.Markdown("## Yield Prediction")
|
41 |
+
gr.Markdown("Predict crop yield based on environmental and agricultural inputs.")
|
42 |
+
|
43 |
+
with gr.Row():
|
44 |
+
crop = gr.Dropdown(label="Select Crop", choices=list(crop_mapping.keys()), value="Wheat")
|
45 |
+
year = gr.Number(label="Year", value=2025)
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
rainfall = gr.Number(label="Rainfall (in mm)", value=700.0)
|
49 |
+
temperature = gr.Number(label="Temperature (in °C)", value=25.0)
|
50 |
+
fertilizer = gr.Number(label="Fertilizer (in kg/ha)", value=120.0)
|
51 |
+
|
52 |
+
output = gr.Textbox(label="Predicted Yield", interactive=False)
|
53 |
+
|
54 |
+
predict_button = gr.Button("Predict Yield")
|
55 |
+
predict_button.click(
|
56 |
+
predict_yield,
|
57 |
+
inputs=[crop, year, rainfall, temperature, fertilizer],
|
58 |
+
outputs=output
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
+
yield_app.launch()
|