Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,35 +78,39 @@ def submit_ingredients():
|
|
| 78 |
|
| 79 |
return jsonify({'success': True})
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
@app.route('/get_dish_suggestions', methods=['POST'])
|
| 82 |
def get_dish_suggestions():
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 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 |
-
|
| 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
|
| 108 |
|
| 109 |
|
| 110 |
|
|
|
|
| 111 |
if __name__ == '__main__':
|
| 112 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 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 |
@app.route('/get_dish_suggestions', methods=['POST'])
|
| 87 |
def get_dish_suggestions():
|
| 88 |
+
try:
|
| 89 |
+
selected_ingredients = request.json.get('ingredients', [])
|
| 90 |
+
if not selected_ingredients:
|
| 91 |
+
return jsonify({'error': 'No ingredients selected'}), 400
|
| 92 |
|
| 93 |
+
ingredients_text = ', '.join([ingredient['name'] for ingredient in selected_ingredients])
|
| 94 |
+
prompt = f"Suggest a recipe based on the following ingredients: {ingredients_text}"
|
| 95 |
|
|
|
|
|
|
|
| 96 |
response = openai.ChatCompletion.create(
|
| 97 |
model="gpt-4", # Ensure you're using the correct model
|
| 98 |
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
| 99 |
+
{"role": "user", "content": prompt}],
|
| 100 |
max_tokens=150,
|
| 101 |
temperature=0.7
|
| 102 |
)
|
| 103 |
|
|
|
|
| 104 |
dish_suggestions = response['choices'][0]['message']['content'].strip()
|
| 105 |
return jsonify({"suggestions": dish_suggestions})
|
| 106 |
|
|
|
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
+
# Log the error and send back a generic response
|
| 109 |
+
logging.error(f"Error in /get_dish_suggestions: {e}")
|
| 110 |
return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
+
|
| 115 |
if __name__ == '__main__':
|
| 116 |
app.run(debug=True, host='0.0.0.0', port=7860)
|