sethchitty commited on
Commit
1878ae8
·
verified ·
1 Parent(s): d91f705

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -120
app.py DELETED
@@ -1,120 +0,0 @@
1
-
2
- import gradio as gr
3
- import torch
4
- from diffusers import AutoPipelineForText2Image, DDIMScheduler
5
- from transformers import CLIPVisionModelWithProjection
6
- from diffusers.utils import load_image
7
- from PIL import Image
8
- import os
9
- import json
10
- import gc
11
- import traceback
12
-
13
- STYLE_MAP = {
14
- "pixar": [
15
- "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img0.png",
16
- "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img1.png",
17
- "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img2.png",
18
- "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img3.png",
19
- "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img4.png"
20
- ]
21
- }
22
-
23
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
24
- device = "cuda" if torch.cuda.is_available() else "cpu"
25
-
26
- print(f"🚀 Device: {device}, torch_dtype: {torch_dtype}")
27
-
28
- image_encoder = CLIPVisionModelWithProjection.from_pretrained(
29
- "h94/IP-Adapter",
30
- subfolder="models/image_encoder",
31
- torch_dtype=torch_dtype,
32
- )
33
-
34
- pipeline = AutoPipelineForText2Image.from_pretrained(
35
- "stabilityai/stable-diffusion-xl-base-1.0",
36
- torch_dtype=torch_dtype,
37
- image_encoder=image_encoder,
38
- variant="fp16" if torch.cuda.is_available() else None
39
- ).to(device)
40
-
41
- pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
42
- pipeline.load_ip_adapter(
43
- "h94/IP-Adapter",
44
- subfolder="sdxl_models",
45
- weight_name=[
46
- "ip-adapter-plus_sdxl_vit-h.safetensors",
47
- "ip-adapter-plus-face_sdxl_vit-h.safetensors"
48
- ]
49
- )
50
- pipeline.set_ip_adapter_scale([0.7, 0.3])
51
- pipeline.enable_model_cpu_offload()
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=512,
77
- height=768,
78
- guidance_scale=7.5,
79
- num_inference_steps=20,
80
- generator=torch.Generator(device).manual_seed(i + 42)
81
- )
82
-
83
- print(f"⚙️ Pipeline result type: {type(result)}")
84
- image = result.images[0]
85
- print(f"🖼️ Image type: {type(image)}")
86
-
87
- if isinstance(image, Image.Image):
88
- images.append(image)
89
- print(f"✅ Scene {i+1} generated successfully.")
90
- else:
91
- print(f"⚠️ Invalid image type for scene {i+1}")
92
-
93
- except Exception as e:
94
- print(f"❌ Exception during scene {i+1}: {e}")
95
- traceback.print_exc()
96
-
97
- # TEMPORARY: Stop after first image for testing
98
- break
99
-
100
- print(f"📦 Returning {len(images)} image(s)")
101
- return images
102
-
103
- def generate_storybook_from_textbox(json_input_text):
104
- try:
105
- data = json.loads(json_input_text)
106
- return generate_storybook(data)
107
- except Exception as e:
108
- print(f"❌ JSON parse or generation error: {e}")
109
- traceback.print_exc()
110
- return [f"Error: {str(e)}"]
111
-
112
- iface = gr.Interface(
113
- fn=generate_storybook_from_textbox,
114
- inputs=gr.Textbox(label="Input JSON", lines=20, placeholder="{...}"),
115
- outputs=gr.Gallery(label="Generated Story Scenes", show_label=True, columns=1),
116
- title="AI Storybook Generator (Enhanced Debug)",
117
- description="Paste JSON to generate images with logging and debugging."
118
- )
119
-
120
- iface.launch()