WebashalarForML's picture
Upload 5 files
be28faf verified
raw
history blame
No virus
4.33 kB
from flask import Flask, request, redirect, flash, session, render_template, url_for
import os
import json
import logging
from werkzeug.utils import secure_filename
from utils.error import handle_file_not_found, handle_invalid_file_type, handle_file_processing_error, page_not_found, internal_server_error
from utils.spacy import Parser_from_model
from utils.mistral import process_resume_data
import platform
from waitress import serve
# Initialize the Flask application
app = Flask(__name__)
app.secret_key = 'your_secret_key'
app.config['UPLOAD_FOLDER'] = 'uploads'
# Allowed file extensions
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'}
# Configure logging
logging.basicConfig(level=logging.DEBUG)
# Error handlers
app.register_error_handler(404, page_not_found)
app.register_error_handler(500, internal_server_error)
def allowed_file(filename):
"""Check if the file has an allowed extension."""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
"""Display the index page with the uploaded file information."""
uploaded_file = session.get('uploaded_file', None)
return render_template('index.html', uploaded_file=uploaded_file)
@app.route('/upload_and_process', methods=['POST'])
def upload_and_process():
"""Handle file upload and process the file."""
if 'file' not in request.files or request.files['file'].filename == '':
flash('No file selected for upload.')
return redirect(request.url)
file = request.files['file']
# Check if the file is allowed
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
logging.debug(f"File uploaded: {filename}")
session['uploaded_file'] = filename
# Process the file after uploading
try:
parsed_data = process_resume_data(file_path)
if not parsed_data or 'error' in parsed_data:
flash('An error occurred during file processing.')
return redirect(url_for('index'))
session['processed_data'] = parsed_data
flash('File uploaded and data processed successfully.')
return redirect(url_for('result'))
except Exception as e:
logging.error(f"File processing error: {str(e)}")
flash('An error occurred while processing the file.')
return handle_file_processing_error()
else:
return handle_invalid_file_type()
@app.route('/remove_file', methods=['POST'])
def remove_file():
"""Remove the uploaded file and reset the session."""
uploaded_file = session.get('uploaded_file')
if uploaded_file:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
if os.path.exists(file_path):
os.remove(file_path)
session.pop('uploaded_file', None)
flash('File successfully removed.')
else:
flash('No file to remove.')
return redirect(url_for('index'))
@app.route('/reset_upload')
def reset_upload():
"""Reset the uploaded file and the processed data."""
uploaded_file = session.get('uploaded_file')
if uploaded_file:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
if os.path.exists(file_path):
os.remove(file_path)
session.pop('uploaded_file', None)
session.pop('processed_data', None)
flash('File and data reset. You can upload a new file.')
return redirect(url_for('index'))
@app.route('/result')
def result():
"""Display the processed data result."""
processed_data = session.get('processed_data', None)
if not processed_data:
flash('No data to display. Please upload and process a file.')
return redirect(url_for('index'))
return render_template('result.html', parsed_data=processed_data)
if __name__ == '__main__':
# For Windows development
if platform.system() == "Windows":
app.run(debug=True)
# For Linux or production with Waitress
else:
serve(app, host="0.0.0.0", port=7860)