Spaces:
Running
Running
shubham5027
commited on
Upload 3 files
Browse files- RF_Crop.sav +0 -0
- app.py +52 -0
- requirements.txt +2 -0
RF_Crop.sav
ADDED
Binary file (684 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load the RandomForest model
|
7 |
+
working_dir = os.path.dirname(os.path.abspath(__file__))
|
8 |
+
model = pickle.load(open(f'{working_dir}/RF_Crop.sav', 'rb'))
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
# Define the prediction function
|
13 |
+
def predict_crop(N, P, K, temperature, humidity, pH, rainfall):
|
14 |
+
user_input = np.array([[N, P, K, temperature, humidity, pH, rainfall]])
|
15 |
+
if np.all(user_input == 0):
|
16 |
+
return "Please enter valid values."
|
17 |
+
else:
|
18 |
+
prediction = model.predict(user_input)
|
19 |
+
crop = prediction[0]
|
20 |
+
return f"Hey, you should grow **{crop}** based on your soil and environmental factors."
|
21 |
+
|
22 |
+
# Gradio UI components
|
23 |
+
def main_interface():
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
|
26 |
+
|
27 |
+
with gr.Tab("Get Recommendation"):
|
28 |
+
gr.Markdown("Enter the details about your soil and environmental factors to get a crop recommendation.")
|
29 |
+
gr.Markdown("**Example Values:** [104, 18, 30, 23.6, 60.3, 6.7, 140.91] or [60, 18, 30, 23.6, 60.3, 8, 40.91]")
|
30 |
+
|
31 |
+
N = gr.Number(label="Nitrogen (N)", value=0, precision=0)
|
32 |
+
P = gr.Number(label="Phosphorus (P)", value=0, precision=0)
|
33 |
+
K = gr.Number(label="Potassium (K)", value=0, precision=0)
|
34 |
+
temperature = gr.Number(label="Temperature (°C)", value=0.0)
|
35 |
+
humidity = gr.Number(label="Humidity (%)", value=0.0)
|
36 |
+
pH = gr.Number(label="pH", value=0.0)
|
37 |
+
rainfall = gr.Number(label="Rainfall (mm)", value=0.0)
|
38 |
+
|
39 |
+
output = gr.Textbox(label="Recommendation", interactive=False)
|
40 |
+
|
41 |
+
gr.Button("Predict").click(
|
42 |
+
predict_crop,
|
43 |
+
inputs=[N, P, K, temperature, humidity, pH, rainfall],
|
44 |
+
outputs=output
|
45 |
+
)
|
46 |
+
|
47 |
+
return demo
|
48 |
+
|
49 |
+
# Run the Gradio app
|
50 |
+
if __name__ == "__main__":
|
51 |
+
app = main_interface()
|
52 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.2.2
|
2 |
+
numpy==1.26.4
|