Spaces:
Sleeping
Sleeping
Raghava Pulugu
Optimize HuggingFace CPU backend: Default to InstructPix2Pix with DPM scheduler, bypass custom model, and set CPU step bounds
7fdfb1a | import http.client, json, uuid, os, base64, pathlib | |
| file_path = 'outputs/demo.png' | |
| if not os.path.exists(file_path): | |
| raise SystemExit('demo image not found') | |
| with open(file_path, 'rb') as f: | |
| file_data = f.read() | |
| boundary = '----WebKitFormBoundary' + uuid.uuid4().hex | |
| body = bytearray() | |
| body.extend(f'--{boundary}\r\n'.encode()) | |
| body.extend(f'Content-Disposition: form-data; name="image"; filename="{os.path.basename(file_path)}"\r\n'.encode()) | |
| body.extend(b'Content-Type: image/png\r\n\r\n') | |
| body.extend(file_data) | |
| body.extend(b'\r\n') | |
| body.extend(f'--{boundary}\r\n'.encode()) | |
| body.extend(b'Content-Disposition: form-data; name="prompt"\r\n\r\n') | |
| body.extend(b'change outfit to bikini beach style\r\n') | |
| body.extend(f'--{boundary}--\r\n'.encode()) | |
| headers = { | |
| 'Content-Type': f'multipart/form-data; boundary={boundary}', | |
| 'Content-Length': str(len(body)), | |
| } | |
| conn = http.client.HTTPConnection('127.0.0.1', 8000, timeout=600) | |
| conn.request('POST', '/edit', body, headers) | |
| res = conn.getresponse() | |
| res_body = res.read() | |
| conn.close() | |
| try: | |
| j = json.loads(res_body) | |
| if j.get('success') and j.get('image_base64'): | |
| img_b = base64.b64decode(j['image_base64']) | |
| pathlib.Path('outputs/bikini_edit.png').write_bytes(img_b) | |
| print('[OK] Saved outputs/bikini_edit.png size:', pathlib.Path('outputs/bikini_edit.png').stat().st_size) | |
| msg = j.get('message', '').replace('\u2192', '->') | |
| print('[OK] Message:', msg) | |
| else: | |
| print('[ERROR] Response has no image:', j) | |
| except Exception as e: | |
| print('[ERROR] Error:', e) | |