Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import cv2
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import torch
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
MODEL_REPO = "SARVM/Refined_ViT"
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
@@ -28,20 +35,50 @@ processor = ViTImageProcessor(
|
|
| 28 |
image_std=[0.229, 0.224, 0.225],
|
| 29 |
)
|
| 30 |
|
| 31 |
-
print("Loading custom model
|
| 32 |
|
| 33 |
-
# 2.
|
| 34 |
-
|
| 35 |
-
MODEL_REPO,
|
|
|
|
| 36 |
token=HF_TOKEN,
|
| 37 |
-
trust_remote_code=True
|
| 38 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
| 40 |
model = AutoModelForImageClassification.from_pretrained(
|
| 41 |
MODEL_REPO,
|
| 42 |
config=config,
|
| 43 |
token=HF_TOKEN,
|
| 44 |
-
trust_remote_code=True
|
| 45 |
)
|
| 46 |
|
| 47 |
model.to(DEVICE)
|
|
@@ -146,7 +183,7 @@ def predict(image):
|
|
| 146 |
with torch.no_grad():
|
| 147 |
outputs = model(**inputs)
|
| 148 |
|
| 149 |
-
# Extract logits
|
| 150 |
if isinstance(outputs, dict):
|
| 151 |
logits = outputs.get("logits")
|
| 152 |
attentions = outputs.get("attentions", None)
|
|
@@ -165,7 +202,7 @@ def predict(image):
|
|
| 165 |
prediction = model.config.id2label.get(predicted_index, str(predicted_index))
|
| 166 |
confidence_text = f"{confidence * 100:.2f}%"
|
| 167 |
|
| 168 |
-
# Generate attention visualization
|
| 169 |
overlay = create_attention_overlay(image, attentions)
|
| 170 |
|
| 171 |
return prediction, confidence_text, overlay
|
|
|
|
| 1 |
+
import importlib.util
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
import cv2
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import torch
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
from PIL import Image
|
| 10 |
+
from transformers import (
|
| 11 |
+
AutoConfig,
|
| 12 |
+
AutoModelForImageClassification,
|
| 13 |
+
ViTImageProcessor,
|
| 14 |
+
)
|
| 15 |
|
| 16 |
MODEL_REPO = "SARVM/Refined_ViT"
|
| 17 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 35 |
image_std=[0.229, 0.224, 0.225],
|
| 36 |
)
|
| 37 |
|
| 38 |
+
print("Loading custom model code...")
|
| 39 |
|
| 40 |
+
# 2. Download custom config and modeling files directly from HF Hub
|
| 41 |
+
config_path = hf_hub_download(
|
| 42 |
+
repo_id=MODEL_REPO,
|
| 43 |
+
filename="configuration_vit_refiner.py",
|
| 44 |
token=HF_TOKEN,
|
|
|
|
| 45 |
)
|
| 46 |
+
model_path = hf_hub_download(
|
| 47 |
+
repo_id=MODEL_REPO,
|
| 48 |
+
filename="modeling_vit_refiner.py",
|
| 49 |
+
token=HF_TOKEN,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# 3. Dynamically import configuration module
|
| 53 |
+
config_spec = importlib.util.spec_from_file_location(
|
| 54 |
+
"configuration_vit_refiner", config_path
|
| 55 |
+
)
|
| 56 |
+
config_module = importlib.util.module_from_spec(config_spec)
|
| 57 |
+
sys.modules["configuration_vit_refiner"] = config_module
|
| 58 |
+
config_spec.loader.exec_module(config_module)
|
| 59 |
+
|
| 60 |
+
# 4. Dynamically import modeling module
|
| 61 |
+
model_spec = importlib.util.spec_from_file_location(
|
| 62 |
+
"modeling_vit_refiner", model_path
|
| 63 |
+
)
|
| 64 |
+
model_module = importlib.util.module_from_spec(model_spec)
|
| 65 |
+
sys.modules["modeling_vit_refiner"] = model_module
|
| 66 |
+
model_spec.loader.exec_module(model_module)
|
| 67 |
+
|
| 68 |
+
# 5. Get the actual custom classes from imported modules
|
| 69 |
+
ViTRefinerConfig = config_module.ViTRefinerConfig
|
| 70 |
+
ViTRefinerModel = model_module.ViTRefinerModel
|
| 71 |
+
|
| 72 |
+
# 6. Explicitly register 'vit_refiner' with Transformers AutoClasses
|
| 73 |
+
AutoConfig.register("vit_refiner", ViTRefinerConfig)
|
| 74 |
+
AutoModelForImageClassification.register(ViTRefinerConfig, ViTRefinerModel)
|
| 75 |
|
| 76 |
+
# 7. Now safely load via standard AutoClasses
|
| 77 |
+
config = AutoConfig.from_pretrained(MODEL_REPO, token=HF_TOKEN)
|
| 78 |
model = AutoModelForImageClassification.from_pretrained(
|
| 79 |
MODEL_REPO,
|
| 80 |
config=config,
|
| 81 |
token=HF_TOKEN,
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
model.to(DEVICE)
|
|
|
|
| 183 |
with torch.no_grad():
|
| 184 |
outputs = model(**inputs)
|
| 185 |
|
| 186 |
+
# Extract logits and attentions safely
|
| 187 |
if isinstance(outputs, dict):
|
| 188 |
logits = outputs.get("logits")
|
| 189 |
attentions = outputs.get("attentions", None)
|
|
|
|
| 202 |
prediction = model.config.id2label.get(predicted_index, str(predicted_index))
|
| 203 |
confidence_text = f"{confidence * 100:.2f}%"
|
| 204 |
|
| 205 |
+
# Generate attention visualization
|
| 206 |
overlay = create_attention_overlay(image, attentions)
|
| 207 |
|
| 208 |
return prediction, confidence_text, overlay
|