xingqiang commited on
Commit
47d2557
·
1 Parent(s): 6c9f6b4

Add demo mode fallback when PaliGemma model is not accessible

Browse files
Files changed (1) hide show
  1. app.py +56 -11
app.py CHANGED
@@ -153,19 +153,37 @@ class TechnicalReportGenerator:
153
 
154
  # Initialize model with HF token from environment
155
  model = None
 
 
156
  try:
157
- model = RadarDetectionModel(use_auth_token=os.getenv("HF_TOKEN"))
 
 
 
 
 
158
  except Exception as e:
159
  print(f"Warning: Model initialization failed: {str(e)}")
160
- print("The app will initialize the model on first request.")
 
161
 
162
  def initialize_model():
163
- global model
 
 
 
164
  if model is None:
165
  try:
166
- model = RadarDetectionModel(use_auth_token=os.getenv("HF_TOKEN"))
 
 
 
 
 
167
  except Exception as e:
168
- return None, f"Error initializing model: {str(e)}"
 
 
169
  return model, None
170
 
171
  def create_confidence_chart(scores, labels):
@@ -271,10 +289,13 @@ def process_image_streaming(image, generate_tech_report=False, progress=gr.Progr
271
 
272
  # Initialize model if needed
273
  progress(0.1, desc="Initializing model...")
274
- global model
275
- model, error = initialize_model()
276
- if error:
277
- raise gr.Error(error)
 
 
 
278
 
279
  try:
280
  # Convert to PIL Image if needed
@@ -283,7 +304,17 @@ def process_image_streaming(image, generate_tech_report=False, progress=gr.Progr
283
 
284
  # Run detection
285
  progress(0.2, desc="Running detection...")
286
- detection_result = model.detect(image)
 
 
 
 
 
 
 
 
 
 
287
 
288
  # Extract features
289
  progress(0.3, desc="Extracting features...")
@@ -292,7 +323,9 @@ def process_image_streaming(image, generate_tech_report=False, progress=gr.Progr
292
  amplitude_class = classify_amplitude(amplitude)
293
 
294
  if len(detection_result['boxes']) > 0:
295
- box = detection_result['boxes'][0].tolist()
 
 
296
  distribution_range = calculate_distribution_range(box)
297
  distribution_class = classify_distribution_range(distribution_range)
298
  else:
@@ -457,6 +490,18 @@ with gr.Blocks(theme=THEME) as iface:
457
  gr.Markdown("# Radar Image Analysis System")
458
  dark_mode_btn = gr.Button("🌓 Toggle Dark Mode", scale=0)
459
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  gr.Markdown("Upload a radar image to analyze defects and generate technical reports")
461
 
462
  with gr.Tabs() as tabs:
 
153
 
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
+ if not hf_token:
161
+ print("Warning: HF_TOKEN environment variable not set. Using demo mode.")
162
+ USE_DEMO_MODE = True
163
+ else:
164
+ model = RadarDetectionModel(use_auth_token=hf_token)
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
+ hf_token = os.getenv("HF_TOKEN")
178
+ if not hf_token:
179
+ USE_DEMO_MODE = True
180
+ return None, None
181
+
182
+ model = RadarDetectionModel(use_auth_token=hf_token)
183
  except Exception as e:
184
+ USE_DEMO_MODE = True
185
+ return None, None
186
+
187
  return model, None
188
 
189
  def create_confidence_chart(scores, labels):
 
289
 
290
  # Initialize model if needed
291
  progress(0.1, desc="Initializing model...")
292
+ global model, USE_DEMO_MODE
293
+
294
+ if not USE_DEMO_MODE:
295
+ model, error = initialize_model()
296
+ if error:
297
+ progress(0.15, desc="Switching to demo mode...")
298
+ USE_DEMO_MODE = True
299
 
300
  try:
301
  # Convert to PIL Image if needed
 
304
 
305
  # Run detection
306
  progress(0.2, desc="Running detection...")
307
+
308
+ if USE_DEMO_MODE:
309
+ # Use mock detection results in demo mode
310
+ detection_result = {
311
+ 'boxes': [[100, 100, 200, 200], [300, 300, 400, 400]],
312
+ 'scores': [0.92, 0.85],
313
+ 'labels': ['Crack', 'Corrosion'],
314
+ 'image': image
315
+ }
316
+ else:
317
+ detection_result = model.detect(image)
318
 
319
  # Extract features
320
  progress(0.3, desc="Extracting features...")
 
323
  amplitude_class = classify_amplitude(amplitude)
324
 
325
  if len(detection_result['boxes']) > 0:
326
+ box = detection_result['boxes'][0]
327
+ if not isinstance(box, list):
328
+ box = box.tolist()
329
  distribution_range = calculate_distribution_range(box)
330
  distribution_class = classify_distribution_range(distribution_range)
331
  else:
 
490
  gr.Markdown("# Radar Image Analysis System")
491
  dark_mode_btn = gr.Button("🌓 Toggle Dark Mode", scale=0)
492
 
493
+ if USE_DEMO_MODE:
494
+ gr.Markdown("""
495
+ ### ⚠️ Running in Demo Mode
496
+ The app is currently running in demo mode because access to the PaliGemma model is restricted.
497
+
498
+ To use the full model capabilities:
499
+ 1. Get access to the PaliGemma model at [Hugging Face](https://huggingface.co/google/paligemma-3b-ft-coco35l-224)
500
+ 2. Add your Hugging Face token as an environment variable named `HF_TOKEN` in the Space settings
501
+
502
+ Demo mode will show simulated results for demonstration purposes.
503
+ """, elem_id="demo-mode-warning")
504
+
505
  gr.Markdown("Upload a radar image to analyze defects and generate technical reports")
506
 
507
  with gr.Tabs() as tabs: