Replicate 🔄

Run any open-source ML model with a single API call.

🎨

What you can do: Image generation (FLUX, SDXL), video generation, audio, language models, and thousands of community models with simple API access.

Setup

Add your Replicate API token in the ProtectMyAPI Dashboard.


Run Predictions

Basic Prediction

let replicate = ProtectMyAPI.replicateService()
 
let prediction = try await replicate.createPrediction(
    request: ReplicatePredictionRequest(
        model: "meta/llama-2-70b-chat",
        input: [
            "prompt": "Write a haiku about programming",
            "max_new_tokens": 100
        ]
    )
)
 
// Predictions run asynchronously - check status
print("Status: \(prediction.status)")
print("Output: \(prediction.output ?? "pending")")

Wait for Completion

// Create and wait for prediction
let prediction = try await replicate.createPredictionAndWait(
    request: ReplicatePredictionRequest(
        model: "meta/llama-2-70b-chat",
        input: ["prompt": "Hello, world!"]
    ),
    timeout: 60 // seconds
)
 
print("Result: \(prediction.output!)")

Image Generation

FLUX (Fast)

let image = try await replicate.generateFluxImage(
    prompt: "A cyberpunk cat wearing VR goggles",
    options: FluxOptions(
        model: .schnell, // Fast generation
        width: 1024,
        height: 1024,
        numOutputs: 1,
        outputFormat: .png
    )
)
 
// image.output contains the URL of generated image
if let url = image.output?.first {
    print("Image URL: \(url)")
}

FLUX Pro (Higher Quality)

let image = try await replicate.generateFluxImage(
    prompt: "Professional product photography of a luxury watch",
    options: FluxOptions(
        model: .pro,
        width: 1024,
        height: 1024,
        aspectRatio: "1:1",
        promptUpsampling: true, // Enhance prompt automatically
        safetyTolerance: 2
    )
)

FLUX Dev (Development)

let image = try await replicate.generateFluxImage(
    prompt: "Watercolor painting of a mountain landscape",
    options: FluxOptions(
        model: .dev,
        numInferenceSteps: 28, // More steps = higher quality
        guidanceScale: 3.5
    )
)

SDXL Generation

let image = try await replicate.createPredictionAndWait(
    request: ReplicatePredictionRequest(
        model: "stability-ai/sdxl",
        input: [
            "prompt": "Futuristic city skyline at night, neon lights",
            "negative_prompt": "blurry, low quality",
            "width": 1024,
            "height": 1024,
            "num_inference_steps": 30,
            "guidance_scale": 7.5
        ]
    )
)

Check Prediction Status

// Start a prediction
let prediction = try await replicate.createPrediction(
    request: ReplicatePredictionRequest(
        model: "stability-ai/sdxl",
        input: ["prompt": "Beautiful sunset"]
    )
)
 
// Check status later
let status = try await replicate.getPrediction(id: prediction.id)
 
switch status.status {
case "succeeded":
    print("Done: \(status.output!)")
case "failed":
    print("Error: \(status.error ?? "Unknown")")
case "processing":
    print("Still running...")
default:
    print("Status: \(status.status)")
}

Cancel Prediction

try await replicate.cancelPrediction(id: prediction.id)

Video Generation

let video = try await replicate.createPredictionAndWait(
    request: ReplicatePredictionRequest(
        model: "anotherjesse/zeroscope-v2-xl",
        input: [
            "prompt": "A spaceship flying through an asteroid field",
            "num_frames": 24,
            "fps": 8
        ]
    ),
    timeout: 300 // Videos take longer
)
 
print("Video URL: \(video.output!)")

Audio Generation

Music with MusicGen

let audio = try await replicate.createPredictionAndWait(
    request: ReplicatePredictionRequest(
        model: "meta/musicgen",
        input: [
            "prompt": "Upbeat electronic dance music with heavy bass",
            "duration": 10
        ]
    )
)

Image Generation

ModelDescriptionSpeed
black-forest-labs/flux-schnellFast FLUX generation⚡ Fast
black-forest-labs/flux-devHigh quality FLUXMedium
black-forest-labs/flux-proBest quality FLUXSlower
stability-ai/sdxlStable Diffusion XLMedium
stability-ai/stable-diffusion-3SD3Medium

Language Models

ModelDescription
meta/llama-2-70b-chatLlama 2 chat
meta/llama-2-13b-chatLlama 2 smaller
mistralai/mistral-7b-instruct-v0.2Mistral instruct

Video Models

ModelDescription
anotherjesse/zeroscope-v2-xlText to video
stability-ai/stable-video-diffusionImage to video

Audio Models

ModelDescription
meta/musicgenMusic generation
cjwbw/riffusionMusic from spectrograms

FLUX Model Options

OptionDescriptionDefault
widthImage width (256-1440)1024
heightImage height (256-1440)1024
numOutputsNumber of images (1-4)1
numInferenceStepsDenoising steps4 (schnell), 28 (dev)
guidanceScalePrompt adherence3.5
outputFormatpng, jpg, webpwebp
seedReproducibilityRandom
promptUpsamplingEnhance promptsfalse

Pricing

Replicate charges per second of compute time. Pricing varies by model and hardware:

  • CPU: ~$0.000100/sec
  • GPU (T4): ~$0.000225/sec
  • GPU (A40): ~$0.000575/sec
  • GPU (A100): ~$0.001150/sec

Check their pricing page for current rates.