Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the model name in quotes.
Using the wrong class name.
✗ Incorrect
The model name must be a string, so it needs to be in quotes.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing raw text without quotes.
Passing a list of words instead of a string.
✗ Incorrect
The tokenizer expects a string input, so the text must be in quotes.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only input_ids instead of the whole inputs dictionary.
Using a wrong key like 'tokens' or 'tensor'.
✗ Incorrect
The model expects the whole inputs dictionary, not just input_ids, to handle attention masks and other inputs.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args instead of request.json.
Passing the whole inputs dictionary instead of input_ids.
✗ Incorrect
Use request.json to get JSON data and pass inputs['input_ids'] to the model.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'localhost' limits access to local machine only.
Setting debug to False during development.
✗ Incorrect
Use host '0.0.0.0' to allow external access, port 8080, and debug mode True for development.