Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ from flask import Flask, render_template, send_from_directory, request, jsonify
|
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
-
import json
|
6 |
import requests
|
7 |
|
8 |
# Load environment variables from .env file
|
@@ -63,71 +62,46 @@ def get_ingredients():
|
|
63 |
except Exception as e:
|
64 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
65 |
|
66 |
-
#
|
67 |
@app.route('/generate_recipes', methods=['POST'])
|
68 |
def generate_recipes():
|
69 |
data = request.json
|
70 |
selected_ingredients = data.get('ingredients', [])
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
"key_ingredients": ["rice", "moong dal", "ghee", "pepper", "cumin"]
|
83 |
-
}
|
84 |
-
},
|
85 |
-
{
|
86 |
-
"name": "Payasam",
|
87 |
-
"image_url": "https://via.placeholder.com/100?text=Payasam",
|
88 |
-
"description": "A creamy Kerala dessert made with milk, jaggery, and rice, enriched with cardamom and coconut, a staple in South Indian festivities.",
|
89 |
-
"details": {
|
90 |
-
"preparation": "Boil milk, add rice and jaggery, simmer until thick, and flavor with cardamom and coconut.",
|
91 |
-
"key_ingredients": ["milk", "jaggery", "rice", "cardamom", "coconut"]
|
92 |
-
}
|
93 |
-
},
|
94 |
-
{
|
95 |
-
"name": "Idli",
|
96 |
-
"image_url": "https://via.placeholder.com/100?text=Idli",
|
97 |
-
"description": "A fluffy steamed cake from Karnataka, made with fermented rice and urad dal, served with sambar and chutney, embodying South Indian comfort food.",
|
98 |
-
"details": {
|
99 |
-
"preparation": "Ferment rice and urad dal batter, steam in idli molds, and serve with sambar and coconut chutney.",
|
100 |
-
"key_ingredients": ["rice", "urad dal", "sambar", "chutney"]
|
101 |
-
}
|
102 |
-
},
|
103 |
-
{
|
104 |
-
"name": "Dosa",
|
105 |
-
"image_url": "https://via.placeholder.com/100?text=Dosa",
|
106 |
-
"description": "A crispy Andhra Pradesh crepe made from fermented rice and urad dal, often filled with potato masala, a versatile South Indian delight.",
|
107 |
-
"details": {
|
108 |
-
"preparation": "Spread fermented batter on a hot griddle, cook until crispy, add potato filling, and serve with chutney.",
|
109 |
-
"key_ingredients": ["rice", "urad dal", "potato", "chutney"]
|
110 |
-
}
|
111 |
-
},
|
112 |
-
{
|
113 |
-
"name": "Upma",
|
114 |
-
"image_url": "https://via.placeholder.com/100?text=Upma",
|
115 |
-
"description": "A savory Tamil Nadu porridge made with semolina, tempered with mustard seeds and curry leaves, a quick yet flavorful breakfast option.",
|
116 |
-
"details": {
|
117 |
-
"preparation": "Roast semolina, cook with water, and temper with mustard seeds, curry leaves, and vegetables.",
|
118 |
-
"key_ingredients": ["semolina", "mustard seeds", "curry leaves", "vegetables"]
|
119 |
-
}
|
120 |
-
}
|
121 |
-
]
|
122 |
}
|
|
|
|
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
if __name__ == '__main__':
|
133 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
|
|
5 |
import requests
|
6 |
|
7 |
# Load environment variables from .env file
|
|
|
62 |
except Exception as e:
|
63 |
return jsonify({"error": f"Failed to fetch ingredients: {str(e)}"}), 500
|
64 |
|
65 |
+
# Endpoint to generate South Indian recipes using OpenAI ChatGPT
|
66 |
@app.route('/generate_recipes', methods=['POST'])
|
67 |
def generate_recipes():
|
68 |
data = request.json
|
69 |
selected_ingredients = data.get('ingredients', [])
|
70 |
|
71 |
+
if not selected_ingredients:
|
72 |
+
return jsonify({"error": "No ingredients selected"}), 400
|
73 |
+
|
74 |
+
api_key = os.getenv('CHATGPT_API_KEY')
|
75 |
+
if not api_key:
|
76 |
+
return jsonify({"error": "CHATGPT_API_KEY not configured in .env"}), 500
|
77 |
+
|
78 |
+
headers = {
|
79 |
+
'Authorization': f'Bearer {api_key}',
|
80 |
+
'Content-Type': 'application/json'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
}
|
82 |
+
ingredients_str = ', '.join(selected_ingredients)
|
83 |
+
prompt = f"Generate 5 authentic South Indian recipes using {ingredients_str}. For each recipe, provide: name, image_url (use placeholder like https://via.placeholder.com/100?text={{name}}), description (a short engaging summary), details (an object with preparation steps and key ingredients as a list). Return the response as a JSON array of recipe objects."
|
84 |
|
85 |
+
payload = {
|
86 |
+
'model': 'gpt-3.5-turbo',
|
87 |
+
'messages': [{'role': 'user', 'content': prompt}],
|
88 |
+
'max_tokens': 500
|
89 |
+
}
|
90 |
+
|
91 |
+
try:
|
92 |
+
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=payload)
|
93 |
+
response.raise_for_status()
|
94 |
+
result = response.json()
|
95 |
+
content = result['choices'][0]['message']['content']
|
96 |
+
|
97 |
+
# Parse the ChatGPT response (assuming it returns JSON-like string)
|
98 |
+
import json
|
99 |
+
recipes = json.loads(content) # Adjust parsing based on actual ChatGPT output format
|
100 |
+
return jsonify({"recipes": recipes})
|
101 |
+
except requests.exceptions.RequestException as e:
|
102 |
+
return jsonify({"error": f"Failed to connect to ChatGPT API: {str(e)}"}), 500
|
103 |
+
except json.JSONDecodeError as e:
|
104 |
+
return jsonify({"error": f"Failed to parse ChatGPT response: {str(e)}. Raw response: {content}"}), 500
|
105 |
|
106 |
if __name__ == '__main__':
|
107 |
app.run(debug=True, host='0.0.0.0', port=7860)
|