Skip to content

Automatic generation of Flickr title/description/tags using the OpenAI GPT API

Notifications You must be signed in to change notification settings

mitcdh/flickr-autotagger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flickr GPT Autotagger

This Python script automates updating Flickr image metadata using OpenAI's GPT-4 vision model. It analyses images in Flickr photosets, generates titles, descriptions, and relevant keywords, and then updates the Flickr metadata accordingly.

Features

  • OAuth authentication with Flickr API
  • Retrieval of all photosets or processing of a specific photoset
  • Image analysis using OpenAI's GPT-4 vision model
  • Automatic updating of Flickr image tags, titles, and descriptions
  • Cost tracking for OpenAI API usage

Prerequisites

  • Python 3.x
  • Flickr API key and secret
  • OpenAI API key

Installation

  1. Clone this repository or download the script.
  2. Install the required Python packages: pip install flickrapi openai python-dotenv
  3. Create a .env file (or otherwise set the environment variables) in the same directory as the script with the following content:
FLICKR_API_KEY=your_flickr_api_key
FLICKR_API_SECRET=your_flickr_api_secret
OPENAI_API_KEY=your_openai_api_key
FLICKR_PHOTOSET_ID=optional_specific_photoset_id

Usage

Run the script using Python: python flickr_autotagger.py

The script will:

  1. Authenticate with Flickr using OAuth (requesting oob input)
  2. Retrieve photosets (all or a specific one)
  3. Process each image in the photosets
  4. Analyse images using OpenAI's GPT-4 vision model
  5. Update Flickr metadata with the generated information
  6. Track and display the cost of OpenAI API usage

Configuration

The following optional environment variables can be set to configure how the script works, defaults have been set so an execution should be successful without configuring any of the below:

  • OPENAI_MODEL: Specifies the OpenAI model to use (default: "gpt-4o-2024-08-06")
  • OPENAI_COST_PER_PROMPT_TOKEN: Cost per prompt token (default: 0.00250)
  • OPENAI_COST_PER_COMPLETION_TOKEN: Cost per completion token (default: 0.01000)
  • OPENAI_VISION_COST_PER_IMAGE: Fixed cost per image analysis (default: 0.000213)
  • FLICKR_PRIVACY_FILTER: Tells the Flickr API to return only images with a specific privacy level (0. none, 1. public/default, 2. friends, 3. family, 4. friends & family, 5. private)
  • MAX_KEYWORDS: Sets the maximum number of keywords to request GPT to generate (default: 10)
  • FLICKR_TOKEN_FILE: Specifies the file name to store the Flickr OAuth token (default: "flickr_token.json")
  • DESCRIPTIONS_TO_ANALYZE: A list of strings that, if found at the start of an existing Flickr description, will cause the script to analyze and update the image metadata (default: '["OLYMPUS DIGITAL CAMERA", "Untitled", "DSC_", "IMG_", "DCIM"]')
  • SKIP_PREFIX: A list of characters that, if found at the start of a photoset name, will cause the script to skip processing that photoset (default: '["#", "@"]')
  • UPDATED_METADATA_FILE: Specifies the file name to store the updated metadata for all processed images (default: "updated_metadata.json")

As of 2024-08-16, the script is configured to use gpt-4o, however, if you'd like to use the more economical gpt-4o-mini, the following could be used:

OPENAI_MODEL = "gpt-4o-mini"
OPENAI_COST_PER_1K_PROMPT_TOKEN = 0.000150 
OPENAI_COST_PER_1K_COMPLETION_TOKEN = 0.000600
OPENAI_VISION_COST_PER_IMAGE = 0.000425

Notes

  • The script skips photosets with names starting with "#" or "@"
  • Images that already have descriptions are skipped unless they start with some that appear autogenerated (e.g. DCIM, IMG_, DSC_)

About

Automatic generation of Flickr title/description/tags using the OpenAI GPT API

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages