Matt Grannell commited on
Commit
e9f8bf5
·
1 Parent(s): 6531e0c

Fix HF auth: use login() to authenticate globally from HF_TOKEN secret

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -9,16 +9,21 @@ from fastapi import FastAPI, File, UploadFile, Form
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from fastapi.responses import RedirectResponse
11
  from PIL import Image
12
- from huggingface_hub import snapshot_download
13
  from transformers import AutoProcessor, AutoModelForImageTextToText
14
 
15
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
 
 
 
 
16
 
17
  # ---------------------------------------------------------------------------
18
  # MedImageInsight — CLIP-style encoder for zero-shot label scoring
19
  # ---------------------------------------------------------------------------
20
  print("Downloading MedImageInsights repo...", flush=True)
21
- repo_path = snapshot_download("lion-ai/MedImageInsights", token=HF_TOKEN)
22
  print(f"Downloaded to: {repo_path}", flush=True)
23
 
24
  sys.path.insert(0, repo_path)
@@ -41,14 +46,13 @@ print("MedImageInsight ready.", flush=True)
41
  MEDGEMMA_ID = "google/medgemma-1.5-4b-it"
42
 
43
  print("Loading MedGemma processor...", flush=True)
44
- gemma_processor = AutoProcessor.from_pretrained(MEDGEMMA_ID, token=HF_TOKEN)
45
 
46
  print("Loading MedGemma model (bfloat16)...", flush=True)
47
  gemma_model = AutoModelForImageTextToText.from_pretrained(
48
  MEDGEMMA_ID,
49
  torch_dtype=torch.bfloat16,
50
  device_map="auto",
51
- token=HF_TOKEN,
52
  )
53
  gemma_model.eval()
54
  print("MedGemma ready.", flush=True)
 
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from fastapi.responses import RedirectResponse
11
  from PIL import Image
12
+ from huggingface_hub import snapshot_download, login
13
  from transformers import AutoProcessor, AutoModelForImageTextToText
14
 
15
  HF_TOKEN = os.environ.get("HF_TOKEN")
16
+ if HF_TOKEN:
17
+ login(token=HF_TOKEN)
18
+ print("Authenticated with HF token.", flush=True)
19
+ else:
20
+ print("WARNING: HF_TOKEN not set — gated models will fail.", flush=True)
21
 
22
  # ---------------------------------------------------------------------------
23
  # MedImageInsight — CLIP-style encoder for zero-shot label scoring
24
  # ---------------------------------------------------------------------------
25
  print("Downloading MedImageInsights repo...", flush=True)
26
+ repo_path = snapshot_download("lion-ai/MedImageInsights")
27
  print(f"Downloaded to: {repo_path}", flush=True)
28
 
29
  sys.path.insert(0, repo_path)
 
46
  MEDGEMMA_ID = "google/medgemma-1.5-4b-it"
47
 
48
  print("Loading MedGemma processor...", flush=True)
49
+ gemma_processor = AutoProcessor.from_pretrained(MEDGEMMA_ID)
50
 
51
  print("Loading MedGemma model (bfloat16)...", flush=True)
52
  gemma_model = AutoModelForImageTextToText.from_pretrained(
53
  MEDGEMMA_ID,
54
  torch_dtype=torch.bfloat16,
55
  device_map="auto",
 
56
  )
57
  gemma_model.eval()
58
  print("MedGemma ready.", flush=True)