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-ൽ മടങ്ങുക - എല്ലാ ബൈറ്റുകളും ഒരേസമയം മെമ്മറിയിലേക്ക് ലോഡുചെയ്യുന്നതിന് പകരം വലിയ ഫലങ്ങൾ സ്ട്രീം ചെയ്യുക