0
0
NLPml~10 mins

Model serving for NLP - Interactive Code Practice

Choose your learning style9 modes available
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.