geethareddy commited on
Commit
78a386a
·
verified ·
1 Parent(s): 05691b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -15
app.py CHANGED
@@ -29,15 +29,6 @@ sf = get_salesforce_connection()
29
  # Initialize OpenAI client
30
  openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
31
 
32
- # Mock recipe suggestions for fallback
33
- MOCK_RECIPES = {
34
- 'default': [
35
- "1. Tomato and Yogurt Raita",
36
- "2. Onion and Tomato Curry",
37
- "3. Yogurt Marinated Tomato Salad"
38
- ]
39
- }
40
-
41
  @app.route('/')
42
  def index():
43
  return render_template('index.html')
@@ -77,14 +68,14 @@ def get_food_suggestions():
77
  if not selected_ingredients:
78
  return jsonify({"error": "No ingredients selected"}), 400
79
 
80
- # Prepare prompt for OpenAI
81
- prompt = f"Suggest 3-5 recipe ideas using the following ingredients: {', '.join(selected_ingredients)}. Provide the recipe names in a numbered list format."
82
 
83
  try:
84
  response = openai_client.chat.completions.create(
85
- model="gpt-3.5-turbo",
86
  messages=[
87
- {"role": "system", "content": "You are a helpful chef assistant."},
88
  {"role": "user", "content": prompt}
89
  ],
90
  max_tokens=150
@@ -92,8 +83,7 @@ def get_food_suggestions():
92
  suggestions = response.choices[0].message.content.strip().split('\n')
93
  return jsonify({"suggestions": [s.strip() for s in suggestions if s.strip()]})
94
  except Exception as e:
95
- print(f"OpenAI API error: {str(e)} - Falling back to mock data")
96
- return jsonify({"suggestions": MOCK_RECIPES.get('default', ["No suggestions available"])})
97
 
98
  if __name__ == '__main__':
99
  app.run(debug=True, host='0.0.0.0', port=7860)
 
29
  # Initialize OpenAI client
30
  openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
31
 
 
 
 
 
 
 
 
 
 
32
  @app.route('/')
33
  def index():
34
  return render_template('index.html')
 
68
  if not selected_ingredients:
69
  return jsonify({"error": "No ingredients selected"}), 400
70
 
71
+ # Prepare prompt for OpenAI with specific ingredients
72
+ prompt = f"Suggest 3-5 recipe ideas using only the following ingredients: {', '.join(selected_ingredients)}. Provide the recipe names in a numbered list format, ensuring the recipes are realistic and use only the listed ingredients."
73
 
74
  try:
75
  response = openai_client.chat.completions.create(
76
+ model="gpt-3.5-turbo", # Use "gpt-4" if you have access
77
  messages=[
78
+ {"role": "system", "content": "You are a helpful chef assistant specializing in creating recipes from limited ingredient lists."},
79
  {"role": "user", "content": prompt}
80
  ],
81
  max_tokens=150
 
83
  suggestions = response.choices[0].message.content.strip().split('\n')
84
  return jsonify({"suggestions": [s.strip() for s in suggestions if s.strip()]})
85
  except Exception as e:
86
+ return jsonify({"error": f"Failed to get food suggestions: {str(e)}"}), 500
 
87
 
88
  if __name__ == '__main__':
89
  app.run(debug=True, host='0.0.0.0', port=7860)