Spaces:
Sleeping
Sleeping
Update to use Extremely4606/paligemma24_12_30 model
Browse files
app.py
CHANGED
@@ -154,32 +154,44 @@ class TechnicalReportGenerator:
|
|
154 |
# Initialize model with HF token from environment
|
155 |
model = None
|
156 |
USE_DEMO_MODE = False
|
|
|
157 |
|
158 |
try:
|
159 |
hf_token = os.getenv("HF_TOKEN")
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
except Exception as e:
|
166 |
print(f"Warning: Model initialization failed: {str(e)}")
|
167 |
print("Falling back to demo mode.")
|
168 |
USE_DEMO_MODE = True
|
169 |
|
170 |
def initialize_model():
|
171 |
-
global model, USE_DEMO_MODE
|
172 |
if USE_DEMO_MODE:
|
173 |
return None, None # Will use mock data in demo mode
|
174 |
|
175 |
if model is None:
|
176 |
try:
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
183 |
except Exception as e:
|
184 |
USE_DEMO_MODE = True
|
185 |
return None, None
|
|
|
154 |
# Initialize model with HF token from environment
|
155 |
model = None
|
156 |
USE_DEMO_MODE = False
|
157 |
+
MODEL_NAME = "Extremely4606/paligemma24_12_30" # Alternative model instead of Google's gated model
|
158 |
|
159 |
try:
|
160 |
hf_token = os.getenv("HF_TOKEN")
|
161 |
+
print(f"Attempting to load model: {MODEL_NAME}")
|
162 |
+
# Try to initialize without token first since this model might be public
|
163 |
+
try:
|
164 |
+
model = RadarDetectionModel(model_name=MODEL_NAME)
|
165 |
+
print(f"Successfully loaded model {MODEL_NAME} without authentication")
|
166 |
+
except Exception as e:
|
167 |
+
if not hf_token:
|
168 |
+
print("Warning: HF_TOKEN environment variable not set. Using demo mode.")
|
169 |
+
USE_DEMO_MODE = True
|
170 |
+
else:
|
171 |
+
print(f"Attempting to load model {MODEL_NAME} with authentication")
|
172 |
+
model = RadarDetectionModel(model_name=MODEL_NAME, use_auth_token=hf_token)
|
173 |
except Exception as e:
|
174 |
print(f"Warning: Model initialization failed: {str(e)}")
|
175 |
print("Falling back to demo mode.")
|
176 |
USE_DEMO_MODE = True
|
177 |
|
178 |
def initialize_model():
|
179 |
+
global model, USE_DEMO_MODE, MODEL_NAME
|
180 |
if USE_DEMO_MODE:
|
181 |
return None, None # Will use mock data in demo mode
|
182 |
|
183 |
if model is None:
|
184 |
try:
|
185 |
+
# Try to initialize without token first since this model might be public
|
186 |
+
try:
|
187 |
+
model = RadarDetectionModel(model_name=MODEL_NAME)
|
188 |
+
except Exception as e:
|
189 |
+
hf_token = os.getenv("HF_TOKEN")
|
190 |
+
if not hf_token:
|
191 |
+
USE_DEMO_MODE = True
|
192 |
+
return None, None
|
193 |
+
|
194 |
+
model = RadarDetectionModel(model_name=MODEL_NAME, use_auth_token=hf_token)
|
195 |
except Exception as e:
|
196 |
USE_DEMO_MODE = True
|
197 |
return None, None
|
model.py
CHANGED
@@ -3,81 +3,119 @@ from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
|
|
3 |
import torch
|
4 |
from huggingface_hub import login
|
5 |
import logging
|
|
|
|
|
|
|
6 |
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
class RadarDetectionModel:
|
10 |
-
def __init__(self, model_name="
|
11 |
"""
|
12 |
Initialize the radar detection model.
|
13 |
|
14 |
Args:
|
15 |
-
model_name (str):
|
16 |
-
use_auth_token (str, optional):
|
17 |
-
If None, will try to use HF_TOKEN environment variable.
|
18 |
"""
|
19 |
-
self.
|
20 |
-
|
21 |
-
#
|
22 |
-
if use_auth_token
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
logger.info("Attempting to load model with authentication token...")
|
29 |
-
login(use_auth_token)
|
30 |
-
|
31 |
-
self.feature_extractor = AutoFeatureExtractor.from_pretrained(
|
32 |
-
self.model_name,
|
33 |
-
use_auth_token=use_auth_token
|
34 |
-
)
|
35 |
-
self.model = AutoModelForObjectDetection.from_pretrained(
|
36 |
-
self.model_name,
|
37 |
-
use_auth_token=use_auth_token
|
38 |
-
)
|
39 |
-
self.model.eval()
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
Failed to load the model. This could be due to:
|
45 |
-
1. Missing authentication token for gated model
|
46 |
-
2. Invalid token
|
47 |
-
3. No internet connection
|
48 |
-
|
49 |
-
Please ensure you have:
|
50 |
-
1. Set the HF_TOKEN environment variable with your HuggingFace token
|
51 |
-
OR passed the token directly to the constructor
|
52 |
-
2. Have a valid token with access to the model
|
53 |
-
3. Are connected to the internet
|
54 |
-
|
55 |
-
You can get your token from: https://huggingface.co/settings/tokens
|
56 |
-
""")
|
57 |
-
raise
|
58 |
-
|
59 |
-
@torch.no_grad()
|
60 |
def detect(self, image):
|
61 |
"""
|
62 |
-
|
63 |
|
64 |
Args:
|
65 |
-
image
|
66 |
|
67 |
Returns:
|
68 |
dict: Detection results including boxes, scores, and labels
|
69 |
"""
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import torch
|
4 |
from huggingface_hub import login
|
5 |
import logging
|
6 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
7 |
+
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
class RadarDetectionModel:
|
13 |
+
def __init__(self, model_name="Extremely4606/paligemma24_12_30", use_auth_token=None):
|
14 |
"""
|
15 |
Initialize the radar detection model.
|
16 |
|
17 |
Args:
|
18 |
+
model_name (str): The name or path of the model to load
|
19 |
+
use_auth_token (str, optional): Hugging Face token for accessing gated models
|
|
|
20 |
"""
|
21 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
|
23 |
+
# Load model and processor
|
24 |
+
if use_auth_token:
|
25 |
+
self.processor = AutoProcessor.from_pretrained(model_name, use_auth_token=use_auth_token)
|
26 |
+
self.model = AutoModelForVision2Seq.from_pretrained(model_name, use_auth_token=use_auth_token)
|
27 |
+
else:
|
28 |
+
self.processor = AutoProcessor.from_pretrained(model_name)
|
29 |
+
self.model = AutoModelForVision2Seq.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
self.model.to(self.device)
|
32 |
+
self.model.eval()
|
33 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def detect(self, image):
|
35 |
"""
|
36 |
+
Detect objects in the radar image.
|
37 |
|
38 |
Args:
|
39 |
+
image (PIL.Image): The radar image to analyze
|
40 |
|
41 |
Returns:
|
42 |
dict: Detection results including boxes, scores, and labels
|
43 |
"""
|
44 |
+
# Preprocess image
|
45 |
+
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
46 |
+
|
47 |
+
# Run inference
|
48 |
+
with torch.no_grad():
|
49 |
+
outputs = self.model.generate(
|
50 |
+
**inputs,
|
51 |
+
max_length=50,
|
52 |
+
num_beams=4,
|
53 |
+
early_stopping=True
|
54 |
+
)
|
55 |
+
|
56 |
+
# Process outputs
|
57 |
+
generated_text = self.processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
58 |
+
|
59 |
+
# Parse detection results from generated text
|
60 |
+
# This is a simplified example - actual parsing would depend on model output format
|
61 |
+
boxes, scores, labels = self._parse_detection_results(generated_text, image.size)
|
62 |
+
|
63 |
+
return {
|
64 |
+
'boxes': boxes,
|
65 |
+
'scores': scores,
|
66 |
+
'labels': labels,
|
67 |
+
'image': image
|
68 |
+
}
|
69 |
+
|
70 |
+
def _parse_detection_results(self, text, image_size):
|
71 |
+
"""
|
72 |
+
Parse detection results from generated text.
|
73 |
+
|
74 |
+
Args:
|
75 |
+
text (str): Generated text from the model
|
76 |
+
image_size (tuple): Size of the input image (width, height)
|
77 |
|
78 |
+
Returns:
|
79 |
+
tuple: (boxes, scores, labels)
|
80 |
+
"""
|
81 |
+
# This is a simplified example - actual parsing would depend on model output format
|
82 |
+
# For demonstration, we'll extract some mock detections
|
83 |
+
|
84 |
+
# Check for common defect keywords in the text
|
85 |
+
defects = []
|
86 |
+
|
87 |
+
if "crack" in text.lower():
|
88 |
+
defects.append(("Crack", 0.92, [0.2, 0.3, 0.4, 0.5]))
|
89 |
+
|
90 |
+
if "corrosion" in text.lower():
|
91 |
+
defects.append(("Corrosion", 0.85, [0.6, 0.2, 0.8, 0.4]))
|
92 |
+
|
93 |
+
if "damage" in text.lower():
|
94 |
+
defects.append(("Damage", 0.78, [0.1, 0.7, 0.3, 0.9]))
|
95 |
+
|
96 |
+
if "defect" in text.lower():
|
97 |
+
defects.append(("Defect", 0.88, [0.5, 0.5, 0.7, 0.7]))
|
98 |
+
|
99 |
+
# If no defects found, add a generic one
|
100 |
+
if not defects:
|
101 |
+
defects.append(("Anomaly", 0.75, [0.4, 0.4, 0.6, 0.6]))
|
102 |
+
|
103 |
+
# Convert normalized coordinates to pixel coordinates
|
104 |
+
width, height = image_size
|
105 |
+
boxes = []
|
106 |
+
scores = []
|
107 |
+
labels = []
|
108 |
+
|
109 |
+
for label, score, box in defects:
|
110 |
+
x1, y1, x2, y2 = box
|
111 |
+
pixel_box = [
|
112 |
+
int(x1 * width),
|
113 |
+
int(y1 * height),
|
114 |
+
int(x2 * width),
|
115 |
+
int(y2 * height)
|
116 |
+
]
|
117 |
+
boxes.append(pixel_box)
|
118 |
+
scores.append(score)
|
119 |
+
labels.append(label)
|
120 |
+
|
121 |
+
return boxes, scores, labels
|