Spaces:
cutechicken
/
Runtime error

openfree commited on
Commit
a862d59
ยท
verified ยท
1 Parent(s): 04bc9b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -9,8 +9,8 @@ import torch
9
  from diffusers import DiffusionPipeline
10
  from PIL import Image
11
 
12
- # Create permanent storage directory
13
- SAVE_DIR = "saved_images" # Gradio will handle the persistence
14
  if not os.path.exists(SAVE_DIR):
15
  os.makedirs(SAVE_DIR, exist_ok=True)
16
 
@@ -26,16 +26,15 @@ MAX_SEED = np.iinfo(np.int32).max
26
  MAX_IMAGE_SIZE = 1024
27
 
28
  def save_generated_image(image, prompt):
29
- # Generate unique filename with timestamp
30
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
31
  unique_id = str(uuid.uuid4())[:8]
32
  filename = f"{timestamp}_{unique_id}.png"
33
  filepath = os.path.join(SAVE_DIR, filename)
34
 
35
- # Save the image
36
  image.save(filepath)
37
 
38
- # Save metadata
39
  metadata_file = os.path.join(SAVE_DIR, "metadata.txt")
40
  with open(metadata_file, "a", encoding="utf-8") as f:
41
  f.write(f"{filename}|{prompt}|{timestamp}\n")
@@ -46,22 +45,28 @@ def load_generated_images():
46
  if not os.path.exists(SAVE_DIR):
47
  return []
48
 
49
- # Load all images from the directory
50
  image_files = [
51
  os.path.join(SAVE_DIR, f)
52
  for f in os.listdir(SAVE_DIR)
53
  if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))
54
  ]
55
- # Sort by creation time (newest first)
56
  image_files.sort(key=lambda x: os.path.getctime(x), reverse=True)
57
  return image_files
58
 
59
  def load_predefined_images():
60
- # Return empty list since we're not using predefined images
61
  return []
62
 
63
- # ๊ฐ„๋‹จํ•œ CSS: ํƒ€์ดํ‹€ ํฌ๊ธฐ, ์ค‘์•™ ์ •๋ ฌ, footer ์ˆจ๊น€ ๋“ฑ
64
  css = """
 
 
 
 
 
 
 
65
  .title {
66
  font-size: 1.8em;
67
  font-weight: bold;
@@ -99,30 +104,25 @@ def inference(
99
  joint_attention_kwargs={"scale": lora_scale},
100
  ).images[0]
101
 
102
- # Save the generated image
103
  filepath = save_generated_image(image, prompt)
104
-
105
- # Return the image, seed, and updated gallery
106
  return image, seed, load_generated_images()
107
 
 
 
108
  examples = [
109
  "Pepe the frog playing fetch with a golden retriever in a sunny park. He wears casual weekend clothes and tosses a bright red frisbee with a goofy grin. The dog leaps gracefully through the air, tail wagging with excitement. The warm afternoon sunlight filters through the trees, creating a humorous meme-like atmosphere. [pepe]",
110
-
111
  "Pepe the frog dressed in full military gear, standing at attention with a standard-issue rifle. His crisp uniform is adorned with cartoonish medals. Other frog soldiers march in formation behind him during a grand meme parade, conveying discipline mixed with comical charm. [pepe]",
112
-
113
  "A medieval Pepe knight in gleaming armor, proudly holding an ornate sword and shield. He stands in front of a majestic castle with a swirling moat. His shield features a cartoon frog crest, and sunlight gleams off his polished armor, adding a humorous yet epic feel. [pepe]",
114
-
115
  "A charismatic Pepe the frog addressing a crowd from a podium. He wears a well-fitted suit and gestures with exaggerated cartoon expressions while speaking. The audience is filled with fellow frog characters holding supportive banners. Cameras capture this grand meme moment. [pepe]",
116
-
117
  "Pepe the frog enjoying a peaceful morning at home, reading a newspaper at his kitchen table. He wears comfy pajamas and sips coffee from a novelty frog mug. Sunlight streams through the window, illuminating a quaint plant on the countertop in this cozy, meme-inspired scene. [pepe]",
118
-
119
  "Businessman Pepe walking confidently through a sleek office lobby, briefcase in hand. He wears a tailored navy suit, and his wide frog eyes convey determination. Floor-to-ceiling windows reveal a bustling cityscape behind him, blending corporate professionalism with meme humor. [pepe]"
120
  ]
121
 
122
 
123
- # Blocks์—์„œ ๊ธฐ์กด theme ์ œ๊ฑฐ ํ›„ css๋งŒ ์ ์šฉ
124
- with gr.Blocks(css=css, analytics_enabled=False) as demo:
125
- gr.HTML('<div class="title"> PEPE Meme Generator </div>')
 
126
 
127
  gr.HTML("""
128
  <a href="https://visitorbadge.io/status?path=https%3A%2F%2Fopenfree-pepe.hf.space">
@@ -153,7 +153,10 @@ with gr.Blocks(css=css, analytics_enabled=False) as demo:
153
  step=1,
154
  value=42,
155
  )
156
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
 
 
157
 
158
  with gr.Row():
159
  width = gr.Slider(
@@ -201,7 +204,7 @@ with gr.Blocks(css=css, analytics_enabled=False) as demo:
201
  )
202
 
203
  with gr.Tab("Gallery"):
204
- gallery_header = gr.Markdown("### Generated Images Gallery")
205
  generated_gallery = gr.Gallery(
206
  label="Generated Images",
207
  columns=6,
@@ -212,8 +215,7 @@ with gr.Blocks(css=css, analytics_enabled=False) as demo:
212
  )
213
  refresh_btn = gr.Button("๐Ÿ”„ Refresh Gallery")
214
 
215
-
216
- # Event handlers
217
  def refresh_gallery():
218
  return load_generated_images()
219
 
@@ -223,6 +225,7 @@ with gr.Blocks(css=css, analytics_enabled=False) as demo:
223
  outputs=generated_gallery,
224
  )
225
 
 
226
  gr.on(
227
  triggers=[run_button.click, prompt.submit],
228
  fn=inference,
 
9
  from diffusers import DiffusionPipeline
10
  from PIL import Image
11
 
12
+ # ---------- ์ดˆ๊ธฐ ์„ค์ • ๋ฐ ๋ชจ๋ธ ๋กœ๋“œ ----------
13
+ SAVE_DIR = "saved_images" # Gradio๊ฐ€ ์ €์žฅ์†Œ ๊ด€๋ฆฌ๋ฅผ ์ˆ˜ํ–‰
14
  if not os.path.exists(SAVE_DIR):
15
  os.makedirs(SAVE_DIR, exist_ok=True)
16
 
 
26
  MAX_IMAGE_SIZE = 1024
27
 
28
  def save_generated_image(image, prompt):
 
29
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
30
  unique_id = str(uuid.uuid4())[:8]
31
  filename = f"{timestamp}_{unique_id}.png"
32
  filepath = os.path.join(SAVE_DIR, filename)
33
 
34
+ # ์ด๋ฏธ์ง€ ์ €์žฅ
35
  image.save(filepath)
36
 
37
+ # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ €์žฅ
38
  metadata_file = os.path.join(SAVE_DIR, "metadata.txt")
39
  with open(metadata_file, "a", encoding="utf-8") as f:
40
  f.write(f"{filename}|{prompt}|{timestamp}\n")
 
45
  if not os.path.exists(SAVE_DIR):
46
  return []
47
 
48
+ # ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋กœ๋“œ
49
  image_files = [
50
  os.path.join(SAVE_DIR, f)
51
  for f in os.listdir(SAVE_DIR)
52
  if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))
53
  ]
54
+ # ์ƒ์„ฑ ์‹œ๊ฐ ๊ธฐ์ค€ ์ •๋ ฌ (์ตœ์‹  ํŒŒ์ผ ์šฐ์„ )
55
  image_files.sort(key=lambda x: os.path.getctime(x), reverse=True)
56
  return image_files
57
 
58
  def load_predefined_images():
59
+ # ๋ณ„๋„ ์‚ฌ์ „ ์ด๋ฏธ์ง€ ์—†์Œ
60
  return []
61
 
 
62
  css = """
63
+ /* ๋ฐฐ๊ฒฝ ๊ทธ๋ผ๋””์–ธํŠธ๋ฅผ ์ฃผ๊ฑฐ๋‚˜, ํฐํŠธ/ํƒ€์ดํ‹€ ํฌ๊ธฐ ๋“ฑ์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ๊พธ๋ฐ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. */
64
+ body {
65
+ font-family: 'Open Sans', sans-serif;
66
+ background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
67
+ margin: 0; /* ๊ธฐ๋ณธ ์—ฌ๋ฐฑ ์ œ๊ฑฐ */
68
+ padding: 0;
69
+ }
70
  .title {
71
  font-size: 1.8em;
72
  font-weight: bold;
 
104
  joint_attention_kwargs={"scale": lora_scale},
105
  ).images[0]
106
 
 
107
  filepath = save_generated_image(image, prompt)
 
 
108
  return image, seed, load_generated_images()
109
 
110
+
111
+ # ---------- ์˜ˆ์‹œ ํ”„๋กฌํ”„ํŠธ ----------
112
  examples = [
113
  "Pepe the frog playing fetch with a golden retriever in a sunny park. He wears casual weekend clothes and tosses a bright red frisbee with a goofy grin. The dog leaps gracefully through the air, tail wagging with excitement. The warm afternoon sunlight filters through the trees, creating a humorous meme-like atmosphere. [pepe]",
 
114
  "Pepe the frog dressed in full military gear, standing at attention with a standard-issue rifle. His crisp uniform is adorned with cartoonish medals. Other frog soldiers march in formation behind him during a grand meme parade, conveying discipline mixed with comical charm. [pepe]",
 
115
  "A medieval Pepe knight in gleaming armor, proudly holding an ornate sword and shield. He stands in front of a majestic castle with a swirling moat. His shield features a cartoon frog crest, and sunlight gleams off his polished armor, adding a humorous yet epic feel. [pepe]",
 
116
  "A charismatic Pepe the frog addressing a crowd from a podium. He wears a well-fitted suit and gestures with exaggerated cartoon expressions while speaking. The audience is filled with fellow frog characters holding supportive banners. Cameras capture this grand meme moment. [pepe]",
 
117
  "Pepe the frog enjoying a peaceful morning at home, reading a newspaper at his kitchen table. He wears comfy pajamas and sips coffee from a novelty frog mug. Sunlight streams through the window, illuminating a quaint plant on the countertop in this cozy, meme-inspired scene. [pepe]",
 
118
  "Businessman Pepe walking confidently through a sleek office lobby, briefcase in hand. He wears a tailored navy suit, and his wide frog eyes convey determination. Floor-to-ceiling windows reveal a bustling cityscape behind him, blending corporate professionalism with meme humor. [pepe]"
119
  ]
120
 
121
 
122
+ # ---------- UI ----------
123
+ # ์›ํ•˜๋Š” ๊ทธ๋ผ๋””์˜ค ํ…Œ๋งˆ๋ฅผ ์„ ํƒํ•ด ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” Soft ํ…Œ๋งˆ์— primary_hue="emerald"๋ฅผ ์ง€์ •ํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.
124
+ with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="emerald"), analytics_enabled=False) as demo:
125
+ gr.HTML('<div class="title">PEPE Meme Generator</div>')
126
 
127
  gr.HTML("""
128
  <a href="https://visitorbadge.io/status?path=https%3A%2F%2Fopenfree-pepe.hf.space">
 
153
  step=1,
154
  value=42,
155
  )
156
+ randomize_seed = gr.Checkbox(
157
+ label="Randomize seed",
158
+ value=True
159
+ )
160
 
161
  with gr.Row():
162
  width = gr.Slider(
 
204
  )
205
 
206
  with gr.Tab("Gallery"):
207
+ gr.Markdown("### Generated Images Gallery")
208
  generated_gallery = gr.Gallery(
209
  label="Generated Images",
210
  columns=6,
 
215
  )
216
  refresh_btn = gr.Button("๐Ÿ”„ Refresh Gallery")
217
 
218
+ # Gallery ์ƒˆ๋กœ๊ณ ์นจ ํ•ธ๋“ค๋Ÿฌ
 
219
  def refresh_gallery():
220
  return load_generated_images()
221
 
 
225
  outputs=generated_gallery,
226
  )
227
 
228
+ # Run ๋ฒ„ํŠผ & ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
229
  gr.on(
230
  triggers=[run_button.click, prompt.submit],
231
  fn=inference,