Sebastian Krüger 1a5c686dfc
Some checks failed
Build and Push Docker Image / build (push) Failing after 9s
fix: align Freepik API paths with OpenAPI spec
The original implementation used guessed endpoint paths that don't match
the actual Freepik API. Key fixes based on their OpenAPI spec:

- Task polling is per-endpoint (e.g. GET /v1/ai/text-to-image/flux-dev/{task-id})
  not a generic /v1/ai/tasks/{id}. freepik_client now returns TaskResult
  with status_path, and task_tracker polls using that path.
- Fixed endpoint paths: flux-pro -> flux-pro-v1-1, upscale -> image-upscaler,
  relight -> image-relight, style-transfer -> image-style-transfer,
  expand -> image-expand/flux-pro, inpaint -> ideogram-image-edit,
  remove-background -> beta/remove-background, classifier -> classifier/image,
  audio-isolate -> audio-isolation, icon -> text-to-icon
- Fixed video paths: kling -> kling-o1-pro with kling-o1 status path,
  minimax -> minimax-hailuo-02-1080p, seedance -> seedance-pro-1080p
- Fixed request schemas to match actual API params (e.g. scale_factor
  not scale, reference_image not style_reference, image_url for bg removal)
- Fixed response parsing: status is uppercase (COMPLETED not completed),
  results in data.generated[] array, classifier returns [{class_name, probability}]

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 16:26:42 +01:00
2026-02-16 14:07:36 +01:00
2026-02-16 14:07:36 +01:00
2026-02-16 14:07:36 +01:00
2026-02-16 14:07:36 +01:00
2026-02-16 14:07:36 +01:00
2026-02-16 14:07:36 +01:00

Freepik API

REST API wrapping Freepik's cloud AI API for image generation, video generation, image editing, and media processing.

Containerized and deployed via Gitea CI/CD. Designed to be orchestrated alongside facefusion-api — both share the same project patterns (FastAPI, Pydantic, router/schema/service structure).

Quick Start

# Copy and configure environment
cp .env.example .env
# Edit .env and set FP_FREEPIK_API_KEY

# Build and run
docker compose up --build

# Verify
curl http://localhost:8001/api/v1/health
curl http://localhost:8001/api/v1/system

API Endpoints

Image Generation

Endpoint Model Description
POST /api/v1/generate/image/mystic Mystic Freepik's ultra-realistic model
POST /api/v1/generate/image/flux-dev Flux Dev Black Forest Labs Flux Dev
POST /api/v1/generate/image/flux-pro Flux Pro 1.1 Black Forest Labs Flux Pro
POST /api/v1/generate/image/seedream SeedReam 4 ByteDance SeedReam

All image generation endpoints accept ?sync=true to block until the result is ready.

Video Generation

Endpoint Model Description
POST /api/v1/generate/video/kling Kling O1 Pro 1080p, 5/10s clips
POST /api/v1/generate/video/minimax MiniMax Hailuo 768p video
POST /api/v1/generate/video/seedance Seedance 1.0 ByteDance Seedance

Video endpoints always return async task IDs (generation takes 1-5 min).

Image Editing

Endpoint Description
POST /api/v1/edit/upscale/creative AI creative upscale with prompt guidance
POST /api/v1/edit/upscale/precision Precision 2x/4x upscale
POST /api/v1/edit/relight AI relighting
POST /api/v1/edit/style-transfer Style transfer from reference image
POST /api/v1/edit/expand Outpainting / image expansion
POST /api/v1/edit/inpaint AI inpainting with mask and prompt

Utilities

Endpoint Description
POST /api/v1/util/remove-background Background removal (sync)
POST /api/v1/util/classify AI-generated image classifier
POST /api/v1/util/audio-isolate Audio track isolation
POST /api/v1/generate/icon Text-to-icon generation

Task Management

Endpoint Description
GET /api/v1/tasks List tracked tasks
GET /api/v1/tasks/{id} Get task status
GET /api/v1/tasks/{id}/result Download task result
DELETE /api/v1/tasks/{id} Delete task and cleanup files

System

Endpoint Description
GET /api/v1/health Health check
GET /api/v1/system API key validity, active tasks, memory

Usage Examples

Generate an image (async)

curl -X POST http://localhost:8001/api/v1/generate/image/flux-dev \
  -H 'Content-Type: application/json' \
  -d '{"prompt": "a cat wearing a top hat, oil painting"}'

# Response: {"task_id": "abc-123", "status": "pending", ...}

# Poll until complete
curl http://localhost:8001/api/v1/tasks/abc-123

# Download result
curl http://localhost:8001/api/v1/tasks/abc-123/result -o result.png

Generate an image (sync)

curl -X POST 'http://localhost:8001/api/v1/generate/image/mystic?sync=true' \
  -H 'Content-Type: application/json' \
  -d '{"prompt": "mountain landscape at sunset"}' \
  -o result.png

Remove background

curl -X POST http://localhost:8001/api/v1/util/remove-background \
  -H 'Content-Type: application/json' \
  -d '{"image": "<base64-encoded-image>"}'

Environment Variables

All prefixed with FP_:

Variable Default Description
FP_FREEPIK_API_KEY (required) Freepik API key
FP_FREEPIK_BASE_URL https://api.freepik.com API base URL
FP_OUTPUT_DIR /data/outputs Result file storage
FP_TEMP_DIR /data/temp Temporary file storage
FP_MAX_UPLOAD_SIZE_MB 50 Max upload size
FP_TASK_POLL_INTERVAL_SECONDS 5 Polling interval for async tasks
FP_TASK_POLL_TIMEOUT_SECONDS 600 Max polling duration (10 min)
FP_AUTO_CLEANUP_HOURS 24 Auto-delete old results after N hours
FP_WEBHOOK_SECRET (empty) Optional webhook signature verification

Production Deployment

docker compose -f docker-compose.prod.yml up -d

The production compose pulls from the Gitea container registry (dev.pivoine.art/valknar/freepik-api:latest) and uses named volumes for persistent storage.

Architecture

app/
  main.py              # FastAPI app, lifespan, httpx client lifecycle
  config.py            # Pydantic BaseSettings (FP_ env prefix)
  routers/             # API endpoint handlers
  schemas/             # Pydantic request/response models
  services/
    freepik_client.py  # httpx async client with retry on 429
    task_tracker.py    # In-memory task tracking with asyncio polling
    file_manager.py    # Download results, serve files, cleanup
    webhook.py         # Optional webhook receiver for task completion

This is a thin async HTTP client — all AI processing happens on Freepik's cloud infrastructure. No GPU or local model inference required.

Description
No description provided
Readme 57 KiB
Languages
Python 98.7%
Dockerfile 1.3%