Evgueni Poloukarov commited on
Commit
2dc6653
·
1 Parent(s): 7d5b63d

feat: add comprehensive diagnostic endpoint for Space debugging

Browse files
Files changed (2) hide show
  1. app.py +51 -0
  2. diagnostic.py +187 -0
app.py CHANGED
@@ -67,6 +67,48 @@ def forecast_api(run_date_str, forecast_type):
67
  return error_path
68
 
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  # Build Gradio interface
71
  with gr.Blocks(title="FBMC Chronos-2 Forecasting") as demo:
72
  gr.Markdown("""
@@ -118,6 +160,15 @@ with gr.Blocks(title="FBMC Chronos-2 Forecasting") as demo:
118
  - Precision: bfloat16
119
  """)
120
 
 
 
 
 
 
 
 
 
 
121
  # Wire up the interface
122
  submit_btn.click(
123
  fn=forecast_api,
 
67
  return error_path
68
 
69
 
70
+
71
+
72
+ def run_diagnostic():
73
+ """Run comprehensive diagnostic tests. Returns path to report file."""
74
+ import subprocess
75
+ import sys
76
+
77
+ output_path = "/tmp/diagnostic_report.txt"
78
+
79
+ try:
80
+ result = subprocess.run(
81
+ [sys.executable, "diagnostic.py"],
82
+ capture_output=True,
83
+ text=True,
84
+ timeout=600
85
+ )
86
+
87
+ with open(output_path, 'w') as f:
88
+ f.write("=== DIAGNOSTIC REPORT ===
89
+
90
+ ")
91
+ f.write("STDOUT:
92
+ " + result.stdout)
93
+ f.write("
94
+
95
+ STDERR:
96
+ " + result.stderr)
97
+ f.write(f"
98
+
99
+ Return code: {result.returncode}
100
+ ")
101
+
102
+ print(f"Diagnostic completed: {output_path}")
103
+ except Exception as e:
104
+ with open(output_path, 'w') as f:
105
+ f.write(f"Diagnostic ERROR: {str(e)}
106
+ ")
107
+ import traceback
108
+ f.write(traceback.format_exc())
109
+
110
+ return output_path
111
+
112
  # Build Gradio interface
113
  with gr.Blocks(title="FBMC Chronos-2 Forecasting") as demo:
114
  gr.Markdown("""
 
160
  - Precision: bfloat16
161
  """)
162
 
163
+
164
+ gr.Markdown("---")
165
+ gr.Markdown("### Diagnostics")
166
+ with gr.Row():
167
+ diagnostic_btn = gr.Button("Run Diagnostics", variant="secondary")
168
+ diagnostic_output = gr.File(label="Diagnostic Report", type="filepath")
169
+
170
+ diagnostic_btn.click(fn=run_diagnostic, inputs=[], outputs=diagnostic_output)
171
+
172
  # Wire up the interface
173
  submit_btn.click(
174
  fn=forecast_api,
diagnostic.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Diagnostic script to test inference pipeline components
4
+ Run this in the Space environment to identify issues
5
+ """
6
+
7
+ import sys
8
+ import os
9
+ from datetime import datetime
10
+
11
+ print("="*60)
12
+ print("FBMC CHRONOS-2 DIAGNOSTIC SCRIPT")
13
+ print("="*60)
14
+
15
+ # Test 1: Python environment
16
+ print("\n[1] Python Environment")
17
+ print(f" Python version: {sys.version}")
18
+ print(f" Python path: {sys.executable}")
19
+
20
+ # Test 2: Import dependencies
21
+ print("\n[2] Importing Dependencies")
22
+ try:
23
+ import torch
24
+ print(f" PyTorch: {torch.__version__}")
25
+ print(f" CUDA available: {torch.cuda.is_available()}")
26
+ if torch.cuda.is_available():
27
+ print(f" CUDA device: {torch.cuda.get_device_name(0)}")
28
+ except Exception as e:
29
+ print(f" PyTorch ERROR: {e}")
30
+
31
+ try:
32
+ import polars as pl
33
+ print(f" Polars: {pl.__version__}")
34
+ except Exception as e:
35
+ print(f" Polars ERROR: {e}")
36
+
37
+ try:
38
+ import numpy as np
39
+ print(f" NumPy: {np.__version__}")
40
+ except Exception as e:
41
+ print(f" NumPy ERROR: {e}")
42
+
43
+ try:
44
+ from chronos import ChronosPipeline
45
+ print(f" Chronos: OK")
46
+ except Exception as e:
47
+ print(f" Chronos ERROR: {e}")
48
+
49
+ try:
50
+ from datasets import load_dataset
51
+ print(f" HF Datasets: OK")
52
+ except Exception as e:
53
+ print(f" HF Datasets ERROR: {e}")
54
+
55
+ # Test 3: Environment variables
56
+ print("\n[3] Environment Variables")
57
+ print(f" HF_TOKEN: {'SET' if os.getenv('HF_TOKEN') else 'NOT SET'}")
58
+ print(f" DEVICE: {os.getenv('DEVICE', 'cuda')}")
59
+
60
+ # Test 4: Load dataset
61
+ print("\n[4] Loading Dataset")
62
+ try:
63
+ from datasets import load_dataset
64
+ hf_token = os.getenv("HF_TOKEN")
65
+ print(f" Loading evgueni-p/fbmc-features-24month...")
66
+ dataset = load_dataset(
67
+ "evgueni-p/fbmc-features-24month",
68
+ split="train",
69
+ token=hf_token
70
+ )
71
+ print(f" Dataset rows: {len(dataset)}")
72
+
73
+ # Convert to Polars
74
+ import polars as pl
75
+ df = pl.from_arrow(dataset.data.table)
76
+ print(f" Polars shape: {df.shape}")
77
+
78
+ # Check for target columns
79
+ target_cols = [col for col in df.columns if col.startswith('target_border_')]
80
+ print(f" Target borders: {len(target_cols)}")
81
+ if target_cols:
82
+ print(f" First border: {target_cols[0]}")
83
+
84
+ except Exception as e:
85
+ print(f" Dataset ERROR: {e}")
86
+ import traceback
87
+ traceback.print_exc()
88
+
89
+ # Test 5: Load Chronos model
90
+ print("\n[5] Loading Chronos Model")
91
+ try:
92
+ from chronos import ChronosPipeline
93
+ import torch
94
+
95
+ device = "cuda" if torch.cuda.is_available() else "cpu"
96
+ print(f" Device: {device}")
97
+ print(f" Loading amazon/chronos-t5-large...")
98
+
99
+ pipeline = ChronosPipeline.from_pretrained(
100
+ "amazon/chronos-t5-large",
101
+ device_map=device,
102
+ torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
103
+ )
104
+ print(f" Model loaded successfully!")
105
+
106
+ # Test inference with dummy data
107
+ print(f"\n Testing inference with dummy data...")
108
+ import numpy as np
109
+ dummy_context = np.random.randn(512).astype(np.float32)
110
+
111
+ forecast = pipeline.predict(
112
+ context=dummy_context,
113
+ prediction_length=24,
114
+ num_samples=5
115
+ )
116
+
117
+ forecast_np = forecast.numpy()
118
+ print(f" Forecast shape: {forecast_np.shape}")
119
+
120
+ # Test quantile calculation
121
+ median = np.median(forecast_np, axis=0)
122
+ q10 = np.quantile(forecast_np, 0.1, axis=0)
123
+ q90 = np.quantile(forecast_np, 0.9, axis=0)
124
+
125
+ print(f" Quantiles calculated successfully!")
126
+ print(f" Median shape: {median.shape}")
127
+ print(f" Q10 shape: {q10.shape}")
128
+ print(f" Q90 shape: {q90.shape}")
129
+
130
+ except Exception as e:
131
+ print(f" Model ERROR: {e}")
132
+ import traceback
133
+ traceback.print_exc()
134
+
135
+ # Test 6: Test dynamic_forecast import
136
+ print("\n[6] Testing Module Imports")
137
+ try:
138
+ from src.forecasting.dynamic_forecast import DynamicForecast
139
+ print(f" DynamicForecast: OK")
140
+ except Exception as e:
141
+ print(f" DynamicForecast ERROR: {e}")
142
+ import traceback
143
+ traceback.print_exc()
144
+
145
+ try:
146
+ from src.forecasting.feature_availability import FeatureAvailability
147
+ print(f" FeatureAvailability: OK")
148
+ except Exception as e:
149
+ print(f" FeatureAvailability ERROR: {e}")
150
+
151
+ # Test 7: Quick inference test
152
+ print("\n[7] Full Pipeline Test (Minimal)")
153
+ try:
154
+ print(f" Testing run_inference function...")
155
+ from src.forecasting.chronos_inference import run_inference
156
+
157
+ # This will be slow but should work
158
+ print(f" Running smoke test for 2025-09-30...")
159
+ print(f" (This may take 60+ seconds...)")
160
+
161
+ result_path = run_inference(
162
+ run_date="2025-09-30",
163
+ forecast_type="smoke_test",
164
+ output_dir="/tmp"
165
+ )
166
+
167
+ print(f" Result file: {result_path}")
168
+
169
+ # Check if file has data
170
+ import polars as pl
171
+ df = pl.read_parquet(result_path)
172
+ print(f" Result shape: {df.shape}")
173
+ print(f" Columns: {df.columns}")
174
+
175
+ if len(df.columns) > 1:
176
+ print(f" [SUCCESS] Forecast has data!")
177
+ else:
178
+ print(f" [ERROR] Forecast is empty (only timestamps)")
179
+
180
+ except Exception as e:
181
+ print(f" Pipeline ERROR: {e}")
182
+ import traceback
183
+ traceback.print_exc()
184
+
185
+ print("\n" + "="*60)
186
+ print("DIAGNOSTIC COMPLETE")
187
+ print("="*60)