sethchitty commited on
Commit
8ab153c
·
verified ·
1 Parent(s): ca6bb8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -52
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 generate_storybook(data):
54
- print("📥 Input JSON received:")
55
  print(json.dumps(data, indent=2))
56
 
57
- character_image_url = data["character_image_url"]
58
- style = data["style"]
59
- scenes = data["scenes"]
60
-
61
- face_image = load_image(character_image_url)
62
- style_images = [load_image(url) for url in STYLE_MAP.get(style, [])]
63
-
64
- images = []
65
-
66
- for i, prompt in enumerate(scenes):
67
- print(f"🎬 Generating scene {i+1}: {prompt}")
68
- try:
69
- torch.cuda.empty_cache()
70
- gc.collect()
71
-
72
- result = pipeline(
73
- prompt=prompt,
74
- ip_adapter_image=[style_images, face_image],
75
- negative_prompt="blurry, bad anatomy, low quality",
76
- width=448,
77
- height=672,
78
- guidance_scale=5.0,
79
- num_inference_steps=15,
80
- generator=torch.Generator(device).manual_seed(i + 42)
81
- )
82
-
83
- image = result.images[0] if hasattr(result, "images") else result
84
- print(f"🖼️ Image type: {type(image)}")
85
-
86
- if isinstance(image, Image.Image):
87
- images.append(image)
88
- print(f"✅ Scene {i+1} added to image list.")
89
- else:
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 generate_storybook(data)
103
  except Exception as e:
104
  print(f"❌ JSON parse or generation error: {e}")
105
  traceback.print_exc()
106
- return [f"Error: {str(e)}"]
107
 
108
  iface = gr.Interface(
109
- fn=generate_storybook_from_textbox,
110
- inputs=gr.Textbox(label="Input JSON", lines=20, placeholder="{...}"),
111
- outputs=gr.Gallery(label="Generated Story Scenes", show_label=True, columns=1),
112
- title="AI Storybook Generator (Low VRAM Mode)",
113
- description="Optimized for lower VRAM GPUs. Paste JSON to generate consistent scenes."
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()