Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,20 +18,22 @@ RESULTS_FOLDER = os.path.join(BASE_DIR, 'results')
|
|
| 18 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 19 |
os.makedirs(RESULTS_FOLDER, exist_ok=True)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
-
classifier = YOLO(
|
| 25 |
-
detector = YOLO(
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Model loading error: {e}")
|
| 28 |
-
# Fallback mechanism or alternative model loading
|
| 29 |
classifier = None
|
| 30 |
detector = None
|
| 31 |
|
| 32 |
-
# Initialize EasyOCR
|
| 33 |
try:
|
| 34 |
-
reader = Reader(['en'])
|
| 35 |
except Exception as e:
|
| 36 |
print(f"EasyOCR initialization error: {e}")
|
| 37 |
reader = None
|
|
@@ -269,11 +271,12 @@ def about():
|
|
| 269 |
def contact():
|
| 270 |
return render_template('contact.html')
|
| 271 |
|
| 272 |
-
@app.route('/
|
| 273 |
def upload_files():
|
| 274 |
-
|
|
|
|
| 275 |
if 'zipfile' not in request.files or 'excelfile' not in request.files:
|
| 276 |
-
return jsonify({"error": "Both
|
| 277 |
|
| 278 |
zip_file = request.files['zipfile']
|
| 279 |
excel_file = request.files['excelfile']
|
|
@@ -282,85 +285,51 @@ def upload_files():
|
|
| 282 |
if not zip_file.filename.endswith('.zip') or not excel_file.filename.endswith('.xlsx'):
|
| 283 |
return jsonify({"error": "Invalid file formats. Need .zip and .xlsx files."}), 400
|
| 284 |
|
| 285 |
-
# Create absolute paths
|
| 286 |
-
zip_path = os.path.join(app.config['UPLOAD_FOLDER'], zip_file.filename)
|
| 287 |
-
excel_path = os.path.join(app.config['UPLOAD_FOLDER'], excel_file.filename)
|
| 288 |
-
|
| 289 |
-
# Ensure directories exist
|
| 290 |
-
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 291 |
-
os.makedirs(app.config['RESULTS_FOLDER'], exist_ok=True)
|
| 292 |
-
|
| 293 |
-
# Save files with error handling
|
| 294 |
try:
|
|
|
|
|
|
|
|
|
|
| 295 |
zip_file.save(zip_path)
|
| 296 |
excel_file.save(excel_path)
|
| 297 |
-
except Exception as e:
|
| 298 |
-
return jsonify({"error": f"File save error: {str(e)}"}), 500
|
| 299 |
|
| 300 |
-
|
| 301 |
-
try:
|
| 302 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 303 |
-
zip_ref.extractall(
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
if key not in processed_results:
|
| 321 |
-
extracted_data = process_image(image_path)
|
| 322 |
-
if extracted_data:
|
| 323 |
-
processed_results[key] = extracted_data
|
| 324 |
-
# Clean up processed image
|
| 325 |
-
if os.path.exists(image_path):
|
| 326 |
-
os.remove(image_path)
|
| 327 |
-
except Exception as e:
|
| 328 |
-
print(f"Error processing image {image_path}: {str(e)}")
|
| 329 |
-
continue
|
| 330 |
-
|
| 331 |
-
# Process Excel with error handling
|
| 332 |
-
try:
|
| 333 |
df = pd.read_excel(excel_path)
|
| 334 |
df = df.astype(str)
|
| 335 |
-
comparison_results = compare_data(df, processed_results)
|
| 336 |
comparison_results['Accepted/Rejected'] = np.where(
|
| 337 |
-
comparison_results['Final Remarks'] == 'All matched',
|
| 338 |
-
'Accepted',
|
| 339 |
'Rejected'
|
| 340 |
)
|
| 341 |
|
| 342 |
-
#
|
| 343 |
results_df = pd.DataFrame(comparison_results)
|
| 344 |
-
results_file_path =
|
| 345 |
results_df.to_excel(results_file_path, index=False)
|
| 346 |
|
| 347 |
-
visualization_data = create_visualizations(comparison_results)
|
| 348 |
-
|
| 349 |
-
# Database operations with error handling
|
| 350 |
-
try:
|
| 351 |
-
create_database_and_table()
|
| 352 |
-
save_results_to_database(comparison_results[
|
| 353 |
-
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 354 |
-
].to_dict(orient='records'))
|
| 355 |
-
except Exception as e:
|
| 356 |
-
print(f"Database operation error: {str(e)}")
|
| 357 |
-
|
| 358 |
-
# Clean up input files
|
| 359 |
-
os.remove(zip_path)
|
| 360 |
-
os.remove(excel_path)
|
| 361 |
|
|
|
|
| 362 |
return jsonify({
|
| 363 |
-
"message": "Files processed successfully!",
|
| 364 |
"results": comparison_results[
|
| 365 |
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 366 |
].to_dict(orient='records'),
|
|
@@ -368,21 +337,21 @@ def upload_files():
|
|
| 368 |
})
|
| 369 |
|
| 370 |
except Exception as e:
|
| 371 |
-
return jsonify({"error": f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
-
|
| 374 |
-
return
|
| 375 |
-
|
| 376 |
-
finally:
|
| 377 |
-
# Cleanup any remaining temporary files
|
| 378 |
-
for f in os.listdir(app.config['UPLOAD_FOLDER']):
|
| 379 |
-
try:
|
| 380 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f)
|
| 381 |
-
if os.path.isfile(file_path):
|
| 382 |
-
os.remove(file_path)
|
| 383 |
-
except Exception as e:
|
| 384 |
-
print(f"Cleanup error: {str(e)}")
|
| 385 |
|
| 386 |
if __name__ == '__main__':
|
| 387 |
# Configure HTTPS with self-signed certificate
|
| 388 |
-
app.run(host=
|
|
|
|
| 18 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 19 |
os.makedirs(RESULTS_FOLDER, exist_ok=True)
|
| 20 |
|
| 21 |
+
# Model and data paths - Hugging Face assumes these are in the root directory
|
| 22 |
+
MODEL_CLASSIFICATION_PATH = "classification.pt"
|
| 23 |
+
MODEL_DETECTION_PATH = "detection.pt"
|
| 24 |
+
|
| 25 |
+
# Load YOLOv8 models
|
| 26 |
try:
|
| 27 |
+
classifier = YOLO(MODEL_CLASSIFICATION_PATH)
|
| 28 |
+
detector = YOLO(MODEL_DETECTION_PATH)
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Model loading error: {e}")
|
|
|
|
| 31 |
classifier = None
|
| 32 |
detector = None
|
| 33 |
|
| 34 |
+
# Initialize EasyOCR
|
| 35 |
try:
|
| 36 |
+
reader = Reader(['en'])
|
| 37 |
except Exception as e:
|
| 38 |
print(f"EasyOCR initialization error: {e}")
|
| 39 |
reader = None
|
|
|
|
| 271 |
def contact():
|
| 272 |
return render_template('contact.html')
|
| 273 |
|
| 274 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 275 |
def upload_files():
|
| 276 |
+
if request.method == 'POST':
|
| 277 |
+
# Check if the post request has the files
|
| 278 |
if 'zipfile' not in request.files or 'excelfile' not in request.files:
|
| 279 |
+
return jsonify({"error": "Both zipfile and excelfile are required."}), 400
|
| 280 |
|
| 281 |
zip_file = request.files['zipfile']
|
| 282 |
excel_file = request.files['excelfile']
|
|
|
|
| 285 |
if not zip_file.filename.endswith('.zip') or not excel_file.filename.endswith('.xlsx'):
|
| 286 |
return jsonify({"error": "Invalid file formats. Need .zip and .xlsx files."}), 400
|
| 287 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
try:
|
| 289 |
+
# Save files temporarily (Hugging Face Spaces has a temporary file system)
|
| 290 |
+
zip_path = zip_file.filename
|
| 291 |
+
excel_path = excel_file.filename
|
| 292 |
zip_file.save(zip_path)
|
| 293 |
excel_file.save(excel_path)
|
|
|
|
|
|
|
| 294 |
|
| 295 |
+
# Extract zip
|
|
|
|
| 296 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 297 |
+
zip_ref.extractall('.')
|
| 298 |
+
|
| 299 |
+
# Process images
|
| 300 |
+
image_paths = [f for f in os.listdir('.') if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
| 301 |
+
processed_results = {}
|
| 302 |
+
for image_path in image_paths:
|
| 303 |
+
try:
|
| 304 |
+
file_name = os.path.basename(image_path)
|
| 305 |
+
key = file_name.split('.')[0][:3]
|
| 306 |
+
if key not in processed_results:
|
| 307 |
+
extracted_data = process_image(image_path) # Assuming process_image is defined
|
| 308 |
+
if extracted_data:
|
| 309 |
+
processed_results[key] = extracted_data
|
| 310 |
+
except Exception as e:
|
| 311 |
+
print(f"Error processing image {image_path}: {str(e)}")
|
| 312 |
+
|
| 313 |
+
# Process Excel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
df = pd.read_excel(excel_path)
|
| 315 |
df = df.astype(str)
|
| 316 |
+
comparison_results = compare_data(df, processed_results) # Assuming compare_data is defined
|
| 317 |
comparison_results['Accepted/Rejected'] = np.where(
|
| 318 |
+
comparison_results['Final Remarks'] == 'All matched',
|
| 319 |
+
'Accepted',
|
| 320 |
'Rejected'
|
| 321 |
)
|
| 322 |
|
| 323 |
+
# Prepare for download - adjust path if needed
|
| 324 |
results_df = pd.DataFrame(comparison_results)
|
| 325 |
+
results_file_path = 'results.xlsx'
|
| 326 |
results_df.to_excel(results_file_path, index=False)
|
| 327 |
|
| 328 |
+
visualization_data = create_visualizations(comparison_results) # Assuming create_visualizations is defined
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
+
# For now, return results as JSON, consider a download link for the Excel
|
| 331 |
return jsonify({
|
| 332 |
+
"message": "Files processed successfully!",
|
| 333 |
"results": comparison_results[
|
| 334 |
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 335 |
].to_dict(orient='records'),
|
|
|
|
| 337 |
})
|
| 338 |
|
| 339 |
except Exception as e:
|
| 340 |
+
return jsonify({"error": f"Processing error: {str(e)}"}), 500
|
| 341 |
+
|
| 342 |
+
finally:
|
| 343 |
+
# Clean up temporary files
|
| 344 |
+
if os.path.exists(zip_path):
|
| 345 |
+
os.remove(zip_path)
|
| 346 |
+
if os.path.exists(excel_path):
|
| 347 |
+
os.remove(excel_path)
|
| 348 |
+
for image_path in image_paths:
|
| 349 |
+
if os.path.exists(image_path):
|
| 350 |
+
os.remove(image_path)
|
| 351 |
|
| 352 |
+
else: # GET request
|
| 353 |
+
return render_template("services.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
if __name__ == '__main__':
|
| 356 |
# Configure HTTPS with self-signed certificate
|
| 357 |
+
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7680)))
|