Instructions to use zlymon/my-flux-upscaler-endpoint with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use zlymon/my-flux-upscaler-endpoint with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("zlymon/my-flux-upscaler-endpoint", dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Notebooks
- Google Colab
- Kaggle
lymon commited on
Commit ·
785e158
1
Parent(s): e2c01cb
fix
Browse files- handler.py +22 -7
handler.py
CHANGED
|
@@ -23,14 +23,29 @@ class EndpointHandler:
|
|
| 23 |
)
|
| 24 |
self.pipe.to("cuda")
|
| 25 |
|
| 26 |
-
def __call__(self, data: Dict[str, Any]) ->
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# 可选:调整尺寸(默认 4× 放大)
|
| 36 |
w, h = control_image.size
|
|
|
|
| 23 |
)
|
| 24 |
self.pipe.to("cuda")
|
| 25 |
|
| 26 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 27 |
+
# 兼容包装与非包装请求体
|
| 28 |
+
payload = data.get("inputs", data) # 支持 UI 及直接 POST 两种格式
|
| 29 |
+
|
| 30 |
+
# 读取图像字节
|
| 31 |
+
img_bytes = None
|
| 32 |
+
url = payload.get("control_image_url")
|
| 33 |
+
b64 = payload.get("control_image_base64")
|
| 34 |
+
if b64:
|
| 35 |
+
img_bytes = base64.b64decode(b64)
|
| 36 |
+
elif url:
|
| 37 |
+
resp = requests.get(url)
|
| 38 |
+
resp.raise_for_status() # 捕获 4xx/5xx 错误 :contentReference[oaicite:3]{index=3}
|
| 39 |
+
img_bytes = resp.content
|
| 40 |
else:
|
| 41 |
+
raise ValueError("请在 inputs 中提供 control_image_url 或 control_image_base64")
|
| 42 |
+
|
| 43 |
+
# 用 PIL 解析并转换
|
| 44 |
+
try:
|
| 45 |
+
control_image = Image.open(BytesIO(img_bytes)).convert("RGB")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
# 可能是数据损坏或格式不符
|
| 48 |
+
raise ValueError(f"无法识别图像文件: {str(e)}")
|
| 49 |
|
| 50 |
# 可选:调整尺寸(默认 4× 放大)
|
| 51 |
w, h = control_image.size
|