"""커뮤니케이션북스/바이칼 측 통합 참조용 샘플. 표준 라이브러리만 사용하므로 별도 의존성 설치 불필요. """ from __future__ import annotations import json import os import time import urllib.request API_HOST = os.environ.get("API_HOST", "http://localhost:8000") def _post(path: str, body: dict) -> dict: req = urllib.request.Request( f"{API_HOST}{path}", data=json.dumps(body).encode("utf-8"), headers={"Content-Type": "application/json"}, method="POST", ) with urllib.request.urlopen(req) as resp: return json.loads(resp.read()) def _get(path: str) -> dict: req = urllib.request.Request(f"{API_HOST}{path}") with urllib.request.urlopen(req) as resp: return json.loads(resp.read()) def detect_one() -> None: result = _post( "/v1/plagiarism/detect", { "doc_id": "sample-py-001", "text": ( "어린왕자는 작은 별 B-612에서 온 소년이다. 그는 별을 떠나 여러 행성을 여행한다. " "마침내 지구에 도착해 사막에서 비행기 조종사를 만나고 여우와 친구가 된다." ), "metadata": {"title": "샘플 작품", "author": "tester"}, }, ) print("[detect] is_infringement:", result["is_infringement"]) print("[detect] confidence:", result["confidence"]) for m in result["matches"]: print(f" - {m['source_title']} ({m['similarity']}) :: {m['infringement_type']}") def detect_batch() -> None: created = _post( "/v1/plagiarism/batch", { "items": [ {"doc_id": "b-001", "text": "앤 셜리는 초록 지붕 집에 입양된 상상력 풍부한 소녀다."}, {"doc_id": "b-002", "text": "홍길동은 활빈당을 만들어 부패한 관리의 재물을 빼앗는다."}, ] }, ) job_id = created["job_id"] print(f"[batch] job_id={job_id}, total={created['total']}") for _ in range(20): status = _get(f"/v1/plagiarism/batch/{job_id}") print(f"[batch] status={status['status']} processed={status['processed']}/{status['total']}") if status["status"] in ("completed", "failed"): print("[batch] results:") print(json.dumps(status.get("results"), ensure_ascii=False, indent=2)) return time.sleep(0.5) if __name__ == "__main__": print("=== health ===") print(json.dumps(_get("/v1/health"), ensure_ascii=False, indent=2)) print() print("=== detect (단건) ===") detect_one() print() print("=== batch (비동기) ===") detect_batch()