awacke1 commited on
Commit
3d80262
1 Parent(s): 4aa227b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -52
app.py CHANGED
@@ -10,11 +10,10 @@ import random
10
  import torch
11
  import time
12
  import shutil # Added for zip functionality
 
13
  from PIL import Image
14
  from io import BytesIO
15
  from diffusers import DiffusionPipeline, LCMScheduler, AutoencoderTiny
16
- import zipfile
17
- import glob
18
 
19
  try:
20
  import intel_extension_for_pytorch as ipex
@@ -33,54 +32,12 @@ device = torch.device(
33
  torch_device = device
34
  torch_dtype = torch.float16
35
 
36
-
37
- # Function to create a zip file from a list of files
38
- def create_zip(files):
39
- timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
40
- zip_filename = f"images_{timestamp}.zip"
41
- print('Creating file ' + zip_filename)
42
- with zipfile.ZipFile(zip_filename, 'w') as zipf:
43
- for file in files:
44
- zipf.write(file, os.path.basename(file))
45
- print('added:' + file)
46
- return zip_filename
47
-
48
- # Function to save all images as a zip file and provide a base64 download link
49
- def save_all_images(images):
50
- if len(images) == 0:
51
- return None, None
52
- zip_filename = create_zip(images) # Create a zip file from the list of image files
53
- print(' Zip file created:' + zip_filename)
54
-
55
- zip_base64 = encode_file_to_base64(zip_filename) # Encode the zip file to base64
56
- download_link = f'<a href="data:application/zip;base64,{zip_base64}" download="{zip_filename}">Download All</a>'
57
-
58
- return zip_filename, download_link
59
-
60
- # Function to handle "Save All" button click
61
- def save_all_button_click():
62
- images = [file for file in os.listdir() if file.lower().endswith((".png", ".jpg", ".jpeg"))]
63
- zip_filename, download_link = save_all_images(images)
64
- if zip_filename:
65
- print(zip_filename)
66
- gr.Button(link=zip_filename)
67
- if download_link:
68
- print(download_link)
69
- #gr.HTML(download_link)
70
- gr.Button(link=download_link)
71
-
72
- # Function to handle "Clear All" button click
73
- def clear_all_button_click():
74
- clear_all_images()
75
-
76
  # Function to encode a file to base64
77
  def encode_file_to_base64(file_path):
78
  with open(file_path, "rb") as file:
79
  encoded = base64.b64encode(file.read()).decode()
80
  return encoded
81
 
82
-
83
-
84
  def create_zip_of_files(files):
85
  """
86
  Create a zip file from a list of files.
@@ -102,8 +59,6 @@ def get_zip_download_link(zip_file):
102
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
103
  return href
104
 
105
-
106
-
107
  # Function to clear all image files
108
  def clear_all_images():
109
  base_dir = os.getcwd() # Get the current base directory
@@ -113,8 +68,7 @@ def clear_all_images():
113
  for file in img_files:
114
  os.remove(file)
115
  print('removed:' + file)
116
-
117
-
118
  # add file save and download and clear:
119
  # Function to create a zip file from a list of files
120
  def create_zip(files):
@@ -324,11 +278,9 @@ with gr.Blocks(css=css) as demo:
324
 
325
  ```py
326
  from diffusers import DiffusionPipeline, LCMScheduler
327
-
328
  pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
329
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
330
  pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
331
-
332
  results = pipe(
333
  prompt="ImageEditor",
334
  num_inference_steps=4,
@@ -352,7 +304,5 @@ with gr.Blocks(css=css) as demo:
352
  save_all_button.click(save_all_button_click)
353
  clear_all_button.click(clear_all_button_click)
354
 
355
-
356
-
357
  demo.queue()
358
  demo.launch()
 
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:
19
  import intel_extension_for_pytorch as ipex
 
32
  torch_device = device
33
  torch_dtype = torch.float16
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Function to encode a file to base64
36
  def encode_file_to_base64(file_path):
37
  with open(file_path, "rb") as file:
38
  encoded = base64.b64encode(file.read()).decode()
39
  return encoded
40
 
 
 
41
  def create_zip_of_files(files):
42
  """
43
  Create a zip file from a list of files.
 
59
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
60
  return href
61
 
 
 
62
  # Function to clear all image files
63
  def clear_all_images():
64
  base_dir = os.getcwd() # Get the current base directory
 
68
  for file in img_files:
69
  os.remove(file)
70
  print('removed:' + file)
71
+
 
72
  # add file save and download and clear:
73
  # Function to create a zip file from a list of files
74
  def create_zip(files):
 
278
 
279
  ```py
280
  from diffusers import DiffusionPipeline, LCMScheduler
 
281
  pipe = DiffusionPipeline.from_pretrained("Lykon/dreamshaper-7").to("cuda")
282
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
283
  pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") #yes, it's a normal LoRA
 
284
  results = pipe(
285
  prompt="ImageEditor",
286
  num_inference_steps=4,
 
304
  save_all_button.click(save_all_button_click)
305
  clear_all_button.click(clear_all_button_click)
306
 
 
 
307
  demo.queue()
308
  demo.launch()