Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,76 @@ import torch
|
|
4 |
import logging
|
5 |
from diffusers import DiffusionPipeline
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import logging
|
5 |
from diffusers import DiffusionPipeline
|
6 |
|
7 |
+
def _get_output(cmd):
|
8 |
+
try:
|
9 |
+
return subprocess.check_output(cmd).decode("utf-8")
|
10 |
+
except Exception as ex:
|
11 |
+
logging.exception(ex)
|
12 |
|
13 |
+
return None
|
14 |
+
|
15 |
+
def install_cuda_toolkit():
|
16 |
+
# CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run"
|
17 |
+
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run"
|
18 |
+
CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL)
|
19 |
+
subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE])
|
20 |
+
subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE])
|
21 |
+
subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"])
|
22 |
+
|
23 |
+
os.environ["CUDA_HOME"] = "/usr/local/cuda"
|
24 |
+
os.environ["PATH"] = "%s/bin:%s" % (os.environ["CUDA_HOME"], os.environ["PATH"])
|
25 |
+
os.environ["LD_LIBRARY_PATH"] = "%s/lib:%s" % (
|
26 |
+
os.environ["CUDA_HOME"],
|
27 |
+
"" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"],
|
28 |
+
)
|
29 |
+
# Fix: arch_list[-1] += '+PTX'; IndexError: list index out of range
|
30 |
+
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
31 |
+
|
32 |
+
def setup_runtime_env():
|
33 |
+
logging.info("Python Version: %s" % _get_output(["python", "--version"]))
|
34 |
+
logging.info("CUDA Version: %s" % _get_output(["nvcc", "--version"]))
|
35 |
+
logging.info("GCC Version: %s" % _get_output(["gcc", "--version"]))
|
36 |
+
logging.info("CUDA is available: %s" % torch.cuda.is_available())
|
37 |
+
logging.info("CUDA Device Capability: %s" % (torch.cuda.get_device_capability(),))
|
38 |
+
|
39 |
+
# Install Pre-compiled CUDA extensions (Fallback to this solution on 12/31/24)
|
40 |
+
# Ref: https://huggingface.co/spaces/zero-gpu-explorers/README/discussions/110
|
41 |
+
ext_dir = os.path.join(os.path.dirname(__file__), "wheels")
|
42 |
+
for e in os.listdir(ext_dir):
|
43 |
+
logging.info("Installing Extensions from %s" % e)
|
44 |
+
subprocess.call(
|
45 |
+
["pip", "install", os.path.join(ext_dir, e)], stderr=subprocess.STDOUT
|
46 |
+
)
|
47 |
+
# Compile CUDA extensions
|
48 |
+
# Update on 12/31/24: No module named 'torch'. But it is installed and listed by `pip list`
|
49 |
+
# ext_dir = os.path.join(os.path.dirname(__file__), "citydreamer", "extensions")
|
50 |
+
# for e in os.listdir(ext_dir):
|
51 |
+
# if os.path.isdir(os.path.join(ext_dir, e)):
|
52 |
+
# subprocess.call(["pip", "install", "."], cwd=os.path.join(ext_dir, e))
|
53 |
+
|
54 |
+
logging.info("Installed Python Packages: %s" % _get_output(["pip", "list"]))
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
logging.basicConfig(
|
60 |
+
format="[%(levelname)s] %(asctime)s %(message)s", level=logging.INFO
|
61 |
+
)
|
62 |
+
logging.info("Environment Variables: %s" % os.environ)
|
63 |
+
if _get_output(["nvcc", "--version"]) is None:
|
64 |
+
logging.info("Installing CUDA toolkit...")
|
65 |
+
install_cuda_toolkit()
|
66 |
+
else:
|
67 |
+
logging.info("Detected CUDA: %s" % _get_output(["nvcc", "--version"]))
|
68 |
+
|
69 |
+
logging.info("Installing CUDA extensions...")
|
70 |
+
setup_runtime_env()
|
71 |
+
|
72 |
+
logging.info("Starting the main application...")
|
73 |
+
main(os.getenv("DEBUG") == "1")
|
74 |
+
|
75 |
+
|
76 |
+
# pipe = DiffusionPipeline.from_pretrained("azaneko/HiDream-I1-Fast-nf4")
|
77 |
+
|
78 |
+
# prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
|
79 |
+
# image = pipe(prompt).images[0]
|