Skip to content

Instantly share code, notes, and snippets.

@avilum
Last active July 20, 2025 13:36
Show Gist options
  • Save avilum/5781047906e7c3df1db70ed51c621f68 to your computer and use it in GitHub Desktop.
Save avilum/5781047906e7c3df1db70ed51c621f68 to your computer and use it in GitHub Desktop.
Generate image from terminal using huggingface spaces - command line API for huggingface diffusion models
#!/bin/bash
# Image generation tool using Hugging Face FLUX.1-schnell model
# Usage: genimage [prompt] or just genimage for Black Forest default
function genimage() {
local prompt="${1:-}"
local api_url="https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
local api_token="<your HuggingFace API Token here>"
# Default Black Forest prompt if none provided
if [[ -z "$prompt" ]]; then
prompt="A breathtaking landscape photograph of the Black Forest (Schwarzwald) in Germany during golden hour. Dense, dark green coniferous forest covering rolling hills and mountains. Traditional German half-timbered houses scattered in the valleys. Misty morning fog drifting between the pine and fir trees. A crystal-clear mountain stream winding through the forest. Dramatic lighting with warm sunbeams filtering through the tall evergreen trees. Professional landscape photography, high resolution, cinematic quality, rich colors, atmospheric perspective."
fi
# Generate timestamp for filename
local timestamp=$(date +"%Y%m%d_%H%M%S")
local filename="generated_image_${timestamp}.png"
echo "🎨 Generating image with FLUX.1-schnell..."
echo "πŸ“ Prompt: $prompt"
echo "⏳ Please wait, this may take a moment..."
# Create JSON payload
local json_payload=$(cat <<EOF
{
"inputs": "$prompt",
"parameters": {
"guidance_scale": 7.5,
"num_inference_steps": 4,
"width": 1024,
"height": 768
}
}
EOF
)
# Make API request with curl
local response_code=$(curl -s -w "%{http_code}" \
-X POST \
-H "Authorization: Bearer $api_token" \
-H "Content-Type: application/json" \
-d "$json_payload" \
--max-time 60 \
-o "$filename" \
"$api_url")
# Check response
case $response_code in
200)
echo "βœ… Image generated successfully!"
echo "πŸ“ Saved as: $filename"
echo "πŸ“ Resolution: 1024x768"
echo "πŸ“ Location: $(pwd)/$filename"
;;
503)
echo "⏳ Model is loading, please try again in a few moments..."
rm -f "$filename" 2>/dev/null
return 1
;;
*)
echo "❌ Error: HTTP $response_code"
if [[ -f "$filename" && -s "$filename" ]]; then
echo "Response: $(cat "$filename")"
fi
rm -f "$filename" 2>/dev/null
return 1
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment