Tanseer45203's picture
Update app.py
a9eb774 verified
import os
from flask import Flask, request, jsonify
from groq import Groq
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Groq client setup
api_key = os.getenv("GROQ_API_KEY")
client = Groq(api_key=api_key)
# Initialize Flask app
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to the AI-based Recipe Generator!"
# Recipe Generator Endpoint
@app.route('/generate_recipe', methods=['POST'])
def generate_recipe():
data = request.get_json()
# Extract information from the user input
ingredients = data.get('ingredients')
cuisine = data.get('cuisine')
dish_type = data.get('dish_type')
complexity = data.get('complexity')
time_limit = data.get('time_limit')
dietary_restrictions = data.get('dietary_restrictions')
prompt = create_recipe_prompt(
ingredients, cuisine, dish_type, complexity, time_limit, dietary_restrictions
)
try:
# Generate a recipe using LLaMA model via Groq API
response = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
)
recipe = response.choices[0].message.content
return jsonify({"recipe": recipe})
except Exception as e:
return jsonify({"error": str(e)}), 500
def create_recipe_prompt(ingredients, cuisine, dish_type, complexity, time_limit, dietary_restrictions):
# Craft a dynamic prompt for LLaMA based on user input
prompt = f"Generate a {dish_type} recipe that includes {', '.join(ingredients)}."
if cuisine:
prompt += f" The recipe should be of {cuisine} cuisine."
if complexity:
prompt += f" The recipe should be {complexity} in terms of difficulty."
if time_limit:
prompt += f" The recipe should take no longer than {time_limit} minutes to prepare."
if dietary_restrictions:
prompt += f" It should be suitable for a {dietary_restrictions} diet."
prompt += " Provide the recipe with ingredients, step-by-step instructions, and basic nutritional information."
return prompt
if __name__ == "__main__":
app.run(debug=True)