import requests
import json

PAT = 'a1a09aad41aa43abbe23122fa1996544'
USER_ID = 'deepseek-ai'
APP_ID = 'deepseek-ocr'
MODEL_ID = 'DeepSeek-OCR'
MODEL_VERSION_ID = 'c52cf7da9b1c4095b07e2a1ccb842811'
IMAGE_URL = 'https://files.catbox.moe/yzxg7r.png'
PROMPT = "Trích xuất nguyên văn toàn bộ văn bản (literal transcription). Tuyệt đối KHÔNG tự ý chỉnh sửa lỗi chính tả, KHÔNG thay đổi ký tự cổ, KHÔNG xoá từ lặp, KHÔNG tự ý viết tắt. Hãy gộp các dòng bị ngắt ở cuối dòng thành một đoạn văn hoàn chỉnh theo ngữ nghĩa (không giữ ngắt dòng tuỳ tiện theo sách in)."

url = f"https://api.clarifai.com/v2/users/{USER_ID}/apps/{APP_ID}/models/{MODEL_ID}/versions/{MODEL_VERSION_ID}/outputs"

headers = {
    "Authorization": f"Key {PAT}",
    "Content-Type": "application/json"
}

data = {
    "inputs": [
        {
            "data": {
                "image": {
                    "url": IMAGE_URL
                },
                "text": {
                    "raw": PROMPT
                }
            }
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(response.status_code)
if response.status_code == 200:
    res = response.json()
    try:
        print(res['outputs'][0]['data']['text']['raw'])
    except Exception as e:
        print("Parsing output failed:", e)
else:
    print(response.text)
