Upload 3 files
Browse files- app.py +71 -0
- gbm_model.pkl +3 -0
- templates/index.html +153 -0
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
|
| 5 |
+
# Initialize the Flask app
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
gbm_model = joblib.load('gbm_model.pkl')
|
| 10 |
+
|
| 11 |
+
# Configure Gemini AI
|
| 12 |
+
genai.configure(api_key='AIzaSyBtXV2xJbrWVV57B5RWy_meKXOA59HFMeY')
|
| 13 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 14 |
+
|
| 15 |
+
# Mapping for class decoding
|
| 16 |
+
class_mapping = {
|
| 17 |
+
0: 'BANANA', 1: 'BLACKGRAM', 2: 'CHICKPEA', 3: 'COCONUT', 4: 'COFFEE',
|
| 18 |
+
5: 'COTTON', 6: 'JUTE', 7: 'KIDNEYBEANS', 8: 'LENTIL', 9: 'MAIZE',
|
| 19 |
+
10: 'MANGO', 11: 'MOTHBEANS', 12: 'MUNGBEAN', 13: 'MUSKMELON',
|
| 20 |
+
14: 'ORANGE', 15: 'PAPAYA', 16: 'PIGEONPEAS', 17: 'POMEGRANATE',
|
| 21 |
+
18: 'RICE', 19: 'WATERMELON'
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# AI suggestions from Gemini
|
| 25 |
+
def generate_ai_suggestions(pred_crop_name, parameters):
|
| 26 |
+
prompt = (
|
| 27 |
+
f"For the crop {pred_crop_name} based on the input parameters {parameters}, "
|
| 28 |
+
f"Give descritpion of provided crop in justified 3-4 line sparagraph."
|
| 29 |
+
f"After that spacing of one to two lines"
|
| 30 |
+
f"**in the next line** recokemnd foru other crops based on parpameeters as Other recommended crops : crop names in numbvered order. dont include any special character not bold,italic."
|
| 31 |
+
)
|
| 32 |
+
response = model.generate_content(prompt)
|
| 33 |
+
return response.text
|
| 34 |
+
|
| 35 |
+
@app.route('/')
|
| 36 |
+
def index():
|
| 37 |
+
return render_template('index.html')
|
| 38 |
+
|
| 39 |
+
@app.route('/predict', methods=['POST'])
|
| 40 |
+
def predict():
|
| 41 |
+
# Get input values from the form
|
| 42 |
+
nitrogen = float(request.form['nitrogen'])
|
| 43 |
+
phosphorus = float(request.form['phosphorus'])
|
| 44 |
+
potassium = float(request.form['potassium'])
|
| 45 |
+
temperature = float(request.form['temperature'])
|
| 46 |
+
humidity = float(request.form['humidity'])
|
| 47 |
+
ph = float(request.form['ph'])
|
| 48 |
+
rainfall = float(request.form['rainfall'])
|
| 49 |
+
location = request.form['location']
|
| 50 |
+
|
| 51 |
+
# Prepare the features for the model
|
| 52 |
+
features = [[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]]
|
| 53 |
+
predicted_crop_encoded = gbm_model.predict(features)[0]
|
| 54 |
+
predicted_crop = class_mapping[predicted_crop_encoded]
|
| 55 |
+
|
| 56 |
+
# Get AI suggestions from Gemini
|
| 57 |
+
parameters = {
|
| 58 |
+
"Nitrogen": nitrogen, "Phosphorus": phosphorus, "Potassium": potassium,
|
| 59 |
+
"Temperature": temperature, "Humidity": humidity, "pH": ph, "Rainfall": rainfall,
|
| 60 |
+
"Location": location
|
| 61 |
+
}
|
| 62 |
+
ai_suggestions = generate_ai_suggestions(predicted_crop, parameters)
|
| 63 |
+
|
| 64 |
+
return jsonify({
|
| 65 |
+
'predicted_crop': predicted_crop,
|
| 66 |
+
'ai_suggestions': ai_suggestions,
|
| 67 |
+
'location': location
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
app.run(debug=True)
|
gbm_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:26efba0486acca04b67d21668ceca6ac25af9a575df6ef209076f9ac4bb4dc0c
|
| 3 |
+
size 3554972
|
templates/index.html
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Crop Recommendation System</title>
|
| 7 |
+
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
|
| 8 |
+
<style>
|
| 9 |
+
body {
|
| 10 |
+
background-color: #eef7ee;
|
| 11 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 12 |
+
}
|
| 13 |
+
.container {
|
| 14 |
+
margin: 50px auto;
|
| 15 |
+
padding: 30px;
|
| 16 |
+
border: 2px solid #28a745;
|
| 17 |
+
border-radius: 15px;
|
| 18 |
+
max-width: 1200px;
|
| 19 |
+
background-color: #f9fff9;
|
| 20 |
+
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
|
| 21 |
+
}
|
| 22 |
+
h1 {
|
| 23 |
+
color: #28a745;
|
| 24 |
+
font-weight: bold;
|
| 25 |
+
text-align: center;
|
| 26 |
+
margin-bottom: 30px;
|
| 27 |
+
}
|
| 28 |
+
.input-section {
|
| 29 |
+
display: flex;
|
| 30 |
+
justify-content: space-between;
|
| 31 |
+
align-items: flex-start;
|
| 32 |
+
}
|
| 33 |
+
.form-group {
|
| 34 |
+
width: 60%;
|
| 35 |
+
}
|
| 36 |
+
.form-label {
|
| 37 |
+
display: block;
|
| 38 |
+
margin-top: 15px;
|
| 39 |
+
font-weight: bold;
|
| 40 |
+
}
|
| 41 |
+
.form-control {
|
| 42 |
+
width: 100%;
|
| 43 |
+
}
|
| 44 |
+
.image-section {
|
| 45 |
+
width: 35%;
|
| 46 |
+
display: flex;
|
| 47 |
+
justify-content: center;
|
| 48 |
+
align-items: center;
|
| 49 |
+
margin-top: 50px; /* Moves the image section down */
|
| 50 |
+
margin-left: -30px; /* Aligns the image section a little to the left */
|
| 51 |
+
}
|
| 52 |
+
.image-section img {
|
| 53 |
+
max-width: 100%;
|
| 54 |
+
height: 580px;
|
| 55 |
+
border-radius: 10px;
|
| 56 |
+
border: 3px solid #28a745; /* Green border around the image */
|
| 57 |
+
}
|
| 58 |
+
.btn-predict {
|
| 59 |
+
background-color: #28a745;
|
| 60 |
+
color: white;
|
| 61 |
+
font-weight: bold;
|
| 62 |
+
margin-top: 20px;
|
| 63 |
+
width: 100%;
|
| 64 |
+
}
|
| 65 |
+
.result-container {
|
| 66 |
+
margin-top: 30px;
|
| 67 |
+
border: 2px solid #28a745;
|
| 68 |
+
padding: 20px;
|
| 69 |
+
background-color: #e3ffe3;
|
| 70 |
+
border-radius: 10px;
|
| 71 |
+
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
|
| 72 |
+
}
|
| 73 |
+
.result-container h3 {
|
| 74 |
+
color: #28a745;
|
| 75 |
+
text-align: center;
|
| 76 |
+
}
|
| 77 |
+
.ai-suggestions {
|
| 78 |
+
margin-top: 20px;
|
| 79 |
+
background-color: #ffffff;
|
| 80 |
+
padding: 15px;
|
| 81 |
+
border-radius: 10px;
|
| 82 |
+
border: 1px solid #28a745;
|
| 83 |
+
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
|
| 84 |
+
text-align: left;
|
| 85 |
+
}
|
| 86 |
+
</style>
|
| 87 |
+
</head>
|
| 88 |
+
<body>
|
| 89 |
+
<div class="container">
|
| 90 |
+
<h1>Crop Recommendation System</h1>
|
| 91 |
+
<form id="cropForm" method="POST">
|
| 92 |
+
<div class="input-section">
|
| 93 |
+
<div class="form-group">
|
| 94 |
+
<label class="form-label" for="location">Enter Location</label>
|
| 95 |
+
<input type="text" class="form-control" id="location" name="location" required>
|
| 96 |
+
|
| 97 |
+
<label class="form-label" for="nitrogen">Enter Nitrogen (N)</label>
|
| 98 |
+
<input type="text" class="form-control" id="nitrogen" name="nitrogen" required>
|
| 99 |
+
|
| 100 |
+
<label class="form-label" for="phosphorus">Enter Phosphorus (p)</label>
|
| 101 |
+
<input type="text" class="form-control" id="phosphorus" name="phosphorus" required>
|
| 102 |
+
|
| 103 |
+
<label class="form-label" for="potassium">Enter Potassium (k)</label>
|
| 104 |
+
<input type="text" class="form-control" id="potassium" name="potassium" required>
|
| 105 |
+
|
| 106 |
+
<label class="form-label" for="temperature">Enter Temperature (°C)</label>
|
| 107 |
+
<input type="text" class="form-control" id="temperature" name="temperature" required>
|
| 108 |
+
|
| 109 |
+
<label class="form-label" for="humidity">Enter Humidity (%)</label>
|
| 110 |
+
<input type="text" class="form-control" id="humidity" name="humidity" required>
|
| 111 |
+
|
| 112 |
+
<label class="form-label" for="ph">Enter pH Level</label>
|
| 113 |
+
<input type="text" class="form-control" id="ph" name="ph" required>
|
| 114 |
+
|
| 115 |
+
<label class="form-label" for="rainfall">Enter Rainfall (mm)</label>
|
| 116 |
+
<input type="text" class="form-control" id="rainfall" name="rainfall" required>
|
| 117 |
+
</div>
|
| 118 |
+
<div class="image-section">
|
| 119 |
+
<img src="https://www.shutterstock.com/image-photo/young-corn-plants-growing-on-600nw-2299683499.jpg" alt="Crop Image">
|
| 120 |
+
</div>
|
| 121 |
+
</div>
|
| 122 |
+
<button type="submit" class="btn btn-predict">Predict Crop</button>
|
| 123 |
+
</form>
|
| 124 |
+
|
| 125 |
+
<!-- Results will be shown here -->
|
| 126 |
+
<div class="result-container" id="resultContainer" style="display:none;">
|
| 127 |
+
<h3>Predicted Crop: <span id="predictedCrop"></span></h3>
|
| 128 |
+
<div class="ai-suggestions" id="aiSuggestions"></div>
|
| 129 |
+
</div>
|
| 130 |
+
</div>
|
| 131 |
+
|
| 132 |
+
<!-- Bootstrap and jQuery JS -->
|
| 133 |
+
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
|
| 134 |
+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
|
| 135 |
+
<script>
|
| 136 |
+
$(document).ready(function() {
|
| 137 |
+
$('#cropForm').on('submit', function(event) {
|
| 138 |
+
event.preventDefault();
|
| 139 |
+
$.ajax({
|
| 140 |
+
url: '/predict',
|
| 141 |
+
type: 'POST',
|
| 142 |
+
data: $(this).serialize(),
|
| 143 |
+
success: function(response) {
|
| 144 |
+
$('#predictedCrop').text(response.predicted_crop);
|
| 145 |
+
$('#aiSuggestions').text(response.ai_suggestions);
|
| 146 |
+
$('#resultContainer').show();
|
| 147 |
+
}
|
| 148 |
+
});
|
| 149 |
+
});
|
| 150 |
+
});
|
| 151 |
+
</script>
|
| 152 |
+
</body>
|
| 153 |
+
</html>
|