Spaces:
Paused
Paused
from flask import Flask, render_template, redirect, url_for, flash, request | |
from werkzeug.utils import secure_filename | |
import os | |
import sys | |
import json | |
from datetime import datetime | |
# Add the parent directory to sys.path to help with imports | |
current_dir = os.path.dirname(os.path.abspath(__file__)) | |
parent_dir = os.path.dirname(current_dir) | |
sys.path.append(parent_dir) | |
sys.path.append(current_dir) | |
# Import local modules with error handling | |
try: | |
from form.JobApplicationForm import JobApplicationForm | |
except ImportError: | |
try: | |
from backend.form.JobApplicationForm import JobApplicationForm | |
except ImportError: | |
print("Error importing JobApplicationForm. Check the path.") | |
sys.exit(1) | |
try: | |
from models.database import db, Job, Application, init_db | |
except ImportError: | |
try: | |
from backend.models.database import db, Job, Application, init_db | |
except ImportError: | |
print("Error importing database models. Check the path.") | |
sys.exit(1) | |
try: | |
from models.resume_parser.resume_to_features import extract_resume_features | |
except ImportError: | |
try: | |
from backend.models.resume_parser.resume_to_features import extract_resume_features | |
except ImportError: | |
print("Error importing resume_to_features. Check if the function is defined in the module.") | |
sys.exit(1) | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'your-secret-key' | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///codingo.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | |
app.config['UPLOAD_FOLDER'] = 'uploads/resumes' | |
# Create upload folder if it doesn't exist | |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) | |
# Initialize the database with the app | |
init_db(app) | |
# Routes | |
def index(): | |
return render_template('index.html') | |
def jobs(): | |
all_jobs = Job.query.order_by(Job.date_posted.desc()).all() | |
return render_template('jobs.html', jobs=all_jobs) | |
def job_detail(job_id): | |
job = Job.query.get_or_404(job_id) | |
return render_template('job_detail.html', job=job) | |
def apply(job_id): | |
job = Job.query.get_or_404(job_id) | |
form = JobApplicationForm() | |
form.job_id.data = job_id | |
if form.validate_on_submit(): | |
# Save resume file | |
resume_file = form.resume.data | |
filename = secure_filename( | |
f"{form.name.data.replace(' ', '_')}_{datetime.now().strftime('%Y%m%d%H%M%S')}.{resume_file.filename.split('.')[-1]}") | |
resume_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
resume_file.save(resume_path) | |
# Extract features from resume | |
try: | |
features = extract_resume_features(resume_path) | |
features_json = json.dumps(features) | |
except Exception as e: | |
print(f"Error extracting features: {e}") | |
features_json = "{}" | |
# Create new application | |
application = Application( | |
job_id=job_id, | |
name=form.name.data, | |
email=form.email.data, | |
resume_path=resume_path, | |
cover_letter=form.cover_letter.data, | |
extracted_features=features_json | |
) | |
db.session.add(application) | |
db.session.commit() | |
flash('Your application has been submitted successfully!', 'success') | |
return redirect(url_for('jobs')) | |
return render_template('apply.html', form=form, job=job) | |
def parse_resume(): | |
if 'resume' not in request.files: | |
return {"error": "No file uploaded"}, 400 | |
file = request.files['resume'] | |
if file.filename == '': | |
return {"error": "No selected file"}, 400 | |
filename = secure_filename(file.filename) | |
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
file.save(file_path) | |
# Extract features from resume | |
try: | |
features = extract_resume_features(file_path) | |
response = { | |
"name": features.get('name', ''), | |
"email": features.get('email', ''), | |
"mobile_number": features.get('mobile_number', ''), | |
"skills": features.get('skills', []), | |
"experience": features.get('experience', []) | |
} | |
return response, 200 | |
except Exception as e: | |
print(f"Error parsing resume: {e}") | |
return {"error": "Failed to parse resume"}, 500 | |
if __name__ == '__main__': | |
print("Starting Codingo application...") | |
app.run(debug=True) |