Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pickle | |
import numpy as np | |
import os | |
# Load the RandomForest model | |
working_dir = os.path.dirname(os.path.abspath(__file__)) | |
model = pickle.load(open(f'{working_dir}/RF_Crop.sav', 'rb')) | |
# Overview section content | |
overview_text = """ | |
### Welcome to the Crop Recommendation App! | |
This application assists farmers in selecting the optimal crop to cultivate, considering soil composition | |
and environmental conditions. By providing information such as nitrogen, phosphorus, and | |
potassium levels, as well as temperature, humidity, pH, and rainfall, users receive tailored | |
recommendations for the most suitable crop out of a selection of 22 options. | |
### How to Use the App | |
1. Navigate to the "Crop Recommendation" section. | |
2. Enter the values for the soil and environmental factors in the input fields. | |
3. Click the "Predict" button to get the crop recommendation. | |
### About the Model | |
The recommendation is made using a Random Forest model trained on agricultural data. | |
This model considers various factors to predict the best crop for your field. | |
The model has been developed by analyzing many models like SVM, Random Forest, | |
Decision Tree, Logistic Regression, Gaussian Naive Bayes. Random Forest has been selected based on | |
the Cross Validation Accuracy & Test Accuracy. | |
### Benefits of Using Crop Recommendation | |
- **Increased Yield**: By planting the most suitable crop, you can maximize your harvest. | |
- **Cost Efficiency**: Avoid wasting resources on crops that are not suited to your soil and climate. | |
- **Sustainable Farming**: Promote better land use and reduce environmental impact. | |
### Contact Us | |
If you have any questions or feedback about the project, feel free to reach out: | |
- **Email**: kanchanrai2307@gmail.com | |
- **Github**: [kanchanrai7](https://github.com/kanchanrai7) | |
""" | |
# Define the prediction function | |
def predict_crop(N, P, K, temperature, humidity, pH, rainfall): | |
user_input = np.array([[N, P, K, temperature, humidity, pH, rainfall]]) | |
if np.all(user_input == 0): | |
return "Please enter valid values." | |
else: | |
prediction = model.predict(user_input) | |
crop = prediction[0] | |
return f"Hey, you should grow **{crop}** based on your soil and environmental factors." | |
# Gradio UI components | |
def main_interface(): | |
with gr.Blocks() as demo: | |
with gr.Tab("Overview"): | |
gr.Markdown(overview_text) | |
gr.Image("Images/image1.jpg", label="Healthy Crops") | |
gr.Image("Images/mod_comparison.png", label="Model Comparison") | |
with gr.Tab("Get Recommendation"): | |
gr.Markdown("Enter the details about your soil and environmental factors to get a crop recommendation.") | |
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]") | |
N = gr.Number(label="Nitrogen (N)", value=0, precision=0) | |
P = gr.Number(label="Phosphorus (P)", value=0, precision=0) | |
K = gr.Number(label="Potassium (K)", value=0, precision=0) | |
temperature = gr.Number(label="Temperature (°C)", value=0.0) | |
humidity = gr.Number(label="Humidity (%)", value=0.0) | |
pH = gr.Number(label="pH", value=0.0) | |
rainfall = gr.Number(label="Rainfall (mm)", value=0.0) | |
output = gr.Textbox(label="Recommendation", interactive=False) | |
gr.Button("Predict").click( | |
predict_crop, | |
inputs=[N, P, K, temperature, humidity, pH, rainfall], | |
outputs=output | |
) | |
return demo | |
# Run the Gradio app | |
if __name__ == "__main__": | |
app = main_interface() | |
app.launch() | |