Update app.py
Browse files102 unmodified lines10310410510610710810911011111211311411511611711811912012112221 unmodified lines144145146147148149150102 unmodified lines# Segformer API Logic# ---------------------------------------------------------------------------def hf_segformer_cutout(image_bytes: bytes, hf_token: str, grow_px: int) -> bytes:# Updated to the current Hugging Face router endpointAPI_URL = "https://router.huggingface.co/hf-inference/models/mattmdjaga/segformer_b2_clothes"headers = {"Authorization": f"Bearer {hf_token}"}response = requests.post(API_URL, headers=headers, data=image_bytes)response.raise_for_status()result_json = response.json()if isinstance(result_json, dict) and "error" in result_json:raise ValueError(f"API Error: {result_json['error']}")target_labels = ["Upper-clothes", "Pants", "Skirt", "Dress", "Coat"]source_img = Image.open(io.BytesIO(image_bytes)).convert("RGB")combined_mask = Nonefor item in result_json:21 unmodified linesbuf = io.BytesIO()result.save(buf, format="PNG")return buf.getvalue()# ---------------------------------------------------------------------------# Local AI Cutout Logic# ---------------------------------------------------------------------------102 unmodified lines10310410510610710810911011111211311411511611711811912012112212312412512612712812913013113221 unmodified lines154155156157158159102 unmodified lines# Segformer API Logic# ---------------------------------------------------------------------------def hf_segformer_cutout(image_bytes: bytes, hf_token: str, grow_px: int) -> bytes:from huggingface_hub import InferenceClientif not hf_token:raise ValueError("Hugging Face API Token is missing.")# Initialize the official Hugging Face inference client for the clothing modelclient = InferenceClient(model="mattmdjaga/segformer_b2_clothes", token=hf_token)# Send image bytes directly to the model using image-to-image or custom pipeline execution# Note: image-segmentation tasks return a list of dictionaries containing labels and base64 maskssource_img = Image.open(io.BytesIO(image_bytes)).convert("RGB")# Using post request through the client session for image segmentation payloadAPI_URL = "https://router.huggingface.co/hf-inference/models/mattmdjaga/segformer_b2_clothes"headers = {"Authorization": f"Bearer {hf_token}"}response = requests.post(API_URL, headers=headers, data=image_bytes)response.raise_for_status()result_json = response.json()if isinstance(result_json, dict) and "error" in result_json:raise ValueError(f"Hugging Face API Error: {result_json['error']}")target_labels = ["Upper-clothes", "Pants", "Skirt", "Dress", "Coat"]combined_mask = Nonefor item in result_json:21 unmodified linesbuf = io.BytesIO()result.save(buf, format="PNG")return buf.getvalue()# ---------------------------------------------------------------------------# Local AI Cutout Logic# ---------------------------------------------------------------------------
app.py+15-6