OnsAouedi commited on
Commit
795f111
·
verified ·
1 Parent(s): bbf632f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -22
app.py CHANGED
@@ -31,6 +31,13 @@ try:
31
  except:
32
  pass # In case we don't have permission to change permissions
33
 
 
 
 
 
 
 
 
34
  # Global variables for progress tracking
35
  current_progress = {'step': 'idle', 'progress': 0, 'details': ''}
36
 
@@ -606,14 +613,27 @@ def run_inference_pipeline(csv_file_path, model_path, normalization_path):
606
  # Step 11: Save results
607
  update_progress('Saving results', 95, 'Saving inference results...')
608
 
609
- # Create results directory
610
- results_dir = Path('results/inference_atlantic')
611
- results_dir.mkdir(parents=True, exist_ok=True)
612
-
613
- # Save to results directory
614
- timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
615
- results_file = results_dir / f'inference_results_{timestamp}.csv'
616
- results_df.to_csv(results_file, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
617
 
618
  # Also save to temporary file for compatibility
619
  output_file = tempfile.NamedTemporaryFile(
@@ -842,23 +862,37 @@ def get_progress():
842
 
843
  @app.route('/download_results')
844
  def download_results():
845
- # Find the most recent results file
846
- upload_dir = app.config['UPLOAD_FOLDER']
847
- temp_dir = tempfile.gettempdir()
 
 
 
 
 
 
848
 
849
- # Look for results files in both directories
850
- for directory in [upload_dir, temp_dir]:
851
  if os.path.exists(directory):
852
- files = [f for f in os.listdir(directory) if f.endswith('_inference_results.csv')]
853
- if files:
854
- latest_file = max(files, key=lambda x: os.path.getctime(os.path.join(directory, x)))
855
- return send_file(
856
- os.path.join(directory, latest_file),
857
- as_attachment=True,
858
- download_name='vessel_inference_results.csv'
859
- )
 
 
 
 
 
 
 
 
 
860
 
861
- return jsonify({'error': 'No results file found'}), 404
862
 
863
  ########################################
864
  # SOCKETIO EVENTS #
 
31
  except:
32
  pass # In case we don't have permission to change permissions
33
 
34
+ # Try to create results directory at startup
35
+ try:
36
+ os.makedirs('results/inference_atlantic', exist_ok=True)
37
+ os.chmod('results/inference_atlantic', 0o755)
38
+ except:
39
+ print("⚠️ WARNING: Could not create results directory - will use temp directory for results")
40
+
41
  # Global variables for progress tracking
42
  current_progress = {'step': 'idle', 'progress': 0, 'details': ''}
43
 
 
613
  # Step 11: Save results
614
  update_progress('Saving results', 95, 'Saving inference results...')
615
 
616
+ # Create results directory with permission handling
617
+ try:
618
+ results_dir = Path('results/inference_atlantic')
619
+ results_dir.mkdir(parents=True, exist_ok=True)
620
+
621
+ # Save to results directory
622
+ timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
623
+ results_file = results_dir / f'inference_results_{timestamp}.csv'
624
+ results_df.to_csv(results_file, index=False)
625
+ print(f"✅ Results saved to: {results_file}")
626
+ except PermissionError:
627
+ # Fallback to temp directory if results directory has permission issues
628
+ import tempfile
629
+ temp_dir = Path(tempfile.gettempdir())
630
+ timestamp = pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')
631
+ results_file = temp_dir / f'inference_results_{timestamp}.csv'
632
+ results_df.to_csv(results_file, index=False)
633
+ print(f"⚠️ WARNING: Results saved to temp directory due to permissions: {results_file}")
634
+ except Exception as e:
635
+ print(f"❌ ERROR: Could not save results file: {str(e)}")
636
+ # Continue anyway - we still have the temp file below
637
 
638
  # Also save to temporary file for compatibility
639
  output_file = tempfile.NamedTemporaryFile(
 
862
 
863
  @app.route('/download_results')
864
  def download_results():
865
+ # Find the most recent results file in multiple locations
866
+ search_directories = [
867
+ 'results/inference_atlantic', # Default results directory
868
+ app.config['UPLOAD_FOLDER'], # Uploads directory
869
+ tempfile.gettempdir() # System temp directory
870
+ ]
871
+
872
+ latest_file = None
873
+ latest_time = 0
874
 
875
+ for directory in search_directories:
 
876
  if os.path.exists(directory):
877
+ try:
878
+ files = [f for f in os.listdir(directory) if f.endswith('_inference_results.csv')]
879
+ for file in files:
880
+ file_path = os.path.join(directory, file)
881
+ file_time = os.path.getctime(file_path)
882
+ if file_time > latest_time:
883
+ latest_time = file_time
884
+ latest_file = file_path
885
+ except PermissionError:
886
+ continue # Skip directories we can't read
887
+
888
+ if latest_file and os.path.exists(latest_file):
889
+ return send_file(
890
+ latest_file,
891
+ as_attachment=True,
892
+ download_name='vessel_inference_results.csv'
893
+ )
894
 
895
+ return jsonify({'error': 'No results file found. Please run inference first.'}), 404
896
 
897
  ########################################
898
  # SOCKETIO EVENTS #