Spaces:
Sleeping
Sleeping
Update start.py
Browse files
start.py
CHANGED
|
@@ -1,34 +1,18 @@
|
|
| 1 |
-
# start.py
|
| 2 |
-
|
| 3 |
-
# Hugging Face Spaces runs pip install from requirements.txt during build; but some packages'
|
| 4 |
-
# setup.py import torch at that time causing failures. We ensure torch exists before importing tinyllava.
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
import subprocess
|
| 8 |
-
import sys
|
| 9 |
-
import pkg_resources
|
| 10 |
-
import importlib
|
| 11 |
|
| 12 |
def ensure_torch():
|
| 13 |
try:
|
| 14 |
-
import torch
|
| 15 |
-
print("torch
|
| 16 |
return
|
| 17 |
except Exception:
|
| 18 |
-
print("
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
subprocess.check_call(cmd)
|
| 24 |
-
# verify
|
| 25 |
-
import torch # type: ignore
|
| 26 |
-
print("Installed torch", torch.__version__)
|
| 27 |
-
|
| 28 |
-
def main():
|
| 29 |
-
ensure_torch()
|
| 30 |
-
# Now launch the main app. We exec app.py so that environment is clean and imports happen after torch install.
|
| 31 |
-
os.execvp(sys.executable, [sys.executable, "app.py"])
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
|
|
|
|
|
|
| 1 |
+
# start.py - ensure torch is installed at runtime then run app.py
|
| 2 |
+
import os, sys, subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def ensure_torch():
|
| 5 |
try:
|
| 6 |
+
import torch
|
| 7 |
+
print("torch present:", torch.__version__)
|
| 8 |
return
|
| 9 |
except Exception:
|
| 10 |
+
print("Installing CPU torch...")
|
| 11 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir",
|
| 12 |
+
"torch==2.2.0+cpu", "--extra-index-url", "https://download.pytorch.org/whl/cpu"])
|
| 13 |
+
import torch
|
| 14 |
+
print("Installed torch", torch.__version__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
| 17 |
+
ensure_torch()
|
| 18 |
+
os.execv(sys.executable, [sys.executable, "app.py"])
|