Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,34 +83,32 @@ import logging
|
|
| 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 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
return jsonify({'error': 'No ingredients selected'}), 400
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 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)
|
|
|
|
| 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-4",
|
| 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)
|