fantos commited on
Commit
ac3894a
β€’
1 Parent(s): d6d81d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -29
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
- # Setup and initialization code
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
- # Change gallery path to user's home directory for persistence
21
- # Setup and initialization code
22
- # Hugging Face Spaces permanent storage path
23
- gallery_path = "/data/gallery" # 영ꡬ μ €μž₯을 μœ„ν•œ Spaces /data 디렉토리 μ‚¬μš©
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
- if isinstance(image, Image.Image):
168
- # Save with maximum quality
169
- image.save(filepath, "PNG", quality=100)
170
- else:
171
- image = Image.fromarray(image)
172
- image.save(filepath, "PNG", quality=100)
 
173
 
174
- # Verify the file was saved correctly
175
- if not os.path.exists(filepath):
176
- print(f"Warning: Failed to verify saved image at {filepath}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  return None
178
 
179
- return filepath
180
  except Exception as e:
181
- print(f"Error saving image: {str(e)}")
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=["/data"]) # /data 디렉토리 전체에 λŒ€ν•œ μ ‘κ·Ό ν—ˆμš©
 
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])