ShesterG commited on
Commit
4bac563
·
1 Parent(s): b5b01b8

fix Space startup: lazy model load + module-level launch (no __main__ guard)

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -19,15 +19,23 @@ RES, N_PREFIX, MS = 224, 5, 4
19
  DEV = "cuda" if torch.cuda.is_available() else "cpu"
20
  TOKEN = os.environ.get("HF_TOKEN")
21
 
22
- model = AutoModel.from_pretrained(REPO, dtype=torch.float32, token=TOKEN,
23
- attn_implementation="eager").eval().to(DEV)
24
  MEAN = torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
25
  STD = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
26
  TREE_HTML = open("tree_widget.html").read()
27
  REVEAL_HTML = open("reveal_widget.html").read()
28
 
 
 
 
 
 
 
 
 
 
29
 
30
  def build_tree(image):
 
31
  img = image.convert("RGB").resize((RES, RES), Image.BICUBIC)
32
  arr = np.asarray(img).astype(np.float32) / 255
33
  x = ((torch.from_numpy(arr).permute(2, 0, 1) - MEAN) / STD).unsqueeze(0).to(DEV)
@@ -109,5 +117,4 @@ if gr is not None:
109
  btn.click(process, inp, [out_tree, out_rev])
110
  inp.upload(process, inp, [out_tree, out_rev])
111
 
112
- if __name__ == "__main__":
113
- demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
 
19
  DEV = "cuda" if torch.cuda.is_available() else "cpu"
20
  TOKEN = os.environ.get("HF_TOKEN")
21
 
 
 
22
  MEAN = torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
23
  STD = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
24
  TREE_HTML = open("tree_widget.html").read()
25
  REVEAL_HTML = open("reveal_widget.html").read()
26
 
27
+ _model = None
28
+ def get_model():
29
+ """Lazy-load so the app binds its port immediately (avoids startup timeout)."""
30
+ global _model
31
+ if _model is None:
32
+ _model = AutoModel.from_pretrained(REPO, dtype=torch.float32, token=TOKEN,
33
+ attn_implementation="eager").eval().to(DEV)
34
+ return _model
35
+
36
 
37
  def build_tree(image):
38
+ model = get_model()
39
  img = image.convert("RGB").resize((RES, RES), Image.BICUBIC)
40
  arr = np.asarray(img).astype(np.float32) / 255
41
  x = ((torch.from_numpy(arr).permute(2, 0, 1) - MEAN) / STD).unsqueeze(0).to(DEV)
 
117
  btn.click(process, inp, [out_tree, out_rev])
118
  inp.upload(process, inp, [out_tree, out_rev])
119
 
120
+ demo.queue().launch() # module-level: HF Spaces imports this file, so no __main__ guard