Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 91 |
model="gpt-4", # Ensure you're using the correct model
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
stop=None,
|
| 96 |
temperature=0.7
|
| 97 |
)
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
return jsonify({"suggestions": dish_suggestions})
|
| 100 |
-
|
|
|
|
| 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
|