Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -11,19 +11,18 @@ import gradio as gr
|
|
11 |
import torch
|
12 |
from diffusers import FluxPipeline
|
13 |
from PIL import Image
|
14 |
-
|
15 |
-
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
16 |
-
# μ΄ λΆλΆμ μμ
|
17 |
|
18 |
# Setup and initialization code
|
19 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
25 |
os.environ["HF_HUB_CACHE"] = cache_path
|
26 |
os.environ["HF_HOME"] = cache_path
|
|
|
27 |
torch.backends.cuda.matmul.allow_tf32 = True
|
28 |
|
29 |
# Create gallery directory if it doesn't exist
|
@@ -151,36 +150,44 @@ footer {display: none !important}
|
|
151 |
max-width: 100% !important;
|
152 |
}
|
153 |
"""
|
154 |
-
|
155 |
def save_image(image):
|
156 |
"""Save the generated image and return the path"""
|
157 |
-
# Ensure gallery directory exists
|
158 |
-
os.makedirs(gallery_path, exist_ok=True)
|
159 |
-
|
160 |
-
# Generate unique filename with timestamp and random suffix
|
161 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
162 |
-
random_suffix = os.urandom(4).hex()
|
163 |
-
filename = f"generated_{timestamp}_{random_suffix}.png"
|
164 |
-
filepath = os.path.join(gallery_path, filename)
|
165 |
-
|
166 |
try:
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
173 |
|
174 |
-
#
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
return None
|
178 |
|
179 |
-
return filepath
|
180 |
except Exception as e:
|
181 |
-
print(f"Error
|
182 |
return None
|
183 |
|
|
|
184 |
def load_gallery():
|
185 |
"""Load all images from the gallery directory"""
|
186 |
try:
|
@@ -363,4 +370,4 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
363 |
)
|
364 |
|
365 |
if __name__ == "__main__":
|
366 |
-
demo.launch(allowed_paths=[
|
|
|
11 |
import torch
|
12 |
from diffusers import FluxPipeline
|
13 |
from PIL import Image
|
14 |
+
|
|
|
|
|
15 |
|
16 |
# Setup and initialization code
|
17 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
18 |
+
# Use PERSISTENT_DIR environment variable for Spaces
|
19 |
+
PERSISTENT_DIR = os.environ.get("PERSISTENT_DIR", ".")
|
20 |
+
gallery_path = path.join(PERSISTENT_DIR, "gallery")
|
21 |
+
|
22 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
23 |
os.environ["HF_HUB_CACHE"] = cache_path
|
24 |
os.environ["HF_HOME"] = cache_path
|
25 |
+
|
26 |
torch.backends.cuda.matmul.allow_tf32 = True
|
27 |
|
28 |
# Create gallery directory if it doesn't exist
|
|
|
150 |
max-width: 100% !important;
|
151 |
}
|
152 |
"""
|
|
|
153 |
def save_image(image):
|
154 |
"""Save the generated image and return the path"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
try:
|
156 |
+
# Ensure gallery directory exists
|
157 |
+
if not os.path.exists(gallery_path):
|
158 |
+
try:
|
159 |
+
os.makedirs(gallery_path, exist_ok=True)
|
160 |
+
except Exception as e:
|
161 |
+
print(f"Failed to create gallery directory: {str(e)}")
|
162 |
+
return None
|
163 |
|
164 |
+
# Generate unique filename with timestamp and random suffix
|
165 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
166 |
+
random_suffix = os.urandom(4).hex()
|
167 |
+
filename = f"generated_{timestamp}_{random_suffix}.png"
|
168 |
+
filepath = os.path.join(gallery_path, filename)
|
169 |
+
|
170 |
+
try:
|
171 |
+
if isinstance(image, Image.Image):
|
172 |
+
image.save(filepath, "PNG", quality=100)
|
173 |
+
else:
|
174 |
+
image = Image.fromarray(image)
|
175 |
+
image.save(filepath, "PNG", quality=100)
|
176 |
+
|
177 |
+
if not os.path.exists(filepath):
|
178 |
+
print(f"Warning: Failed to verify saved image at {filepath}")
|
179 |
+
return None
|
180 |
+
|
181 |
+
return filepath
|
182 |
+
except Exception as e:
|
183 |
+
print(f"Failed to save image: {str(e)}")
|
184 |
return None
|
185 |
|
|
|
186 |
except Exception as e:
|
187 |
+
print(f"Error in save_image: {str(e)}")
|
188 |
return None
|
189 |
|
190 |
+
|
191 |
def load_gallery():
|
192 |
"""Load all images from the gallery directory"""
|
193 |
try:
|
|
|
370 |
)
|
371 |
|
372 |
if __name__ == "__main__":
|
373 |
+
demo.launch(allowed_paths=[PERSISTENT_DIR])
|