Novadotgg commited on
Commit ·
03d5b0a
1
Parent(s): 6b87645
Fix active session override bug and feature array inflation
Browse files- executable_code/app.py +16 -22
executable_code/app.py
CHANGED
|
@@ -163,12 +163,12 @@ def chatbot_route():
|
|
| 163 |
return jsonify({'reply': random.choice(fallback_responses)})
|
| 164 |
|
| 165 |
def recommend_crop(user_message):
|
| 166 |
-
recommend_synonyms = ['recommend', 'suggest', 'advise', 'what crops can i grow', 'what should i plant', 'crop recommendation'
|
| 167 |
positive_feedback = ['thanks', 'great', 'good', 'helpful', 'useful', 'love', 'like', 'happy', 'nic']
|
| 168 |
negative_feedback = ['bad', 'not good', 'useless', 'hate', 'dislike', 'terrible', 'worst']
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
if any(word in user_message.lower() for word in recommend_synonyms)
|
| 172 |
session['intent'] = 'recommend_crop'
|
| 173 |
session['step'] = 0
|
| 174 |
session['data'] = []
|
|
@@ -222,7 +222,10 @@ def recommend_crop(user_message):
|
|
| 222 |
if session.get('intent') == 'recommend_crop':
|
| 223 |
try:
|
| 224 |
val = float(user_message)
|
| 225 |
-
session['data']
|
|
|
|
|
|
|
|
|
|
| 226 |
except ValueError:
|
| 227 |
return jsonify({'reply': "Please enter a valid numeric value for the soil reading."})
|
| 228 |
|
|
@@ -232,21 +235,14 @@ def recommend_crop(user_message):
|
|
| 232 |
return jsonify({'reply': questions[session['step'] - 2]})
|
| 233 |
else:
|
| 234 |
try:
|
| 235 |
-
#
|
| 236 |
-
|
| 237 |
-
session['
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
rain = session['weather']['rainfall']
|
| 244 |
-
|
| 245 |
-
# Use DataFrame to avoid scikit-learn un-named feature warnings
|
| 246 |
-
features_df = pd.DataFrame([[n, p, k, temp, hum, ph, rain]],
|
| 247 |
-
columns=['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall'])
|
| 248 |
-
|
| 249 |
-
predicted_crops = crop_model.predict(features_df)
|
| 250 |
session['recommendations'] = predicted_crops
|
| 251 |
|
| 252 |
if len(predicted_crops) == 0:
|
|
@@ -295,9 +291,7 @@ def recommend_crop(user_message):
|
|
| 295 |
|
| 296 |
except Exception as e:
|
| 297 |
print(f"Prediction error: {e}")
|
| 298 |
-
|
| 299 |
-
session.pop('step', None)
|
| 300 |
-
return jsonify({'reply': f'An error occurred during prediction ({str(e)}). Please type "recommend" to try again.'})
|
| 301 |
|
| 302 |
def handle_soil_conditions(user_message):
|
| 303 |
try:
|
|
|
|
| 163 |
return jsonify({'reply': random.choice(fallback_responses)})
|
| 164 |
|
| 165 |
def recommend_crop(user_message):
|
| 166 |
+
recommend_synonyms = ['recommend', 'suggest', 'advise', 'what crops can i grow', 'what should i plant', 'crop recommendation']
|
| 167 |
positive_feedback = ['thanks', 'great', 'good', 'helpful', 'useful', 'love', 'like', 'happy', 'nic']
|
| 168 |
negative_feedback = ['bad', 'not good', 'useless', 'hate', 'dislike', 'terrible', 'worst']
|
| 169 |
|
| 170 |
+
# Check if user explicitly wants to restart / start a new recommendation
|
| 171 |
+
if any(word in user_message.lower() for word in recommend_synonyms):
|
| 172 |
session['intent'] = 'recommend_crop'
|
| 173 |
session['step'] = 0
|
| 174 |
session['data'] = []
|
|
|
|
| 222 |
if session.get('intent') == 'recommend_crop':
|
| 223 |
try:
|
| 224 |
val = float(user_message)
|
| 225 |
+
if len(session['data']) >= len(questions):
|
| 226 |
+
session['data'][-1] = val # replace last value if already full
|
| 227 |
+
else:
|
| 228 |
+
session['data'].append(val)
|
| 229 |
except ValueError:
|
| 230 |
return jsonify({'reply': "Please enter a valid numeric value for the soil reading."})
|
| 231 |
|
|
|
|
| 235 |
return jsonify({'reply': questions[session['step'] - 2]})
|
| 236 |
else:
|
| 237 |
try:
|
| 238 |
+
# Prepare features for crop model
|
| 239 |
+
features = session['data'] + [
|
| 240 |
+
session['weather']['temperature'],
|
| 241 |
+
session['weather']['humidity'],
|
| 242 |
+
session['weather']['rainfall']
|
| 243 |
+
]
|
| 244 |
+
features_arr = np.array(features, dtype=float).reshape(1, -1)
|
| 245 |
+
predicted_crops = crop_model.predict(features_arr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
session['recommendations'] = predicted_crops
|
| 247 |
|
| 248 |
if len(predicted_crops) == 0:
|
|
|
|
| 291 |
|
| 292 |
except Exception as e:
|
| 293 |
print(f"Prediction error: {e}")
|
| 294 |
+
return jsonify({'reply': 'An error occurred during prediction. Please try again or check model configuration.'})
|
|
|
|
|
|
|
| 295 |
|
| 296 |
def handle_soil_conditions(user_message):
|
| 297 |
try:
|