o2o-plagiarism-ai/scripts/sample_python.py
hbyang 4913bf3ecc API Key 인증 완전 제거
사내 운영 전제로 X-API-Key 인증을 전면 제거.
- app/core/auth.py 삭제
- routes.py에서 Depends(require_api_key) 모두 제거
- config.Settings에서 api_keys / api_key_set 제거
- .env / .env.example에서 API_KEYS 제거
- docker-compose.yml에서 API_KEYS 환경변수 제거 (KOSIMCSE_MODEL 추가)
- UI(index.html)에서 DEFAULT_API_KEY 상수 + X-API-Key 헤더 모두 제거
- scripts/sample_curl.sh, sample_python.py에서 키 헤더 제거
- tests: test_detect_requires_api_key → test_detect_no_auth_required로 갱신
- README: 인증 컬럼 제거, curl 예시에서 헤더 제거
2026-05-14 08:58:28 +09:00

81 lines
2.6 KiB
Python

"""커뮤니케이션북스/바이칼 측 통합 참조용 샘플.
표준 라이브러리만 사용하므로 별도 의존성 설치 불필요.
"""
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()