Spaces:
Running
on
L4
Running
on
L4
VicFonch
commited on
app.py: try-catch cuda bug when try to execte the model
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ transform = Compose([
|
|
| 21 |
Normalize(mean=[0.5]*3, std=[0.5]*3),
|
| 22 |
])
|
| 23 |
|
| 24 |
-
def to_numpy(img_tensor):
|
| 25 |
img_np = denorm(img_tensor, mean=[0.5]*3, std=[0.5]*3).squeeze().permute(1, 2, 0).cpu().numpy()
|
| 26 |
img_np = np.clip(img_np, 0, 1)
|
| 27 |
return (img_np * 255).astype(np.uint8)
|
|
@@ -30,21 +30,25 @@ def interpolate(img0_pil, img2_pil, tau=0.5, num_samples=1):
|
|
| 30 |
img0 = transform(img0_pil.convert("RGB")).unsqueeze(0).to(device)
|
| 31 |
img2 = transform(img2_pil.convert("RGB")).unsqueeze(0).to(device)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
demo = gr.Interface(
|
| 50 |
fn=interpolate,
|
|
|
|
| 21 |
Normalize(mean=[0.5]*3, std=[0.5]*3),
|
| 22 |
])
|
| 23 |
|
| 24 |
+
def to_numpy(img_tensor: torch.Tensor) -> np.ndarray:
|
| 25 |
img_np = denorm(img_tensor, mean=[0.5]*3, std=[0.5]*3).squeeze().permute(1, 2, 0).cpu().numpy()
|
| 26 |
img_np = np.clip(img_np, 0, 1)
|
| 27 |
return (img_np * 255).astype(np.uint8)
|
|
|
|
| 30 |
img0 = transform(img0_pil.convert("RGB")).unsqueeze(0).to(device)
|
| 31 |
img2 = transform(img2_pil.convert("RGB")).unsqueeze(0).to(device)
|
| 32 |
|
| 33 |
+
try:
|
| 34 |
+
if num_samples == 1:
|
| 35 |
+
# Unique image
|
| 36 |
+
img1 = model.reverse_process([img0, img2], tau)
|
| 37 |
+
return Image.fromarray(to_numpy(img1)), None
|
| 38 |
+
else:
|
| 39 |
+
# Múltiples imágenes → video
|
| 40 |
+
frames = [to_numpy(img0)]
|
| 41 |
+
for t in np.linspace(0, 1, num_samples):
|
| 42 |
+
img = model.reverse_process([img0, img2], float(t))
|
| 43 |
+
frames.append(to_numpy(img))
|
| 44 |
+
frames.append(to_numpy(img2))
|
| 45 |
|
| 46 |
+
temp_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 47 |
+
imageio.mimsave(temp_path, frames, fps=8)
|
| 48 |
+
return None, temp_path
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error during interpolation: {e}")
|
| 51 |
+
return None, None
|
| 52 |
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=interpolate,
|