loghugging25 commited on
Commit
bb498b2
Β·
1 Parent(s): ffb4f17

app.py fixed

Browse files
Files changed (1) hide show
  1. dashboard/app.py +67 -55
dashboard/app.py CHANGED
@@ -276,98 +276,110 @@ def run(from_results_dir, datasource, port):
276
 
277
 
278
  @click.command()
279
- @click.option('--from-results-dir', default=None, help='Load inference-benchmarker results from a directory')
280
- @click.option('--datasource', default='file://benchmarks.parquet', help='Load a Parquet file already generated')
281
  @click.option('--port', default=7860, help='Port to run the dashboard')
282
- def main(from_results_dir, datasource, port):
283
  print("===== Starting Application =====")
284
- print(f"Environment variables: {os.environ}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
 
286
- if from_results_dir is not None:
287
- # If from_results_dir is specified, results are built into 'benchmarks.parquet'
288
- # within that directory.
 
 
289
  output_filename = 'benchmarks.parquet'
290
- print(f"πŸ’‘ Debug - Building results from directory: {from_results_dir}")
291
 
292
- # Check if results directory exists
293
- check_file_exists(from_results_dir, "Results directory")
294
 
295
- # Create absolute path for results directory
296
- abs_results_dir = os.path.abspath(from_results_dir)
297
- print(f"πŸ’‘ Debug - Absolute results directory: {abs_results_dir}")
 
 
298
 
299
- # Create the results directory if it doesn't exist
300
- if not os.path.exists(abs_results_dir):
301
- print(f"πŸ’‘ Debug - Creating results directory: {abs_results_dir}")
302
- os.makedirs(abs_results_dir, exist_ok=True)
303
-
304
- # Call build_results with absolute paths
305
- full_output_path = os.path.join(abs_results_dir, output_filename)
306
- print(f"πŸ’‘ Debug - Expected output path: {full_output_path}")
307
 
308
  try:
309
- build_results(abs_results_dir, output_filename, None)
310
- print("βœ… Build results completed")
311
- except Exception as e:
312
- print(f"❌ Error in build_results: {str(e)}")
313
  print(f"Stack trace: {traceback.format_exc()}")
314
  print("⚠️ Attempting fallback method: direct JSON processing")
315
-
316
- # Fallback: Directly process the JSON files
317
  try:
318
- # Find all JSON files in the results directory
319
- json_files = glob.glob(os.path.join(abs_results_dir, "*.json"))
320
- print(f"Found {len(json_files)} JSON files: {json_files}")
321
-
322
  if not json_files:
323
- raise FileNotFoundError("No JSON files found in results directory")
324
 
325
- # Create a combined DataFrame from the JSON files
326
  combined_data = []
327
  for json_file in json_files:
328
  try:
329
  with open(json_file, 'r') as f:
330
  data = json.load(f)
331
-
332
- # Extract model name from filename
333
  filename = os.path.basename(json_file)
334
- model_name = filename.split('_')[0] + '_' + filename.split('_')[1]
 
335
 
336
- # Process the benchmarks - basic structure expected in the JSON files
337
  if 'benchmarks' in data:
338
  for benchmark in data['benchmarks']:
339
  benchmark['model'] = model_name
340
  benchmark['run_id'] = os.path.splitext(filename)[0]
341
  combined_data.append(benchmark)
342
  else:
343
- print(f"⚠️ No 'benchmarks' key in {json_file}")
344
  except Exception as json_err:
345
- print(f"❌ Error processing {json_file}: {str(json_err)}")
346
 
347
  if combined_data:
348
  df_direct = pd.DataFrame(combined_data)
349
- # Save the DataFrame to parquet
350
- df_direct.to_parquet(full_output_path)
351
- print(f"βœ… Created parquet file via fallback method: {full_output_path}")
352
  else:
353
- raise ValueError("No data could be extracted from JSON files")
354
-
355
- except Exception as fallback_err:
356
- print(f"❌ Fallback method failed: {str(fallback_err)}")
357
  print(f"Stack trace: {traceback.format_exc()}")
358
 
359
- # Check if the file was created
360
- check_file_exists(full_output_path, "Generated parquet")
361
-
362
- # The file to load is now in from_results_dir/output_filename
363
- parquet_file_to_load = full_output_path
 
 
 
364
  else:
365
- # If not building from results_dir, use the provided datasource directly.
366
- parquet_file_to_load = datasource
 
367
 
368
- print(f"Final parameters: from_results_dir={from_results_dir}, datasource={datasource}, port={port}")
369
 
370
- run(None, parquet_file_to_load, port)
 
 
371
 
372
 
373
  if __name__ == '__main__':
 
276
 
277
 
278
  @click.command()
279
+ @click.option('--from-results-dir', 'cli_from_results_dir', default=None, help='Load inference-benchmarker results from this directory. Overrides DASHBOARD_FROM_RESULTS_DIR.')
280
+ @click.option('--datasource', 'cli_datasource', default='file://benchmarks.parquet', help='Load this Parquet file directly if not building from a results directory.')
281
  @click.option('--port', default=7860, help='Port to run the dashboard')
282
+ def main(cli_from_results_dir, cli_datasource, port):
283
  print("===== Starting Application =====")
284
+ # print(f"Environment variables: {os.environ}") # Already in user's code or logs
285
+
286
+ # Determine the directory from which to process JSON results
287
+ # Priority: 1. CLI option, 2. Env Var, 3. Default to 'results' dir
288
+ processing_dir = cli_from_results_dir
289
+
290
+ if processing_dir is None:
291
+ env_var_value = os.environ.get('DASHBOARD_FROM_RESULTS_DIR')
292
+ if env_var_value:
293
+ print(f"Using environment variable DASHBOARD_FROM_RESULTS_DIR='{env_var_value}' for processing.")
294
+ processing_dir = env_var_value
295
+ elif os.path.exists('results') and os.path.isdir('results'):
296
+ print(f"No --from-results-dir option or DASHBOARD_FROM_RESULTS_DIR env var. Defaulting to 'results' directory for processing as it exists.")
297
+ processing_dir = 'results'
298
+ else:
299
+ print(f"No directory specified for processing (no --from-results-dir, no DASHBOARD_FROM_RESULTS_DIR env var, and 'results' dir not found).")
300
+ # processing_dir remains None
301
 
302
+ path_to_load_by_run_function = None # This will be the path to the .parquet file
303
+
304
+ if processing_dir:
305
+ # A directory for processing JSONs has been determined.
306
+ # Use the existing logic to build/fallback and generate benchmarks.parquet.
307
  output_filename = 'benchmarks.parquet'
308
+ abs_processing_dir = os.path.abspath(processing_dir)
309
 
310
+ print(f"πŸ’‘ Debug - Will process JSONs from directory: {abs_processing_dir}")
311
+ check_file_exists(abs_processing_dir, "Source directory for JSONs")
312
 
313
+ # Ensure the directory exists (it might be 'results' or user-provided)
314
+ # build_results might expect the output directory to exist.
315
+ if not os.path.exists(abs_processing_dir):
316
+ print(f"πŸ’‘ Debug - Creating directory for processing/output: {abs_processing_dir}")
317
+ os.makedirs(abs_processing_dir, exist_ok=True)
318
 
319
+ # The generated parquet file will be placed inside the abs_processing_dir
320
+ generated_parquet_filepath = os.path.join(abs_processing_dir, output_filename)
321
+ print(f"πŸ’‘ Debug - Expected path for generated parquet file: {generated_parquet_filepath}")
 
 
 
 
 
322
 
323
  try:
324
+ build_results(abs_processing_dir, output_filename, None) # output_filename is relative to abs_processing_dir
325
+ print("βœ… Build results completed using build_results.")
326
+ except Exception as e_build:
327
+ print(f"❌ Error in build_results: {str(e_build)}")
328
  print(f"Stack trace: {traceback.format_exc()}")
329
  print("⚠️ Attempting fallback method: direct JSON processing")
 
 
330
  try:
331
+ json_files = glob.glob(os.path.join(abs_processing_dir, "*.json"))
332
+ print(f"Found {len(json_files)} JSON files for fallback: {json_files}")
 
 
333
  if not json_files:
334
+ raise FileNotFoundError("Fallback: No JSON files found in results directory")
335
 
 
336
  combined_data = []
337
  for json_file in json_files:
338
  try:
339
  with open(json_file, 'r') as f:
340
  data = json.load(f)
 
 
341
  filename = os.path.basename(json_file)
342
+ model_name_parts = filename.split('_')
343
+ model_name = f"{model_name_parts[0]}_{model_name_parts[1]}" if len(model_name_parts) > 1 else model_name_parts[0]
344
 
 
345
  if 'benchmarks' in data:
346
  for benchmark in data['benchmarks']:
347
  benchmark['model'] = model_name
348
  benchmark['run_id'] = os.path.splitext(filename)[0]
349
  combined_data.append(benchmark)
350
  else:
351
+ print(f"⚠️ Fallback: No 'benchmarks' key in {json_file}")
352
  except Exception as json_err:
353
+ print(f"❌ Fallback: Error processing {json_file}: {str(json_err)}")
354
 
355
  if combined_data:
356
  df_direct = pd.DataFrame(combined_data)
357
+ df_direct.to_parquet(generated_parquet_filepath)
358
+ print(f"βœ… Created parquet file via fallback method: {generated_parquet_filepath}")
 
359
  else:
360
+ raise ValueError("Fallback: No data could be extracted from JSON files")
361
+ except Exception as e_fallback:
362
+ print(f"❌ Fallback method failed: {str(e_fallback)}")
 
363
  print(f"Stack trace: {traceback.format_exc()}")
364
 
365
+ # After attempting to build/generate, check if the file exists
366
+ check_file_exists(generated_parquet_filepath, "Parquet file after build/fallback attempts")
367
+ if os.path.exists(generated_parquet_filepath):
368
+ path_to_load_by_run_function = generated_parquet_filepath
369
+ else:
370
+ print(f"❌ CRITICAL: Failed to generate or find parquet file at '{generated_parquet_filepath}' after all attempts.")
371
+ # path_to_load_by_run_function remains None.
372
+ # The run() function's error handling will create an empty DataFrame.
373
  else:
374
+ # No directory for processing JSONs was determined. Use the cli_datasource directly.
375
+ print(f"Not processing a results directory. Using --datasource option directly: '{cli_datasource}'")
376
+ path_to_load_by_run_function = cli_datasource
377
 
378
+ print(f"πŸ’‘ Final path to be loaded by run() function: '{path_to_load_by_run_function}'")
379
 
380
+ # Call run(). The first argument (from_results_dir for run()) is None because main handles processing.
381
+ # The second argument (datasource for run()) is the actual file path to load.
382
+ run(None, path_to_load_by_run_function, port)
383
 
384
 
385
  if __name__ == '__main__':