Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,12 +19,32 @@ def style_transfer(img):
|
|
| 19 |
if img is None:
|
| 20 |
return None
|
| 21 |
|
| 22 |
-
#
|
| 23 |
if isinstance(img, dict):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
img_data = np.array(img_data)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
input_tensor = transform(img).unsqueeze(0) # Add batch dimension
|
| 30 |
with torch.no_grad():
|
|
|
|
| 19 |
if img is None:
|
| 20 |
return None
|
| 21 |
|
| 22 |
+
# If img is a dict, extract the image array correctly
|
| 23 |
if isinstance(img, dict):
|
| 24 |
+
# Sketchpad returns: {'image': <numpy array> or base64 string}
|
| 25 |
+
img_data = img.get("image")
|
| 26 |
+
if isinstance(img_data, dict):
|
| 27 |
+
# Sometimes nested dict - extract actual array
|
| 28 |
+
img_data = img_data.get("data", img_data)
|
| 29 |
+
|
| 30 |
+
# If img_data is a numpy array, convert directly
|
| 31 |
+
if isinstance(img_data, (list, tuple)):
|
| 32 |
+
# In some cases img_data could be nested lists
|
| 33 |
img_data = np.array(img_data)
|
| 34 |
+
|
| 35 |
+
if isinstance(img_data, np.ndarray):
|
| 36 |
+
img = Image.fromarray((img_data * 255).astype(np.uint8))
|
| 37 |
+
elif isinstance(img_data, str):
|
| 38 |
+
# If base64 string, decode it
|
| 39 |
+
import base64
|
| 40 |
+
from io import BytesIO
|
| 41 |
+
decoded = base64.b64decode(img_data.split(",")[1])
|
| 42 |
+
img = Image.open(BytesIO(decoded))
|
| 43 |
+
else:
|
| 44 |
+
raise ValueError(f"Unexpected image data type: {type(img_data)}")
|
| 45 |
+
elif isinstance(img, np.ndarray):
|
| 46 |
+
img = Image.fromarray((img * 255).astype(np.uint8))
|
| 47 |
+
# else assume PIL Image
|
| 48 |
|
| 49 |
input_tensor = transform(img).unsqueeze(0) # Add batch dimension
|
| 50 |
with torch.no_grad():
|