Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,73 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify
|
| 2 |
-
import joblib
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
f"
|
| 30 |
-
f"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
'
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize the Flask app
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Load the trained model
|
| 10 |
+
gbm_model = joblib.load('gbm_model.pkl')
|
| 11 |
+
|
| 12 |
+
api_key=os.getenv('GEMINI_API')
|
| 13 |
+
# Configure Gemini AI
|
| 14 |
+
genai.configure(api_key=api_key)
|
| 15 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 16 |
+
|
| 17 |
+
# Mapping for class decoding
|
| 18 |
+
class_mapping = {
|
| 19 |
+
0: 'BANANA', 1: 'BLACKGRAM', 2: 'CHICKPEA', 3: 'COCONUT', 4: 'COFFEE',
|
| 20 |
+
5: 'COTTON', 6: 'JUTE', 7: 'KIDNEYBEANS', 8: 'LENTIL', 9: 'MAIZE',
|
| 21 |
+
10: 'MANGO', 11: 'MOTHBEANS', 12: 'MUNGBEAN', 13: 'MUSKMELON',
|
| 22 |
+
14: 'ORANGE', 15: 'PAPAYA', 16: 'PIGEONPEAS', 17: 'POMEGRANATE',
|
| 23 |
+
18: 'RICE', 19: 'WATERMELON'
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# AI suggestions from Gemini
|
| 27 |
+
def generate_ai_suggestions(pred_crop_name, parameters):
|
| 28 |
+
prompt = (
|
| 29 |
+
f"For the crop {pred_crop_name} based on the input parameters {parameters}, "
|
| 30 |
+
f"Give descritpion of provided crop in justified 3-4 line sparagraph."
|
| 31 |
+
f"After that spacing of one to two lines"
|
| 32 |
+
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."
|
| 33 |
+
)
|
| 34 |
+
response = model.generate_content(prompt)
|
| 35 |
+
return response.text
|
| 36 |
+
|
| 37 |
+
@app.route('/')
|
| 38 |
+
def index():
|
| 39 |
+
return render_template('index.html')
|
| 40 |
+
|
| 41 |
+
@app.route('/predict', methods=['POST'])
|
| 42 |
+
def predict():
|
| 43 |
+
# Get input values from the form
|
| 44 |
+
nitrogen = float(request.form['nitrogen'])
|
| 45 |
+
phosphorus = float(request.form['phosphorus'])
|
| 46 |
+
potassium = float(request.form['potassium'])
|
| 47 |
+
temperature = float(request.form['temperature'])
|
| 48 |
+
humidity = float(request.form['humidity'])
|
| 49 |
+
ph = float(request.form['ph'])
|
| 50 |
+
rainfall = float(request.form['rainfall'])
|
| 51 |
+
location = request.form['location']
|
| 52 |
+
|
| 53 |
+
# Prepare the features for the model
|
| 54 |
+
features = [[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]]
|
| 55 |
+
predicted_crop_encoded = gbm_model.predict(features)[0]
|
| 56 |
+
predicted_crop = class_mapping[predicted_crop_encoded]
|
| 57 |
+
|
| 58 |
+
# Get AI suggestions from Gemini
|
| 59 |
+
parameters = {
|
| 60 |
+
"Nitrogen": nitrogen, "Phosphorus": phosphorus, "Potassium": potassium,
|
| 61 |
+
"Temperature": temperature, "Humidity": humidity, "pH": ph, "Rainfall": rainfall,
|
| 62 |
+
"Location": location
|
| 63 |
+
}
|
| 64 |
+
ai_suggestions = generate_ai_suggestions(predicted_crop, parameters)
|
| 65 |
+
|
| 66 |
+
return jsonify({
|
| 67 |
+
'predicted_crop': predicted_crop,
|
| 68 |
+
'ai_suggestions': ai_suggestions,
|
| 69 |
+
'location': location
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
app.run(port=7860,host='0.0.0.0')
|