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
]
)
)Popular Models
Image Generation
| Model | Description | Speed |
|---|---|---|
black-forest-labs/flux-schnell | Fast FLUX generation | ⚡ Fast |
black-forest-labs/flux-dev | High quality FLUX | Medium |
black-forest-labs/flux-pro | Best quality FLUX | Slower |
stability-ai/sdxl | Stable Diffusion XL | Medium |
stability-ai/stable-diffusion-3 | SD3 | Medium |
Language Models
| Model | Description |
|---|---|
meta/llama-2-70b-chat | Llama 2 chat |
meta/llama-2-13b-chat | Llama 2 smaller |
mistralai/mistral-7b-instruct-v0.2 | Mistral instruct |
Video Models
| Model | Description |
|---|---|
anotherjesse/zeroscope-v2-xl | Text to video |
stability-ai/stable-video-diffusion | Image to video |
Audio Models
| Model | Description |
|---|---|
meta/musicgen | Music generation |
cjwbw/riffusion | Music from spectrograms |
FLUX Model Options
| Option | Description | Default |
|---|---|---|
width | Image width (256-1440) | 1024 |
height | Image height (256-1440) | 1024 |
numOutputs | Number of images (1-4) | 1 |
numInferenceSteps | Denoising steps | 4 (schnell), 28 (dev) |
guidanceScale | Prompt adherence | 3.5 |
outputFormat | png, jpg, webp | webp |
seed | Reproducibility | Random |
promptUpsampling | Enhance prompts | false |
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.