Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,10 +10,11 @@ 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:
|
| 19 |
import intel_extension_for_pytorch as ipex
|
|
@@ -33,6 +34,44 @@ 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):
|
|
|
|
| 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
|
|
|
|
| 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):
|