Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import logging
|
|
| 6 |
import uuid
|
| 7 |
from datetime import datetime
|
| 8 |
|
|
|
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
| 11 |
|
|
@@ -126,6 +127,7 @@ def get_menu_items():
|
|
| 126 |
if not sf:
|
| 127 |
sf = get_salesforce_connection()
|
| 128 |
if not sf:
|
|
|
|
| 129 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 130 |
|
| 131 |
data = request.json
|
|
@@ -133,56 +135,63 @@ def get_menu_items():
|
|
| 133 |
search_term = data.get('search_term', '').strip()
|
| 134 |
try:
|
| 135 |
items = []
|
|
|
|
| 136 |
if not search_term:
|
| 137 |
-
|
| 138 |
-
if dietary_preference == 'vegetarian':
|
| 139 |
-
soql += " WHERE Category__c = 'Veg'"
|
| 140 |
-
elif dietary_preference == 'non-vegetarian':
|
| 141 |
-
soql += " WHERE Category__c = 'Non-Veg'"
|
| 142 |
-
soql += " LIMIT 200"
|
| 143 |
-
result = sf.query(soql)
|
| 144 |
-
items = [
|
| 145 |
-
{
|
| 146 |
-
"name": r['Name'],
|
| 147 |
-
"image_url": r.get('Image_URL__c', ''),
|
| 148 |
-
"category": r.get('Category__c', ''),
|
| 149 |
-
"description": r.get('Description__c', 'No description'),
|
| 150 |
-
"source": "Sector_Detail__c"
|
| 151 |
-
}
|
| 152 |
-
for r in result['records'] if 'Name' in r
|
| 153 |
-
]
|
| 154 |
-
else:
|
| 155 |
-
soql_menu = "SELECT Name, Description__c, Image1__c, Ingredientsinfo__c, NutritionalInfo__c, Price__c, Sector__c, Spice_Levels__c, Veg_NonVeg__c, Category__c, Dynamic_Dish__c FROM Menu_Item__c"
|
| 156 |
-
soql_menu += f" WHERE Name LIKE '%{search_term}%' OR Ingredientsinfo__c LIKE '%{search_term}%'"
|
| 157 |
if dietary_preference == 'vegetarian':
|
| 158 |
-
|
| 159 |
elif dietary_preference == 'non-vegetarian':
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
logger.info(f"Executing SOQL query for
|
| 163 |
-
|
| 164 |
-
|
| 165 |
{
|
| 166 |
"name": record['Name'],
|
| 167 |
-
"
|
| 168 |
-
"image_url": record.get('Image1__c', ''),
|
| 169 |
-
"ingredients": record.get('Ingredientsinfo__c', ''),
|
| 170 |
-
"nutritional_info": record.get('NutritionalInfo__c', ''),
|
| 171 |
-
"price": record.get('Price__c', 0.0),
|
| 172 |
-
"sector": record.get('Sector__c', ''),
|
| 173 |
-
"spice_levels": record.get('Spice_Levels__c', ''),
|
| 174 |
-
"veg_nonveg": record.get('Veg_NonVeg__c', ''),
|
| 175 |
"category": record.get('Category__c', ''),
|
| 176 |
-
"
|
| 177 |
-
"source": "
|
| 178 |
}
|
| 179 |
-
for record in
|
| 180 |
]
|
| 181 |
-
items.extend(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
return jsonify({"menu_items": items})
|
| 183 |
except Exception as e:
|
| 184 |
-
logger.error(f"Failed to fetch items: {e}")
|
| 185 |
-
return jsonify({"error": f"Failed to fetch items: {e}"}), 500
|
| 186 |
|
| 187 |
@app.route('/get_sector_item_details', methods=['POST'])
|
| 188 |
def get_sector_item_details():
|
|
@@ -209,13 +218,14 @@ def get_sector_item_details():
|
|
| 209 |
"name": record.get('Name', ''),
|
| 210 |
"image_url": record.get('Image_URL__c', 'https://via.placeholder.com/30'),
|
| 211 |
"category": record.get('Category__c', ''),
|
| 212 |
-
"description": record.get('Description__c', 'No description available')
|
|
|
|
| 213 |
}
|
| 214 |
logger.info(f"Fetched details for '{item_name}' from Sector_Detail__c")
|
| 215 |
return jsonify({"item_details": item_details})
|
| 216 |
except Exception as e:
|
| 217 |
-
logger.error(f"Failed to fetch item details from Sector_Detail__c: {e}")
|
| 218 |
-
return jsonify({"error": f"Failed to fetch item details: {e}"}), 500
|
| 219 |
|
| 220 |
@app.route('/suggest_items', methods=['POST'])
|
| 221 |
def suggest_items():
|
|
@@ -240,8 +250,8 @@ def suggest_items():
|
|
| 240 |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
| 241 |
return jsonify({"suggestions": suggestions})
|
| 242 |
except Exception as e:
|
| 243 |
-
logger.error(f"Failed to fetch suggestions: {e}")
|
| 244 |
-
return jsonify({"error": f"Failed to fetch suggestions: {e}"}), 500
|
| 245 |
|
| 246 |
@app.route('/get_item_details', methods=['POST'])
|
| 247 |
def get_item_details():
|
|
@@ -266,13 +276,14 @@ def get_item_details():
|
|
| 266 |
record = result['records'][0]
|
| 267 |
item_details = {
|
| 268 |
"name": record.get('Ingredient_Name__c', ''),
|
| 269 |
-
"image_url": record.get('Image_URL__c', '')
|
|
|
|
| 270 |
}
|
| 271 |
-
logger.info(f"Fetched details for '{item_name}'")
|
| 272 |
return jsonify({"item_details": item_details})
|
| 273 |
except Exception as e:
|
| 274 |
-
logger.error(f"Failed to fetch item details: {e}")
|
| 275 |
-
return jsonify({"error": f"Failed to fetch item details: {e}"}), 500
|
| 276 |
|
| 277 |
@app.route('/submit_items', methods=['POST'])
|
| 278 |
def submit_items():
|
|
@@ -301,10 +312,11 @@ def submit_items():
|
|
| 301 |
'Quantity__c': item.get('quantity', 1)
|
| 302 |
})
|
| 303 |
|
|
|
|
| 304 |
return jsonify({"success": f"Submitted {len(items)} items under {ingredient_name}", "ingredient_name": ingredient_name})
|
| 305 |
except Exception as e:
|
| 306 |
-
logger.error(f"Failed to submit items: {e}")
|
| 307 |
-
return jsonify({"error": f"Failed to submit items: {e}"}), 500
|
| 308 |
|
| 309 |
@app.route('/get_item_info', methods=['POST'])
|
| 310 |
def get_item_info():
|
|
@@ -317,7 +329,7 @@ def get_item_info():
|
|
| 317 |
soql = f"SELECT Name, Description__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
| 318 |
result = sf.query(soql)
|
| 319 |
if result['totalSize'] == 0:
|
| 320 |
-
return jsonify({"error": f"No item found matching '{item_name}'"}), 404
|
| 321 |
|
| 322 |
record = result['records'][0]
|
| 323 |
base_info = record.get('Description__c', 'No description available')
|
|
@@ -339,8 +351,8 @@ def get_item_info():
|
|
| 339 |
logger.info(f"Generated info for '{item_name}'")
|
| 340 |
return jsonify({"item_info": item_info})
|
| 341 |
except Exception as e:
|
| 342 |
-
logger.error(f"Failed to get item info: {e}")
|
| 343 |
-
return jsonify({"error": f"Failed to get item info: {e}"}), 500
|
| 344 |
|
| 345 |
@app.route('/chat', methods=['POST'])
|
| 346 |
def chat():
|
|
@@ -351,7 +363,7 @@ def chat():
|
|
| 351 |
|
| 352 |
response = get_chatgpt_response(user_message, session_id)
|
| 353 |
logger.info(f"Chat response sent: {response}")
|
| 354 |
-
return jsonify({"response": response
|
| 355 |
|
| 356 |
if __name__ == '__main__':
|
| 357 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 6 |
import uuid
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
+
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
| 127 |
if not sf:
|
| 128 |
sf = get_salesforce_connection()
|
| 129 |
if not sf:
|
| 130 |
+
logger.error("Salesforce connection failed after retry")
|
| 131 |
return jsonify({"error": "Unable to connect to Salesforce"}), 500
|
| 132 |
|
| 133 |
data = request.json
|
|
|
|
| 135 |
search_term = data.get('search_term', '').strip()
|
| 136 |
try:
|
| 137 |
items = []
|
| 138 |
+
# Query Sector_Detail__c for dietary preferences
|
| 139 |
if not search_term:
|
| 140 |
+
soql_sector = "SELECT Name, Image_URL__c, Category__c, Description__c FROM Sector_Detail__c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
if dietary_preference == 'vegetarian':
|
| 142 |
+
soql_sector += " WHERE Category__c = 'Veg'"
|
| 143 |
elif dietary_preference == 'non-vegetarian':
|
| 144 |
+
soql_sector += " WHERE Category__c = 'Non-Veg'"
|
| 145 |
+
soql_sector += " LIMIT 200"
|
| 146 |
+
logger.info(f"Executing SOQL query for Sector_Detail__c: {soql_sector}")
|
| 147 |
+
result_sector = sf.query(soql_sector)
|
| 148 |
+
sector_items = [
|
| 149 |
{
|
| 150 |
"name": record['Name'],
|
| 151 |
+
"image_url": record.get('Image_URL__c', ''),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
"category": record.get('Category__c', ''),
|
| 153 |
+
"description": record.get('Description__c', 'No description available'),
|
| 154 |
+
"source": "Sector_Detail__c"
|
| 155 |
}
|
| 156 |
+
for record in result_sector['records'] if 'Name' in record
|
| 157 |
]
|
| 158 |
+
items.extend(sector_items)
|
| 159 |
+
|
| 160 |
+
# Query Menu_Item__c
|
| 161 |
+
soql_menu = "SELECT Name, Description__c, Image1__c, Ingredientsinfo__c, NutritionalInfo__c, Price__c, Sector__c, Spice_Levels__c, Veg_NonVeg__c, Category__c, Dynamic_Dish__c FROM Menu_Item__c"
|
| 162 |
+
if search_term:
|
| 163 |
+
soql_menu += f" WHERE Name LIKE '%{search_term}%' OR Ingredientsinfo__c LIKE '%{search_term}%'"
|
| 164 |
+
elif dietary_preference == 'vegetarian':
|
| 165 |
+
soql_menu += " WHERE Veg_NonVeg__c = 'Vegetarian'"
|
| 166 |
+
elif dietary_preference == 'non-vegetarian':
|
| 167 |
+
soql_menu += " WHERE Veg_NonVeg__c = 'Non-Vegetarian'"
|
| 168 |
+
soql_menu += " LIMIT 200"
|
| 169 |
+
logger.info(f"Executing SOQL query for Menu_Item__c: {soql_menu}")
|
| 170 |
+
result_menu = sf.query(soql_menu)
|
| 171 |
+
menu_items = [
|
| 172 |
+
{
|
| 173 |
+
"name": record['Name'],
|
| 174 |
+
"description": record.get('Description__c', 'No description available'),
|
| 175 |
+
"image_url": record.get('Image1__c', ''),
|
| 176 |
+
"ingredients": record.get('Ingredientsinfo__c', ''),
|
| 177 |
+
"nutritional_info": record.get('NutritionalInfo__c', ''),
|
| 178 |
+
"price": record.get('Price__c', 0.0),
|
| 179 |
+
"sector": record.get('Sector__c', ''),
|
| 180 |
+
"spice_levels": record.get('Spice_Levels__c', ''),
|
| 181 |
+
"veg_nonveg": record.get('Veg_NonVeg__c', ''),
|
| 182 |
+
"category": record.get('Category__c', ''),
|
| 183 |
+
"dynamic_dish": record.get('Dynamic_Dish__c', False),
|
| 184 |
+
"source": "Menu_Item__c"
|
| 185 |
+
}
|
| 186 |
+
for record in result_menu['records'] if 'Name' in record
|
| 187 |
+
]
|
| 188 |
+
items.extend(menu_items)
|
| 189 |
+
|
| 190 |
+
logger.info(f"Fetched {len(items)} items (Sector_Detail__c: {len([i for i in items if i['source'] == 'Sector_Detail__c'])}, Menu_Item__c: {len([i for i in items if i['source'] == 'Menu_Item__c'])}) for {dietary_preference or search_term}")
|
| 191 |
return jsonify({"menu_items": items})
|
| 192 |
except Exception as e:
|
| 193 |
+
logger.error(f"Failed to fetch items: {str(e)}")
|
| 194 |
+
return jsonify({"error": f"Failed to fetch items from Salesforce: {str(e)}"}), 500
|
| 195 |
|
| 196 |
@app.route('/get_sector_item_details', methods=['POST'])
|
| 197 |
def get_sector_item_details():
|
|
|
|
| 218 |
"name": record.get('Name', ''),
|
| 219 |
"image_url": record.get('Image_URL__c', 'https://via.placeholder.com/30'),
|
| 220 |
"category": record.get('Category__c', ''),
|
| 221 |
+
"description": record.get('Description__c', 'No description available'),
|
| 222 |
+
"source": "Sector_Detail__c"
|
| 223 |
}
|
| 224 |
logger.info(f"Fetched details for '{item_name}' from Sector_Detail__c")
|
| 225 |
return jsonify({"item_details": item_details})
|
| 226 |
except Exception as e:
|
| 227 |
+
logger.error(f"Failed to fetch item details from Sector_Detail__c: {str(e)}")
|
| 228 |
+
return jsonify({"error": f"Failed to fetch item details from Salesforce: {str(e)}"}), 500
|
| 229 |
|
| 230 |
@app.route('/suggest_items', methods=['POST'])
|
| 231 |
def suggest_items():
|
|
|
|
| 250 |
logger.info(f"Fetched {len(suggestions)} suggestions for '{search_term}'")
|
| 251 |
return jsonify({"suggestions": suggestions})
|
| 252 |
except Exception as e:
|
| 253 |
+
logger.error(f"Failed to fetch suggestions: {str(e)}")
|
| 254 |
+
return jsonify({"error": f"Failed to fetch suggestions: {str(e)}"}), 500
|
| 255 |
|
| 256 |
@app.route('/get_item_details', methods=['POST'])
|
| 257 |
def get_item_details():
|
|
|
|
| 276 |
record = result['records'][0]
|
| 277 |
item_details = {
|
| 278 |
"name": record.get('Ingredient_Name__c', ''),
|
| 279 |
+
"image_url": record.get('Image_URL__c', ''),
|
| 280 |
+
"source": "Ingredient_Object__c"
|
| 281 |
}
|
| 282 |
+
logger.info(f"Fetched details for '{item_name}' from Ingredient_Object__c")
|
| 283 |
return jsonify({"item_details": item_details})
|
| 284 |
except Exception as e:
|
| 285 |
+
logger.error(f"Failed to fetch item details: {str(e)}")
|
| 286 |
+
return jsonify({"error": f"Failed to fetch item details: {str(e)}"}), 500
|
| 287 |
|
| 288 |
@app.route('/submit_items', methods=['POST'])
|
| 289 |
def submit_items():
|
|
|
|
| 312 |
'Quantity__c': item.get('quantity', 1)
|
| 313 |
})
|
| 314 |
|
| 315 |
+
logger.info(f"Submitted {len(items)} items under {ingredient_name}")
|
| 316 |
return jsonify({"success": f"Submitted {len(items)} items under {ingredient_name}", "ingredient_name": ingredient_name})
|
| 317 |
except Exception as e:
|
| 318 |
+
logger.error(f"Failed to submit items: {str(e)}")
|
| 319 |
+
return jsonify({"error": f"Failed to submit items: {str(e)}"}), 500
|
| 320 |
|
| 321 |
@app.route('/get_item_info', methods=['POST'])
|
| 322 |
def get_item_info():
|
|
|
|
| 329 |
soql = f"SELECT Name, Description__c FROM Sector_Detail__c WHERE Name LIKE '%{item_name}%' LIMIT 1"
|
| 330 |
result = sf.query(soql)
|
| 331 |
if result['totalSize'] == 0:
|
| 332 |
+
return jsonify({"error": f"No item found matching '{item_name}' in Sector_Detail__c"}), 404
|
| 333 |
|
| 334 |
record = result['records'][0]
|
| 335 |
base_info = record.get('Description__c', 'No description available')
|
|
|
|
| 351 |
logger.info(f"Generated info for '{item_name}'")
|
| 352 |
return jsonify({"item_info": item_info})
|
| 353 |
except Exception as e:
|
| 354 |
+
logger.error(f"Failed to get item info: {str(e)}")
|
| 355 |
+
return jsonify({"error": f"Failed to get item info: {str(e)}"}), 500
|
| 356 |
|
| 357 |
@app.route('/chat', methods=['POST'])
|
| 358 |
def chat():
|
|
|
|
| 363 |
|
| 364 |
response = get_chatgpt_response(user_message, session_id)
|
| 365 |
logger.info(f"Chat response sent: {response}")
|
| 366 |
+
return jsonify({"response": response})
|
| 367 |
|
| 368 |
if __name__ == '__main__':
|
| 369 |
app.run(debug=True, host='0.0.0.0', port=7860)
|