aiqtech commited on
Commit
7f98410
·
verified ·
1 Parent(s): 70c128f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -87
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import random
2
-
 
 
3
  import gradio as gr
4
  import numpy as np
5
  import spaces
@@ -7,6 +9,11 @@ import torch
7
  from diffusers import DiffusionPipeline
8
  from PIL import Image
9
 
 
 
 
 
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  repo_id = "black-forest-labs/FLUX.1-dev"
12
  adapter_id = "openfree/claude-monet"
@@ -15,10 +22,30 @@ pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16
15
  pipeline.load_lora_weights(adapter_id)
16
  pipeline = pipeline.to(device)
17
 
18
-
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  @spaces.GPU(duration=120)
24
  def inference(
@@ -45,8 +72,12 @@ def inference(
45
  generator=generator,
46
  joint_attention_kwargs={"scale": lora_scale},
47
  ).images[0]
48
-
49
- return image, seed
 
 
 
 
50
 
51
  def load_predefined_images():
52
  predefined_images = [
@@ -74,84 +105,97 @@ footer {
74
  }
75
  """
76
 
77
- with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css
78
- ) as demo:
79
  gr.HTML('<div class="title"> Claude Monet STUDIO </div>')
80
  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>')
81
 
82
-
83
- with gr.Column(elem_id="col-container"):
84
- with gr.Row():
85
- prompt = gr.Text(
86
- label="Prompt",
87
- show_label=False,
88
- max_lines=1,
89
- placeholder="Enter your prompt",
90
- container=False,
91
- )
92
-
93
- run_button = gr.Button("Run", scale=0)
94
-
95
- result = gr.Image(label="Result", show_label=False)
96
-
97
- with gr.Accordion("Advanced Settings", open=False):
98
- seed = gr.Slider(
99
- label="Seed",
100
- minimum=0,
101
- maximum=MAX_SEED,
102
- step=1,
103
- value=42,
104
- )
105
-
106
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
107
-
108
- with gr.Row():
109
- width = gr.Slider(
110
- label="Width",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=1024,
115
- )
116
-
117
- height = gr.Slider(
118
- label="Height",
119
- minimum=256,
120
- maximum=MAX_IMAGE_SIZE,
121
- step=32,
122
- value=768,
123
- )
124
-
125
- with gr.Row():
126
- guidance_scale = gr.Slider(
127
- label="Guidance scale",
128
- minimum=0.0,
129
- maximum=10.0,
130
- step=0.1,
131
- value=3.5,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  )
133
 
134
- num_inference_steps = gr.Slider(
135
- label="Number of inference steps",
136
- minimum=1,
137
- maximum=50,
138
- step=1,
139
- value=30,
140
- )
141
-
142
- lora_scale = gr.Slider(
143
- label="LoRA scale",
144
- minimum=0.0,
145
- maximum=1.0,
146
- step=0.1,
147
- value=1.0,
148
- )
149
 
150
- gr.Examples(
151
- examples=examples,
152
- inputs=[prompt],
153
- outputs=[result, seed],
154
- )
 
 
 
 
155
 
156
  gr.on(
157
  triggers=[run_button.click, prompt.submit],
@@ -166,17 +210,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css
166
  num_inference_steps,
167
  lora_scale,
168
  ],
169
- outputs=[result, seed],
170
- )
171
-
172
- # Add gallery section at the bottom
173
- gr.Markdown("### Claude Monet Style Examples")
174
- predefined_gallery = gr.Gallery(
175
- label="Sample Images",
176
- columns=3,
177
- rows=2,
178
- show_label=False,
179
- value=load_predefined_images()
180
  )
181
 
182
  demo.queue()
 
1
  import random
2
+ import os
3
+ import uuid
4
+ from datetime import datetime
5
  import gradio as gr
6
  import numpy as np
7
  import spaces
 
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"
19
  adapter_id = "openfree/claude-monet"
 
22
  pipeline.load_lora_weights(adapter_id)
23
  pipeline = pipeline.to(device)
24
 
 
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]
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
+ return filepath
38
+
39
+ def load_generated_images():
40
+ if not os.path.exists(SAVE_DIR):
41
+ return []
42
+
43
+ # Load all images from the directory
44
+ image_files = [os.path.join(SAVE_DIR, f) for f in os.listdir(SAVE_DIR)
45
+ if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
46
+ # Sort by creation time (newest first)
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(
 
72
  generator=generator,
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 = [
 
105
  }
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
 
112
+ with gr.Tabs() as tabs:
113
+ with gr.Tab("Generation"):
114
+ with gr.Column(elem_id="col-container"):
115
+ with gr.Row():
116
+ prompt = gr.Text(
117
+ label="Prompt",
118
+ show_label=False,
119
+ max_lines=1,
120
+ placeholder="Enter your prompt",
121
+ container=False,
122
+ )
123
+ run_button = gr.Button("Run", scale=0)
124
+
125
+ result = gr.Image(label="Result", show_label=False)
126
+
127
+ with gr.Accordion("Advanced Settings", open=False):
128
+ seed = gr.Slider(
129
+ label="Seed",
130
+ minimum=0,
131
+ maximum=MAX_SEED,
132
+ step=1,
133
+ value=42,
134
+ )
135
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
136
+
137
+ with gr.Row():
138
+ width = gr.Slider(
139
+ label="Width",
140
+ minimum=256,
141
+ maximum=MAX_IMAGE_SIZE,
142
+ step=32,
143
+ value=1024,
144
+ )
145
+ height = gr.Slider(
146
+ label="Height",
147
+ minimum=256,
148
+ maximum=MAX_IMAGE_SIZE,
149
+ step=32,
150
+ value=768,
151
+ )
152
+
153
+ with gr.Row():
154
+ guidance_scale = gr.Slider(
155
+ label="Guidance scale",
156
+ minimum=0.0,
157
+ maximum=10.0,
158
+ step=0.1,
159
+ value=3.5,
160
+ )
161
+ num_inference_steps = gr.Slider(
162
+ label="Number of inference steps",
163
+ minimum=1,
164
+ maximum=50,
165
+ step=1,
166
+ value=30,
167
+ )
168
+ lora_scale = gr.Slider(
169
+ label="LoRA scale",
170
+ minimum=0.0,
171
+ maximum=1.0,
172
+ step=0.1,
173
+ value=1.0,
174
+ )
175
+
176
+ gr.Examples(
177
+ examples=examples,
178
+ inputs=[prompt],
179
+ outputs=[result, seed],
180
  )
181
 
182
+ with gr.Tab("Gallery"):
183
+ generated_gallery = gr.Gallery(
184
+ label="Generated Images",
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")
192
+ predefined_gallery = gr.Gallery(
193
+ label="Sample Images",
194
+ columns=3,
195
+ rows=2,
196
+ show_label=False,
197
+ value=load_predefined_images()
198
+ )
199
 
200
  gr.on(
201
  triggers=[run_button.click, prompt.submit],
 
210
  num_inference_steps,
211
  lora_scale,
212
  ],
213
+ outputs=[result, seed, generated_gallery],
 
 
 
 
 
 
 
 
 
 
214
  )
215
 
216
  demo.queue()