Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,7 +56,7 @@ pipe = FluxPipeline.from_pretrained(
|
|
| 56 |
use_auth_token=HF_TOKEN
|
| 57 |
)
|
| 58 |
|
| 59 |
-
# Hyper-SD LoRA ๋ก๋
|
| 60 |
pipe.load_lora_weights(
|
| 61 |
hf_hub_download(
|
| 62 |
"ByteDance/Hyper-SD",
|
|
@@ -89,10 +89,6 @@ def save_image(image):
|
|
| 89 |
image = Image.fromarray(image)
|
| 90 |
image.save(filepath, "PNG", quality=100)
|
| 91 |
|
| 92 |
-
if not os.path.exists(filepath):
|
| 93 |
-
print(f"Warning: Failed to verify saved image at {filepath}")
|
| 94 |
-
return None
|
| 95 |
-
|
| 96 |
return filepath
|
| 97 |
except Exception as e:
|
| 98 |
print(f"Failed to save image: {str(e)}")
|
|
@@ -102,13 +98,102 @@ def save_image(image):
|
|
| 102 |
print(f"Error in save_image: {str(e)}")
|
| 103 |
return None
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
with gr.Column(scale=3):
|
| 109 |
prompt = gr.Textbox(
|
| 110 |
label="Image Description",
|
| 111 |
-
placeholder="Describe the image you want to create...",
|
| 112 |
lines=3
|
| 113 |
)
|
| 114 |
|
|
@@ -145,9 +230,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 145 |
value=3.5
|
| 146 |
)
|
| 147 |
|
| 148 |
-
def get_random_seed():
|
| 149 |
-
return torch.randint(0, 1000000, (1,)).item()
|
| 150 |
-
|
| 151 |
seed = gr.Number(
|
| 152 |
label="Seed (random by default, set for reproducibility)",
|
| 153 |
value=get_random_seed(),
|
|
@@ -165,51 +247,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 165 |
output = gr.Image(
|
| 166 |
label="Generated Image",
|
| 167 |
elem_id="output-image",
|
| 168 |
-
elem_classes=["output-image", "fixed-width"]
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
# ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ
|
| 182 |
-
translated = translator(prompt)[0]['translation_text']
|
| 183 |
-
prompt = translated
|
| 184 |
-
|
| 185 |
-
# ํ๋กฌํํธ ํ์ ๊ฐ์
|
| 186 |
-
formatted_prompt = f"wbgmsst, 3D, {prompt} ,white background"
|
| 187 |
-
|
| 188 |
-
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
| 189 |
-
try:
|
| 190 |
-
generated_image = pipe(
|
| 191 |
-
prompt=[formatted_prompt],
|
| 192 |
-
generator=torch.Generator().manual_seed(int(seed)),
|
| 193 |
-
num_inference_steps=int(steps),
|
| 194 |
-
guidance_scale=float(scales),
|
| 195 |
-
height=int(height),
|
| 196 |
-
width=int(width),
|
| 197 |
-
max_sequence_length=256
|
| 198 |
-
).images[0]
|
| 199 |
-
|
| 200 |
-
saved_path = save_image(generated_image)
|
| 201 |
-
if saved_path is None:
|
| 202 |
-
print("Warning: Failed to save generated image")
|
| 203 |
-
|
| 204 |
-
return generated_image
|
| 205 |
-
except Exception as e:
|
| 206 |
-
print(f"Error in image generation: {str(e)}")
|
| 207 |
-
return None
|
| 208 |
-
|
| 209 |
def update_seed():
|
| 210 |
return get_random_seed()
|
| 211 |
|
| 212 |
-
#
|
| 213 |
generate_btn.click(
|
| 214 |
process_and_save_image,
|
| 215 |
inputs=[height, width, steps, scales, prompt, seed],
|
|
|
|
| 56 |
use_auth_token=HF_TOKEN
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Hyper-SD LoRA ๋ก๋
|
| 60 |
pipe.load_lora_weights(
|
| 61 |
hf_hub_download(
|
| 62 |
"ByteDance/Hyper-SD",
|
|
|
|
| 89 |
image = Image.fromarray(image)
|
| 90 |
image.save(filepath, "PNG", quality=100)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return filepath
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Failed to save image: {str(e)}")
|
|
|
|
| 98 |
print(f"Error in save_image: {str(e)}")
|
| 99 |
return None
|
| 100 |
|
| 101 |
+
# ์์ ํ๋กฌํํธ ์ ์
|
| 102 |
+
examples = [
|
| 103 |
+
["A 3D Star Wars Darth Vader helmet, highly detailed metallic finish"],
|
| 104 |
+
["A 3D Iron Man mask with glowing eyes and metallic red-gold finish"],
|
| 105 |
+
["A detailed 3D Pokemon Pikachu figure with glossy surface"],
|
| 106 |
+
["A 3D geometric abstract cube transforming into a sphere, metallic finish"],
|
| 107 |
+
["A 3D steampunk mechanical heart with brass and copper details"],
|
| 108 |
+
["A 3D crystal dragon with transparent iridescent scales"],
|
| 109 |
+
["A 3D futuristic hovering drone with neon light accents"],
|
| 110 |
+
["A 3D ancient Greek warrior helmet with ornate details"],
|
| 111 |
+
["A 3D robotic butterfly with mechanical wings and metallic finish"],
|
| 112 |
+
["A 3D floating magical crystal orb with internal energy swirls"]
|
| 113 |
+
]
|
| 114 |
+
|
| 115 |
+
@spaces.GPU
|
| 116 |
+
def process_and_save_image(height=1024, width=1024, steps=8, scales=3.5, prompt="", seed=None):
|
| 117 |
+
global pipe
|
| 118 |
+
|
| 119 |
+
if seed is None:
|
| 120 |
+
seed = torch.randint(0, 1000000, (1,)).item()
|
| 121 |
+
|
| 122 |
+
# ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ
|
| 123 |
+
def contains_korean(text):
|
| 124 |
+
return any(ord('๊ฐ') <= ord(c) <= ord('ํฃ') for c in text)
|
| 125 |
+
|
| 126 |
+
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ
|
| 127 |
+
if contains_korean(prompt):
|
| 128 |
+
translated = translator(prompt)[0]['translation_text']
|
| 129 |
+
prompt = translated
|
| 130 |
+
|
| 131 |
+
formatted_prompt = f"wbgmsst, 3D, {prompt} ,white background"
|
| 132 |
+
|
| 133 |
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
| 134 |
+
try:
|
| 135 |
+
generated_image = pipe(
|
| 136 |
+
prompt=[formatted_prompt],
|
| 137 |
+
generator=torch.Generator().manual_seed(int(seed)),
|
| 138 |
+
num_inference_steps=int(steps),
|
| 139 |
+
guidance_scale=float(scales),
|
| 140 |
+
height=int(height),
|
| 141 |
+
width=int(width),
|
| 142 |
+
max_sequence_length=256
|
| 143 |
+
).images[0]
|
| 144 |
+
|
| 145 |
+
saved_path = save_image(generated_image)
|
| 146 |
+
if saved_path is None:
|
| 147 |
+
print("Warning: Failed to save generated image")
|
| 148 |
+
|
| 149 |
+
return generated_image
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print(f"Error in image generation: {str(e)}")
|
| 152 |
+
return None
|
| 153 |
+
|
| 154 |
+
def get_random_seed():
|
| 155 |
+
return torch.randint(0, 1000000, (1,)).item()
|
| 156 |
+
|
| 157 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 158 |
+
with gr.Blocks(
|
| 159 |
+
theme=gr.themes.Soft(),
|
| 160 |
+
css="""
|
| 161 |
+
.container {
|
| 162 |
+
background: linear-gradient(to bottom right, #1a1a1a, #4a4a4a);
|
| 163 |
+
border-radius: 20px;
|
| 164 |
+
padding: 20px;
|
| 165 |
+
}
|
| 166 |
+
.generate-btn {
|
| 167 |
+
background: linear-gradient(45deg, #2196F3, #00BCD4);
|
| 168 |
+
border: none;
|
| 169 |
+
color: white;
|
| 170 |
+
font-weight: bold;
|
| 171 |
+
border-radius: 10px;
|
| 172 |
+
}
|
| 173 |
+
.output-image {
|
| 174 |
+
border-radius: 15px;
|
| 175 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
|
| 176 |
+
}
|
| 177 |
+
.fixed-width {
|
| 178 |
+
max-width: 1024px;
|
| 179 |
+
margin: auto;
|
| 180 |
+
}
|
| 181 |
+
"""
|
| 182 |
+
) as demo:
|
| 183 |
+
gr.HTML(
|
| 184 |
+
"""
|
| 185 |
+
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
|
| 186 |
+
<h1 style="font-size: 2.5rem; color: #2196F3;">3D Style Image Generator</h1>
|
| 187 |
+
<p style="font-size: 1.2rem; color: #666;">Create amazing 3D-style images with AI</p>
|
| 188 |
+
</div>
|
| 189 |
+
"""
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
with gr.Row(elem_classes="container"):
|
| 193 |
with gr.Column(scale=3):
|
| 194 |
prompt = gr.Textbox(
|
| 195 |
label="Image Description",
|
| 196 |
+
placeholder="Describe the 3D image you want to create...",
|
| 197 |
lines=3
|
| 198 |
)
|
| 199 |
|
|
|
|
| 230 |
value=3.5
|
| 231 |
)
|
| 232 |
|
|
|
|
|
|
|
|
|
|
| 233 |
seed = gr.Number(
|
| 234 |
label="Seed (random by default, set for reproducibility)",
|
| 235 |
value=get_random_seed(),
|
|
|
|
| 247 |
output = gr.Image(
|
| 248 |
label="Generated Image",
|
| 249 |
elem_id="output-image",
|
| 250 |
+
elem_classes=["output-image", "fixed-width"],
|
| 251 |
+
value="3d.webp"
|
| 252 |
)
|
| 253 |
|
| 254 |
+
# Examples ์น์
|
| 255 |
+
gr.Examples(
|
| 256 |
+
examples=examples,
|
| 257 |
+
inputs=prompt,
|
| 258 |
+
outputs=output,
|
| 259 |
+
fn=process_and_save_image,
|
| 260 |
+
cache_examples=True,
|
| 261 |
+
examples_per_page=5
|
| 262 |
+
)
|
| 263 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
def update_seed():
|
| 265 |
return get_random_seed()
|
| 266 |
|
| 267 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 268 |
generate_btn.click(
|
| 269 |
process_and_save_image,
|
| 270 |
inputs=[height, width, steps, scales, prompt, seed],
|