A Python script that uses OpenAI's API to generate unique smoothie recipes with detailed prompts and uploads them to a Sanity CMS. This application also generates unique slugs for each smoothie recipe and associates visually appealing images generated by OpenAI's DALL-E model.
- AI-Powered Recipe Generation: Generates unique and SEO-friendly smoothie recipes using OpenAI's GPT-4.
- Image Generation: Creates enticing images for each recipe using OpenAI's DALL-E model.
- Sanity CMS Integration: Uploads recipes, images, and metadata to a Sanity CMS project.
- Slug Management: Ensures unique, URL-safe slugs for each smoothie recipe.
- Daily or Random Prompts: Generates recipes based on predefined daily themes or random prompts.
- Error Handling: Logs and manages errors during the API interactions or uploads.
- Clone this repository:
git clone https://github.com/yourusername/smoothie-recipe-generator.git cd smoothie-recipe-generator
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables:
- Create a .env file in the root directory with the following variables:
SANITY_PROJECT_ID=your_sanity_project_id SANITY_DATASET=production SANITY_WRITE_TOKEN=your_sanity_write_token OPENAI_API_KEY=your_openai_api_key
Run the script to generate a smoothie recipe based on the daily prompt:
python generate_smoothie.py
Use a Random Prompt
python generate_smoothie.py --random
Dry Run (Test without Uploading to Sanity)
python generate_smoothie.py --dry-run
The script automatically generates a unique, URL-safe slug for each recipe title. If a conflict is detected (e.g., duplicate titles), a random identifier (using the uuid library) is appended to the slug to ensure uniqueness.
Example:
- Title: Triple Berry Bliss Smoothie: A Radiant Mix of Nutritious Berries
- Slug: triple-berry-bliss-smoothie-a-radiant-mix-of-nutritious-berries
generate_smoothie.py
: Main script to generate, parse, and upload recipes.schemaTypes/blockContent.ts
: Sanity schema for block content (used in blog posts and descriptions)..env
: Environment variables (not included in the repository).requirements.txt
: List of dependencies for the script.
- Python 3.7 or higher
- OpenAI API access
- Sanity CMS project
Logs are output to the console with detailed information about each step:
- Fetching recipe data from OpenAI
- Generating unique slugs
- Uploading recipes and images to Sanity CMS
- Error messages (if any)
Common Errors
- Missing Environment Variables: Ensure all required variables are set in your .env file.
- OpenAI API Errors: Verify your OpenAI API key and check your usage limits.
- Sanity Upload Issues: Ensure your Sanity project ID, dataset, and write token are correct.
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License.