This Python script reads image captions from a CSV file, shortens them using OpenAI's API, and then saves them in a JSON file. This could be particularly useful if you need image captions equal to or less than a certain character count for accessibility, social media, or training an image model.
Securely loads API keys and other sensitive settings from a .env file.
Uses OpenAI’s GPT-3.5-turbo model to shorten captions effectively.
Reads captions from a CSV file, which allows for batch processing of multiple captions.
Outputs the processed captions in a JSON format, making it easy to integrate with web applications or other media. Setup and Usage
- Python installed on your system.
- An OpenAI API key.
- A CSV file with at least two columns: filename and caption.
- Set up a Python environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install required packages:
pip install openai python-dotenv
- Prepare your environment:
Create a .env file in the same directory as the script and add your OpenAI API key:
OPENAI_API_KEY='your_api_key_here'
Ensure your CSV file is formatted correctly with filename and caption headers.
Modify the script to include the correct path to your CSV file in the process_captions('Your path here') call.
Output will be in captions.json
.