Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,67 +50,57 @@ pipeline.set_ip_adapter_scale([0.7, 0.3])
|
|
| 50 |
pipeline.enable_model_cpu_offload()
|
| 51 |
pipeline.enable_vae_tiling()
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
print("📥
|
| 55 |
print(json.dumps(data, indent=2))
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
print(f"⚠️ Scene {i+1} is not a valid image object.")
|
| 91 |
-
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"❌ Exception during scene {i+1}: {e}")
|
| 94 |
-
traceback.print_exc()
|
| 95 |
-
|
| 96 |
-
print(f"📦 Returning {len(images)} image(s)")
|
| 97 |
-
return images
|
| 98 |
-
|
| 99 |
-
def generate_storybook_from_textbox(json_input_text):
|
| 100 |
try:
|
| 101 |
data = json.loads(json_input_text)
|
| 102 |
-
return
|
| 103 |
except Exception as e:
|
| 104 |
print(f"❌ JSON parse or generation error: {e}")
|
| 105 |
traceback.print_exc()
|
| 106 |
-
return
|
| 107 |
|
| 108 |
iface = gr.Interface(
|
| 109 |
-
fn=
|
| 110 |
-
inputs=gr.Textbox(label="Input JSON", lines=
|
| 111 |
-
outputs=gr.
|
| 112 |
-
title="
|
| 113 |
-
description="
|
| 114 |
)
|
| 115 |
|
| 116 |
iface.launch()
|
|
|
|
| 50 |
pipeline.enable_model_cpu_offload()
|
| 51 |
pipeline.enable_vae_tiling()
|
| 52 |
|
| 53 |
+
def generate_single_scene(data):
|
| 54 |
+
print("📥 Single scene input received:")
|
| 55 |
print(json.dumps(data, indent=2))
|
| 56 |
|
| 57 |
+
try:
|
| 58 |
+
character_image_url = data["character_image_url"]
|
| 59 |
+
style = data["style"]
|
| 60 |
+
scene_prompt = data["scene"]
|
| 61 |
+
|
| 62 |
+
face_image = load_image(character_image_url)
|
| 63 |
+
style_images = [load_image(url) for url in STYLE_MAP.get(style, [])]
|
| 64 |
+
|
| 65 |
+
torch.cuda.empty_cache()
|
| 66 |
+
gc.collect()
|
| 67 |
+
|
| 68 |
+
result = pipeline(
|
| 69 |
+
prompt=scene_prompt,
|
| 70 |
+
ip_adapter_image=[style_images, face_image],
|
| 71 |
+
negative_prompt="blurry, bad anatomy, low quality",
|
| 72 |
+
width=512,
|
| 73 |
+
height=768,
|
| 74 |
+
guidance_scale=5.0,
|
| 75 |
+
num_inference_steps=15,
|
| 76 |
+
generator=torch.Generator(device).manual_seed(42)
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
image = result.images[0] if hasattr(result, "images") else result
|
| 80 |
+
print(f"✅ Scene generated. Type: {type(image)}")
|
| 81 |
+
|
| 82 |
+
return image
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"❌ Error: {e}")
|
| 86 |
+
traceback.print_exc()
|
| 87 |
+
return None
|
| 88 |
+
|
| 89 |
+
def generate_from_json(json_input_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
data = json.loads(json_input_text)
|
| 92 |
+
return generate_single_scene(data)
|
| 93 |
except Exception as e:
|
| 94 |
print(f"❌ JSON parse or generation error: {e}")
|
| 95 |
traceback.print_exc()
|
| 96 |
+
return None
|
| 97 |
|
| 98 |
iface = gr.Interface(
|
| 99 |
+
fn=generate_from_json,
|
| 100 |
+
inputs=gr.Textbox(label="Input JSON", lines=10, placeholder='{"character_image_url": "...", "style": "pixar", "scene": "..."}'),
|
| 101 |
+
outputs=gr.Image(label="Generated Scene"),
|
| 102 |
+
title="Single-Scene Storybook Generator",
|
| 103 |
+
description="Send one scene at a time to generate consistent character-based images."
|
| 104 |
)
|
| 105 |
|
| 106 |
iface.launch()
|