import json import os from pathlib import Path import requests BASE_URL = os.getenv("INSTRUCTION_API_URL", "http://localhost:7860").rstrip("/") headers = {} if os.getenv("API_KEY"): headers["X-API-Key"] = os.environ["API_KEY"] payload = json.loads(Path(__file__).with_name("example_request.json").read_text(encoding="utf-8")) for endpoint in ["/health", "/model"]: response = requests.get(f"{BASE_URL}{endpoint}", timeout=30) print(endpoint, response.status_code) print(json.dumps(response.json(), indent=2, ensure_ascii=False)) response.raise_for_status() response = requests.post(f"{BASE_URL}/generate", json=payload, headers=headers, timeout=360) print("/generate", response.status_code) print(json.dumps(response.json(), indent=2, ensure_ascii=False)) response.raise_for_status() assert 1 <= len(response.json()["output"]["instructions"]) <= 5 print("Instruction API test passed.")