File size: 6,933 Bytes
ba72f62
 
 
 
 
 
 
504df0f
d8acd61
504df0f
 
 
 
 
d8acd61
504df0f
 
 
22b00f2
 
5d095b2
 
 
 
 
 
 
d8acd61
8ec51a1
 
671ea59
 
68d1258
 
 
8ec51a1
671ea59
8ec51a1
 
68d1258
d8acd61
da815dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68d1258
d8acd61
22b00f2
 
d8d1294
22b00f2
 
 
 
d8acd61
5d095b2
d8acd61
 
 
 
 
 
 
 
 
 
 
5d095b2
d8acd61
5d095b2
 
d8acd61
22b00f2
d8acd61
 
22b00f2
504df0f
d8acd61
22b00f2
 
 
 
d8acd61
 
22b00f2
 
 
 
 
 
 
d8acd61
 
 
 
504df0f
22b00f2
504df0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8acd61
504df0f
 
22b00f2
2ae57cb
 
 
22b00f2
2ae57cb
 
 
22b00f2
504df0f
 
2ae57cb
 
 
 
504df0f
22b00f2
504df0f
 
22b00f2
504df0f
 
22b00f2
2ae57cb
 
 
 
 
22b00f2
 
 
2ae57cb
 
504df0f
 
d8acd61
 
22b00f2
d8acd61
 
22b00f2
d8acd61
 
22b00f2
d8acd61
 
 
 
 
2ae57cb
 
 
d8acd61
 
504df0f
2ae57cb
 
 
 
22b00f2
 
 
 
 
2ae57cb
 
 
22b00f2
2ae57cb
 
504df0f
 
 
d8acd61
 
22b00f2
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os

# Ensure Hugging Face writes cache to a safe writable location on Spaces
os.environ["HF_HOME"] = "/tmp/huggingface"
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface/transformers"
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/huggingface/hub"

from flask import Flask, render_template, redirect, url_for, flash, request
from flask_login import LoginManager, login_required, current_user
from werkzeug.utils import secure_filename
import sys
import json
from datetime import datetime

# Adjust sys.path for import flexibility
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)



# Import and initialize DB
from backend.models.database import db, Job, Application, init_db
from backend.models.user import User
from backend.routes.auth import auth_bp
from backend.routes.interview_api import interview_api
from backend.models.resume_parser.resume_to_features import extract_resume_features

# Initialize Flask app
safe_instance_path = os.path.abspath("flask_instance")

app = Flask(
    __name__,
    static_folder='backend/static',
    static_url_path='/static',
    template_folder='backend/templates',
    instance_path=safe_instance_path  # ✅ force-safe location
)
os.makedirs(safe_instance_path, exist_ok=True)


app.config['SECRET_KEY'] = 'your-secret-key'
#
# Configure the database connection
#
# By default the application wrote its SQLite database into the
# `/tmp` directory. On local machines this works, but on hosted
# platforms like Hugging Face Spaces the `/tmp` directory is not
# persistent across sessions. That means any data stored in
# `/tmp/codingo.db` would be lost once the process restarts, and
# newly created user accounts would appear to disappear immediately.
#
# To fix this we store the database file inside the project under
# `backend/instance/codingo.db`. The `backend/instance` directory
# already exists (it is created by `os.makedirs` below) and is
# persisted across requests, so user registrations and other data
# remain available. SQLAlchemy requires three slashes for a relative
# SQLite URI (e.g. `sqlite:///relative/path.db`). Here we use four
# leading slashes because the path is relative to the project
# directory when using `sqlite:///backend/instance/codingo.db`.
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///codingo.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
from flask_wtf.csrf import CSRFProtect

# csrf = CSRFProtect(app)

# Create necessary directories
os.makedirs('static/audio', exist_ok=True)
os.makedirs('temp', exist_ok=True)

# Initialize DB with app
init_db(app)

# Flask-Login setup
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)

@login_manager.user_loader
def load_user(user_id):
    return db.session.get(User, int(user_id))

# Register blueprints
app.register_blueprint(auth_bp)
app.register_blueprint(interview_api, url_prefix="/api")

def handle_resume_upload(file):
    """Save uploaded file temporarily, extract features, then clean up."""
    if not file or file.filename == '':
        return None, "No file uploaded", None
    
    try:
        filename = secure_filename(file.filename)
        temp_dir = os.path.join(current_dir, 'temp')
        os.makedirs(temp_dir, exist_ok=True)
        filepath = os.path.join(temp_dir, filename)
        
        file.save(filepath)
        features = extract_resume_features(filepath)
        
        # Clean up
        try:
            os.remove(filepath)
        except:
            pass
            
        return features, None, filename
    except Exception as e:
        print(f"Error in handle_resume_upload: {e}")
        return None, str(e), None

# Routes (keep your existing routes)
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/jobs')
def jobs():
    all_jobs = Job.query.order_by(Job.date_posted.desc()).all()
    return render_template('jobs.html', jobs=all_jobs)

@app.route('/job/<int:job_id>')
def job_detail(job_id):
    job = Job.query.get_or_404(job_id)
    return render_template('job_detail.html', job=job)

@app.route('/apply/<int:job_id>', methods=['GET', 'POST'])
@login_required
def apply(job_id):
    job = Job.query.get_or_404(job_id)
    
    if request.method == 'POST':
        file = request.files.get('resume')
        features, error, _ = handle_resume_upload(file)
        
        if error or not features:
            flash("Resume parsing failed. Please try again.", "danger")
            return render_template('apply.html', job=job)
        
        application = Application(
            job_id=job_id,
            user_id=current_user.id,
            name=current_user.username,
            email=current_user.email,
            extracted_features=json.dumps(features)
        )
        
        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', job=job)

@app.route('/my_applications')
@login_required
def my_applications():
    applications = Application.query.filter_by(
        user_id=current_user.id
    ).order_by(Application.date_applied.desc()).all()
    return render_template('my_applications.html', applications=applications)

@app.route('/parse_resume', methods=['POST'])
def parse_resume():
    file = request.files.get('resume')
    features, error, _ = handle_resume_upload(file)
    
    if error:
        return {"error": "Error parsing resume. Please try again."}, 400
    
    if not features:
        return {"error": "Failed to extract resume details."}, 400
    
    response = {
        "name": features.get('name', ''),
        "email": features.get('email', ''),
        "mobile_number": features.get('mobile_number', ''),
        "skills": features.get('skills', []),
        "experience": features.get('experience', []),
        "education": features.get('education', []),
        "summary": features.get('summary', '')
    }
    return response, 200

@app.route("/interview/<int:job_id>")
@login_required
def interview_page(job_id):
    job = Job.query.get_or_404(job_id)
    application = Application.query.filter_by(
        user_id=current_user.id, 
        job_id=job_id
    ).first()
    
    if not application or not application.extracted_features:
        flash("Please apply for this job and upload your resume first.", "warning")
        return redirect(url_for('job_detail', job_id=job_id))
    
    cv_data = json.loads(application.extracted_features)
    return render_template("interview.html", job=job, cv=cv_data)

if __name__ == '__main__':
    print("Starting Codingo application...")
    with app.app_context():
        db.create_all()
    
    # Use port from environment or default to 7860
    port = int(os.environ.get('PORT', 7860))
    app.run(debug=True, host='0.0.0.0', port=port)