File size: 2,465 Bytes
8f36190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
import os
from shl_recommendation import SHLRecommendationEngine

app = Flask(__name__)

# Load the engine before starting the server
csv_path = os.path.join(os.path.dirname(__file__), 'shl_final_catalog.csv')
print("🚀 Loading recommendation engine...")
engine = SHLRecommendationEngine(csv_path='shl_final_catalog.csv', model_name='paraphrase-MiniLM-L3-v2')
print("✅ Engine loaded successfully.")

# Health Check Endpoint
@app.route('/health', methods=['GET'])
def health():
    return jsonify({"status": "healthy"}), 200

# Recommendation Endpoint
@app.route('/recommend', methods=['POST'])
def recommend():
    data = request.get_json()
    if not data or "query" not in data:
        return jsonify({"error": "Missing 'query' parameter"}), 400

    try:
        recommendations = engine.get_recommendations(data["query"], top_k=10)
        results = []

        for rec in recommendations.to_dict(orient='records'):
            # Handle duration
            duration_value = str(rec.get("duration", ""))
            duration_int = int(''.join(filter(str.isdigit, duration_value))) if any(c.isdigit() for c in duration_value) else 0

            # Handle test_type
            test_type_value = rec.get("test_type", "")
            if not isinstance(test_type_value, str):
                try:
                    from pandas import isna
                    if isna(test_type_value):
                        test_type_value = ""
                    else:
                        test_type_value = str(test_type_value)
                except ImportError:
                    test_type_value = str(test_type_value)

            test_type_array = [t.strip() for t in test_type_value.split(",") if t.strip()]

            results.append({
                "url": rec.get("url"),
                "adaptive_support": rec.get("adaptive_support"),
                "description": rec.get("assessment_name"),
                "duration": duration_int,
                "remote_support": rec.get("remote_testing_support"),
                "test_type": test_type_array
            })

        return jsonify({"recommended_assessments": results}), 200

    except Exception as e:
        import traceback
        print(traceback.format_exc())
        return jsonify({"error": str(e)}), 500

# Run locally on port 10000
if __name__ == '__main__':
    port = int(os.environ.get("PORT", 10000))
    app.run(host='0.0.0.0', port=port)