|
|
import joblib |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
from flask import Flask, request, jsonify |
|
|
from flask_cors import CORS |
|
|
|
|
|
|
|
|
app = Flask("Pharmacy College Predictor") |
|
|
CORS(app) |
|
|
|
|
|
|
|
|
pipeline = joblib.load('xgb_pipeline_gpu.pkl') |
|
|
target_encoder = joblib.load('target_encoder.pkl') |
|
|
choice_code_map = pd.read_csv('choice_code_map.csv').set_index('Choice Code') |
|
|
|
|
|
|
|
|
@app.get('/') |
|
|
def home(): |
|
|
return "✅ Welcome to Pharmacy College Predictor API!" |
|
|
|
|
|
|
|
|
@app.post('/predict') |
|
|
def predict(): |
|
|
try: |
|
|
|
|
|
data = request.get_json() |
|
|
|
|
|
|
|
|
required_fields = ['Category', 'Rank', 'Percentage'] |
|
|
missing = [f for f in required_fields if f not in data] |
|
|
if missing: |
|
|
return jsonify({"error": f"Missing fields: {missing}"}), 400 |
|
|
|
|
|
|
|
|
sample_df = pd.DataFrame([{ |
|
|
'Category': data['Category'], |
|
|
'Rank': data['Rank'], |
|
|
'Percentage': data['Percentage'] |
|
|
}]) |
|
|
|
|
|
|
|
|
proba = pipeline.predict_proba(sample_df)[0] |
|
|
|
|
|
|
|
|
top_20_idx = np.argsort(proba)[::-1][:20] |
|
|
|
|
|
|
|
|
top_20_probs = proba[top_20_idx] |
|
|
top_20_probs_normalized = top_20_probs / top_20_probs.sum() * 100 |
|
|
|
|
|
results = [] |
|
|
for rank, (idx, prob) in enumerate(zip(top_20_idx, top_20_probs_normalized), start=1): |
|
|
choice_code = target_encoder.inverse_transform([idx])[0] |
|
|
row = choice_code_map.loc[int(choice_code)] |
|
|
college_name = row['College Name'] |
|
|
results.append({ |
|
|
"rank": rank, |
|
|
"choice_code": choice_code, |
|
|
"college_name": college_name, |
|
|
"probability_percent": round(float(prob), 2) |
|
|
}) |
|
|
|
|
|
return jsonify({"top_20_predictions": results}) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=False, host='0.0.0.0', port=7860) |
|
|
|