sayedM commited on
Commit
e8fc9bd
Β·
verified Β·
1 Parent(s): 471a3ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -11,9 +11,8 @@ import os
11
  # ----------------------------
12
  # Configuration
13
  # ----------------------------
14
- # The model will be downloaded from the Hugging Face Hub
15
- # Using the specific revision that works well with transformers AutoModel
16
- MODEL_ID = "facebook/dinov3-vith16plus"
17
  PATCH_SIZE = 16
18
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
19
 
@@ -28,17 +27,22 @@ def load_model_from_hub():
28
  """Loads the DINOv3 model from the Hugging Face Hub."""
29
  print(f"Loading model '{MODEL_ID}' from Hugging Face Hub...")
30
  try:
31
- # Use your HF token if the model is gated
32
- # You can set this as a secret in your Hugging Face Space settings
33
  token = os.environ.get("HF_TOKEN")
 
34
  model = AutoModel.from_pretrained(MODEL_ID, token=token, trust_remote_code=True)
35
  model.to(DEVICE).eval()
36
  print(f"βœ… Model loaded successfully on device: {DEVICE}")
37
  return model
38
  except Exception as e:
39
  print(f"❌ Failed to load model: {e}")
40
- # This will display an error message in the Gradio interface
41
- raise gr.Error(f"Could not load model from Hub. If it's a gated model, ensure you have access and have set your HF_TOKEN secret in the Space settings. Error: {e}")
 
 
 
 
 
42
 
43
  # Load the model globally when the app starts
44
  model = load_model_from_hub()
@@ -103,8 +107,7 @@ def generate_pca_visuals(
103
 
104
  # πŸ’‘ FIX: The model output includes a [CLS] token AND 4 register tokens.
105
  # We must skip all of them (total 5) to get only the patch embeddings.
106
- # The original code only skipped 1, causing the size mismatch.
107
- n_special_tokens = 5 # 1 [CLS] token + 4 register tokens
108
  patch_embeddings = outputs.last_hidden_state.squeeze(0)[n_special_tokens:, :]
109
 
110
  # 3. PCA Calculation
 
11
  # ----------------------------
12
  # Configuration
13
  # ----------------------------
14
+ # πŸ’‘ FIX: Use the full, correct model ID from the Hugging Face Hub.
15
+ MODEL_ID = "facebook/dinov3-vith16plus-pretrain-lvd1689m"
 
16
  PATCH_SIZE = 16
17
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
18
 
 
27
  """Loads the DINOv3 model from the Hugging Face Hub."""
28
  print(f"Loading model '{MODEL_ID}' from Hugging Face Hub...")
29
  try:
30
+ # This will use the HF_TOKEN secret if you set it in your Space settings.
 
31
  token = os.environ.get("HF_TOKEN")
32
+ # trust_remote_code is necessary for DINOv3
33
  model = AutoModel.from_pretrained(MODEL_ID, token=token, trust_remote_code=True)
34
  model.to(DEVICE).eval()
35
  print(f"βœ… Model loaded successfully on device: {DEVICE}")
36
  return model
37
  except Exception as e:
38
  print(f"❌ Failed to load model: {e}")
39
+ # This will display a clear error message in the Gradio interface
40
+ raise gr.Error(
41
+ f"Could not load model '{MODEL_ID}'. "
42
+ "This is a gated model. Please ensure you have accepted the terms on its Hugging Face page "
43
+ "and set your HF_TOKEN as a secret in your Space settings. "
44
+ f"Original error: {e}"
45
+ )
46
 
47
  # Load the model globally when the app starts
48
  model = load_model_from_hub()
 
107
 
108
  # πŸ’‘ FIX: The model output includes a [CLS] token AND 4 register tokens.
109
  # We must skip all of them (total 5) to get only the patch embeddings.
110
+ n_special_tokens = 5 # 1 [CLS] token + 4 register tokens for ViT-H/16+
 
111
  patch_embeddings = outputs.last_hidden_state.squeeze(0)[n_special_tokens:, :]
112
 
113
  # 3. PCA Calculation