Upload fooocus.py
Browse files- fooocus.py +75 -0
fooocus.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import shutil
|
4 |
+
import subprocess
|
5 |
+
import signal
|
6 |
+
from urllib.request import Request, urlopen
|
7 |
+
from tqdm import tqdm # Progress bar library
|
8 |
+
|
9 |
+
# Constants
|
10 |
+
MODEL_URL = "https://huggingface.co/oieieio/juggernautXL_v8Rundiffusion/resolve/main/juggernautXL_v8Rundiffusion.safetensors"
|
11 |
+
MODEL_PATH = "models/checkpoints/juggernautXL_v8Rundiffusion.safetensors"
|
12 |
+
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
13 |
+
|
14 |
+
# Cleanup temporary directory
|
15 |
+
def cleanup_temp_dir():
|
16 |
+
temp_dir = "/tmp/fooocus"
|
17 |
+
print(f"[Cleanup] Attempting to delete content of temp dir {temp_dir}")
|
18 |
+
try:
|
19 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
20 |
+
print("[Cleanup] Cleanup successful")
|
21 |
+
except Exception as e:
|
22 |
+
print(f"[Cleanup] Failed to delete content of temp dir: {e}")
|
23 |
+
|
24 |
+
# Download the model with progress
|
25 |
+
def download_file(url, target_path):
|
26 |
+
if os.path.exists(target_path):
|
27 |
+
print(f"[Model Download] Model already exists at {target_path}")
|
28 |
+
return
|
29 |
+
|
30 |
+
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
31 |
+
print(f"[Model Download] Downloading: \"{url}\" to {target_path}")
|
32 |
+
|
33 |
+
headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"} if HUGGINGFACE_TOKEN else {}
|
34 |
+
req = Request(url, headers=headers)
|
35 |
+
|
36 |
+
try:
|
37 |
+
with urlopen(req) as response, open(target_path, "wb") as out_file:
|
38 |
+
total_size = int(response.getheader("Content-Length", 0))
|
39 |
+
with tqdm(total=total_size, unit="B", unit_scale=True, desc="Downloading") as progress_bar:
|
40 |
+
for chunk in iter(lambda: response.read(1024 * 8), b""):
|
41 |
+
out_file.write(chunk)
|
42 |
+
progress_bar.update(len(chunk))
|
43 |
+
print(f"[Model Download] Successfully downloaded model to {target_path}")
|
44 |
+
except Exception as e:
|
45 |
+
print(f"[Model Download] Failed to download {url}. Error: {e}")
|
46 |
+
sys.exit(1)
|
47 |
+
|
48 |
+
# Launch Fooocus application
|
49 |
+
def launch_fooocus():
|
50 |
+
print("[Fooocus] Launching application...")
|
51 |
+
try:
|
52 |
+
subprocess.run([sys.executable, "launch.py"], check=True)
|
53 |
+
except subprocess.CalledProcessError as e:
|
54 |
+
print(f"[Fooocus] Failed to launch application: {e}")
|
55 |
+
sys.exit(1)
|
56 |
+
|
57 |
+
# Signal handling for graceful shutdown
|
58 |
+
def signal_handler(signum, frame):
|
59 |
+
print("[Fooocus] Shutting down gracefully...")
|
60 |
+
sys.exit(0)
|
61 |
+
|
62 |
+
signal.signal(signal.SIGINT, signal_handler)
|
63 |
+
signal.signal(signal.SIGTERM, signal_handler)
|
64 |
+
|
65 |
+
# Main logic
|
66 |
+
def main():
|
67 |
+
print(f"Python {sys.version}")
|
68 |
+
print("Fooocus version: 2.5.5")
|
69 |
+
|
70 |
+
cleanup_temp_dir()
|
71 |
+
download_file(MODEL_URL, MODEL_PATH)
|
72 |
+
launch_fooocus()
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
main()
|