Spaces:
Runtime error
Runtime error
Update backup.app.py
Browse files- backup.app.py +126 -23
backup.app.py
CHANGED
@@ -9,11 +9,10 @@ import re
|
|
9 |
import random
|
10 |
import torch
|
11 |
import time
|
12 |
-
import
|
13 |
-
|
14 |
from PIL import Image
|
15 |
from io import BytesIO
|
16 |
-
from PIL import Image
|
17 |
from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
|
18 |
|
19 |
try:
|
@@ -33,6 +32,97 @@ device = torch.device(
|
|
33 |
torch_device = device
|
34 |
torch_dtype = torch.float16
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
print(f"SAFETY_CHECKER: {SAFETY_CHECKER}")
|
37 |
print(f"TORCH_COMPILE: {TORCH_COMPILE}")
|
38 |
print(f"device: {device}")
|
@@ -105,23 +195,25 @@ def predict(prompt, guidance, steps, seed=1231231):
|
|
105 |
if nsfw_content_detected:
|
106 |
nsfw=gr.Button("🕹️NSFW🎨", scale=1)
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
125 |
|
126 |
return results.images[0] if len(results.images) > 0 else None
|
127 |
|
@@ -138,9 +230,10 @@ css = """
|
|
138 |
}
|
139 |
"""
|
140 |
with gr.Blocks(css=css) as demo:
|
|
|
141 |
with gr.Column(elem_id="container"):
|
142 |
gr.Markdown(
|
143 |
-
"""
|
144 |
elem_id="intro",
|
145 |
)
|
146 |
with gr.Row():
|
@@ -166,6 +259,12 @@ with gr.Blocks(css=css) as demo:
|
|
166 |
label="Generated Images", show_label=False, elem_id="gallery"
|
167 |
)
|
168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
# Advanced Generate Options
|
170 |
with gr.Accordion("Advanced options", open=False):
|
171 |
guidance = gr.Slider(
|
@@ -186,11 +285,9 @@ with gr.Blocks(css=css) as demo:
|
|
186 |
|
187 |
```py
|
188 |
from diffusers import DiffusionPipeline, LCMScheduler
|
189 |
-
|
190 |
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
|
191 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
192 |
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
|
193 |
-
|
194 |
results = pipe(
|
195 |
prompt="ImageEditor",
|
196 |
num_inference_steps=4,
|
@@ -210,5 +307,11 @@ with gr.Blocks(css=css) as demo:
|
|
210 |
steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
211 |
seed.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
demo.queue()
|
214 |
demo.launch()
|
|
|
9 |
import random
|
10 |
import torch
|
11 |
import time
|
12 |
+
import shutil # Added for zip functionality
|
13 |
+
import zipfile
|
14 |
from PIL import Image
|
15 |
from io import BytesIO
|
|
|
16 |
from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
|
17 |
|
18 |
try:
|
|
|
32 |
torch_device = device
|
33 |
torch_dtype = torch.float16
|
34 |
|
35 |
+
|
36 |
+
|
37 |
+
# Function to encode a file to base64
|
38 |
+
def encode_file_to_base64(file_path):
|
39 |
+
with open(file_path, "rb") as file:
|
40 |
+
encoded = base64.b64encode(file.read()).decode()
|
41 |
+
return encoded
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
def create_zip_of_files(files):
|
46 |
+
"""
|
47 |
+
Create a zip file from a list of files.
|
48 |
+
"""
|
49 |
+
zip_name = "all_files.zip"
|
50 |
+
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
51 |
+
for file in files:
|
52 |
+
zipf.write(file)
|
53 |
+
return zip_name
|
54 |
+
|
55 |
+
|
56 |
+
def get_zip_download_link(zip_file):
|
57 |
+
"""
|
58 |
+
Generate a link to download the zip file.
|
59 |
+
"""
|
60 |
+
with open(zip_file, 'rb') as f:
|
61 |
+
data = f.read()
|
62 |
+
b64 = base64.b64encode(data).decode()
|
63 |
+
href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
|
64 |
+
return href
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
# Function to clear all image files
|
69 |
+
def clear_all_images():
|
70 |
+
base_dir = os.getcwd() # Get the current base directory
|
71 |
+
img_files = [file for file in os.listdir(base_dir) if file.lower().endswith((".png", ".jpg", ".jpeg"))] # List all files ending with ".jpg" or ".jpeg"
|
72 |
+
|
73 |
+
# Remove all image files
|
74 |
+
for file in img_files:
|
75 |
+
os.remove(file)
|
76 |
+
print('removed:' + file)
|
77 |
+
|
78 |
+
|
79 |
+
# add file save and download and clear:
|
80 |
+
# Function to create a zip file from a list of files
|
81 |
+
def create_zip(files):
|
82 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
83 |
+
zip_filename = f"images_{timestamp}.zip"
|
84 |
+
print('Creating file ' + zip_filename)
|
85 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
86 |
+
for file in files:
|
87 |
+
zipf.write(file, os.path.basename(file))
|
88 |
+
print('added:' + file)
|
89 |
+
return zip_filename
|
90 |
+
|
91 |
+
# Function to save all images as a zip file and provide a base64 download link
|
92 |
+
def save_all_images(images):
|
93 |
+
if len(images) == 0:
|
94 |
+
return None, None
|
95 |
+
zip_filename = create_zip(images) # Create a zip file from the list of image files
|
96 |
+
print(' Zip file created:' + zip_filename)
|
97 |
+
|
98 |
+
# gr.Button(link="/file=" + zip_filename)
|
99 |
+
|
100 |
+
# remove?
|
101 |
+
zip_base64 = encode_file_to_base64(zip_filename) # Encode the zip file to base64
|
102 |
+
download_link = f'<a href="data:application/zip;base64,{zip_base64}" download="{zip_filename}">Download All</a>'
|
103 |
+
|
104 |
+
# redirect_button = gr.Button("Clear", variant='secondary')
|
105 |
+
# redirect_button.click(None, None,None, _js="window.location.assign('https://google.com');")
|
106 |
+
|
107 |
+
return zip_filename, download_link
|
108 |
+
|
109 |
+
# Function to handle "Save All" button click
|
110 |
+
def save_all_button_click():
|
111 |
+
images = [file for file in os.listdir() if file.lower().endswith((".png", ".jpg", ".jpeg"))]
|
112 |
+
zip_filename, download_link = save_all_images(images)
|
113 |
+
if zip_filename:
|
114 |
+
print(zip_filename)
|
115 |
+
gr.Button(link=zip_filename)
|
116 |
+
# gr.File(value=zip_filename)
|
117 |
+
if download_link:
|
118 |
+
print(download_link)
|
119 |
+
#gr.HTML(download_link)
|
120 |
+
gr.Button(link=download_link)
|
121 |
+
|
122 |
+
# Function to handle "Clear All" button click
|
123 |
+
def clear_all_button_click():
|
124 |
+
clear_all_images()
|
125 |
+
|
126 |
print(f"SAFETY_CHECKER: {SAFETY_CHECKER}")
|
127 |
print(f"TORCH_COMPILE: {TORCH_COMPILE}")
|
128 |
print(f"device: {device}")
|
|
|
195 |
if nsfw_content_detected:
|
196 |
nsfw=gr.Button("🕹️NSFW🎨", scale=1)
|
197 |
|
198 |
+
try:
|
199 |
+
central = pytz.timezone('US/Central')
|
200 |
+
safe_date_time = datetime.datetime.now().strftime("%Y%m%d")
|
201 |
+
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
202 |
+
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
|
203 |
+
filename = f"{safe_date_time}_{safe_prompt}.png"
|
204 |
+
|
205 |
+
# Save the image
|
206 |
+
if len(results.images) > 0:
|
207 |
+
image_path = os.path.join("", filename) # Specify your directory
|
208 |
+
results.images[0].save(image_path)
|
209 |
+
print(f"#Image saved as {image_path}")
|
210 |
+
gr.File(image_path)
|
211 |
+
gr.Button(link=image_path)
|
212 |
+
# encoded_image = encode_image(image)
|
213 |
+
# html_link = f'<a href="data:image/png;base64,{encoded_image}" download="{filename}">Download Image</a>'
|
214 |
+
# gr.HTML(html_link)
|
215 |
+
except:
|
216 |
+
return results.images[0]
|
217 |
|
218 |
return results.images[0] if len(results.images) > 0 else None
|
219 |
|
|
|
230 |
}
|
231 |
"""
|
232 |
with gr.Blocks(css=css) as demo:
|
233 |
+
|
234 |
with gr.Column(elem_id="container"):
|
235 |
gr.Markdown(
|
236 |
+
"""4📝RT🖼️Images - 🕹️ Real Time 🎨 Image Generator Gallery 🌐""",
|
237 |
elem_id="intro",
|
238 |
)
|
239 |
with gr.Row():
|
|
|
259 |
label="Generated Images", show_label=False, elem_id="gallery"
|
260 |
)
|
261 |
|
262 |
+
with gr.Row(variant="compact"):
|
263 |
+
# Add "Save All" button with emoji
|
264 |
+
save_all_button = gr.Button("💾 Save All", scale=1)
|
265 |
+
# Add "Clear All" button with emoji
|
266 |
+
clear_all_button = gr.Button("🗑️ Clear All", scale=1)
|
267 |
+
|
268 |
# Advanced Generate Options
|
269 |
with gr.Accordion("Advanced options", open=False):
|
270 |
guidance = gr.Slider(
|
|
|
285 |
|
286 |
```py
|
287 |
from diffusers import DiffusionPipeline, LCMScheduler
|
|
|
288 |
pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
|
289 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
290 |
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
|
|
|
291 |
results = pipe(
|
292 |
prompt="ImageEditor",
|
293 |
num_inference_steps=4,
|
|
|
307 |
steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
308 |
seed.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
|
309 |
|
310 |
+
# Attach click event handlers to the buttons
|
311 |
+
save_all_button.click(save_all_button_click)
|
312 |
+
clear_all_button.click(clear_all_button_click)
|
313 |
+
|
314 |
+
|
315 |
+
|
316 |
demo.queue()
|
317 |
demo.launch()
|