Spaces:
Running
Running
Delete index.py
Browse files
index.py
DELETED
|
@@ -1,55 +0,0 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import joblib
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
# Initialize the Flask app
|
| 8 |
-
app = Flask(__name__)
|
| 9 |
-
# This is the key change: it automatically handles CORS for all routes
|
| 10 |
-
CORS(app)
|
| 11 |
-
|
| 12 |
-
# --- Load the model and columns ---
|
| 13 |
-
# Vercel copies the files to a temporary directory, so we find the path
|
| 14 |
-
base_path = os.path.dirname(__file__)
|
| 15 |
-
model_path = os.path.join(base_path, 'alumni_match_model.joblib')
|
| 16 |
-
columns_path = os.path.join(base_path, 'model_feature_columns.joblib')
|
| 17 |
-
|
| 18 |
-
model = joblib.load(model_path)
|
| 19 |
-
model_columns = joblib.load(columns_path)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
@app.route('/api', methods=['POST'])
|
| 23 |
-
def handle_request():
|
| 24 |
-
# Get the JSON data sent from the React app
|
| 25 |
-
incoming_data = request.get_json()
|
| 26 |
-
|
| 27 |
-
# --- PREPARE THE DATA FOR PREDICTION ---
|
| 28 |
-
df = pd.DataFrame([incoming_data])
|
| 29 |
-
|
| 30 |
-
def count_common_skills(row):
|
| 31 |
-
viewer_skills = set(str(row['viewer_skills']).lower().split('|'))
|
| 32 |
-
target_skills = set(str(row['target_skills']).lower().split('|'))
|
| 33 |
-
return len(viewer_skills.intersection(target_skills))
|
| 34 |
-
|
| 35 |
-
df['common_skills_count'] = df.apply(count_common_skills, axis=1)
|
| 36 |
-
df['branch_match'] = (df['viewer_branch'].str.lower() == df['target_branch'].str.lower()).astype(int)
|
| 37 |
-
|
| 38 |
-
for col in model_columns:
|
| 39 |
-
if col.startswith('company_'):
|
| 40 |
-
df[col] = 0
|
| 41 |
-
|
| 42 |
-
company_col_name = f"company_{incoming_data['target_company']}"
|
| 43 |
-
if company_col_name in df.columns:
|
| 44 |
-
df[company_col_name] = 1
|
| 45 |
-
|
| 46 |
-
# Reorder columns to match the model's training order
|
| 47 |
-
final_df = df[model_columns]
|
| 48 |
-
|
| 49 |
-
# --- MAKE THE PREDICTION ---
|
| 50 |
-
prediction_proba = model.predict_proba(final_df)
|
| 51 |
-
match_probability = prediction_proba[0][1]
|
| 52 |
-
final_score = round(match_probability * 10)
|
| 53 |
-
|
| 54 |
-
# Send the score back as a JSON response
|
| 55 |
-
return jsonify({'score': final_score})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|