Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
# In Spaces, files are in the same directory
|
| 11 |
+
model = joblib.load('alumni_match_model.joblib')
|
| 12 |
+
model_columns = joblib.load('model_feature_columns.joblib')
|
| 13 |
+
|
| 14 |
+
@app.route('/', methods=['POST'])
|
| 15 |
+
def handler():
|
| 16 |
+
incoming_data = request.get_json()
|
| 17 |
+
df = pd.DataFrame([incoming_data])
|
| 18 |
+
|
| 19 |
+
def count_common_skills(row):
|
| 20 |
+
viewer_skills = set(str(row.get('viewer_skills', '')).lower().split('|'))
|
| 21 |
+
target_skills = set(str(row.get('target_skills', '')).lower().split('|'))
|
| 22 |
+
return len(viewer_skills.intersection(target_skills))
|
| 23 |
+
|
| 24 |
+
df['common_skills_count'] = df.apply(count_common_skills, axis=1)
|
| 25 |
+
df['branch_match'] = (df['viewer_branch'].str.lower() == df['target_branch'].str.lower()).astype(int)
|
| 26 |
+
|
| 27 |
+
for col in model_columns:
|
| 28 |
+
if col.startswith('company_'):
|
| 29 |
+
df[col] = 0
|
| 30 |
+
|
| 31 |
+
company_name = incoming_data.get('target_company', '')
|
| 32 |
+
if company_name:
|
| 33 |
+
company_col_name = f"company_{company_name}"
|
| 34 |
+
if company_col_name in df.columns:
|
| 35 |
+
df[company_col_name] = 1
|
| 36 |
+
|
| 37 |
+
final_df = df[model_columns]
|
| 38 |
+
prediction_proba = model.predict_proba(final_df)
|
| 39 |
+
match_probability = prediction_proba[0][1]
|
| 40 |
+
final_score = round(match_probability * 10)
|
| 41 |
+
|
| 42 |
+
return jsonify({'score': final_score})
|