Download examples/free-api.py from ServiceX/PDF: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/ServiceX/PDF/resolve/main/examples/free-api.py
- Command line
-
hf download hf://spaces/ServiceX/PDF/examples/free-api.py
-
curl -L -o free-api.py https://huggingface.co/spaces/ServiceX/PDF/resolve/main/examples/free-api.py
1.29 kB
| #!/usr/bin/env python3 | |
| """最小版 api1 主接口调用示例。""" | |
| from __future__ import annotations | |
| import json | |
| import httpx | |
| BASE_URL = "https://api1.pdf2zh-next.com/chatproxy" | |
| TIMEOUT_SECONDS = 30.0 | |
| def build_translate_prompt(text: str, lang_out: str = "zh") -> str: | |
| """复刻上游的翻译 prompt。""" | |
| return ( | |
| r"You are a professional,authentic machine translation engine." | |
| "\n\n" | |
| + r";; Treat next line as plain text input and translate it into " | |
| + lang_out | |
| + r", output translation ONLY. If translation is unnecessary " | |
| + r"(e.g. proper nouns, codes, {{1}}, etc. ), return the original text. " | |
| + r"NO explanations. NO notes. Input:" | |
| + "\n\n" | |
| + text | |
| ) | |
| def main() -> None: | |
| """直接调用主接口。""" | |
| payload = { | |
| "text": build_translate_prompt("Hello world"), | |
| } | |
| with httpx.Client(timeout=TIMEOUT_SECONDS) as client: | |
| response = client.post(BASE_URL, json=payload) | |
| response.raise_for_status() | |
| body = response.json() | |
| print("=== request ===") | |
| print(json.dumps(payload, ensure_ascii=False, indent=2)) | |
| print("\n=== response ===") | |
| print(json.dumps(body, ensure_ascii=False, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |