Skip to content

Instantly share code, notes, and snippets.

@robzolkos
Created December 10, 2025 23:47
Show Gist options
  • Select an option

  • Save robzolkos/1b5713118c7ac09a3bcb86ccff9aef98 to your computer and use it in GitHub Desktop.

Select an option

Save robzolkos/1b5713118c7ac09a3bcb86ccff9aef98 to your computer and use it in GitHub Desktop.
Command line script to generate an image from Replicate using the Nano Banana Pro model
#!/bin/bash
# Generate an image from a text prompt using Replicate API
# Usage: ./generate_image.sh "paris in the spring"
# Make sure to put the REPLICATE_API_TOKEN in your .env or environment
set -e
# Load .env file if it exists (check script dir, then current dir)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "$SCRIPT_DIR/../.env" ]; then
set -a; source "$SCRIPT_DIR/../.env"; set +a
elif [ -f ".env" ]; then
set -a; source ".env"; set +a
fi
if [ -z "$1" ]; then
echo "Usage: $0 \"your prompt here\"" >&2
exit 1
fi
if [ -z "$REPLICATE_API_TOKEN" ]; then
echo "Error: REPLICATE_API_TOKEN environment variable is not set" >&2
exit 1
fi
PROMPT="$1"
OUTPUT_DIR="${2:-/tmp}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
SAFE_PROMPT=$(echo "$PROMPT" | tr ' ' '_' | tr -cd '[:alnum:]_' | cut -c1-50)
OUTPUT_FILE="${OUTPUT_DIR}/image_${SAFE_PROMPT}_${TIMESTAMP}.png"
echo "Generating image for: \"$PROMPT\"" >&2
# Call Replicate API
RESPONSE=$(curl -s -X POST \
-H "Authorization: Bearer $REPLICATE_API_TOKEN" \
-H "Content-Type: application/json" \
-H "Prefer: wait" \
-d "{
\"input\": {
\"prompt\": \"$PROMPT\",
\"resolution\": \"1K\",
\"image_input\": [],
\"aspect_ratio\": \"21:9\",
\"output_format\": \"png\",
\"safety_filter_level\": \"block_only_high\"
}
}" \
https://api.replicate.com/v1/models/google/nano-banana-pro/predictions)
# Check for actual errors (not "error":null)
if echo "$RESPONSE" | grep -qE '"error":\s*"[^"]'; then
ERROR=$(echo "$RESPONSE" | grep -o '"error":"[^"]*"' | head -1)
echo "API Error: $ERROR" >&2
exit 1
fi
# Poll for completion if not yet succeeded
while ! echo "$RESPONSE" | grep -q '"status":"succeeded"'; do
STATUS=$(echo "$RESPONSE" | grep -o '"status":"[^"]*"' | sed 's/"status":"//' | sed 's/"$//')
if [ "$STATUS" = "failed" ] || [ "$STATUS" = "canceled" ]; then
echo "Error: Prediction $STATUS" >&2
exit 1
fi
# Get the polling URL
GET_URL=$(echo "$RESPONSE" | grep -o '"get":"[^"]*"' | sed 's/"get":"//' | sed 's/"$//')
if [ -z "$GET_URL" ]; then
echo "Error: Could not find polling URL" >&2
exit 1
fi
echo "Status: $STATUS, waiting..." >&2
sleep 2
RESPONSE=$(curl -s -H "Authorization: Bearer $REPLICATE_API_TOKEN" "$GET_URL")
done
# Extract image URL from response
IMAGE_URL=$(echo "$RESPONSE" | grep -o '"output":"https://[^"]*"' | sed 's/"output":"//' | sed 's/"$//')
if [ -z "$IMAGE_URL" ]; then
# Try array format output
IMAGE_URL=$(echo "$RESPONSE" | grep -o '"output":\s*\["[^"]*"\]' | grep -o 'https://[^"]*')
fi
if [ -z "$IMAGE_URL" ]; then
echo "Error: Could not extract image URL from response" >&2
echo "Response: $RESPONSE" >&2
exit 1
fi
# Download the image
echo "Downloading image..." >&2
curl -s -o "$OUTPUT_FILE" "$IMAGE_URL"
if [ -f "$OUTPUT_FILE" ] && [ -s "$OUTPUT_FILE" ]; then
echo "$OUTPUT_FILE"
else
echo "Error: Failed to download image" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment