त्वरित प्रारंभ
नीचे दिए गए प्रवाह में, एक फ़ाइल अपलोड करने, अनुवाद कार्य बनाने और परिणाम डाउनलोड करने के लिए एक API कुंजी का उपयोग किया जाता है।
-
क्रेडेंशियल और फ़ाइल तैयार करें
Terminal window export OMNIDOC_API_KEY="sk_your_key"BASE="https://omnidoc.top/api/v1"FILE="report.docx"SIZE=$(wc -c < "$FILE" | tr -d ' ')import uuidfrom pathlib import Pathimport requestsAPI_KEY = "sk_your_key"BASE = "https://omnidoc.top/api/v1"FILE = Path("report.docx")H = {"X-API-Key": API_KEY}import axios from 'axios';import fs from 'node:fs';import path from 'node:path';const API_KEY = 'sk_your_key';const BASE = 'https://omnidoc.top/api/v1';const filePath = 'report.docx';const apiHeaders = { 'X-API-Key': API_KEY }; -
पता मांगें और फ़ाइल अपलोड करें
Terminal window PRESIGN=$(curl --fail-with-body -sS "$BASE/upload/presign" \-H "X-API-Key: $OMNIDOC_API_KEY" \-H "Content-Type: application/json" \-d "$(jq -n --arg filename "$(basename "$FILE")" --argjson size "$SIZE" \'{filename:$filename,file_size:$size}')")UPLOAD_URL=$(printf '%s' "$PRESIGN" | jq -r '.upload_url')UPLOAD_TYPE=$(printf '%s' "$PRESIGN" | jq -r '.upload_headers["Content-Type"]')FINALIZE_TOKEN=$(printf '%s' "$PRESIGN" | jq -r '.finalize_token')curl --fail-with-body -sS -X PUT "$UPLOAD_URL" \-H "Content-Type: $UPLOAD_TYPE" \-H "If-None-Match: *" \--upload-file "$FILE"presign = requests.post(f"{BASE}/upload/presign",headers=H,json={"filename": FILE.name,"file_size": FILE.stat().st_size,},timeout=20,)presign.raise_for_status()ticket = presign.json()with FILE.open("rb") as stream:put_headers = {**ticket["upload_headers"],"Content-Length": str(FILE.stat().st_size),}uploaded = requests.put(ticket["upload_url"], headers=put_headers, data=stream, timeout=300)uploaded.raise_for_status()const stat = fs.statSync(filePath);const { data: ticket } = await axios.post(`${BASE}/upload/presign`,{filename: path.basename(filePath),file_size: stat.size,},{ headers: apiHeaders });await axios.put(ticket.upload_url, fs.createReadStream(filePath), {headers: { ...ticket.upload_headers, 'Content-Length': stat.size },maxBodyLength: Infinity,}); -
अपलोड की पुष्टि करें और दस्तावेज़ ID प्राप्त करें
Terminal window DOCUMENT=$(curl --fail-with-body -sS "$BASE/upload/finalize" \-H "X-API-Key: $OMNIDOC_API_KEY" \-H "Content-Type: application/json" \-d "$(jq -n --arg token "$FINALIZE_TOKEN" '{finalize_token:$token}')")DOCUMENT_ID=$(printf '%s' "$DOCUMENT" | jq -r '.document.id')finalized = requests.post(f"{BASE}/upload/finalize",headers=H,json={"finalize_token": ticket["finalize_token"]},timeout=30,)finalized.raise_for_status()document_id = finalized.json()["document"]["id"]const { data: finalized } = await axios.post(`${BASE}/upload/finalize`,{ finalize_token: ticket.finalize_token },{ headers: apiHeaders });const documentId = finalized.document.id; -
अनुवाद कार्य बनाएं
Terminal window JOB=$(curl --fail-with-body -sS "$BASE/jobs" \-H "X-API-Key: $OMNIDOC_API_KEY" \-H "Content-Type: application/json" \-H "Idempotency-Key: $(uuidgen)" \-d "$(jq -n --arg document_id "$DOCUMENT_ID" \'{document_id:$document_id,target_lang:"en"}')")JOB_ID=$(printf '%s' "$JOB" | jq -r '.id')job_response = requests.post(f"{BASE}/jobs",headers={**H, "Idempotency-Key": str(uuid.uuid4())},json={"document_id": document_id, "target_lang": "en"},timeout=30,)job_response.raise_for_status()job_id = job_response.json()["id"]const { data: job } = await axios.post(`${BASE}/jobs`,{ document_id: documentId, target_lang: 'en' },{ headers: { ...apiHeaders, 'Idempotency-Key': crypto.randomUUID() } });const jobId = job.id; -
पूर्ण होने तक प्रतीक्षा करें और डाउनलोड करें
import reimport timefrom urllib.parse import unquotewhile True:response = requests.get(f"{BASE}/jobs/{job_id}", headers=H, timeout=20)response.raise_for_status()job = response.json()if job["status"] in {"completed", "failed"}:breaktime.sleep(3)if job["status"] == "failed":raise RuntimeError(job.get("error_msg") or "translation failed")artifacts = requests.get(f"{BASE}/artifacts", headers=H, params={"job_id": job_id}, timeout=20)artifacts.raise_for_status()for artifact in artifacts.json():download = requests.get(f"{BASE}/artifacts/{artifact['id']}/download",headers=H,timeout=300,)download.raise_for_status()fallback = f"{artifact['id']}.{artifact['format']}"disposition = download.headers.get("Content-Disposition", "")utf8 = re.search(r"filename\*=UTF-8''([^;]+)", disposition, re.I)plain = re.search(r'filename="([^"]+)"', disposition, re.I)if utf8:name = unquote(utf8.group(1))elif plain:name = plain.group(1)else:name = fallbacksafe_name = Path(name).name or fallbackPath(safe_name).write_bytes(download.content)
आपको क्या मिलता है
Section titled “आपको क्या मिलता है”स्क्रिप्ट पहले job_id प्रिंट करती है। जब जॉब सफल होता है, तो परिणाम वर्तमान डायरेक्टरी में डाउनलोड किए जाते हैं। जब जॉब completed या failed तक पहुँचता है, तो पोलिंग बंद हो जाती है।
इसमें कितना समय लगता है, यह फ़ाइल के आकार और कतार पर निर्भर करता है। उत्पादन में, सीमा के बिना पोलिंग के बजाय webhook या SSE का उपयोग करें।
उदाहरण स्वचालित रूप से निर्माण अनुरोधों को पुनः प्रयास नहीं करता है। नेटवर्क समय-समाप्ति के बाद, जॉब आईडी, इडेम्पोटेंसी की (idempotency key) और अनुरोध शरीर (request body) को बनाए रखें और पहले स्थिति की जाँच करें। स्क्रिप्ट बंद होने पर निर्मित जॉब रद्द नहीं होती।
जब कुछ विफल होता है
Section titled “जब कुछ विफल होता है”401: जांचें कि पर्यावरण चर सेट है और कुंजी मान्य है।402, या जॉब परquota_exceeded: जांचें कि क्रेडिट बैलेंस (ऋण के बाद) स्टार्ट थ्रेशोल्ड को कवर करता है।403: कुंजी का बजट, इसकी अनुमत सेटिंग्स और संसाधन का मालिक कौन है, यह जाँचें।429: दर सीमा, पूर्ण कतार और खत्म हुए बजट को अलग-अलग पहचानने के लिए व्यावसायिक त्रुटि कोड पढ़ें।- डायरेक्ट-अपलोड पता समाप्त हो गया है या अपलोड विफल हुआ: एक नया अपलोड पता मांगें।
X-Request-Id और अनुरोध पथ को लॉग करें, लेकिन पूरी कुंजी या अपलोड क्रेडेंशियल्स को कभी भी लॉग न करें। सभी त्रुटियाँ और पुनः प्रयास विधियाँ त्रुटि संदर्भ में दी गई हैं।