Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
116 lines (98 loc) · 4.31 KB
/
Copy pathclient.py
File metadata and controls
116 lines (98 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""CodeFormer API client — thin, dependency-light wrapper over the Synexa prediction API.
Every call creates a prediction, polls it until it settles, and returns the output.
Set SYNEXA_API_KEY in your environment or pass api_key= to Client().
"""
from __future__ import annotations
import os, time, json
from typing import Any, Dict, Optional
import httpx
API_BASE = os.environ.get("SYNEXA_API_BASE", "https://api.synexa.ai")
DEFAULT_MODEL = "sczhou/codeformer"
MODELS = {
"sczhou/codeformer": {
"category": "super-resolution",
"price_usd": 0.0025,
"required": [
"image"
],
"fields": [
"image",
"upscale",
"face_upsample",
"background_enhance",
"codeformer_fidelity"
],
"url": "https://synexa.ai/explore/sczhou/codeformer"
},
"tencentarc/gfpgan": {
"category": "super-resolution",
"price_usd": 0.0008,
"required": [
"img"
],
"fields": [
"img",
"scale",
"version"
],
"url": "https://synexa.ai/explore/tencentarc/gfpgan"
}
}
__all__ = ["Client", "run", "ModelError", "PredictionTimeout", "MODELS", "DEFAULT_MODEL"]
class ModelError(RuntimeError):
"""The prediction finished with status=failed."""
def __init__(self, message: str, prediction: Optional[dict] = None):
super().__init__(message)
self.prediction = prediction
class PredictionTimeout(TimeoutError):
pass
class Client:
def __init__(self, api_key: Optional[str] = None, base_url: str = API_BASE, timeout: float = 60.0):
self.api_key = api_key or os.environ.get("SYNEXA_API_KEY")
if not self.api_key:
raise ValueError("Missing API key: set SYNEXA_API_KEY or pass api_key=")
self.base_url = base_url.rstrip("/")
self._http = httpx.Client(timeout=timeout, headers={"x-api-key": self.api_key, "user-agent": "codeformer_api/0.1.0"})
# -- low level -----------------------------------------------------------------
def create(self, input: Dict[str, Any], model: str = DEFAULT_MODEL, webhook: Optional[str] = None) -> dict:
body = {"model": model, "input": input}
if webhook:
body["webhook"] = webhook
r = self._http.post(f"{self.base_url}/v1/predictions", json=body)
if r.status_code >= 400:
raise ModelError(f"{r.status_code}: {r.text[:300]}")
return r.json()
def get(self, prediction_id: str) -> dict:
r = self._http.get(f"{self.base_url}/v1/predictions/{prediction_id}")
r.raise_for_status()
return r.json()
def wait(self, prediction: dict, timeout: float = 600, interval: float = 2.0) -> dict:
deadline = time.time() + timeout
while prediction.get("status") not in ("succeeded", "failed"):
if time.time() > deadline:
raise PredictionTimeout(f"prediction {prediction.get('id')} still {prediction.get('status')} after {timeout}s")
time.sleep(interval)
prediction = self.get(prediction["id"])
if prediction["status"] == "failed":
raise ModelError(prediction.get("error") or "prediction failed", prediction)
return prediction
# -- high level ----------------------------------------------------------------
def run(self, input: Dict[str, Any], model: str = DEFAULT_MODEL, wait: bool | float = True, webhook: Optional[str] = None):
"""Create a prediction and (by default) block until it finishes. Returns the model output."""
pred = self.create(input, model=model, webhook=webhook)
if wait is False:
return pred
pred = self.wait(pred, timeout=600 if wait is True else float(wait))
return pred.get("output")
def model_info(self, model: str = DEFAULT_MODEL) -> dict:
owner, name = model.split("/", 1)
r = self._http.get(f"{self.base_url}/models/{owner}/{name}")
r.raise_for_status()
return r.json()
_default: Optional[Client] = None
def run(input: Dict[str, Any], model: str = DEFAULT_MODEL, **kw):
"""Module-level shortcut: `codeformer_api.run({...})` using SYNEXA_API_KEY from the environment."""
global _default
if _default is None:
_default = Client()
return _default.run(input, model=model, **kw)