Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created July 28, 2026 11:49
Show Gist options
  • Select an option

  • Save seungjin/664eac9121d1f9a4e1af56922ba397de to your computer and use it in GitHub Desktop.

Select an option

Save seungjin/664eac9121d1f9a4e1af56922ba397de to your computer and use it in GitHub Desktop.
import requests
import base64
import sys
import os
from io import BytesIO
import tempfile
url = "http://llama-cpp.local/v1/chat/completions"
prompt = (
"Describe this image for use as alt text. "
"Do not include any preamble, introduction, or phrases like "
"'This image shows' or 'This appears to be'. "
"Start directly with the description."
)
def describe_image(image_path, prompt=prompt):
with open(image_path, "rb") as f:
image_bytes = f.read()
b64_image = base64.b64encode(image_bytes).decode("utf-8")
# adjust mime type if not jpeg (e.g. image/png)
data_url = f"data:image/jpeg;base64,{b64_image}"
payload = {
"model": "Qwen2.5-VL-32B",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": data_url}},
],
}
],
"max_tokens": 64,
}
response = requests.post(url, json=payload, timeout=240)
if not response.ok:
print("Status:", response.status_code)
print("Body:", response.text)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
def get_image_source(path_or_url):
if path_or_url.startswith(("http://", "https://")):
response = requests.get(path_or_url, timeout=10)
response.raise_for_status()
suffix = os.path.splitext(path_or_url)[1] or ".jpg"
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
tmp.write(response.content)
tmp.close()
return tmp.name
else:
return path_or_url
if __name__ == "__main__":
if len(sys.argv) > 1:
image_path = sys.argv[1]
else:
print("image path required")
sys.exit(1)
image_source = get_image_source(image_path)
result = describe_image(image_source)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment