aiqtech commited on
Commit
f3cae17
1 Parent(s): 7f98410

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -18
app.py CHANGED
@@ -9,10 +9,10 @@ import torch
9
  from diffusers import DiffusionPipeline
10
  from PIL import Image
11
 
12
- # Create directories if they don't exist
13
- SAVE_DIR = "generated_images"
14
  if not os.path.exists(SAVE_DIR):
15
- os.makedirs(SAVE_DIR)
16
 
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  repo_id = "black-forest-labs/FLUX.1-dev"
@@ -25,7 +25,7 @@ pipeline = pipeline.to(device)
25
  MAX_SEED = np.iinfo(np.int32).max
26
  MAX_IMAGE_SIZE = 1024
27
 
28
- def save_generated_image(image):
29
  # Generate unique filename with timestamp
30
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
31
  unique_id = str(uuid.uuid4())[:8]
@@ -34,6 +34,12 @@ def save_generated_image(image):
34
 
35
  # Save the image
36
  image.save(filepath)
 
 
 
 
 
 
37
  return filepath
38
 
39
  def load_generated_images():
@@ -47,6 +53,28 @@ def load_generated_images():
47
  image_files.sort(key=lambda x: os.path.getctime(x), reverse=True)
48
  return image_files
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  @spaces.GPU(duration=120)
51
  def inference(
52
  prompt: str,
@@ -73,23 +101,12 @@ def inference(
73
  joint_attention_kwargs={"scale": lora_scale},
74
  ).images[0]
75
 
76
- # Save the generated image
77
- save_generated_image(image)
78
 
79
  # Return the image, seed, and updated gallery
80
  return image, seed, load_generated_images()
81
 
82
- def load_predefined_images():
83
- predefined_images = [
84
- "assets/cm1.webp",
85
- "assets/cm2.webp",
86
- "assets/cm3.webp",
87
- "assets/cm4.webp",
88
- "assets/cm5.webp",
89
- "assets/cm6.webp",
90
- ]
91
- return predefined_images
92
-
93
  examples = [
94
  "Claude Monet's 1916 painting, Water Lilies, which is currently on display at the Metropolitan Museum of Art. The painting depicts a tranquil pond with water lilies floating on the surface, surrounded by lush green foliage and a variety of colorful flowers. The colors of the flowers range from bright pinks and purples to deep blues and greens, creating a peaceful and calming atmosphere. [trigger]",
95
  "Claude Monet's 1869 masterpiece, The Magpie, showcasing a snow-covered rural landscape at dawn. A single black magpie perches on a wooden gate, contrasting against the pristine white snow. The scene captures the subtle interplay of light and shadow on the snow's surface, with delicate blue-gray tones in the shadows and warm golden hints where sunlight touches the snow-laden branches. [trigger]",
@@ -106,6 +123,8 @@ footer {
106
  """
107
 
108
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
 
 
109
  gr.HTML('<div class="title"> Claude Monet STUDIO </div>')
110
  gr.HTML('<div class="title">😄Image to Video Explore: <a href="https://huggingface.co/spaces/ginigen/theater" target="_blank">https://huggingface.co/spaces/ginigen/theater</a></div>')
111
 
@@ -185,7 +204,8 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
185
  columns=6,
186
  show_label=False,
187
  value=load_generated_images(),
188
- )
 
189
 
190
  # Add sample gallery section at the bottom
191
  gr.Markdown("### Claude Monet Style Examples")
@@ -197,6 +217,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
197
  value=load_predefined_images()
198
  )
199
 
 
200
  gr.on(
201
  triggers=[run_button.click, prompt.submit],
202
  fn=inference,
@@ -211,6 +232,12 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
211
  lora_scale,
212
  ],
213
  outputs=[result, seed, generated_gallery],
 
 
 
 
 
 
214
  )
215
 
216
  demo.queue()
 
9
  from diffusers import DiffusionPipeline
10
  from PIL import Image
11
 
12
+ # Create permanent storage directory
13
+ SAVE_DIR = "/content/generated_images"
14
  if not os.path.exists(SAVE_DIR):
15
+ os.makedirs(SAVE_DIR, exist_ok=True)
16
 
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  repo_id = "black-forest-labs/FLUX.1-dev"
 
25
  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]
 
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")
42
+
43
  return filepath
44
 
45
  def load_generated_images():
 
53
  image_files.sort(key=lambda x: os.path.getctime(x), reverse=True)
54
  return image_files
55
 
56
+ class ImageFlagging(gr.FlaggingCallback):
57
+ def setup(self, components, flagging_dir: str):
58
+ self.components = components
59
+ self.flagging_dir = SAVE_DIR
60
+
61
+ def flag(self, flag_data, flag_option=None, flag_index=None, username=None) -> int:
62
+ """Save image and metadata permanently"""
63
+ image, prompt = flag_data
64
+ filepath = save_generated_image(image, prompt)
65
+ return 0
66
+
67
+ def load_predefined_images():
68
+ predefined_images = [
69
+ "assets/cm1.webp",
70
+ "assets/cm2.webp",
71
+ "assets/cm3.webp",
72
+ "assets/cm4.webp",
73
+ "assets/cm5.webp",
74
+ "assets/cm6.webp",
75
+ ]
76
+ return predefined_images
77
+
78
  @spaces.GPU(duration=120)
79
  def inference(
80
  prompt: str,
 
101
  joint_attention_kwargs={"scale": lora_scale},
102
  ).images[0]
103
 
104
+ # Save the generated image with the prompt
105
+ save_generated_image(image, prompt)
106
 
107
  # Return the image, seed, and updated gallery
108
  return image, seed, load_generated_images()
109
 
 
 
 
 
 
 
 
 
 
 
 
110
  examples = [
111
  "Claude Monet's 1916 painting, Water Lilies, which is currently on display at the Metropolitan Museum of Art. The painting depicts a tranquil pond with water lilies floating on the surface, surrounded by lush green foliage and a variety of colorful flowers. The colors of the flowers range from bright pinks and purples to deep blues and greens, creating a peaceful and calming atmosphere. [trigger]",
112
  "Claude Monet's 1869 masterpiece, The Magpie, showcasing a snow-covered rural landscape at dawn. A single black magpie perches on a wooden gate, contrasting against the pristine white snow. The scene captures the subtle interplay of light and shadow on the snow's surface, with delicate blue-gray tones in the shadows and warm golden hints where sunlight touches the snow-laden branches. [trigger]",
 
123
  """
124
 
125
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
126
+ flagging_callback = ImageFlagging()
127
+
128
  gr.HTML('<div class="title"> Claude Monet STUDIO </div>')
129
  gr.HTML('<div class="title">😄Image to Video Explore: <a href="https://huggingface.co/spaces/ginigen/theater" target="_blank">https://huggingface.co/spaces/ginigen/theater</a></div>')
130
 
 
204
  columns=6,
205
  show_label=False,
206
  value=load_generated_images(),
207
+ elem_id="generated_gallery"
208
+ ).style(grid=6)
209
 
210
  # Add sample gallery section at the bottom
211
  gr.Markdown("### Claude Monet Style Examples")
 
217
  value=load_predefined_images()
218
  )
219
 
220
+ # Enable flagging for permanent storage
221
  gr.on(
222
  triggers=[run_button.click, prompt.submit],
223
  fn=inference,
 
232
  lora_scale,
233
  ],
234
  outputs=[result, seed, generated_gallery],
235
+ ).then(
236
+ fn=None,
237
+ inputs=[result, prompt],
238
+ outputs=None,
239
+ _js="(res, prompt) => clearInterval(window.GalleryIntervalID)",
240
+ flagging_callback=flagging_callback
241
  )
242
 
243
  demo.queue()