Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,27 +8,9 @@ import spaces
|
|
8 |
import torch
|
9 |
from diffusers import DiffusionPipeline
|
10 |
from PIL import Image
|
11 |
-
from huggingface_hub import HfFolder, Repository
|
12 |
-
from pathlib import Path
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
REPO_TYPE = "space"
|
17 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # Set this in your Space's secrets
|
18 |
-
PERSISTENT_DIR = os.path.join(os.getcwd(), "persistent")
|
19 |
-
|
20 |
-
# Initialize repository
|
21 |
-
if HF_TOKEN:
|
22 |
-
HfFolder.save_token(HF_TOKEN)
|
23 |
-
repo = Repository(
|
24 |
-
local_dir=PERSISTENT_DIR,
|
25 |
-
clone_from=REPO_ID,
|
26 |
-
repo_type=REPO_TYPE,
|
27 |
-
use_auth_token=HF_TOKEN,
|
28 |
-
)
|
29 |
-
|
30 |
-
# Create persistent storage directory
|
31 |
-
SAVE_DIR = os.path.join(PERSISTENT_DIR, "generated_images")
|
32 |
if not os.path.exists(SAVE_DIR):
|
33 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
34 |
|
@@ -43,13 +25,6 @@ pipeline = pipeline.to(device)
|
|
43 |
MAX_SEED = np.iinfo(np.int32).max
|
44 |
MAX_IMAGE_SIZE = 1024
|
45 |
|
46 |
-
def git_push():
|
47 |
-
if HF_TOKEN:
|
48 |
-
repo.git_pull()
|
49 |
-
repo.git_add()
|
50 |
-
repo.git_commit("Update generated images")
|
51 |
-
repo.git_push()
|
52 |
-
|
53 |
def save_generated_image(image, prompt):
|
54 |
# Generate unique filename with timestamp
|
55 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -65,19 +40,12 @@ def save_generated_image(image, prompt):
|
|
65 |
with open(metadata_file, "a", encoding="utf-8") as f:
|
66 |
f.write(f"{filename}|{prompt}|{timestamp}\n")
|
67 |
|
68 |
-
# Push changes to Hugging Face
|
69 |
-
git_push()
|
70 |
-
|
71 |
return filepath
|
72 |
|
73 |
def load_generated_images():
|
74 |
if not os.path.exists(SAVE_DIR):
|
75 |
return []
|
76 |
|
77 |
-
# Pull latest changes
|
78 |
-
if HF_TOKEN:
|
79 |
-
repo.git_pull()
|
80 |
-
|
81 |
# Load all images from the directory
|
82 |
image_files = [os.path.join(SAVE_DIR, f) for f in os.listdir(SAVE_DIR)
|
83 |
if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
|
@@ -122,8 +90,8 @@ def inference(
|
|
122 |
joint_attention_kwargs={"scale": lora_scale},
|
123 |
).images[0]
|
124 |
|
125 |
-
# Save the generated image
|
126 |
-
save_generated_image(image, prompt)
|
127 |
|
128 |
# Return the image, seed, and updated gallery
|
129 |
return image, seed, load_generated_images()
|
@@ -143,7 +111,7 @@ footer {
|
|
143 |
}
|
144 |
"""
|
145 |
|
146 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
147 |
gr.HTML('<div class="title"> Claude Monet STUDIO </div>')
|
148 |
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>')
|
149 |
|
@@ -218,24 +186,37 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
218 |
)
|
219 |
|
220 |
with gr.Tab("Gallery"):
|
|
|
221 |
generated_gallery = gr.Gallery(
|
222 |
label="Generated Images",
|
223 |
columns=6,
|
224 |
show_label=False,
|
225 |
value=load_generated_images(),
|
226 |
-
elem_id="generated_gallery"
|
|
|
227 |
)
|
|
|
228 |
|
229 |
# Add sample gallery section at the bottom
|
230 |
gr.Markdown("### Claude Monet Style Examples")
|
231 |
predefined_gallery = gr.Gallery(
|
232 |
label="Sample Images",
|
233 |
-
columns=3,
|
234 |
rows=2,
|
235 |
show_label=False,
|
236 |
value=load_predefined_images()
|
237 |
)
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
gr.on(
|
240 |
triggers=[run_button.click, prompt.submit],
|
241 |
fn=inference,
|
|
|
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 |
|
|
|
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")
|
|
|
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():
|
46 |
if not os.path.exists(SAVE_DIR):
|
47 |
return []
|
48 |
|
|
|
|
|
|
|
|
|
49 |
# Load all images from the directory
|
50 |
image_files = [os.path.join(SAVE_DIR, f) for f in os.listdir(SAVE_DIR)
|
51 |
if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
|
|
|
90 |
joint_attention_kwargs={"scale": lora_scale},
|
91 |
).images[0]
|
92 |
|
93 |
+
# Save the generated image
|
94 |
+
filepath = save_generated_image(image, prompt)
|
95 |
|
96 |
# Return the image, seed, and updated gallery
|
97 |
return image, seed, load_generated_images()
|
|
|
111 |
}
|
112 |
"""
|
113 |
|
114 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, analytics_enabled=False) as demo:
|
115 |
gr.HTML('<div class="title"> Claude Monet STUDIO </div>')
|
116 |
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>')
|
117 |
|
|
|
186 |
)
|
187 |
|
188 |
with gr.Tab("Gallery"):
|
189 |
+
gallery_header = gr.Markdown("### Generated Images Gallery")
|
190 |
generated_gallery = gr.Gallery(
|
191 |
label="Generated Images",
|
192 |
columns=6,
|
193 |
show_label=False,
|
194 |
value=load_generated_images(),
|
195 |
+
elem_id="generated_gallery",
|
196 |
+
height="auto"
|
197 |
)
|
198 |
+
refresh_btn = gr.Button("🔄 Refresh Gallery")
|
199 |
|
200 |
# Add sample gallery section at the bottom
|
201 |
gr.Markdown("### Claude Monet Style Examples")
|
202 |
predefined_gallery = gr.Gallery(
|
203 |
label="Sample Images",
|
204 |
+
columns=3,
|
205 |
rows=2,
|
206 |
show_label=False,
|
207 |
value=load_predefined_images()
|
208 |
)
|
209 |
|
210 |
+
# Event handlers
|
211 |
+
def refresh_gallery():
|
212 |
+
return load_generated_images()
|
213 |
+
|
214 |
+
refresh_btn.click(
|
215 |
+
fn=refresh_gallery,
|
216 |
+
inputs=None,
|
217 |
+
outputs=generated_gallery,
|
218 |
+
)
|
219 |
+
|
220 |
gr.on(
|
221 |
triggers=[run_button.click, prompt.submit],
|
222 |
fn=inference,
|