Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from flask_cors import CORS
|
| 6 |
+
|
| 7 |
+
# Initialize Flask app
|
| 8 |
+
app = Flask("Engineering College General Predictor")
|
| 9 |
+
CORS(app)
|
| 10 |
+
|
| 11 |
+
# Load trained model & helpers
|
| 12 |
+
pipeline = joblib.load('pipeline.pkl')
|
| 13 |
+
target_encoder = joblib.load('target_encoder.pkl')
|
| 14 |
+
choice_code_map = pd.read_csv('choice_code_map.csv',index_col='Choice Code')
|
| 15 |
+
|
| 16 |
+
# Home route
|
| 17 |
+
@app.get('/')
|
| 18 |
+
def home():
|
| 19 |
+
return "✅ Welcome to Engineering College Predictor API!"
|
| 20 |
+
|
| 21 |
+
# Predict route
|
| 22 |
+
@app.post('/predict')
|
| 23 |
+
def predict():
|
| 24 |
+
try:
|
| 25 |
+
# Parse input JSON
|
| 26 |
+
data = request.get_json()
|
| 27 |
+
|
| 28 |
+
# Validate input
|
| 29 |
+
required_fields = ['Category', 'Rank', 'Percentage', 'Course Name']
|
| 30 |
+
missing = [f for f in required_fields if f not in data]
|
| 31 |
+
if missing:
|
| 32 |
+
return jsonify({"error": f"Missing fields: {missing}"}), 400
|
| 33 |
+
|
| 34 |
+
# Build DataFrame
|
| 35 |
+
sample_df = pd.DataFrame([{
|
| 36 |
+
'Category': data['Category'],
|
| 37 |
+
'Rank': data['Rank'],
|
| 38 |
+
'Percentage': data['Percentage']
|
| 39 |
+
'Course Name' : data['Course Name']
|
| 40 |
+
}])
|
| 41 |
+
|
| 42 |
+
# Predict probabilities
|
| 43 |
+
proba = pipeline.predict_proba(sample_df)[0]
|
| 44 |
+
|
| 45 |
+
# Get top-20 indices (highest probabilities)
|
| 46 |
+
top_20_idx = np.argsort(proba)[::-1][:20]
|
| 47 |
+
|
| 48 |
+
# Normalize top-20 probs to sum to 100
|
| 49 |
+
top_20_probs = proba[top_20_idx]
|
| 50 |
+
top_20_probs_normalized = top_20_probs / top_20_probs.sum() * 100
|
| 51 |
+
|
| 52 |
+
results = []
|
| 53 |
+
for rank, (idx, prob) in enumerate(zip(top_20_idx, top_20_probs_normalized), start=1):
|
| 54 |
+
choice_code = target_encoder.inverse_transform([idx])[0]
|
| 55 |
+
row = choice_code_map.loc[int(choice_code)]
|
| 56 |
+
college_name = row['College Name']
|
| 57 |
+
course_name = row['Course Name']
|
| 58 |
+
results.append({
|
| 59 |
+
"rank": rank,
|
| 60 |
+
"choice_code": choice_code,
|
| 61 |
+
"college_name": college_name,
|
| 62 |
+
"course name" : course_name,
|
| 63 |
+
"probability_percent": round(float(prob), 2)
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
return jsonify({"top_20_predictions": results})
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return jsonify({"error": str(e)}), 500
|
| 70 |
+
|
| 71 |
+
# Run server
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
app.run(debug=False, host='0.0.0.0', port=7860)
|