Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,37 +19,17 @@ def style_transfer(img):
|
|
| 19 |
if img is None:
|
| 20 |
return None
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
|
|
|
|
| 48 |
|
| 49 |
-
input_tensor = transform(img).unsqueeze(0)
|
| 50 |
with torch.no_grad():
|
| 51 |
output_tensor = model(input_tensor)
|
| 52 |
-
output_tensor = (output_tensor.squeeze(0) * 0.5 + 0.5).clamp(0, 1)
|
| 53 |
return transforms.ToPILImage()(output_tensor)
|
| 54 |
|
| 55 |
demo = gr.Interface(
|
|
|
|
| 19 |
if img is None:
|
| 20 |
return None
|
| 21 |
|
| 22 |
+
if isinstance(img, str) and img.startswith("data:image/png;base64,"):
|
| 23 |
+
img = decode_base64_img(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
elif isinstance(img, np.ndarray):
|
| 25 |
img = Image.fromarray((img * 255).astype(np.uint8))
|
| 26 |
+
elif not isinstance(img, Image.Image):
|
| 27 |
+
raise TypeError(f"Unexpected input type: {type(img)}")
|
| 28 |
|
| 29 |
+
input_tensor = transform(img).unsqueeze(0)
|
| 30 |
with torch.no_grad():
|
| 31 |
output_tensor = model(input_tensor)
|
| 32 |
+
output_tensor = (output_tensor.squeeze(0) * 0.5 + 0.5).clamp(0, 1)
|
| 33 |
return transforms.ToPILImage()(output_tensor)
|
| 34 |
|
| 35 |
demo = gr.Interface(
|