Rajkhanke007 commited on
Commit
beab224
·
verified ·
1 Parent(s): ce5b4e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -71
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
- # 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)
 
 
 
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')