#!/bin/bash
set -e

IMG="buddistmonastic2/001.jpg"
B64_TMP=$(mktemp)
base64 "$IMG" > "$B64_TMP"

jq -n \
    --arg model "https://clarifai.com/deepseek-ai/deepseek-ocr/models/DeepSeek-OCR/versions/c52cf7da9b1c4095b07e2a1ccb842811" \
    --rawfile b64 "$B64_TMP" \
    '{
        model: $model,
        messages: [
            {
                role: "system",
                content: "Extract all text exactly as appears. Preserve all characters, diacritics, punctuation, and line breaks. No commentary."
            },
            {
                role: "user",
                content: [
                    {
                        type: "text",
                        text: "Extract text."
                    },
                    {
                        type: "image_url",
                        image_url: {
                            url: ("data:image/jpeg;base64," + $b64)
                        }
                    }
                ]
            }
        ],
        max_tokens: 4096,
        temperature: 0
    }' | curl -s -X POST \
    -H "Authorization: Bearer a1a09aad41aa43abbe23122fa1996544" \
    -H "Content-Type: application/json" \
    -d @- \
    https://api.clarifai.com/v2/ext/openai/v1/chat/completions | jq -r '.choices[0].message.content'

rm "$B64_TMP"