geethareddy commited on
Commit
a3745bb
·
verified ·
1 Parent(s): 5b13bf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -29,6 +29,15 @@ sf = get_salesforce_connection()
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,11 +77,12 @@ def get_food_suggestions():
68
  if not selected_ingredients:
69
  return jsonify({"error": "No ingredients selected"}), 400
70
 
71
- prompt = f"Suggest some food items using the following ingredients: {', '.join(selected_ingredients)}. Provide a list of 3-5 recipe ideas."
 
72
 
73
  try:
74
  response = openai_client.chat.completions.create(
75
- model="gpt-3.5-turbo", # or "gpt-4" if you have access
76
  messages=[
77
  {"role": "system", "content": "You are a helpful chef assistant."},
78
  {"role": "user", "content": prompt}
@@ -80,9 +90,10 @@ def get_food_suggestions():
80
  max_tokens=150
81
  )
82
  suggestions = response.choices[0].message.content.strip().split('\n')
83
- return jsonify({"suggestions": suggestions})
84
  except Exception as e:
85
- return jsonify({"error": f"Failed to get food suggestions: {str(e)}"}), 500
 
86
 
87
  if __name__ == '__main__':
88
  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
+ # 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
  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}
 
90
  max_tokens=150
91
  )
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)