Rajkhanke007 commited on
Commit
467cf44
·
verified ·
1 Parent(s): 28909e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -81
app.py CHANGED
@@ -1,81 +1,83 @@
1
- from flask import Flask, render_template, request
2
- import joblib
3
- import pandas as pd
4
- import google.generativeai as genai
5
-
6
-
7
- app = Flask(__name__)
8
-
9
- # Load the trained Random Forest models
10
- rf_ferti_name = joblib.load('rf_ferti_name.pkl')
11
- rf_ferti_value = joblib.load('rf_ferti_value.pkl')
12
-
13
- # Manually define the encodings based on the provided dictionaries
14
- soil_type_encodings = {'Black': 0, 'Clayey': 1, 'Loamy': 2, 'Red': 3, 'Sandy': 4}
15
- crop_type_encodings = {'Barley': 0, 'Cotton': 1, 'Ground Nuts': 2, 'Maize': 3, 'Millets': 4,
16
- 'Oil seeds': 5, 'Other Variety': 6, 'Paddy': 7, 'Pulses': 8, 'Sugarcane': 9,
17
- 'Tobacco': 10, 'Wheat': 11}
18
- fertilizer_name_encodings = {'10-26-26': 0, '14-35-14': 1, '15-15-15': 2, '17-17-17': 3, '20-20': 4,
19
- '20-20-20': 5, '28-28': 6, 'Ammonium sulfate': 7, 'Biofertilizer (e.g., Rhizobium)': 8,
20
- 'Calcium nitrate': 9, 'DAP': 10, 'Ferrous sulfate': 11, 'Magnesium sulfate': 12,
21
- 'Potassium chloride/Muriate of potash (MOP)': 13, 'Potassium sulfate/Sulfate of potash (SOP)': 14,
22
- 'Rock phosphate (RP)': 15, 'Single superphosphate (SSP)': 16, 'Triple superphosphate (TSP)': 17,
23
- 'Urea': 18, 'Zinc sulfate': 19}
24
-
25
-
26
- # AI configuration
27
- genai.configure(api_key='AIzaSyBtXV2xJbrWVV57B5RWy_meKXOA59HFMeY')
28
- model = genai.GenerativeModel("gemini-1.5-flash")
29
-
30
- def generate_ai_suggestions(pred_fertilizer_name):
31
- prompt = (
32
- f"For {pred_fertilizer_name} fertlizer, generate 3-4 sentences each on a new line, note text shoudl be jsutidied should not contian anyu special character"
33
- )
34
- response = model.generate_content(prompt)
35
- return response.text
36
-
37
-
38
-
39
- @app.route('/', methods=['GET', 'POST'])
40
- def index():
41
- if request.method == 'POST':
42
- # Retrieve form data
43
- temperature = float(request.form['temperature'])
44
- humidity = float(request.form['humidity'])
45
- moisture = float(request.form['moisture'])
46
- soil_type = request.form['soil_type']
47
- crop_type = request.form['crop_type']
48
- nitrogen = float(request.form['nitrogen'])
49
- potassium = float(request.form['potassium'])
50
- phosphorous = float(request.form['phosphorous'])
51
-
52
- # Encode categorical data
53
- soil_type_encoded = soil_type_encodings.get(soil_type, -1)
54
- crop_type_encoded = crop_type_encodings.get(crop_type, -1)
55
-
56
- # Create a DataFrame for the input
57
- user_input = pd.DataFrame({
58
- 'Temperature': [temperature],
59
- 'Humidity': [humidity],
60
- 'Moisture': [moisture],
61
- 'Nitrogen': [nitrogen],
62
- 'Potassium': [potassium],
63
- 'Phosphorous': [phosphorous],
64
- 'Soil Type': [soil_type_encoded],
65
- 'Crop Type': [crop_type_encoded]
66
- })
67
-
68
- # Predict Fertilizer Name
69
- pred_fertilizer_name = rf_ferti_name.predict(user_input)[0]
70
- pred_fertilizer_name = [name for name, value in fertilizer_name_encodings.items() if value == pred_fertilizer_name][0]
71
-
72
- # Predict Fertilizer Quantity
73
- pred_fertilizer_qty = rf_ferti_value.predict(user_input)[0]
74
- pred_info = generate_ai_suggestions(pred_fertilizer_name)
75
-
76
- return render_template('index.html', prediction=True, fertilizer_name=pred_fertilizer_name,
77
- fertilizer_qty=pred_fertilizer_qty, optimal_usage=pred_fertilizer_qty,pred_info=pred_info)
78
- return render_template('index.html', prediction=False)
79
-
80
- if __name__ == '__main__':
81
- app.run(debug=True)
 
 
 
1
+ from flask import Flask, render_template, request
2
+ import joblib
3
+ import pandas as pd
4
+ import google.generativeai as genai
5
+ import os
6
+
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Load the trained Random Forest models
11
+ rf_ferti_name = joblib.load('rf_ferti_name.pkl')
12
+ rf_ferti_value = joblib.load('rf_ferti_value.pkl')
13
+
14
+ # Manually define the encodings based on the provided dictionaries
15
+ soil_type_encodings = {'Black': 0, 'Clayey': 1, 'Loamy': 2, 'Red': 3, 'Sandy': 4}
16
+ crop_type_encodings = {'Barley': 0, 'Cotton': 1, 'Ground Nuts': 2, 'Maize': 3, 'Millets': 4,
17
+ 'Oil seeds': 5, 'Other Variety': 6, 'Paddy': 7, 'Pulses': 8, 'Sugarcane': 9,
18
+ 'Tobacco': 10, 'Wheat': 11}
19
+ fertilizer_name_encodings = {'10-26-26': 0, '14-35-14': 1, '15-15-15': 2, '17-17-17': 3, '20-20': 4,
20
+ '20-20-20': 5, '28-28': 6, 'Ammonium sulfate': 7, 'Biofertilizer (e.g., Rhizobium)': 8,
21
+ 'Calcium nitrate': 9, 'DAP': 10, 'Ferrous sulfate': 11, 'Magnesium sulfate': 12,
22
+ 'Potassium chloride/Muriate of potash (MOP)': 13, 'Potassium sulfate/Sulfate of potash (SOP)': 14,
23
+ 'Rock phosphate (RP)': 15, 'Single superphosphate (SSP)': 16, 'Triple superphosphate (TSP)': 17,
24
+ 'Urea': 18, 'Zinc sulfate': 19}
25
+
26
+
27
+ # AI configuration
28
+ api_key=os.getenv('GEMINI_API')
29
+ genai.configure(api_key=api_key)
30
+ model = genai.GenerativeModel("gemini-1.5-flash")
31
+
32
+ def generate_ai_suggestions(pred_fertilizer_name):
33
+ prompt = (
34
+ f"For {pred_fertilizer_name} fertlizer, generate 3-4 sentences each on a new line, note text shoudl be jsutidied should not contian anyu special character"
35
+ )
36
+ response = model.generate_content(prompt)
37
+ return response.text
38
+
39
+
40
+
41
+ @app.route('/', methods=['GET', 'POST'])
42
+ def index():
43
+ if request.method == 'POST':
44
+ # Retrieve form data
45
+ temperature = float(request.form['temperature'])
46
+ humidity = float(request.form['humidity'])
47
+ moisture = float(request.form['moisture'])
48
+ soil_type = request.form['soil_type']
49
+ crop_type = request.form['crop_type']
50
+ nitrogen = float(request.form['nitrogen'])
51
+ potassium = float(request.form['potassium'])
52
+ phosphorous = float(request.form['phosphorous'])
53
+
54
+ # Encode categorical data
55
+ soil_type_encoded = soil_type_encodings.get(soil_type, -1)
56
+ crop_type_encoded = crop_type_encodings.get(crop_type, -1)
57
+
58
+ # Create a DataFrame for the input
59
+ user_input = pd.DataFrame({
60
+ 'Temperature': [temperature],
61
+ 'Humidity': [humidity],
62
+ 'Moisture': [moisture],
63
+ 'Nitrogen': [nitrogen],
64
+ 'Potassium': [potassium],
65
+ 'Phosphorous': [phosphorous],
66
+ 'Soil Type': [soil_type_encoded],
67
+ 'Crop Type': [crop_type_encoded]
68
+ })
69
+
70
+ # Predict Fertilizer Name
71
+ pred_fertilizer_name = rf_ferti_name.predict(user_input)[0]
72
+ pred_fertilizer_name = [name for name, value in fertilizer_name_encodings.items() if value == pred_fertilizer_name][0]
73
+
74
+ # Predict Fertilizer Quantity
75
+ pred_fertilizer_qty = rf_ferti_value.predict(user_input)[0]
76
+ pred_info = generate_ai_suggestions(pred_fertilizer_name)
77
+
78
+ return render_template('index.html', prediction=True, fertilizer_name=pred_fertilizer_name,
79
+ fertilizer_qty=pred_fertilizer_qty, optimal_usage=pred_fertilizer_qty,pred_info=pred_info)
80
+ return render_template('index.html', prediction=False)
81
+
82
+ if __name__ == '__main__':
83
+ app.run(debug=True)