Update handler.py
Browse files- handler.py +14 -22
handler.py
CHANGED
@@ -11,7 +11,7 @@ class EndpointHandler:
|
|
11 |
|
12 |
# Load Flux Kontext model from Hugging Face Hub
|
13 |
self.pipe = FluxKontextPipeline.from_pretrained(
|
14 |
-
"black-forest-labs/FLUX.1-Kontext-dev", # replace
|
15 |
torch_dtype=torch.float16,
|
16 |
)
|
17 |
self.pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -20,40 +20,32 @@ class EndpointHandler:
|
|
20 |
def __call__(self, data: Dict) -> Dict:
|
21 |
print("🔧 Received data:", data)
|
22 |
|
|
|
23 |
inputs = data.get("inputs")
|
24 |
-
if not inputs:
|
25 |
-
return {"error": "'inputs'
|
26 |
-
|
27 |
-
if not isinstance(inputs, dict):
|
28 |
-
return {"error": "'inputs' must be a JSON object with 'prompt' and optionally 'image'."}
|
29 |
|
30 |
prompt = inputs.get("prompt")
|
31 |
image_input = inputs.get("image")
|
32 |
|
33 |
if not prompt:
|
34 |
-
return {"error": "
|
|
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
image_bytes = base64.b64decode(image_input)
|
43 |
-
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
44 |
-
except Exception as e:
|
45 |
-
return {"error": f"Failed to decode base64 image input: {str(e)}"}
|
46 |
-
elif isinstance(image_input, Image.Image):
|
47 |
-
image = image_input
|
48 |
-
else:
|
49 |
-
return {"error": "'image' must be a base64 string or a PIL.Image object."}
|
50 |
|
51 |
# Generate edited image with Kontext
|
52 |
try:
|
53 |
output = self.pipe(
|
54 |
prompt=prompt,
|
55 |
image=image,
|
56 |
-
num_inference_steps=28, #
|
57 |
guidance_scale=3.5
|
58 |
).images[0]
|
59 |
print("🎨 Image generated.")
|
|
|
11 |
|
12 |
# Load Flux Kontext model from Hugging Face Hub
|
13 |
self.pipe = FluxKontextPipeline.from_pretrained(
|
14 |
+
"black-forest-labs/FLUX.1-Kontext-dev", # replace if using your own model repo
|
15 |
torch_dtype=torch.float16,
|
16 |
)
|
17 |
self.pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
20 |
def __call__(self, data: Dict) -> Dict:
|
21 |
print("🔧 Received data:", data)
|
22 |
|
23 |
+
# Validate data structure
|
24 |
inputs = data.get("inputs")
|
25 |
+
if not inputs or not isinstance(inputs, dict):
|
26 |
+
return {"error": "'inputs' must be a JSON object containing 'prompt' and 'image'."}
|
|
|
|
|
|
|
27 |
|
28 |
prompt = inputs.get("prompt")
|
29 |
image_input = inputs.get("image")
|
30 |
|
31 |
if not prompt:
|
32 |
+
return {"error": "'prompt' is required in 'inputs'."}
|
33 |
+
if not image_input:
|
34 |
+
return {"error": "'image' (base64 encoded string) is required in 'inputs'."}
|
35 |
|
36 |
+
# Decode image from base64
|
37 |
+
try:
|
38 |
+
image_bytes = base64.b64decode(image_input)
|
39 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
40 |
+
except Exception as e:
|
41 |
+
return {"error": f"Failed to decode 'image' input as base64: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Generate edited image with Kontext
|
44 |
try:
|
45 |
output = self.pipe(
|
46 |
prompt=prompt,
|
47 |
image=image,
|
48 |
+
num_inference_steps=28, # Kontext standard
|
49 |
guidance_scale=3.5
|
50 |
).images[0]
|
51 |
print("🎨 Image generated.")
|