From 37b4081448e88a9e7ccde70d7e51e2b25b6afdd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Sat, 21 Feb 2026 13:32:06 +0100 Subject: [PATCH] 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 --- img_api_generate.sh | 128 ++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 77 deletions(-) diff --git a/img_api_generate.sh b/img_api_generate.sh index f004db0..23fcd79 100755 --- a/img_api_generate.sh +++ b/img_api_generate.sh @@ -67,11 +67,10 @@ MODEL="flux-dev" TOKEN="" FACE_IMAGE="" OUTPUT_FILE="" -NUM_IMAGES=1 SEED="" -NEGATIVE_PROMPT="" -GUIDANCE_SCALE="" ASPECT_RATIO="" +RESOLUTION="" +STYLING="" INPUT_IMAGE="" ASYNC_MODE=false DRY_RUN=false @@ -270,33 +269,10 @@ validate_inputs() { ((errors++)) fi - # Numeric range checks - if [[ -n "$NUM_IMAGES" ]]; then - if ! [[ "$NUM_IMAGES" =~ ^[1-4]$ ]]; then - print_error "Number of images must be 1-4 (got: $NUM_IMAGES)" - ((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="" + # Resolution (mystic only) + if [[ -n "$RESOLUTION" && "$MODEL" != "mystic" ]]; then + print_warning "Resolution is only supported for mystic model (ignoring)" + RESOLUTION="" fi if ((errors > 0)); then @@ -334,38 +310,42 @@ build_payload() { # Start with required fields payload=$(jq -n \ --arg prompt "$PROMPT" \ - --argjson num_images "$NUM_IMAGES" \ - '{prompt: $prompt, num_images: $num_images}') + '{prompt: $prompt}') # Optional: seed if [[ -n "$SEED" ]]; then payload=$(echo "$payload" | jq --argjson seed "$SEED" '. + {seed: $seed}') fi - # Optional: negative prompt (mystic only) - 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) + # Optional: aspect ratio (All models) if [[ -n "$ASPECT_RATIO" ]]; then payload=$(echo "$payload" | jq --arg ar "$ASPECT_RATIO" '. + {aspect_ratio: $ar}') 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 local b64 b64=$(base64 -w 0 "$INPUT_IMAGE") - local mime - mime=$(file -b --mime-type "$INPUT_IMAGE") - payload=$(echo "$payload" | jq \ - --arg img "data:${mime};base64,${b64}" \ - '. + {image: $img}') + if [[ "$MODEL" == "mystic" ]]; then + payload=$(echo "$payload" | jq --arg img "$b64" '. + {structure_reference: $img}') + fi fi echo "$payload" @@ -534,6 +514,7 @@ fix_extension() { generate_image() { local payload="$1" + # Note: URL prefix aligned with actual API endpoints local url="${BASE_URL}/freepik/generate/image/${MODEL}" if [[ "$ASYNC_MODE" == false ]]; then @@ -561,9 +542,9 @@ generate_image() { print_field "$PALETTE" "Model" "$MODEL" print_field "$SPARKLES" "Prompt" "$(echo "$PROMPT" | head -c 80)$([ ${#PROMPT} -gt 80 ] && echo '...')" [[ -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 "$RESOLUTION" ]] && print_field " " "Resolution" "$RESOLUTION" + [[ -n "$STYLING" ]] && print_field " " "Styling" "$STYLING" [[ -n "$INPUT_IMAGE" ]] && print_field " " "Input Image" "$INPUT_IMAGE" echo "" @@ -846,12 +827,11 @@ REQUIRED: MODEL & GENERATION: -m, --model MODEL Model name (default: flux-dev) Available: mystic, flux-dev, flux-pro, seedream - -n, --num-images N Number of images 1-4 (default: 1) -s, --seed N Seed for reproducibility - --negative-prompt TEXT Negative prompt (mystic model only) - --guidance-scale N Guidance scale 1.0-20.0 (flux models only) - --aspect-ratio RATIO Aspect ratio (seedream model only) - --image FILE Input image for img2img (base64-encoded automatically) + --aspect-ratio RATIO Aspect ratio (e.g. square_1_1, widescreen_16_9) + --resolution RES Resolution (mystic model only: 1k, 2k, 4k) + --styling STYLE Styling (JSON string or name) + --image FILE Input image for reference (mystic model only) AUTHENTICATION: -t, --token TOKEN API token (X-Api-Key) @@ -863,7 +843,7 @@ FACE SWAP: OUTPUT: -o, --output FILE Output file path (default: auto-generated) - -u, --upscale Upscale final image using Real-ESRGAN (RealESRGAN_x4plus) + -u, --upscale Upscale final image using Real-ESRGAN (RealESRGAN_x4plus) MODES: --async Use async mode with polling instead of sync @@ -872,12 +852,11 @@ MODES: -h, --help Show this help message EXAMPLES: - # Generate with flux-dev (default model) - ./img_api_generate.sh -p "a cat sitting on a rainbow" + # Generate with flux-dev (default model) and custom aspect ratio + ./img_api_generate.sh -p "a cat sitting on a rainbow" --aspect-ratio widescreen_16_9 - # Generate with mystic model and negative prompt - ./img_api_generate.sh -p "photorealistic portrait" -m mystic \ - --negative-prompt "blurry, low quality" + # Generate with mystic model and specific resolution + ./img_api_generate.sh -p "photorealistic portrait" -m mystic --resolution 2k # Generate and face swap ./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 ./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 } @@ -918,26 +894,22 @@ parse_args() { OUTPUT_FILE="$2" shift 2 ;; - -n|--num-images) - NUM_IMAGES="$2" - shift 2 - ;; -s|--seed) SEED="$2" shift 2 ;; - --negative-prompt) - NEGATIVE_PROMPT="$2" - shift 2 - ;; - --guidance-scale) - GUIDANCE_SCALE="$2" - shift 2 - ;; --aspect-ratio) ASPECT_RATIO="$2" shift 2 ;; + --resolution) + RESOLUTION="$2" + shift 2 + ;; + --styling) + STYLING="$2" + shift 2 + ;; --image) INPUT_IMAGE="$2" shift 2 @@ -1089,6 +1061,8 @@ main() { print_field "$FACE" "Face Swap" "$faceswap_path" fi [[ -n "$SEED" ]] && print_field " " "Seed" "$SEED" + [[ -n "$ASPECT_RATIO" ]] && print_field " " "Aspect" "$ASPECT_RATIO" + [[ -n "$RESOLUTION" ]] && print_field " " "Resolution" "$RESOLUTION" echo "" print_banner "${SPARKLES} Complete ${SPARKLES}"