ginipick commited on
Commit
9c4b08b
ยท
verified ยท
1 Parent(s): 3ede006

Update app-backup.py

Browse files
Files changed (1) hide show
  1. 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
- 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):
@@ -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} ,white background"
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 get_random_seed():
155
- return torch.randint(0, 1000000, (1,)).item()
 
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
- # Examples ์„น์…˜
267
- gr.Examples(
268
- examples=examples,
269
- inputs=prompt,
270
- outputs=output,
271
- fn=process_example, # ์ˆ˜์ •๋œ ํ•จ์ˆ˜ ์‚ฌ์šฉ
272
- cache_examples=False,
273
- examples_per_page=5
274
- )
275
-
276
- def update_seed():
277
- return get_random_seed()
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
- update_seed,
291
- outputs=[seed]
 
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])