nagasurendra commited on
Commit
e4381f9
·
verified ·
1 Parent(s): 0ba5d64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -77,6 +77,7 @@ def submit_ingredients():
77
  print(f"Ingredients submitted: {ingredients}")
78
 
79
  return jsonify({'success': True})
 
80
  @app.route('/get_dish_suggestions', methods=['POST'])
81
  def get_dish_suggestions():
82
  selected_ingredients = request.json.get('ingredients', [])
@@ -87,17 +88,20 @@ def get_dish_suggestions():
87
  prompt = f"Suggest a recipe based on the following ingredients: {ingredients_text}"
88
 
89
  try:
90
- response = openai.Completion.create(
 
91
  model="gpt-4", # Ensure you're using the correct model
92
- prompt=prompt,
93
- max_tokens=100,
94
- n=1,
95
- stop=None,
96
  temperature=0.7
97
  )
98
- dish_suggestions = response.choices[0].text.strip()
 
 
99
  return jsonify({"suggestions": dish_suggestions})
100
- except openai.error.OpenAIError as e:
 
101
  return jsonify({"error": f"OpenAI API error: {str(e)}"}), 500
102
  except Exception as e:
103
  return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
 
77
  print(f"Ingredients submitted: {ingredients}")
78
 
79
  return jsonify({'success': True})
80
+
81
  @app.route('/get_dish_suggestions', methods=['POST'])
82
  def get_dish_suggestions():
83
  selected_ingredients = request.json.get('ingredients', [])
 
88
  prompt = f"Suggest a recipe based on the following ingredients: {ingredients_text}"
89
 
90
  try:
91
+ # Use the correct ChatCompletion API method
92
+ response = openai.ChatCompletion.create(
93
  model="gpt-4", # Ensure you're using the correct model
94
+ messages=[{"role": "system", "content": "You are a helpful assistant."},
95
+ {"role": "user", "content": prompt}],
96
+ max_tokens=150,
 
97
  temperature=0.7
98
  )
99
+
100
+ # Extract the generated recipe suggestion from the response
101
+ dish_suggestions = response['choices'][0]['message']['content'].strip()
102
  return jsonify({"suggestions": dish_suggestions})
103
+
104
+ except openai.OpenAIError as e:
105
  return jsonify({"error": f"OpenAI API error: {str(e)}"}), 500
106
  except Exception as e:
107
  return jsonify({"error": f"Unexpected error: {str(e)}"}), 500