#!/bin/bash
set -e

API_KEY="a1a09aad41aa43abbe23122fa1996544"
ENDPOINT="https://api.clarifai.com/v2/users/deepseek-ai/apps/deepseek-ocr/models/DeepSeek-OCR/versions/c52cf7da9b1c4095b07e2a1ccb842811/outputs"

IMG="$1"
if [ ! -f "$IMG" ]; then
    echo "Image $IMG not found"
    exit 1
fi

B64=$(base64 "$IMG" | tr -d '\n')

JSON=$(jq -n \
    --arg b64 "$B64" \
    '{
        inputs: [
            {
                data: {
                    image: {
                        base64: $b64
                    },
                    text: {
                        raw: "Extract all text exactly as appears. Preserve all characters, diacritics, punctuation, and line breaks. No commentary."
                    }
                }
            }
        ]
    }')

response=$(curl -s -X POST "$ENDPOINT" \
    -H "Authorization: Key $API_KEY" \
    -H "Content-Type: application/json" \
    -d "$JSON")

echo "$response" | jq .