Novadotgg commited on
Commit
fcc0b94
ยท
1 Parent(s): 5377661

Bugfix: Fix soil advice multi-turn conversation logic to prevent session hijacking

Browse files
Files changed (1) hide show
  1. executable_code/app.py +19 -6
executable_code/app.py CHANGED
@@ -147,8 +147,11 @@ def chatbot_route():
147
  return handle_soil_conditions(user_message)
148
 
149
  # Check for active session context
150
- if session.get('intent') == 'recommend_crop':
 
151
  return recommend_crop(user_message)
 
 
152
 
153
  # Determine Intent via ML if no clear keyword found
154
  intent_label = classify_intent(user_message)
@@ -340,6 +343,7 @@ def recommend_crop(user_message):
340
 
341
  def handle_soil_conditions(user_message):
342
  try:
 
343
  crop_list = ["bajra", "barley", "turmeric", "tur", "sugarcane", "soybeans",
344
  "ragi", "potato", "onion", "maize", "ladyfinger", "jute",
345
  "jowar", "green gram", "cotton", "coffee", "chickpea",
@@ -347,24 +351,29 @@ def handle_soil_conditions(user_message):
347
 
348
  user_message_lower = user_message.lower()
349
 
350
- # Try to find crop first
351
- crop_found = None
352
  for c in crop_list:
353
  if c in user_message_lower:
354
  crop_found = c
 
355
  break
356
 
357
  if not crop_found:
358
  return jsonify({'reply': "๐ŸŒฟ Which crop would you like soil advice for? (e.g., 'soil for rice')", 'status': 'need_crop'})
359
 
360
- # Try to find location (look for 'in [location]')
361
  location = None
 
362
  if ' in ' in user_message_lower:
363
  parts = user_message_lower.split(' in ')
364
  location = parts[1].strip()
 
 
 
365
 
366
  if not location:
367
- return jsonify({'reply': f"๐Ÿ“ I've got {crop_found.title()}. Now, which <b>location</b> (district) are you planting in?", 'status': 'need_location'})
368
 
369
  temperature, humidity, rainfall = fetch_weather_data(location)
370
 
@@ -391,6 +400,10 @@ def handle_soil_conditions(user_message):
391
  f"<br>๐Ÿ“Š <b>Soil Balance Chart:</b>"
392
  )
393
 
 
 
 
 
394
  return jsonify({
395
  'reply': reply_message,
396
  'status': 'success',
@@ -398,7 +411,7 @@ def handle_soil_conditions(user_message):
398
  'labels': ["Nitrogen", "Phosphorus", "Potassium", "pH Value"],
399
  'values': vals
400
  },
401
- 'chart_type': 'soil_requirement'
402
  })
403
 
404
  except Exception as e:
 
147
  return handle_soil_conditions(user_message)
148
 
149
  # Check for active session context
150
+ intent = session.get('intent')
151
+ if intent == 'recommend_crop':
152
  return recommend_crop(user_message)
153
+ elif intent == 'soil_advice':
154
+ return handle_soil_conditions(user_message)
155
 
156
  # Determine Intent via ML if no clear keyword found
157
  intent_label = classify_intent(user_message)
 
343
 
344
  def handle_soil_conditions(user_message):
345
  try:
346
+ session['intent'] = 'soil_advice'
347
  crop_list = ["bajra", "barley", "turmeric", "tur", "sugarcane", "soybeans",
348
  "ragi", "potato", "onion", "maize", "ladyfinger", "jute",
349
  "jowar", "green gram", "cotton", "coffee", "chickpea",
 
351
 
352
  user_message_lower = user_message.lower()
353
 
354
+ # 1. Attempt to find a crop in the current message or retrieve from session
355
+ crop_found = session.get('soil_crop')
356
  for c in crop_list:
357
  if c in user_message_lower:
358
  crop_found = c
359
+ session['soil_crop'] = c
360
  break
361
 
362
  if not crop_found:
363
  return jsonify({'reply': "๐ŸŒฟ Which crop would you like soil advice for? (e.g., 'soil for rice')", 'status': 'need_crop'})
364
 
365
+ # 2. Attempt to find location
366
  location = None
367
+ # Check if the user used the 'in [location]' format
368
  if ' in ' in user_message_lower:
369
  parts = user_message_lower.split(' in ')
370
  location = parts[1].strip()
371
+ # If no 'in', and we ALREADY had the crop, treat the whole message as the location
372
+ elif session.get('soil_crop') and user_message_lower not in ['soil', 'soil analysis', session.get('soil_crop')]:
373
+ location = user_message.strip()
374
 
375
  if not location:
376
+ return jsonify({'reply': f"๐Ÿ“ I've got <b>{crop_found.title()}</b>. Now, which <b>location</b> (district) are you planting in?", 'status': 'need_location'})
377
 
378
  temperature, humidity, rainfall = fetch_weather_data(location)
379
 
 
400
  f"<br>๐Ÿ“Š <b>Soil Balance Chart:</b>"
401
  )
402
 
403
+ # Clear intent after success
404
+ session.pop('intent', None)
405
+ session.pop('soil_crop', None)
406
+
407
  return jsonify({
408
  'reply': reply_message,
409
  'status': 'success',
 
411
  'labels': ["Nitrogen", "Phosphorus", "Potassium", "pH Value"],
412
  'values': vals
413
  },
414
+ 'chart_type': 'input_analysis'
415
  })
416
 
417
  except Exception as e: