Spaces:
Running on Zero
Running on Zero
Add error handling to expose exceptions in API responses
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import numpy as np
|
|
| 6 |
import random
|
| 7 |
import spaces
|
| 8 |
import torch
|
|
|
|
| 9 |
from diffusers import Flux2KleinPipeline
|
| 10 |
from PIL import Image
|
| 11 |
|
|
@@ -30,27 +31,45 @@ def infer(
|
|
| 30 |
num_inference_steps: int = 4,
|
| 31 |
guidance_scale: float = 1.0,
|
| 32 |
):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
|
|
|
| 6 |
import random
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
+
import traceback
|
| 10 |
from diffusers import Flux2KleinPipeline
|
| 11 |
from PIL import Image
|
| 12 |
|
|
|
|
| 31 |
num_inference_steps: int = 4,
|
| 32 |
guidance_scale: float = 1.0,
|
| 33 |
):
|
| 34 |
+
try:
|
| 35 |
+
if isinstance(seed, str): seed = int(seed)
|
| 36 |
+
if isinstance(randomize_seed, str): randomize_seed = randomize_seed.lower() == "true"
|
| 37 |
+
width = int(float(width))
|
| 38 |
+
height = int(float(height))
|
| 39 |
+
num_inference_steps = int(float(num_inference_steps))
|
| 40 |
+
if isinstance(guidance_scale, str): guidance_scale = float(guidance_scale)
|
| 41 |
+
if randomize_seed:
|
| 42 |
+
seed = random.randint(0, MAX_SEED)
|
| 43 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 44 |
+
pipe_kwargs = {
|
| 45 |
+
"prompt": prompt,
|
| 46 |
+
"height": height,
|
| 47 |
+
"width": width,
|
| 48 |
+
"num_inference_steps": num_inference_steps,
|
| 49 |
+
"guidance_scale": guidance_scale,
|
| 50 |
+
"generator": generator,
|
| 51 |
+
}
|
| 52 |
+
print(f"input_images type: {type(input_images)}, value: {input_images}")
|
| 53 |
+
if input_images is not None and len(input_images) > 0:
|
| 54 |
+
imgs = []
|
| 55 |
+
for item in input_images:
|
| 56 |
+
if isinstance(item, tuple):
|
| 57 |
+
imgs.append(item[0])
|
| 58 |
+
elif isinstance(item, Image.Image):
|
| 59 |
+
imgs.append(item)
|
| 60 |
+
else:
|
| 61 |
+
print(f"Unknown item type: {type(item)}, value: {item}")
|
| 62 |
+
imgs.append(item)
|
| 63 |
+
pipe_kwargs["image"] = imgs
|
| 64 |
+
print(f"pipe_kwargs keys: {list(pipe_kwargs.keys())}")
|
| 65 |
+
print(f"image count: {len(pipe_kwargs.get('image', []))}")
|
| 66 |
+
result_image = pipe(**pipe_kwargs).images[0]
|
| 67 |
+
return result_image, seed
|
| 68 |
+
except Exception as e:
|
| 69 |
+
tb = traceback.format_exc()
|
| 70 |
+
print(f"ERROR: {e}")
|
| 71 |
+
print(tb)
|
| 72 |
+
raise gr.Error(f"{type(e).__name__}: {e}")
|
| 73 |
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|