0
0
NLPml~20 mins

Model serving for NLP - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Model serving for NLP
Problem:You have trained a text classification model that can predict the sentiment of movie reviews. Now, you want to make this model available so others can send text and get predictions instantly.
Current Metrics:Model accuracy on test data: 88%. Model is saved but not yet served.
Issue:The model is not accessible for real-time predictions. Users cannot send text inputs and receive sentiment predictions through an API.
Your Task
Create a simple web API to serve the NLP model so users can send text and get sentiment predictions instantly.
Use Python and Flask for the API.
Load the saved model from disk.
Accept POST requests with JSON containing a text field.
Return JSON with the predicted sentiment label.
Keep the API simple and easy to understand.
Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
NLP
from flask import Flask, request, jsonify
import joblib

app = Flask(__name__)

# Load the saved model
model = joblib.load('sentiment_model.joblib')

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    text = data.get('text', '')
    if not text:
        return jsonify({'error': 'No text provided'}), 400
    # Predict sentiment
    prediction = model.predict([text])[0]
    return jsonify({'sentiment': prediction})

if __name__ == '__main__':
    app.run(debug=True, port=5000)
Created a Flask web API with a /predict endpoint.
Loaded the saved NLP model using joblib.
Implemented POST request handling to receive text input.
Returned the model's sentiment prediction as JSON.
Added error handling for missing text input.
Results Interpretation

Before: Model was saved but not accessible for real-time use.

After: Model is served via a Flask API. Users can send text and get sentiment predictions immediately.

Serving a model through a simple web API makes it usable in real-world applications, allowing instant predictions on new data.
Bonus Experiment
Add input validation to check text length and reject empty or too long inputs.
💡 Hint
Use Python's len() function to check text length and return an error JSON if invalid.