Spaces:
Sleeping
Sleeping
Commit
·
aec4e49
1
Parent(s):
7954a1d
Enhanced error handling and debugging for nemaquant.py execution
Browse files
app.py
CHANGED
|
@@ -107,12 +107,27 @@ def process_images():
|
|
| 107 |
print(f"Weights file exists: {os.path.exists(str(WEIGHTS_FILE))}")
|
| 108 |
print(f"Weights file size: {os.path.getsize(str(WEIGHTS_FILE)) if os.path.exists(str(WEIGHTS_FILE)) else 'File not found'} bytes")
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# Run the script, capture output and errors
|
| 111 |
# Timeout might be needed for long processes on shared infrastructure like HF Spaces
|
| 112 |
-
result = subprocess.run(cmd, capture_output=True, text=True, check=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
status_log = f"NemaQuant Output:\n{result.stdout}\nNemaQuant Errors:\n{result.stderr}"
|
| 114 |
print(status_log) # Log script output
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# Check output directory after command runs
|
| 117 |
print(f"Output directory exists: {os.path.exists(str(job_output_dir))}")
|
| 118 |
if os.path.exists(str(job_output_dir)):
|
|
@@ -120,10 +135,63 @@ def process_images():
|
|
| 120 |
|
| 121 |
# Check output file
|
| 122 |
print(f"Output CSV exists: {os.path.exists(str(output_csv))}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
# --- Parse Results ---
|
| 125 |
if not output_csv.exists():
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
df = pd.read_csv(output_csv)
|
| 129 |
# Expect columns like 'filename', 'num_eggs' (based on nemaquant.py)
|
|
@@ -224,6 +292,29 @@ if __name__ == '__main__':
|
|
| 224 |
print(f"Weights file exists: {os.path.exists(str(WEIGHTS_FILE))}")
|
| 225 |
if os.path.exists(str(WEIGHTS_FILE)):
|
| 226 |
print(f"Weights file size: {os.path.getsize(str(WEIGHTS_FILE))} bytes")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
print("---------------------------------------")
|
| 228 |
|
| 229 |
app.run(debug=True, host='0.0.0.0', port=7860) # Port 7860 is common for HF Spaces
|
|
|
|
| 107 |
print(f"Weights file exists: {os.path.exists(str(WEIGHTS_FILE))}")
|
| 108 |
print(f"Weights file size: {os.path.getsize(str(WEIGHTS_FILE)) if os.path.exists(str(WEIGHTS_FILE)) else 'File not found'} bytes")
|
| 109 |
|
| 110 |
+
# Additional debugging before running the command
|
| 111 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 112 |
+
print(f"Output directory permissions: {oct(os.stat(str(job_output_dir)).st_mode)[-3:] if os.path.exists(str(job_output_dir)) else 'Directory not found'}")
|
| 113 |
+
print(f"Executable Python: {sys.executable}")
|
| 114 |
+
|
| 115 |
# Run the script, capture output and errors
|
| 116 |
# Timeout might be needed for long processes on shared infrastructure like HF Spaces
|
| 117 |
+
result = subprocess.run(cmd, capture_output=True, text=True, check=False, timeout=300) # 5 min timeout, don't raise exception
|
| 118 |
+
|
| 119 |
+
# Get script return code and output
|
| 120 |
+
print(f"NemaQuant script return code: {result.returncode}")
|
| 121 |
+
print(f"NemaQuant stdout: {result.stdout}")
|
| 122 |
+
print(f"NemaQuant stderr: {result.stderr}")
|
| 123 |
+
|
| 124 |
status_log = f"NemaQuant Output:\n{result.stdout}\nNemaQuant Errors:\n{result.stderr}"
|
| 125 |
print(status_log) # Log script output
|
| 126 |
|
| 127 |
+
# Check if the command failed
|
| 128 |
+
if result.returncode != 0:
|
| 129 |
+
raise subprocess.CalledProcessError(result.returncode, cmd, output=result.stdout, stderr=result.stderr)
|
| 130 |
+
|
| 131 |
# Check output directory after command runs
|
| 132 |
print(f"Output directory exists: {os.path.exists(str(job_output_dir))}")
|
| 133 |
if os.path.exists(str(job_output_dir)):
|
|
|
|
| 135 |
|
| 136 |
# Check output file
|
| 137 |
print(f"Output CSV exists: {os.path.exists(str(output_csv))}")
|
| 138 |
+
|
| 139 |
+
# If CSV doesn't exist, try to check if there are any other CSVs created in the output directory
|
| 140 |
+
if not output_csv.exists() and os.path.exists(str(job_output_dir)):
|
| 141 |
+
csv_files = [f for f in os.listdir(str(job_output_dir)) if f.endswith('.csv')]
|
| 142 |
+
if csv_files:
|
| 143 |
+
print(f"Found other CSV files in output directory: {csv_files}")
|
| 144 |
+
# Try using the first found CSV instead
|
| 145 |
+
output_csv = job_output_dir / csv_files[0]
|
| 146 |
+
print(f"Using alternate CSV file: {output_csv}")
|
| 147 |
+
else:
|
| 148 |
+
# No CSV files found - create a fallback CSV file manually
|
| 149 |
+
# This is a workaround in case nemaquant.py's to_csv() fails due to permissions
|
| 150 |
+
try:
|
| 151 |
+
print("No CSV found - trying to create a fallback CSV file")
|
| 152 |
+
# Look for annotated images to get filenames and create a basic CSV
|
| 153 |
+
annotated_files = [f for f in os.listdir(str(job_output_dir)) if '_annotated' in f]
|
| 154 |
+
if annotated_files:
|
| 155 |
+
# Create a simple CSV with the filenames found
|
| 156 |
+
fallback_csv = job_output_dir / f"{job_id}_fallback_results.csv"
|
| 157 |
+
with open(fallback_csv, 'w') as f:
|
| 158 |
+
f.write("filename,num_eggs,fallback\n")
|
| 159 |
+
for img_file in annotated_files:
|
| 160 |
+
# Extract original filename by removing '_annotated'
|
| 161 |
+
orig_filename = img_file.replace('_annotated', '')
|
| 162 |
+
f.write(f"{orig_filename},0,true\n")
|
| 163 |
+
output_csv = fallback_csv
|
| 164 |
+
print(f"Created fallback CSV at {fallback_csv}")
|
| 165 |
+
except Exception as csv_error:
|
| 166 |
+
print(f"Error creating fallback CSV: {csv_error}")
|
| 167 |
|
| 168 |
# --- Parse Results ---
|
| 169 |
if not output_csv.exists():
|
| 170 |
+
error_message = f"Output CSV not found at {output_csv}"
|
| 171 |
+
print(error_message)
|
| 172 |
+
# Check if nemaquant.py exists and try to debug file paths
|
| 173 |
+
nemaquant_script = APP_ROOT / 'nemaquant.py'
|
| 174 |
+
nemaquant_exists = nemaquant_script.exists()
|
| 175 |
+
error_message += f"\nNemaQuant script exists: {nemaquant_exists}"
|
| 176 |
+
if nemaquant_exists:
|
| 177 |
+
# Try to inspect the nemaquant script to understand its file I/O
|
| 178 |
+
try:
|
| 179 |
+
with open(nemaquant_script, 'r') as f:
|
| 180 |
+
script_content = f.read()
|
| 181 |
+
# Look for common file output patterns
|
| 182 |
+
if 'to_csv(' in script_content:
|
| 183 |
+
error_message += "\nNemaQuant script contains 'to_csv()' calls for file output"
|
| 184 |
+
if 'open(' in script_content and 'w' in script_content:
|
| 185 |
+
error_message += "\nNemaQuant script contains file opening in write mode"
|
| 186 |
+
except Exception as e:
|
| 187 |
+
error_message += f"\nCould not read nemaquant.py: {str(e)}"
|
| 188 |
+
|
| 189 |
+
# Check if any files were written at all
|
| 190 |
+
if os.path.exists(str(job_output_dir)):
|
| 191 |
+
files_in_output = os.listdir(str(job_output_dir))
|
| 192 |
+
error_message += f"\nFiles in output directory: {files_in_output}"
|
| 193 |
+
|
| 194 |
+
raise FileNotFoundError(error_message)
|
| 195 |
|
| 196 |
df = pd.read_csv(output_csv)
|
| 197 |
# Expect columns like 'filename', 'num_eggs' (based on nemaquant.py)
|
|
|
|
| 292 |
print(f"Weights file exists: {os.path.exists(str(WEIGHTS_FILE))}")
|
| 293 |
if os.path.exists(str(WEIGHTS_FILE)):
|
| 294 |
print(f"Weights file size: {os.path.getsize(str(WEIGHTS_FILE))} bytes")
|
| 295 |
+
|
| 296 |
+
# Check if running in Docker or Hugging Face Space container
|
| 297 |
+
in_container = os.path.exists('/.dockerenv') or os.environ.get('SPACE_ID')
|
| 298 |
+
print(f"Running in container: {in_container}")
|
| 299 |
+
if in_container:
|
| 300 |
+
print("Container environment detected:")
|
| 301 |
+
print(f"User running process: {os.getuid()}:{os.getgid()}")
|
| 302 |
+
try:
|
| 303 |
+
# Check permissions for key directories
|
| 304 |
+
for path in [UPLOAD_FOLDER, RESULT_FOLDER]:
|
| 305 |
+
stat_info = os.stat(path)
|
| 306 |
+
print(f"Permissions for {path}: {oct(stat_info.st_mode)[-3:]}")
|
| 307 |
+
print(f"Owner for {path}: {stat_info.st_uid}:{stat_info.st_gid}")
|
| 308 |
+
except Exception as e:
|
| 309 |
+
print(f"Error checking permissions: {e}")
|
| 310 |
+
|
| 311 |
+
print("---------------------------------------")
|
| 312 |
+
|
| 313 |
+
# Check for nemaquant.py script
|
| 314 |
+
nemaquant_script = APP_ROOT / 'nemaquant.py'
|
| 315 |
+
print(f"NemaQuant script exists: {nemaquant_script.exists()}")
|
| 316 |
+
if nemaquant_script.exists():
|
| 317 |
+
print(f"NemaQuant script permissions: {oct(os.stat(nemaquant_script).st_mode)[-3:]}")
|
| 318 |
print("---------------------------------------")
|
| 319 |
|
| 320 |
app.run(debug=True, host='0.0.0.0', port=7860) # Port 7860 is common for HF Spaces
|