Tonic commited on
Commit
db47567
·
1 Parent(s): 6347217

adds improved error returns for chronos and model configurations

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -64,7 +64,8 @@ def load_pipeline():
64
  device_map="auto", # Let the machine choose the best device
65
  torch_dtype=torch.float16, # Use float16 for better memory efficiency
66
  low_cpu_mem_usage=True,
67
- trust_remote_code=True # Required for Chronos models
 
68
  )
69
  # Set model to evaluation mode
70
  pipeline.model = pipeline.model.eval()
@@ -72,9 +73,13 @@ def load_pipeline():
72
  for param in pipeline.model.parameters():
73
  param.requires_grad = False
74
  print("Chronos model loaded successfully")
 
 
75
  return pipeline
76
  except Exception as e:
77
  print(f"Error loading pipeline: {str(e)}")
 
 
78
  raise RuntimeError(f"Failed to load model: {str(e)}")
79
 
80
  def is_market_open() -> bool:
@@ -349,9 +354,12 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
349
 
350
  with torch.inference_mode():
351
  try:
352
- # Generate predictions using Chronos
 
 
353
  # First try with predict_quantiles
354
  try:
 
355
  quantiles, mean = pipe.predict_quantiles(
356
  context=context,
357
  prediction_length=actual_prediction_length,
@@ -361,6 +369,8 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
361
  if quantiles is None or mean is None:
362
  raise ValueError("Chronos returned empty prediction")
363
 
 
 
364
  # Convert to numpy arrays
365
  quantiles = quantiles.detach().cpu().numpy()
366
  mean = mean.detach().cpu().numpy()
@@ -374,16 +384,25 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
374
  std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
375
 
376
  except Exception as e:
377
- print(f"predict_quantiles failed, trying predict: {str(e)}")
 
 
378
  # Fallback to predict if predict_quantiles fails
379
  prediction = pipe.predict(
380
  context=context,
381
  prediction_length=actual_prediction_length,
382
  num_samples=100
383
- ).detach().cpu().numpy()
384
 
385
- if prediction is None or prediction.size == 0:
386
- raise ValueError("Chronos returned empty prediction")
 
 
 
 
 
 
 
387
 
388
  # Calculate mean and std from samples
389
  mean_pred = scaler.inverse_transform(prediction.mean(axis=0).reshape(-1, 1)).flatten()
@@ -399,6 +418,8 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
399
 
400
  except Exception as e:
401
  print(f"Chronos prediction error: {str(e)}")
 
 
402
  raise
403
 
404
  except Exception as e:
 
64
  device_map="auto", # Let the machine choose the best device
65
  torch_dtype=torch.float16, # Use float16 for better memory efficiency
66
  low_cpu_mem_usage=True,
67
+ trust_remote_code=True, # Required for Chronos models
68
+ use_safetensors=True # Use safetensors for better loading
69
  )
70
  # Set model to evaluation mode
71
  pipeline.model = pipeline.model.eval()
 
73
  for param in pipeline.model.parameters():
74
  param.requires_grad = False
75
  print("Chronos model loaded successfully")
76
+ print(f"Model device: {next(pipeline.model.parameters()).device}")
77
+ print(f"Model dtype: {next(pipeline.model.parameters()).dtype}")
78
  return pipeline
79
  except Exception as e:
80
  print(f"Error loading pipeline: {str(e)}")
81
+ print(f"Error type: {type(e)}")
82
+ print(f"Error details: {str(e)}")
83
  raise RuntimeError(f"Failed to load model: {str(e)}")
84
 
85
  def is_market_open() -> bool:
 
354
 
355
  with torch.inference_mode():
356
  try:
357
+ print(f"Attempting prediction with context shape: {context.shape}")
358
+ print(f"Prediction length: {actual_prediction_length}")
359
+
360
  # First try with predict_quantiles
361
  try:
362
+ print("Trying predict_quantiles...")
363
  quantiles, mean = pipe.predict_quantiles(
364
  context=context,
365
  prediction_length=actual_prediction_length,
 
369
  if quantiles is None or mean is None:
370
  raise ValueError("Chronos returned empty prediction")
371
 
372
+ print(f"Quantiles shape: {quantiles.shape}, Mean shape: {mean.shape}")
373
+
374
  # Convert to numpy arrays
375
  quantiles = quantiles.detach().cpu().numpy()
376
  mean = mean.detach().cpu().numpy()
 
384
  std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
385
 
386
  except Exception as e:
387
+ print(f"predict_quantiles failed with error: {str(e)}")
388
+ print("Falling back to predict...")
389
+
390
  # Fallback to predict if predict_quantiles fails
391
  prediction = pipe.predict(
392
  context=context,
393
  prediction_length=actual_prediction_length,
394
  num_samples=100
395
+ )
396
 
397
+ if prediction is None:
398
+ raise ValueError("Chronos predict returned None")
399
+
400
+ prediction = prediction.detach().cpu().numpy()
401
+
402
+ if prediction.size == 0:
403
+ raise ValueError("Chronos predict returned empty array")
404
+
405
+ print(f"Prediction shape: {prediction.shape}")
406
 
407
  # Calculate mean and std from samples
408
  mean_pred = scaler.inverse_transform(prediction.mean(axis=0).reshape(-1, 1)).flatten()
 
418
 
419
  except Exception as e:
420
  print(f"Chronos prediction error: {str(e)}")
421
+ print(f"Error type: {type(e)}")
422
+ print(f"Error details: {str(e)}")
423
  raise
424
 
425
  except Exception as e: