The .NET frontend or backend sends a POST request to your Flask API endpoint:
POST http://localhost:5000/recommend
Content-Type: application/json
Example JSON body:
{
"age": 25,
"gender": "Male",
"country": "Egypt",
"is_new_muslim": "No",
"born_muslim": "Yes",
"education_level": "Bachelor",
"religious_level": "Moderate",
"preferred_topic": "Faith"
}Your Flask app has an endpoint defined like this:
@app.route("/recommend", methods=["POST"])
def recommend():
data = request.jsonThe incoming JSON is parsed and stored as a Python dictionary.
- The dictionary is converted into a DataFrame:
user_df = pd.DataFrame([data])- Then it's encoded and scaled using saved preprocessing tools (e.g., joblib-encoded encoders and scalers):
user_df["gender"] = gender_encoder.transform(user_df["gender"])
user_df_scaled = scaler.transform(user_df[numerical_features])- The saved model (e.g., TensorFlow/Keras) is loaded:
model = load_model("model.h5")
prediction = model.predict([user_inputs])- The model returns predictions for all books.
- The predicted scores are sorted and top N indices are selected:
top_indices = prediction[0].argsort()[::-1][:5]
recommended_books = books_df.iloc[top_indices]- Flask sends the selected recommendations back to the .NET client as JSON:
return jsonify({
"recommendations": recommended_books.to_dict(orient="records")
})| Step | Description |
|---|---|
| 1 | Receive POST request from .NET |
| 2 | Parse incoming JSON |
| 3 | Preprocess data (encode/scale) |
| 4 | Feed data into trained model |
| 5 | Get top-N predictions |
| 6 | Return recommendations as JSON |
This API is typically run locally for testing:
python app.py
Visit: http://localhost:5000/recommend
Make sure the following files exist:
model.h5(trained model)encoder.pkl,scaler.pkl(preprocessing tools)books.csvor any book metadata file