youssefreda9 commited on
Commit
756045d
·
1 Parent(s): 8060095

debug: Add startup error tracking + memory info to debug endpoint

Browse files
Files changed (1) hide show
  1. src/app.py +37 -6
src/app.py CHANGED
@@ -92,20 +92,28 @@ def load_models():
92
 
93
  loaded = []
94
  failed = []
 
 
 
 
95
 
96
- # Load only the Summarization model locally (dev mode).
97
  try:
98
  logger.info(f"Loading summarization model from Hugging Face: {HUGGINGFACE_SUMMARIZATION_REPO}")
99
  try:
100
  summarization_model = SummarizationModel(HUGGINGFACE_SUMMARIZATION_REPO)
101
  except Exception as remote_error:
102
  logger.warning(f"Remote load failed, falling back to local model: {remote_error}")
 
103
  logger.info(f"Loading summarization model from local path: {SUMMARIZATION_PATH}")
104
  summarization_model = SummarizationModel(SUMMARIZATION_PATH)
105
  loaded.append("summarization")
106
  logger.info("Summarization model loaded successfully")
107
  except Exception as e:
 
 
108
  failed.append(("summarization", str(e)))
 
109
  logger.error(f"Failed to load summarization model: {str(e)}")
110
 
111
  logger.info(f"Models loaded: {loaded}")
@@ -114,6 +122,8 @@ def load_models():
114
 
115
  return len(loaded) > 0
116
 
 
 
117
 
118
  @app.route('/')
119
  def index():
@@ -178,16 +188,37 @@ def health_check():
178
 
179
  @app.route('/api/debug-models', methods=['GET'])
180
  def debug_models():
181
- """Debug endpoint: test all HF models and return actual errors."""
182
- if not USE_HF_API:
183
- return jsonify({'error': 'Not in HF API mode', 'mode': 'local'}), 400
184
-
185
  from hf_inference import debug_test_all_models
186
  results = debug_test_all_models()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  return jsonify({
188
  'status': 'debug',
189
  'hf_api_token_set': bool(HF_API_TOKEN),
190
- 'hf_api_token_prefix': HF_API_TOKEN[:10] + '...' if HF_API_TOKEN else 'NOT SET',
 
 
 
191
  'models': results,
192
  }), 200
193
 
 
92
 
93
  loaded = []
94
  failed = []
95
+
96
+ # Store startup errors for diagnostics
97
+ global _startup_errors
98
+ _startup_errors = []
99
 
100
+ # Load only the Summarization model locally.
101
  try:
102
  logger.info(f"Loading summarization model from Hugging Face: {HUGGINGFACE_SUMMARIZATION_REPO}")
103
  try:
104
  summarization_model = SummarizationModel(HUGGINGFACE_SUMMARIZATION_REPO)
105
  except Exception as remote_error:
106
  logger.warning(f"Remote load failed, falling back to local model: {remote_error}")
107
+ _startup_errors.append(f"remote_load: {str(remote_error)[:200]}")
108
  logger.info(f"Loading summarization model from local path: {SUMMARIZATION_PATH}")
109
  summarization_model = SummarizationModel(SUMMARIZATION_PATH)
110
  loaded.append("summarization")
111
  logger.info("Summarization model loaded successfully")
112
  except Exception as e:
113
+ import traceback
114
+ err_detail = traceback.format_exc()
115
  failed.append(("summarization", str(e)))
116
+ _startup_errors.append(f"summarization_load_failed: {err_detail[-500:]}")
117
  logger.error(f"Failed to load summarization model: {str(e)}")
118
 
119
  logger.info(f"Models loaded: {loaded}")
 
122
 
123
  return len(loaded) > 0
124
 
125
+ _startup_errors = []
126
+
127
 
128
  @app.route('/')
129
  def index():
 
188
 
189
  @app.route('/api/debug-models', methods=['GET'])
190
  def debug_models():
191
+ """Debug endpoint: report model status and startup errors."""
 
 
 
192
  from hf_inference import debug_test_all_models
193
  results = debug_test_all_models()
194
+
195
+ # Memory info
196
+ import os
197
+ try:
198
+ import resource
199
+ mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
200
+ mem_info = f"{mem} KB"
201
+ except Exception:
202
+ mem_info = "N/A"
203
+
204
+ # /proc/meminfo on Linux
205
+ proc_mem = {}
206
+ try:
207
+ with open('/proc/meminfo', 'r') as f:
208
+ for line in f:
209
+ if any(k in line for k in ['MemTotal', 'MemFree', 'MemAvailable', 'SwapTotal']):
210
+ parts = line.split()
211
+ proc_mem[parts[0].rstrip(':')] = parts[1] + ' ' + (parts[2] if len(parts) > 2 else '')
212
+ except Exception:
213
+ proc_mem = {"error": "cannot read /proc/meminfo"}
214
+
215
  return jsonify({
216
  'status': 'debug',
217
  'hf_api_token_set': bool(HF_API_TOKEN),
218
+ 'summarization_model_loaded': summarization_model is not None,
219
+ 'startup_errors': _startup_errors,
220
+ 'memory': mem_info,
221
+ 'proc_meminfo': proc_mem,
222
  'models': results,
223
  }), 200
224