Python
इस पृष्ठ में requests का उपयोग किया गया है और इसे किसी समर्पित SDK की आवश्यकता नहीं है।
स्थापित करें
Section titled “स्थापित करें”pip install requestsसंपूर्ण उदाहरण
Section titled “संपूर्ण उदाहरण”from __future__ import annotations
import reimport timeimport uuidfrom pathlib import Pathfrom urllib.parse import unquote
import requests
API_KEY = "sk_your_key"BASE = "https://omnidoc.top/api/v1"API_HEADERS = {"X-API-Key": API_KEY}
def upload_document(file_path: Path) -> dict: size = file_path.stat().st_size response = requests.post( f"{BASE}/upload/presign", headers=API_HEADERS, json={ "filename": file_path.name, "file_size": size, }, timeout=20, ) response.raise_for_status() ticket = response.json()
with file_path.open("rb") as stream: put_headers = {**ticket["upload_headers"], "Content-Length": str(size)} uploaded = requests.put( ticket["upload_url"], headers=put_headers, data=stream, timeout=300, ) uploaded.raise_for_status()
finalized = requests.post( f"{BASE}/upload/finalize", headers=API_HEADERS, json={"finalize_token": ticket["finalize_token"]}, timeout=30, ) finalized.raise_for_status() return finalized.json()["document"]
def create_job(document_id: str, target_lang: str, engine: str) -> dict: response = requests.post( f"{BASE}/jobs", headers={**API_HEADERS, "Idempotency-Key": str(uuid.uuid4())}, json={ "document_id": document_id, "target_lang": target_lang, "engine": engine, }, timeout=30, ) response.raise_for_status() return response.json()
def wait_for_job(job_id: str) -> dict: while True: response = requests.get( f"{BASE}/jobs/{job_id}", headers=API_HEADERS, timeout=20 ) response.raise_for_status() job = response.json() if job["status"] == "completed": return job if job["status"] == "failed": raise RuntimeError(job.get("error_msg") or "translation failed") print(f"{job.get('stage', 'processing')}: {job.get('stage_progress', 0)}%") time.sleep(3)
def response_filename(response: requests.Response, fallback: str) -> str: disposition = response.headers.get("Content-Disposition", "") utf8 = re.search(r"filename\*=UTF-8''([^;]+)", disposition, re.I) if utf8: return Path(unquote(utf8.group(1))).name or fallback ascii_name = re.search(r'filename="([^"]+)"', disposition, re.I) if ascii_name: return Path(ascii_name.group(1)).name or fallback return fallback
def download_artifacts(job_id: str, output_dir: Path) -> list[Path]: output_dir.mkdir(parents=True, exist_ok=True) response = requests.get( f"{BASE}/artifacts", headers=API_HEADERS, params={"job_id": job_id}, timeout=20, ) response.raise_for_status()
results: list[Path] = [] for artifact in response.json(): download = requests.get( f"{BASE}/artifacts/{artifact['id']}/download", headers=API_HEADERS, timeout=300, ) download.raise_for_status() filename = response_filename( download, f"{artifact['id']}.{artifact['format']}" ) destination = output_dir / filename destination.write_bytes(download.content) results.append(destination) return results
def translate_document( source: str, *, target_lang: str = "en", engine: str = "xr-397b",) -> list[Path]: document = upload_document(Path(source)) job = create_job(document["id"], target_lang, engine) wait_for_job(job["id"]) return download_artifacts(job["id"], Path("omnidoc-output"))
if __name__ == "__main__": for result in translate_document("report.docx", target_lang="en"): print(result)उत्पादन संबंधी सलाह
Section titled “उत्पादन संबंधी सलाह”- कनेक्शन, रीडिंग और कार्य के लिए समग्र प्रतीक्षा के लिए अलग-अलग टाइमआउट सेट करें; हर चरण को एक असीमित टाइमआउट से कवर न करें।
- जब अपलोड PUT विफल होता है, तो एक नया अपलोड प्राधिकरण मांगें और समाप्त हुए पते को तुरंत नष्ट कर दें।
- प्रत्येक नए सबमिशन के लिए एक नया
Idempotency-Keyजनरेट करें, और केवल उस सबमिशन को दोबारा प्रयास करते समय ही मूल कुंजी का उपयोग करें। Retry-Afterके अनुसार429पर सीमा के साथ पीछे हटें- एक बार में सभी बाइट्स को मेमोरी में लोड करने के बजाय बड़े परिणामों को स्ट्रीम करें