Skip to content

Instantly share code, notes, and snippets.

@kepocnhh
Created May 17, 2026 20:04
Show Gist options
  • Select an option

  • Save kepocnhh/4e508515984b32c6405abe3ad9cf080d to your computer and use it in GitHub Desktop.

Select an option

Save kepocnhh/4e508515984b32c6405abe3ad9cf080d to your computer and use it in GitHub Desktop.
huggingface image to tags
#!/usr/local/bin/bash
HF_TOKEN='foobarbaz'
MODEL='Qwen/Qwen2.5-VL-72B-Instruct'
USER_CONTENT_TEXT='
You are a keyword extraction engine.
Your entire response must contain only lowercase English keywords separated by single spaces.
Never explain, never greet, never describe your reasoning.
Look at the image, including visible text, signs, logos, captions, visual context.
Do not copy text from the image unless it is a generic object/category.
Convert text meaning into concepts.
If the text contains cultural references, include keywords that match the reference.
Return only 1-6 lowercase English semantic keywords
Examples:
- image: a cat riding a bicycle -> cat bike
- image: a poster saying "May the Force be with you" -> movie
- image: cafe sign with "espresso" -> coffee
'
IMAGE_URL='https://static.boredpanda.com/blog/wp-content/uploads/2025/10/13-68f22702c7d5e-png__700.jpg' # problem
MAX_TOKENS=48
JSON_BODY='{}'
STR_VALUE="${MODEL}"
JSON_BODY="$(printf '%s' "${JSON_BODY}" | STR_VALUE="${STR_VALUE}" yq -M -p=json -o=json '.model=strenv(STR_VALUE)')"
STR_VALUE='user'
JSON_BODY="$(printf '%s' "${JSON_BODY}" | STR_VALUE="${STR_VALUE}" yq -M -p=json -o=json '.messages[0].role=strenv(STR_VALUE)')"
STR_VALUE='text'
JSON_BODY="$(printf '%s' "${JSON_BODY}" | STR_VALUE="${STR_VALUE}" yq -M -p=json -o=json '.messages[0].content[0].type=strenv(STR_VALUE)')"
STR_VALUE="${USER_CONTENT_TEXT}"
JSON_BODY="$(printf '%s' "${JSON_BODY}" | STR_VALUE="${STR_VALUE}" yq -M -p=json -o=json '.messages[0].content[0].text=strenv(STR_VALUE)')"
STR_VALUE='image_url'
JSON_BODY="$(printf '%s' "${JSON_BODY}" | STR_VALUE="${STR_VALUE}" yq -M -p=json -o=json '.messages[0].content[1].type=strenv(STR_VALUE)')"
STR_VALUE="${IMAGE_URL}"
JSON_BODY="$(printf '%s' "${JSON_BODY}" | STR_VALUE="${STR_VALUE}" yq -M -p=json -o=json '.messages[0].content[1].image_url.url=strenv(STR_VALUE)')"
JSON_BODY="$(printf '%s' "${JSON_BODY}" | yq -M -p=json -o=json ".max_tokens=${MAX_TOKENS}")"
HF_OUTPUT='.excluded/json/response.json'
rm "${HF_OUTPUT}"
curl 'https://router.huggingface.co/v1/chat/completions' \
-H "Authorization: Bearer ${HF_TOKEN}" \
-H "Content-Type: application/json" \
-d "${JSON_BODY}" \
-o "${HF_OUTPUT}"
yq -P -p=json -o=json "${HF_OUTPUT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment