CodeJackR commited on
Commit
2ea60f3
·
1 Parent(s): d816a26

Input image data format

Browse files
Files changed (1) hide show
  1. handler.py +16 -3
handler.py CHANGED
@@ -33,16 +33,29 @@ class EndpointHandler():
33
  # Handle different input formats
34
  if "inputs" in data:
35
  if isinstance(data["inputs"], str):
 
 
 
 
 
36
  # Base64 encoded image
37
- image_bytes = base64.b64decode(data["inputs"])
38
  elif isinstance(data["inputs"], dict) and "image" in data["inputs"]:
39
  # Nested structure with image field
40
- image_bytes = base64.b64decode(data["inputs"]["image"])
 
 
 
 
41
  else:
42
  raise ValueError("Invalid input format. Expected base64 encoded image string.")
43
  elif "image" in data:
44
  # Direct image field
45
- image_bytes = base64.b64decode(data["image"])
 
 
 
 
46
  else:
47
  raise ValueError("No image found in request. Expected 'inputs' or 'image' field with base64 encoded image.")
48
 
 
33
  # Handle different input formats
34
  if "inputs" in data:
35
  if isinstance(data["inputs"], str):
36
+ # Handle data URL format (data:image/jpeg;base64,...)
37
+ image_data = data["inputs"]
38
+ if image_data.startswith("data:"):
39
+ # Strip data URL prefix
40
+ image_data = image_data.split(",", 1)[1]
41
  # Base64 encoded image
42
+ image_bytes = base64.b64decode(image_data)
43
  elif isinstance(data["inputs"], dict) and "image" in data["inputs"]:
44
  # Nested structure with image field
45
+ image_data = data["inputs"]["image"]
46
+ if image_data.startswith("data:"):
47
+ # Strip data URL prefix
48
+ image_data = image_data.split(",", 1)[1]
49
+ image_bytes = base64.b64decode(image_data)
50
  else:
51
  raise ValueError("Invalid input format. Expected base64 encoded image string.")
52
  elif "image" in data:
53
  # Direct image field
54
+ image_data = data["image"]
55
+ if image_data.startswith("data:"):
56
+ # Strip data URL prefix
57
+ image_data = image_data.split(",", 1)[1]
58
+ image_bytes = base64.b64decode(image_data)
59
  else:
60
  raise ValueError("No image found in request. Expected 'inputs' or 'image' field with base64 encoded image.")
61