C98yhou079 commited on
Commit
d5685a6
·
verified ·
1 Parent(s): 036de35

Update start.py

Browse files
Files changed (1) hide show
  1. start.py +11 -27
start.py CHANGED
@@ -1,34 +1,18 @@
1
- # start.py
2
- # This script installs torch (CPU wheel) at runtime if not present, then launches app.py.
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 # type: ignore
15
- print("torch already installed:", torch.__version__)
16
  return
17
  except Exception:
18
- print("torch not found — installing CPU wheel...")
19
-
20
- # Install CPU-only torch compatible with many Linux builds. Use pip to install the CPU wheel.
21
- # Use a stable torch CPU build (version chosen to be recent and broadly compatible).
22
- cmd = [sys.executable, "-m", "pip", "install", "--no-cache-dir", "torch==2.2.0+cpu", "--extra-index-url", "https://download.pytorch.org/whl/cpu"]
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
- main()
 
 
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"])