Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,9 +8,27 @@ import spaces
|
|
8 |
import torch
|
9 |
from diffusers import DiffusionPipeline
|
10 |
from PIL import Image
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
if not os.path.exists(SAVE_DIR):
|
15 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
16 |
|
@@ -25,6 +43,13 @@ pipeline = pipeline.to(device)
|
|
25 |
MAX_SEED = np.iinfo(np.int32).max
|
26 |
MAX_IMAGE_SIZE = 1024
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def save_generated_image(image, prompt):
|
29 |
# Generate unique filename with timestamp
|
30 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -40,12 +65,19 @@ def save_generated_image(image, prompt):
|
|
40 |
with open(metadata_file, "a", encoding="utf-8") as f:
|
41 |
f.write(f"{filename}|{prompt}|{timestamp}\n")
|
42 |
|
|
|
|
|
|
|
43 |
return filepath
|
44 |
|
45 |
def load_generated_images():
|
46 |
if not os.path.exists(SAVE_DIR):
|
47 |
return []
|
48 |
|
|
|
|
|
|
|
|
|
49 |
# Load all images from the directory
|
50 |
image_files = [os.path.join(SAVE_DIR, f) for f in os.listdir(SAVE_DIR)
|
51 |
if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
|
|
|
8 |
import torch
|
9 |
from diffusers import DiffusionPipeline
|
10 |
from PIL import Image
|
11 |
+
from huggingface_hub import HfFolder, Repository
|
12 |
+
from pathlib import Path
|
13 |
|
14 |
+
# Set up Hugging Face repository for permanent storage
|
15 |
+
REPO_ID = "YOUR_USERNAME/YOUR_SPACE_NAME" # Replace with your actual repo ID
|
16 |
+
REPO_TYPE = "space"
|
17 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Set this in your Space's secrets
|
18 |
+
PERSISTENT_DIR = os.path.join(os.getcwd(), "persistent")
|
19 |
+
|
20 |
+
# Initialize repository
|
21 |
+
if HF_TOKEN:
|
22 |
+
HfFolder.save_token(HF_TOKEN)
|
23 |
+
repo = Repository(
|
24 |
+
local_dir=PERSISTENT_DIR,
|
25 |
+
clone_from=REPO_ID,
|
26 |
+
repo_type=REPO_TYPE,
|
27 |
+
use_auth_token=HF_TOKEN,
|
28 |
+
)
|
29 |
+
|
30 |
+
# Create persistent storage directory
|
31 |
+
SAVE_DIR = os.path.join(PERSISTENT_DIR, "generated_images")
|
32 |
if not os.path.exists(SAVE_DIR):
|
33 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
34 |
|
|
|
43 |
MAX_SEED = np.iinfo(np.int32).max
|
44 |
MAX_IMAGE_SIZE = 1024
|
45 |
|
46 |
+
def git_push():
|
47 |
+
if HF_TOKEN:
|
48 |
+
repo.git_pull()
|
49 |
+
repo.git_add()
|
50 |
+
repo.git_commit("Update generated images")
|
51 |
+
repo.git_push()
|
52 |
+
|
53 |
def save_generated_image(image, prompt):
|
54 |
# Generate unique filename with timestamp
|
55 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
65 |
with open(metadata_file, "a", encoding="utf-8") as f:
|
66 |
f.write(f"{filename}|{prompt}|{timestamp}\n")
|
67 |
|
68 |
+
# Push changes to Hugging Face
|
69 |
+
git_push()
|
70 |
+
|
71 |
return filepath
|
72 |
|
73 |
def load_generated_images():
|
74 |
if not os.path.exists(SAVE_DIR):
|
75 |
return []
|
76 |
|
77 |
+
# Pull latest changes
|
78 |
+
if HF_TOKEN:
|
79 |
+
repo.git_pull()
|
80 |
+
|
81 |
# Load all images from the directory
|
82 |
image_files = [os.path.join(SAVE_DIR, f) for f in os.listdir(SAVE_DIR)
|
83 |
if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
|