1+ import streamlit as st
2+ import pandas as pd
3+ import pickle
4+ # Load and cache your dataset
5+ @st .cache_data
6+ def load_dataset ():
7+ # Replace 'your_dataset.csv' with your actual CSV file name
8+ df = pd .read_csv ('Crop.csv' ) # Change this to your CSV file name
9+ return df
10+ # Load your trained model (adjust path as needed)
11+ @st .cache_data
12+ def load_model ():
13+ # Replace with your actual model loading code
14+ return pickle .load (open ('crop_model.pkl' , 'rb' ))
15+ # Load the dataset
16+ df = load_dataset ()
17+ model = load_model ()
18+ # Streamlit app
19+ st .title ("🌾 Crop Prediction System" )
20+ st .write ("Enter the farming conditions to predict the best crop" )
21+ # Optional: Show dataset info
22+ with st .sidebar :
23+ st .subheader ("Dataset Information" )
24+ st .write (f"Total samples: { len (df )} " )
25+ st .write (f"Features: { list (df .columns )} " )
26+ if st .checkbox ("Show sample data" ):
27+ st .dataframe (df .head ())
28+ # Create input fields for features
29+ st .subheader ("Enter Soil and Climate Conditions" )
30+ col1 , col2 = st .columns (2 )
31+ with col1 :
32+ nitrogen = st .number_input ("Nitrogen (N)" , min_value = 0 , max_value = 300 , value = 50 )
33+ phosphorus = st .number_input ("Phosphorus (P)" , min_value = 0 , max_value = 300 , value = 50 )
34+ potassium = st .number_input ("Potassium (K)" , min_value = 0 , max_value = 300 , value = 50 )
35+ with col2 :
36+ temperature = st .number_input ("Temperature (°C)" , min_value = 0.0 , max_value = 50.0 , value = 25.0 )
37+ humidity = st .slider ("Humidity (%)" , 0 , 100 , 50 )
38+ ph = st .slider ("pH Level" , 0.0 , 14.0 , 7.0 )
39+ rainfall = st .number_input ("Rainfall (mm)" , min_value = 0.0 , max_value = 500.0 , value = 100.0 )
40+ # Prediction
41+ if st .button ("🔮 Predict Best Crop" , type = "primary" ):
42+ # Create input array for prediction
43+ features = [[nitrogen , phosphorus , potassium , temperature , humidity , ph , rainfall ]]
44+ # Convert to DataFrame with correct column names (adjust based on your dataset)
45+ feature_names = ['N' , 'P' , 'K' , 'temperature' , 'humidity' , 'ph' , 'rainfall' ]
46+ input_df = pd .DataFrame (features , columns = feature_names )
47+ # Make prediction
48+ try :
49+ prediction = model .predict (input_df )
50+ # Display result
51+ st .success (f"🌱 Recommended Crop: **{ prediction [0 ]} **" )
52+ # Optional: Show prediction confidence if available
53+ if hasattr (model , 'predict_proba' ):
54+ proba = model .predict_proba (input_df )
55+ confidence = max (proba [0 ]) * 100
56+ st .info (f"Prediction Confidence: { confidence :.1f} %" )
57+ except Exception as e :
58+ st .error (f"Error making prediction: { str (e )} " )
59+ st .info ("Please check that your model file and input features are compatible" )
60+ # Additional information
61+ st .markdown ("---" )
62+ st .subheader ("About this System" )
63+ st .write ("This system uses machine learning to recommend the best crop based on soil nutrients and climate conditions." )
0 commit comments