Spaces:
Running
on
Zero
Running
on
Zero
Update app-backup.py
Browse files- app-backup.py +103 -53
app-backup.py
CHANGED
@@ -12,6 +12,7 @@ import torch
|
|
12 |
from diffusers import FluxPipeline
|
13 |
from PIL import Image
|
14 |
from transformers import pipeline
|
|
|
15 |
|
16 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
17 |
|
@@ -35,6 +36,33 @@ torch.backends.cuda.matmul.allow_tf32 = True
|
|
35 |
if not path.exists(gallery_path):
|
36 |
os.makedirs(gallery_path, exist_ok=True)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
class timer:
|
39 |
def __init__(self, method_name="timed process"):
|
40 |
self.method = method_name
|
@@ -49,11 +77,10 @@ class timer:
|
|
49 |
if not path.exists(cache_path):
|
50 |
os.makedirs(cache_path, exist_ok=True)
|
51 |
|
52 |
-
# ์ธ์ฆ๋ ๋ชจ๋ธ ๋ก๋
|
53 |
pipe = FluxPipeline.from_pretrained(
|
54 |
"black-forest-labs/FLUX.1-dev",
|
55 |
torch_dtype=torch.bfloat16,
|
56 |
-
use_auth_token=HF_TOKEN
|
57 |
)
|
58 |
|
59 |
# Hyper-SD LoRA ๋ก๋
|
@@ -98,26 +125,15 @@ def save_image(image):
|
|
98 |
print(f"Error in save_image: {str(e)}")
|
99 |
return None
|
100 |
|
101 |
-
|
102 |
-
|
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 =
|
121 |
|
122 |
# ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ
|
123 |
def contains_korean(text):
|
@@ -128,7 +144,7 @@ def process_and_save_image(height=1024, width=1024, steps=8, scales=3.5, prompt=
|
|
128 |
translated = translator(prompt)[0]['translation_text']
|
129 |
prompt = translated
|
130 |
|
131 |
-
formatted_prompt = f"wbgmsst, 3D, {prompt}
|
132 |
|
133 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
134 |
try:
|
@@ -151,21 +167,10 @@ def process_and_save_image(height=1024, width=1024, steps=8, scales=3.5, prompt=
|
|
151 |
print(f"Error in image generation: {str(e)}")
|
152 |
return None
|
153 |
|
154 |
-
def
|
155 |
-
|
|
|
156 |
|
157 |
-
|
158 |
-
def process_example(prompt):
|
159 |
-
return process_and_save_image(
|
160 |
-
height=1024,
|
161 |
-
width=1024,
|
162 |
-
steps=8,
|
163 |
-
scales=3.5,
|
164 |
-
prompt=prompt,
|
165 |
-
seed=get_random_seed()
|
166 |
-
)
|
167 |
-
|
168 |
-
|
169 |
# Gradio ์ธํฐํ์ด์ค
|
170 |
with gr.Blocks(
|
171 |
theme=gr.themes.Soft(),
|
@@ -190,13 +195,27 @@ with gr.Blocks(
|
|
190 |
max-width: 1024px;
|
191 |
margin: auto;
|
192 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
"""
|
194 |
) as demo:
|
195 |
gr.HTML(
|
196 |
"""
|
197 |
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
|
198 |
-
<h1 style="font-size: 2.5rem; color: #2196F3;">3D Style Image Generator</h1>
|
199 |
-
<p style="font-size: 1.2rem; color: #666;">Create amazing 3D-style images with AI</p>
|
200 |
</div>
|
201 |
"""
|
202 |
)
|
@@ -263,33 +282,64 @@ with gr.Blocks(
|
|
263 |
value="3d.webp"
|
264 |
)
|
265 |
|
266 |
-
#
|
267 |
-
gr.
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
280 |
generate_btn.click(
|
281 |
-
process_and_save_image,
|
282 |
inputs=[height, width, steps, scales, prompt, seed],
|
283 |
outputs=output
|
284 |
-
).then(
|
285 |
-
update_seed,
|
286 |
-
outputs=[seed]
|
287 |
)
|
288 |
|
289 |
randomize_seed.click(
|
290 |
-
|
291 |
-
|
|
|
292 |
)
|
293 |
|
294 |
if __name__ == "__main__":
|
295 |
-
demo.launch(allowed_paths=[PERSISTENT_DIR])
|
|
|
12 |
from diffusers import FluxPipeline
|
13 |
from PIL import Image
|
14 |
from transformers import pipeline
|
15 |
+
import base64
|
16 |
|
17 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
18 |
|
|
|
36 |
if not path.exists(gallery_path):
|
37 |
os.makedirs(gallery_path, exist_ok=True)
|
38 |
|
39 |
+
# ์ํ ์ด๋ฏธ์ง์ ํ๋กฌํํธ ์ ์
|
40 |
+
SAMPLE_IMAGES = {
|
41 |
+
"3d2.webp": "the most famous hero according to Yuri Milner",
|
42 |
+
"3d3.webp": "purple nest",
|
43 |
+
"3d4.webp": "Timothy's sabbath",
|
44 |
+
"3d5.webp": "A schoolboy friend of Juliรกn Carax, fun-loving and loyal",
|
45 |
+
"3d6.webp": "Friend of Daniel and his father",
|
46 |
+
"3d7.webp": "WHERE ships of purple gently toss On seas of daffodil",
|
47 |
+
"3d8.webp": "Beat the drums of tragedy for me, And let the white violins whir thin and slow",
|
48 |
+
"3d9.webp": "And let the choir sing a stormy song To drown the rattle of my dying breath.",
|
49 |
+
"3d10.webp": "Beat the drums of tragedy and death",
|
50 |
+
"3d11.webp": "Beat the drums of tragedy for me.",
|
51 |
+
"3d12.webp": "Touching the infinite, else far and untrod, With oracles divine that speak of God.",
|
52 |
+
"3d13.webp": "Night, standing on her starry pulpit, free, Utters them in the dread, the silver roll Of spheres, woods, winds and waves, alternately",
|
53 |
+
"3d14.webp": "On sermons deep, fit time to feast the soul.",
|
54 |
+
"3d15.webp": "The bee is cradled in the bud; and far, Cold glittering lights, the azure curtain, throngโ Planet on beaming planet, star on star.",
|
55 |
+
"3d16.webp": "The lark's sweet pipe has ceased its latest song",
|
56 |
+
"3d17.webp": "the snake was a roaming dog",
|
57 |
+
"3d18.webp": "Antonio Battistella portraying Father of Giulia",
|
58 |
+
"3d19.webp": "So straight to her father the brisk young lady went, And said, grant me one favour, do give your consent",
|
59 |
+
"3d20.webp": "Before that we are marryโd let me your father see, All fear is, now miscarryโd, my heart is full of glee",
|
60 |
+
"3d21.webp": "My heart you now have gained, you are all I prize, So make yourself contented, pray be satisfied.",
|
61 |
+
"3d22.webp": "O pray what is the favour that of me you crave? If it lies in my power you the same shall have",
|
62 |
+
"3d23.webp": "Could I but see your father, and my mind reveal, I have both gold and silver, and houses at my will",
|
63 |
+
"3d1.webp": "the most famous hero according to Zhou Qi"
|
64 |
+
}
|
65 |
+
|
66 |
class timer:
|
67 |
def __init__(self, method_name="timed process"):
|
68 |
self.method = method_name
|
|
|
77 |
if not path.exists(cache_path):
|
78 |
os.makedirs(cache_path, exist_ok=True)
|
79 |
|
|
|
80 |
pipe = FluxPipeline.from_pretrained(
|
81 |
"black-forest-labs/FLUX.1-dev",
|
82 |
torch_dtype=torch.bfloat16,
|
83 |
+
use_auth_token=HF_TOKEN # ๊ฒฝ๊ณ ๋ฉ์์ง๊ฐ ๋จ์ง๋ง ๋ฌด์ ๊ฐ๋ฅ
|
84 |
)
|
85 |
|
86 |
# Hyper-SD LoRA ๋ก๋
|
|
|
125 |
print(f"Error in save_image: {str(e)}")
|
126 |
return None
|
127 |
|
128 |
+
def get_random_seed():
|
129 |
+
return torch.randint(0, 1000000, (1,)).item()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
@spaces.GPU
|
132 |
def process_and_save_image(height=1024, width=1024, steps=8, scales=3.5, prompt="", seed=None):
|
133 |
global pipe
|
134 |
|
135 |
if seed is None:
|
136 |
+
seed = get_random_seed()
|
137 |
|
138 |
# ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ
|
139 |
def contains_korean(text):
|
|
|
144 |
translated = translator(prompt)[0]['translation_text']
|
145 |
prompt = translated
|
146 |
|
147 |
+
formatted_prompt = f"wbgmsst, 3D, {prompt}, white background"
|
148 |
|
149 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
150 |
try:
|
|
|
167 |
print(f"Error in image generation: {str(e)}")
|
168 |
return None
|
169 |
|
170 |
+
def update_random_seed():
|
171 |
+
"""๋ฒํผ์ผ๋ก ๋๋ ์ ๋ ์๋ก์ด ์๋๋ฅผ ์
๋ฐ์ดํธ"""
|
172 |
+
return gr.update(value=get_random_seed())
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
# Gradio ์ธํฐํ์ด์ค
|
175 |
with gr.Blocks(
|
176 |
theme=gr.themes.Soft(),
|
|
|
195 |
max-width: 1024px;
|
196 |
margin: auto;
|
197 |
}
|
198 |
+
.gallery-container {
|
199 |
+
margin-top: 40px;
|
200 |
+
padding: 20px;
|
201 |
+
background: #f5f5f5;
|
202 |
+
border-radius: 15px;
|
203 |
+
width: 100%;
|
204 |
+
margin: 0 auto;
|
205 |
+
}
|
206 |
+
.gallery-title {
|
207 |
+
text-align: center;
|
208 |
+
margin-bottom: 20px;
|
209 |
+
color: #333;
|
210 |
+
font-size: 1.5rem;
|
211 |
+
}
|
212 |
"""
|
213 |
) as demo:
|
214 |
gr.HTML(
|
215 |
"""
|
216 |
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
|
217 |
+
<h1 style="font-size: 2.5rem; color: #2196F3;">3D Style Image Generator v2.0</h1>
|
218 |
+
<p style="font-size: 1.2rem; color: #666;">Create amazing 3D-style images with AI. https://discord.gg/openfreeai </p>
|
219 |
</div>
|
220 |
"""
|
221 |
)
|
|
|
282 |
value="3d.webp"
|
283 |
)
|
284 |
|
285 |
+
# gallery-container ๋ถ๋ถ์ Group์ผ๋ก ๊ฐ์ธ ํ๋ฉด ์ ์ฒด์ ํ์ฅ
|
286 |
+
with gr.Group(elem_classes="gallery-container"):
|
287 |
+
gr.HTML("<h2 class='gallery-title'>Gallery</h2>")
|
288 |
+
|
289 |
+
gallery_html = """
|
290 |
+
<div style='
|
291 |
+
display: grid;
|
292 |
+
grid-template-columns: repeat(4, 1fr);
|
293 |
+
gap: 20px;
|
294 |
+
width: 100%;
|
295 |
+
'>
|
296 |
+
"""
|
297 |
+
|
298 |
+
for img_file, prompt_text in SAMPLE_IMAGES.items():
|
299 |
+
img_path = os.path.abspath(img_file)
|
300 |
+
if os.path.exists(img_path):
|
301 |
+
try:
|
302 |
+
with open(img_path, "rb") as img:
|
303 |
+
img_data = base64.b64encode(img.read()).decode()
|
304 |
+
gallery_html += f"""
|
305 |
+
<div style='
|
306 |
+
border: 1px solid #ddd;
|
307 |
+
border-radius: 10px;
|
308 |
+
padding: 10px;
|
309 |
+
background: white;
|
310 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
311 |
+
'>
|
312 |
+
<img src='data:image/webp;base64,{img_data}'
|
313 |
+
style='width: 100%;
|
314 |
+
border-radius: 8px;
|
315 |
+
margin-bottom: 10px;'
|
316 |
+
>
|
317 |
+
<p style='
|
318 |
+
margin: 5px 0;
|
319 |
+
font-weight: bold;
|
320 |
+
color: #333;
|
321 |
+
padding: 10px;
|
322 |
+
'>Prompt: {prompt_text}</p>
|
323 |
+
</div>
|
324 |
+
"""
|
325 |
+
except Exception as e:
|
326 |
+
print(f"Error loading image {img_file}: {str(e)}")
|
327 |
+
|
328 |
+
gallery_html += "</div>"
|
329 |
+
gr.HTML(gallery_html)
|
330 |
+
|
331 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
332 |
generate_btn.click(
|
333 |
+
fn=process_and_save_image,
|
334 |
inputs=[height, width, steps, scales, prompt, seed],
|
335 |
outputs=output
|
|
|
|
|
|
|
336 |
)
|
337 |
|
338 |
randomize_seed.click(
|
339 |
+
fn=update_random_seed,
|
340 |
+
inputs=None,
|
341 |
+
outputs=seed
|
342 |
)
|
343 |
|
344 |
if __name__ == "__main__":
|
345 |
+
demo.launch(allowed_paths=[PERSISTENT_DIR])
|