feat: support aspect ratios and align with image generation schema

- Add support for resolution and styling parameters
- Enable aspect ratio support for all models
- Remove unsupported fields: num_images, negative_prompt, guidance_scale
- Streamline payload builder and input validation
- Align API paths with live Traefik configuration
This commit is contained in:
2026-02-21 13:32:06 +01:00
parent 28e16e09a4
commit 37b4081448

View File

@@ -67,11 +67,10 @@ MODEL="flux-dev"
TOKEN="" TOKEN=""
FACE_IMAGE="" FACE_IMAGE=""
OUTPUT_FILE="" OUTPUT_FILE=""
NUM_IMAGES=1
SEED="" SEED=""
NEGATIVE_PROMPT=""
GUIDANCE_SCALE=""
ASPECT_RATIO="" ASPECT_RATIO=""
RESOLUTION=""
STYLING=""
INPUT_IMAGE="" INPUT_IMAGE=""
ASYNC_MODE=false ASYNC_MODE=false
DRY_RUN=false DRY_RUN=false
@@ -270,33 +269,10 @@ validate_inputs() {
((errors++)) ((errors++))
fi fi
# Numeric range checks # Resolution (mystic only)
if [[ -n "$NUM_IMAGES" ]]; then if [[ -n "$RESOLUTION" && "$MODEL" != "mystic" ]]; then
if ! [[ "$NUM_IMAGES" =~ ^[1-4]$ ]]; then print_warning "Resolution is only supported for mystic model (ignoring)"
print_error "Number of images must be 1-4 (got: $NUM_IMAGES)" RESOLUTION=""
((errors++))
fi
fi
if [[ -n "$GUIDANCE_SCALE" ]]; then
if ! awk "BEGIN {exit ($GUIDANCE_SCALE >= 1.0 && $GUIDANCE_SCALE <= 20.0) ? 0 : 1}" 2>/dev/null; then
print_error "Guidance scale must be 1.0-20.0 (got: $GUIDANCE_SCALE)"
((errors++))
fi
if [[ "$MODEL" != "flux-dev" && "$MODEL" != "flux-pro" ]]; then
print_warning "Guidance scale is only supported for flux models (ignoring)"
GUIDANCE_SCALE=""
fi
fi
if [[ -n "$NEGATIVE_PROMPT" && "$MODEL" != "mystic" ]]; then
print_warning "Negative prompt is only supported for mystic model (ignoring)"
NEGATIVE_PROMPT=""
fi
if [[ -n "$ASPECT_RATIO" && "$MODEL" != "seedream" ]]; then
print_warning "Aspect ratio is only supported for seedream model (ignoring)"
ASPECT_RATIO=""
fi fi
if ((errors > 0)); then if ((errors > 0)); then
@@ -334,38 +310,42 @@ build_payload() {
# Start with required fields # Start with required fields
payload=$(jq -n \ payload=$(jq -n \
--arg prompt "$PROMPT" \ --arg prompt "$PROMPT" \
--argjson num_images "$NUM_IMAGES" \ '{prompt: $prompt}')
'{prompt: $prompt, num_images: $num_images}')
# Optional: seed # Optional: seed
if [[ -n "$SEED" ]]; then if [[ -n "$SEED" ]]; then
payload=$(echo "$payload" | jq --argjson seed "$SEED" '. + {seed: $seed}') payload=$(echo "$payload" | jq --argjson seed "$SEED" '. + {seed: $seed}')
fi fi
# Optional: negative prompt (mystic only) # Optional: aspect ratio (All models)
if [[ -n "$NEGATIVE_PROMPT" ]]; then
payload=$(echo "$payload" | jq --arg neg "$NEGATIVE_PROMPT" '. + {negative_prompt: $neg}')
fi
# Optional: guidance scale (flux models)
if [[ -n "$GUIDANCE_SCALE" ]]; then
payload=$(echo "$payload" | jq --argjson gs "$GUIDANCE_SCALE" '. + {guidance_scale: $gs}')
fi
# Optional: aspect ratio (seedream)
if [[ -n "$ASPECT_RATIO" ]]; then if [[ -n "$ASPECT_RATIO" ]]; then
payload=$(echo "$payload" | jq --arg ar "$ASPECT_RATIO" '. + {aspect_ratio: $ar}') payload=$(echo "$payload" | jq --arg ar "$ASPECT_RATIO" '. + {aspect_ratio: $ar}')
fi fi
# Optional: input image (img2img) # Optional: resolution (mystic only)
if [[ -n "$RESOLUTION" ]]; then
payload=$(echo "$payload" | jq --arg res "$RESOLUTION" '. + {resolution: $res}')
fi
# Optional: styling
if [[ -n "$STYLING" ]]; then
# If STYLING starts with {, treat as JSON, else treat as a simple string if possible
if [[ "$STYLING" == {* ]]; then
payload=$(echo "$payload" | jq --argjson sty "$STYLING" '. + {styling: $sty}')
else
payload=$(echo "$payload" | jq --arg sty "$STYLING" '. + {styling: {style: $sty}}')
fi
fi
# Optional: input image / reference images (mystic supports structure/style ref)
# For now, we keep it simple or align with the specific model's needs.
# The API schema for mystic has structure_reference and style_reference.
if [[ -n "$INPUT_IMAGE" ]]; then if [[ -n "$INPUT_IMAGE" ]]; then
local b64 local b64
b64=$(base64 -w 0 "$INPUT_IMAGE") b64=$(base64 -w 0 "$INPUT_IMAGE")
local mime if [[ "$MODEL" == "mystic" ]]; then
mime=$(file -b --mime-type "$INPUT_IMAGE") payload=$(echo "$payload" | jq --arg img "$b64" '. + {structure_reference: $img}')
payload=$(echo "$payload" | jq \ fi
--arg img "data:${mime};base64,${b64}" \
'. + {image: $img}')
fi fi
echo "$payload" echo "$payload"
@@ -534,6 +514,7 @@ fix_extension() {
generate_image() { generate_image() {
local payload="$1" local payload="$1"
# Note: URL prefix aligned with actual API endpoints
local url="${BASE_URL}/freepik/generate/image/${MODEL}" local url="${BASE_URL}/freepik/generate/image/${MODEL}"
if [[ "$ASYNC_MODE" == false ]]; then if [[ "$ASYNC_MODE" == false ]]; then
@@ -561,9 +542,9 @@ generate_image() {
print_field "$PALETTE" "Model" "$MODEL" print_field "$PALETTE" "Model" "$MODEL"
print_field "$SPARKLES" "Prompt" "$(echo "$PROMPT" | head -c 80)$([ ${#PROMPT} -gt 80 ] && echo '...')" print_field "$SPARKLES" "Prompt" "$(echo "$PROMPT" | head -c 80)$([ ${#PROMPT} -gt 80 ] && echo '...')"
[[ -n "$SEED" ]] && print_field " " "Seed" "$SEED" [[ -n "$SEED" ]] && print_field " " "Seed" "$SEED"
[[ -n "$NEGATIVE_PROMPT" ]] && print_field " " "Negative" "$(echo "$NEGATIVE_PROMPT" | head -c 60)..."
[[ -n "$GUIDANCE_SCALE" ]] && print_field " " "Guidance" "$GUIDANCE_SCALE"
[[ -n "$ASPECT_RATIO" ]] && print_field " " "Aspect" "$ASPECT_RATIO" [[ -n "$ASPECT_RATIO" ]] && print_field " " "Aspect" "$ASPECT_RATIO"
[[ -n "$RESOLUTION" ]] && print_field " " "Resolution" "$RESOLUTION"
[[ -n "$STYLING" ]] && print_field " " "Styling" "$STYLING"
[[ -n "$INPUT_IMAGE" ]] && print_field " " "Input Image" "$INPUT_IMAGE" [[ -n "$INPUT_IMAGE" ]] && print_field " " "Input Image" "$INPUT_IMAGE"
echo "" echo ""
@@ -846,12 +827,11 @@ REQUIRED:
MODEL & GENERATION: MODEL & GENERATION:
-m, --model MODEL Model name (default: flux-dev) -m, --model MODEL Model name (default: flux-dev)
Available: mystic, flux-dev, flux-pro, seedream Available: mystic, flux-dev, flux-pro, seedream
-n, --num-images N Number of images 1-4 (default: 1)
-s, --seed N Seed for reproducibility -s, --seed N Seed for reproducibility
--negative-prompt TEXT Negative prompt (mystic model only) --aspect-ratio RATIO Aspect ratio (e.g. square_1_1, widescreen_16_9)
--guidance-scale N Guidance scale 1.0-20.0 (flux models only) --resolution RES Resolution (mystic model only: 1k, 2k, 4k)
--aspect-ratio RATIO Aspect ratio (seedream model only) --styling STYLE Styling (JSON string or name)
--image FILE Input image for img2img (base64-encoded automatically) --image FILE Input image for reference (mystic model only)
AUTHENTICATION: AUTHENTICATION:
-t, --token TOKEN API token (X-Api-Key) -t, --token TOKEN API token (X-Api-Key)
@@ -872,12 +852,11 @@ MODES:
-h, --help Show this help message -h, --help Show this help message
EXAMPLES: EXAMPLES:
# Generate with flux-dev (default model) # Generate with flux-dev (default model) and custom aspect ratio
./img_api_generate.sh -p "a cat sitting on a rainbow" ./img_api_generate.sh -p "a cat sitting on a rainbow" --aspect-ratio widescreen_16_9
# Generate with mystic model and negative prompt # Generate with mystic model and specific resolution
./img_api_generate.sh -p "photorealistic portrait" -m mystic \ ./img_api_generate.sh -p "photorealistic portrait" -m mystic --resolution 2k
--negative-prompt "blurry, low quality"
# Generate and face swap # Generate and face swap
./img_api_generate.sh -p "portrait of a person" -m mystic -f face.jpg ./img_api_generate.sh -p "portrait of a person" -m mystic -f face.jpg
@@ -885,9 +864,6 @@ EXAMPLES:
# Dry run to preview API calls # Dry run to preview API calls
./img_api_generate.sh --dry-run -p "a cat" -m flux-dev ./img_api_generate.sh --dry-run -p "a cat" -m flux-dev
# Use a specific seed for reproducibility
./img_api_generate.sh -p "landscape at sunset" -s 42 -m flux-pro
EOF EOF
} }
@@ -918,26 +894,22 @@ parse_args() {
OUTPUT_FILE="$2" OUTPUT_FILE="$2"
shift 2 shift 2
;; ;;
-n|--num-images)
NUM_IMAGES="$2"
shift 2
;;
-s|--seed) -s|--seed)
SEED="$2" SEED="$2"
shift 2 shift 2
;; ;;
--negative-prompt)
NEGATIVE_PROMPT="$2"
shift 2
;;
--guidance-scale)
GUIDANCE_SCALE="$2"
shift 2
;;
--aspect-ratio) --aspect-ratio)
ASPECT_RATIO="$2" ASPECT_RATIO="$2"
shift 2 shift 2
;; ;;
--resolution)
RESOLUTION="$2"
shift 2
;;
--styling)
STYLING="$2"
shift 2
;;
--image) --image)
INPUT_IMAGE="$2" INPUT_IMAGE="$2"
shift 2 shift 2
@@ -1089,6 +1061,8 @@ main() {
print_field "$FACE" "Face Swap" "$faceswap_path" print_field "$FACE" "Face Swap" "$faceswap_path"
fi fi
[[ -n "$SEED" ]] && print_field " " "Seed" "$SEED" [[ -n "$SEED" ]] && print_field " " "Seed" "$SEED"
[[ -n "$ASPECT_RATIO" ]] && print_field " " "Aspect" "$ASPECT_RATIO"
[[ -n "$RESOLUTION" ]] && print_field " " "Resolution" "$RESOLUTION"
echo "" echo ""
print_banner "${SPARKLES} Complete ${SPARKLES}" print_banner "${SPARKLES} Complete ${SPARKLES}"