Yaswanth56 commited on
Commit
728ec2a
·
verified ·
1 Parent(s): 8170b7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -51
app.py CHANGED
@@ -2,16 +2,14 @@ from flask import Flask, render_template, send_from_directory, request, jsonify
2
  from simple_salesforce import Salesforce
3
  from dotenv import load_dotenv
4
  import os
5
- import openai
6
 
7
- openai.api_key = "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"
8
 
9
- # Load environment variables from .env file
10
  load_dotenv()
11
 
12
  app = Flask(__name__, template_folder='templates', static_folder='static')
13
 
14
- # Function to get Salesforce connection
15
  def get_salesforce_connection():
16
  try:
17
  sf = Salesforce(
@@ -25,7 +23,6 @@ def get_salesforce_connection():
25
  print(f"Error connecting to Salesforce: {e}")
26
  return None
27
 
28
- # Initialize Salesforce connection
29
  sf = get_salesforce_connection()
30
 
31
  @app.route('/')
@@ -39,19 +36,16 @@ def serve_static(filename):
39
  @app.route('/get_ingredients', methods=['POST'])
40
  def get_ingredients():
41
  dietary_preference = request.json.get('dietary_preference', '').strip().lower()
42
-
43
- # Debugging: print the received dietary preference
44
- print(f"Received dietary preference: {dietary_preference}")
45
 
46
  if dietary_preference == 'veg':
47
- print("Fetching ingredients for Vegetarian...") # Debug when fetching vegetarian ingredients
48
- soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c IN ('Veg', 'Both') LIMIT 200"
49
  elif dietary_preference == 'non-vegetarian':
50
- print("Fetching ingredients for Non-Vegetarian...") # Debug when fetching non-vegetarian ingredients
51
- soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c IN ('Non-Veg', 'Both') LIMIT 200"
52
-
53
  else:
54
- print("Invalid dietary preference received.") # Debug for invalid dietary preference
55
  return jsonify({"error": "Invalid dietary preference."}), 400
56
 
57
  try:
@@ -60,11 +54,36 @@ def get_ingredients():
60
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
61
  for record in result['records'] if 'Name' in record
62
  ]
63
- print(f"Fetched {len(ingredients)} ingredients.") # Debug: print how many ingredients were fetched
64
  return jsonify({"ingredients": ingredients})
65
  except Exception as e:
66
- print(f"Error while fetching ingredients: {str(e)}") # Debug error
67
  return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  @app.route('/submit_ingredients', methods=['POST'])
69
  def submit_ingredients():
70
  data = request.json
@@ -73,42 +92,8 @@ def submit_ingredients():
73
  if not ingredients:
74
  return jsonify({'error': 'No ingredients selected'}), 400
75
 
76
- # You can now process the selected ingredients, store them in a database, etc.
77
- print(f"Ingredients submitted: {ingredients}")
78
-
79
  return jsonify({'success': True})
80
 
81
- import logging
82
-
83
- # Add this to the beginning of your Flask app file
84
- logging.basicConfig(level=logging.DEBUG) # Enable debug-level logging
85
-
86
-
87
-
88
- @app.route('/get_dish_suggestions', methods=['POST'])
89
- def get_dish_suggestions():
90
- selected_ingredients = request.json.get('ingredients', [])
91
- if not selected_ingredients:
92
- return jsonify({'error': 'No ingredients selected'}), 400
93
-
94
- ingredients_text = ', '.join([ingredient['name'] for ingredient in selected_ingredients])
95
- prompt = f"Suggest a recipe based on the following ingredients: {ingredients_text}"
96
-
97
- try:
98
- # Use the correct API structure for openai>=1.0.0
99
- response = openai.Completion.create(
100
- model="gpt-3.5-turbo",
101
- prompt=prompt,
102
- max_tokens=150,
103
- temperature=0.7
104
- )
105
- dish_suggestions = response['choices'][0]['text'].strip()
106
- return jsonify({"suggestions": dish_suggestions})
107
-
108
- except Exception as e:
109
- logging.error(f"Error in /get_dish_suggestions: {e}")
110
- return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
111
-
112
-
113
  if __name__ == '__main__':
114
  app.run(debug=True, host='0.0.0.0', port=7860)
 
2
  from simple_salesforce import Salesforce
3
  from dotenv import load_dotenv
4
  import os
5
+ import logging
6
 
7
+ logging.basicConfig(level=logging.DEBUG)
8
 
 
9
  load_dotenv()
10
 
11
  app = Flask(__name__, template_folder='templates', static_folder='static')
12
 
 
13
  def get_salesforce_connection():
14
  try:
15
  sf = Salesforce(
 
23
  print(f"Error connecting to Salesforce: {e}")
24
  return None
25
 
 
26
  sf = get_salesforce_connection()
27
 
28
  @app.route('/')
 
36
  @app.route('/get_ingredients', methods=['POST'])
37
  def get_ingredients():
38
  dietary_preference = request.json.get('dietary_preference', '').strip().lower()
39
+ logging.debug(f"Received dietary preference: {dietary_preference}")
 
 
40
 
41
  if dietary_preference == 'veg':
42
+ logging.debug("Fetching ingredients for Vegetarian...")
43
+ soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c = 'Veg' LIMIT 200"
44
  elif dietary_preference == 'non-vegetarian':
45
+ logging.debug("Fetching ingredients for Non-Vegetarian...")
46
+ soql = "SELECT Name, Image_URL__c FROM Sector_Detail__c WHERE Category__c = 'Non-Veg' LIMIT 200"
 
47
  else:
48
+ logging.debug("Invalid dietary preference received.")
49
  return jsonify({"error": "Invalid dietary preference."}), 400
50
 
51
  try:
 
54
  {"name": record['Name'], "image_url": record.get('Image_URL__c', '')}
55
  for record in result['records'] if 'Name' in record
56
  ]
57
+ logging.debug(f"Fetched {len(ingredients)} ingredients.")
58
  return jsonify({"ingredients": ingredients})
59
  except Exception as e:
60
+ logging.error(f"Error while fetching ingredients: {str(e)}")
61
  return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
62
+
63
+ @app.route('/get_menu_items', methods=['POST'])
64
+ def get_menu_items():
65
+ category = request.json.get('category', '').strip().lower()
66
+ logging.debug(f"Received category: {category}")
67
+
68
+ if category == 'fish':
69
+ logging.debug("Fetching fish-based menu items...")
70
+ soql = "SELECT Item_Name__c, Image_URL__c FROM Menu_Item__c WHERE Category__c = 'Fish' LIMIT 200"
71
+ else:
72
+ logging.debug("Invalid category received.")
73
+ return jsonify({"error": "Invalid category."}), 400
74
+
75
+ try:
76
+ result = sf.query(soql)
77
+ menu_items = [
78
+ {"name": record['Item_Name__c'], "image_url": record.get('Image_URL__c', '')}
79
+ for record in result['records'] if 'Item_Name__c' in record
80
+ ]
81
+ logging.debug(f"Fetched {len(menu_items)} menu items.")
82
+ return jsonify({"menu_items": menu_items})
83
+ except Exception as e:
84
+ logging.error(f"Error while fetching menu items: {str(e)}")
85
+ return jsonify({"error": f"Failed to fetch menu items: {str(e)}"}), 500
86
+
87
  @app.route('/submit_ingredients', methods=['POST'])
88
  def submit_ingredients():
89
  data = request.json
 
92
  if not ingredients:
93
  return jsonify({'error': 'No ingredients selected'}), 400
94
 
95
+ logging.debug(f"Ingredients submitted: {ingredients}")
 
 
96
  return jsonify({'success': True})
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  if __name__ == '__main__':
99
  app.run(debug=True, host='0.0.0.0', port=7860)