vkaigen commited on
Commit
1ae4f47
·
verified ·
1 Parent(s): 2a301cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import subprocess
4
+ import threading
5
+ import time
6
+
7
+ # -----------------------------
8
+ # Settings (you can tweak these)
9
+ # -----------------------------
10
+ FOOOCUS_REPO = "https://github.com/lllyasviel/Fooocus.git"
11
+ FOOOCUS_DIR = "Fooocus"
12
+
13
+ # Use ephemeral storage so nothing persists on Hugging Face disk
14
+ EPHEMERAL_OUTPUT_DIR = "/tmp/fooocus_outputs"
15
+
16
+ # Auto-delete interval (seconds)
17
+ CLEAN_EVERY_SECONDS = 60
18
+
19
+ # -----------------------------
20
+ # Helpers
21
+ # -----------------------------
22
+ def run(cmd, cwd=None, check=True):
23
+ print(f"\n[RUN] {cmd}\n")
24
+ return subprocess.run(cmd, shell=True, cwd=cwd, check=check)
25
+
26
+ def ensure_ephemeral_outputs_symlink():
27
+ """
28
+ Fooocus writes to: Fooocus/outputs
29
+ We replace that folder with a symlink to /tmp so it doesn't persist.
30
+ """
31
+ # Fresh ephemeral folder each start
32
+ shutil.rmtree(EPHEMERAL_OUTPUT_DIR, ignore_errors=True)
33
+ os.makedirs(EPHEMERAL_OUTPUT_DIR, exist_ok=True)
34
+
35
+ outputs_path = os.path.join(FOOOCUS_DIR, "outputs")
36
+
37
+ # Remove existing outputs folder/symlink if present
38
+ if os.path.islink(outputs_path):
39
+ os.unlink(outputs_path)
40
+ elif os.path.exists(outputs_path):
41
+ shutil.rmtree(outputs_path, ignore_errors=True)
42
+
43
+ # Create symlink: Fooocus/outputs -> /tmp/fooocus_outputs
44
+ os.symlink(EPHEMERAL_OUTPUT_DIR, outputs_path)
45
+ print(f"[OK] Redirected outputs to ephemeral: {outputs_path} -> {EPHEMERAL_OUTPUT_DIR}")
46
+
47
+ def auto_cleaner_loop():
48
+ """
49
+ Continuously deletes everything inside EPHEMERAL_OUTPUT_DIR.
50
+ This prevents any buildup even during long sessions.
51
+ """
52
+ while True:
53
+ try:
54
+ if os.path.exists(EPHEMERAL_OUTPUT_DIR):
55
+ # Delete contents but keep the folder
56
+ for name in os.listdir(EPHEMERAL_OUTPUT_DIR):
57
+ p = os.path.join(EPHEMERAL_OUTPUT_DIR, name)
58
+ if os.path.isdir(p):
59
+ shutil.rmtree(p, ignore_errors=True)
60
+ else:
61
+ try:
62
+ os.remove(p)
63
+ except FileNotFoundError:
64
+ pass
65
+ print(f"[CLEAN] Emptied {EPHEMERAL_OUTPUT_DIR}")
66
+ except Exception as e:
67
+ print(f"[CLEAN-WARN] {e}")
68
+ time.sleep(CLEAN_EVERY_SECONDS)
69
+
70
+ # -----------------------------
71
+ # Main setup
72
+ # -----------------------------
73
+ def main():
74
+ # 1) Clone Fooocus if missing
75
+ if not os.path.exists(FOOOCUS_DIR):
76
+ run(f"git clone {FOOOCUS_REPO}")
77
+
78
+ # 2) Ensure ephemeral outputs (non-persistent)
79
+ ensure_ephemeral_outputs_symlink()
80
+
81
+ # 3) Install dependencies once (marker file inside Fooocus)
82
+ marker = os.path.join(FOOOCUS_DIR, ".installed")
83
+ if not os.path.exists(marker):
84
+ run("pip install -r requirements_versions.txt", cwd=FOOOCUS_DIR)
85
+ with open(marker, "w") as f:
86
+ f.write("ok\n")
87
+ print("[OK] Dependencies installed (one-time).")
88
+ else:
89
+ print("[OK] Dependencies already installed. Skipping pip install.")
90
+
91
+ # 4) Start background auto-cleaner thread (keeps storage clean during runtime)
92
+ t = threading.Thread(target=auto_cleaner_loop, daemon=True)
93
+ t.start()
94
+ print(f"[OK] Auto-cleaner running every {CLEAN_EVERY_SECONDS} seconds.")
95
+
96
+ # 5) Launch Fooocus
97
+ # NOTE: Do not use --share on Hugging Face Spaces
98
+ run("python entry_with_update.py --listen 0.0.0.0 --port 7860", cwd=FOOOCUS_DIR, check=True)
99
+
100
+ # Run
101
+ if __name__ == "__main__":
102
+ main()