Bird
Raised Fist0
NLPml~10 mins

Model serving for NLP - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load a pre-trained NLP model using Hugging Face Transformers.

NLP
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained([1])
Drag options to blanks, or click blank then click option'
Abert-base-uncased
BAutoModel
C"bert-base-uncased"
DSequenceClassifier
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the model name in quotes.
Using the wrong class name.
2fill in blank
medium

Complete the code to tokenize input text for the NLP model.

NLP
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
inputs = tokenizer([1], return_tensors="pt")
Drag options to blanks, or click blank then click option'
Atokenizer
BHello, how are you?
C["Hello", "how", "are", "you"]
D"Hello, how are you?"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing raw text without quotes.
Passing a list of words instead of a string.
3fill in blank
hard

Fix the error in the code to get model predictions from tokenized inputs.

NLP
outputs = model([1])
predictions = outputs.logits.argmax(dim=1)
Drag options to blanks, or click blank then click option'
Ainputs['tokens']
Binputs
Cinputs.tensor
Dinputs['input_ids']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only input_ids instead of the whole inputs dictionary.
Using a wrong key like 'tokens' or 'tensor'.
4fill in blank
hard

Fill both blanks to create a simple Flask API endpoint that serves the NLP model predictions.

NLP
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.[1].get_json()
    inputs = tokenizer(data['text'], return_tensors='pt')
    outputs = model([2])
    pred = outputs.logits.argmax(dim=1).item()
    return jsonify({'prediction': pred})
Drag options to blanks, or click blank then click option'
Ajson
Binput_ids
Cargs
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args instead of request.json.
Passing the whole inputs dictionary instead of input_ids.
5fill in blank
hard

Fill all three blanks to add error handling and run the Flask app for serving the NLP model.

NLP
if __name__ == '__main__':
    try:
        app.run(host=[1], port=[2], debug=[3])
    except Exception as e:
        print(f"Error starting server: {e}")
Drag options to blanks, or click blank then click option'
A"0.0.0.0"
B8080
CTrue
D"localhost"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'localhost' limits access to local machine only.
Setting debug to False during development.

Practice

(1/5)
1. What is the main purpose of model serving in NLP?
easy
A. To visualize model training progress
B. To train NLP models faster
C. To collect more training data
D. To make NLP models available for real-time use

Solution

  1. Step 1: Understand model serving concept

    Model serving means making a trained NLP model ready to answer requests instantly.
  2. Step 2: Identify the main goal

    The goal is to provide real-time NLP results to apps or users, not training or data collection.
  3. Final Answer:

    To make NLP models available for real-time use -> Option D
  4. Quick Check:

    Model serving = real-time use [OK]
Hint: Model serving = ready for instant NLP predictions [OK]
Common Mistakes:
  • Confusing serving with training
  • Thinking serving collects data
  • Assuming serving is for visualization
2. Which of the following is the correct way to serve an NLP model using a Python Flask API?
easy
A. import Flask app = Flask(__name__) @app.route('/predict') def predict(): return 'Prediction result'
B. import flask app = flask() @app.route('/predict') def predict(): return 'Prediction result'
C. from flask import Flask app = Flask(__name__) @app.route('/predict') def predict(): return 'Prediction result'
D. from flask import Flask app = Flask() @app.route('/predict') def predict(): return 'Prediction result'

Solution

  1. Step 1: Check Flask import and app creation

    Correct import is from flask import Flask and app created by Flask(__name__).
  2. Step 2: Verify route decorator and function

    Route decorator @app.route('/predict') and function returning string is correct.
  3. Final Answer:

    Correct Flask API setup with proper import and app creation -> Option C
  4. Quick Check:

    Flask import and app = Flask(__name__) [OK]
Hint: Flask app needs Flask(__name__) and correct import [OK]
Common Mistakes:
  • Using wrong Flask import syntax
  • Missing __name__ in Flask()
  • Incorrect app creation call
3. Given this Flask code snippet serving an NLP sentiment model, what will be the output when accessing /predict?text=happy?
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict')
def predict():
    text = request.args.get('text')
    if 'happy' in text:
        sentiment = 'positive'
    else:
        sentiment = 'neutral'
    return jsonify({'sentiment': sentiment})
medium
A. {"sentiment": "positive"}
B. {"sentiment": "neutral"}
C. Error: Missing text parameter
D. 404 Not Found

Solution

  1. Step 1: Extract query parameter 'text'

    The URL provides text='happy', so text variable is 'happy'.
  2. Step 2: Check condition for sentiment

    Since 'happy' is in text, sentiment is set to 'positive'.
  3. Final Answer:

    {"sentiment": "positive"} -> Option A
  4. Quick Check:

    Text contains 'happy' -> positive sentiment [OK]
Hint: Check if 'happy' in text to decide sentiment [OK]
Common Mistakes:
  • Assuming neutral sentiment for 'happy'
  • Forgetting to pass text parameter
  • Confusing JSON string with Python dict
4. This Flask code for serving an NLP model throws an error. What is the bug?
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict')
def predict():
    text = request.args['text']
    sentiment = 'positive' if 'good' in text else 'negative'
    return jsonify(sentiment=sentiment)

if __name__ == '__main__':
    app.run()
medium
A. Missing return statement in predict function
B. Using request.args['text'] causes KeyError if 'text' missing
C. Flask app is not created properly
D. jsonify() cannot accept keyword arguments

Solution

  1. Step 1: Analyze request.args usage

    Using request.args['text'] raises KeyError if 'text' parameter is missing in URL.
  2. Step 2: Identify safer alternative

    Using request.args.get('text') avoids error by returning None if missing.
  3. Final Answer:

    Using request.args['text'] causes KeyError if 'text' missing -> Option B
  4. Quick Check:

    request.args['text'] can cause KeyError [OK]
Hint: Use request.args.get() to avoid KeyError [OK]
Common Mistakes:
  • Assuming request.args['text'] always exists
  • Thinking jsonify can't take keywords
  • Ignoring app creation correctness
5. You want to serve a summarization NLP model that sometimes returns empty summaries for very short texts. How can you improve the serving code to handle this edge case gracefully?
hard
A. Add a check to return the original text if the summary is empty
B. Always return an empty string for short texts
C. Raise an error when summary is empty
D. Ignore short texts and return null

Solution

  1. Step 1: Identify the problem with empty summaries

    Empty summaries confuse users and reduce usefulness for short texts.
  2. Step 2: Implement fallback logic

    Return the original text if the summary is empty to ensure meaningful output.
  3. Final Answer:

    Add a check to return the original text if the summary is empty -> Option A
  4. Quick Check:

    Fallback to original text if summary empty [OK]
Hint: Return original text if summary is empty to avoid blanks [OK]
Common Mistakes:
  • Returning empty string confuses users
  • Raising error breaks serving
  • Ignoring short texts causes bad UX