Prerequisites
Before installing the microservice, ensure you have:- Python 3.7+ - The service uses modern Python features and type hints
- pip - Python package installer (usually bundled with Python)
- Git (optional) - For cloning the repository
- 4GB+ RAM - Required for loading PyTorch models and NLTK data
Installation steps
Create virtual environment
Set up an isolated Python environment to avoid dependency conflicts:This creates a new directory You should see
text_prediction/ containing the virtual environment.Activate the environment:(text_prediction) prefix in your terminal prompt.Install dependencies
Install all required Python packages from This installs 89 packages including:
requirements.txt:| Package | Version | Purpose |
|---|---|---|
| torch | 1.13.0 | Neural network framework for emotion classification |
| Flask | 2.2.2 | Web framework for the REST API endpoint |
| nltk | 3.7 | Natural language processing and text preprocessing |
| scikit-learn | 1.0.1 | Traditional ML classifier for sentiment analysis |
| pycountry | 22.3.5 | Country name extraction from text |
| numpy | 1.23.5 | Numerical computing for tensor operations |
| pandas | 1.5.2 | Data manipulation utilities |
The installation downloads PyTorch binaries which can take several minutes depending on your connection speed.
Download NLTK data
The microservice automatically downloads required NLTK resources on first run:These downloads happen in microservice.py:91-92 when you start the server. No manual action needed.
Verify model files
Ensure the pre-trained model files exist in the These files are loaded during initialization (microservice.py:36-42):
models/ directory:Run the microservice
Start the Flask development server:Expected output:The server runs on:
- Host: 127.0.0.1 (localhost only, not accessible from network)
- Port: 3200
Configuration options
Change server port
To run the service on a different port, modifyPORT in microservice.py:29:
Enable network access
To accept requests from other machines, change the host:Adjust toxicity threshold
The default threshold for toxicity classification is 0.29 (microservice.py:206-211). To modify sensitivity:Troubleshooting
Missing model files
Error:FileNotFoundError: [Errno 2] No such file or directory: 'models/word_dict.json'
Solution: Ensure all four model files exist in the models/ directory before starting the server.
NLTK data download fails
Error:Resource omw-1.4 not found
Solution: Manually download NLTK data:
Port already in use
Error:OSError: [Errno 48] Address already in use
Solution: Either stop the process using port 3200 or change the PORT variable in microservice.py.
PyTorch CPU warnings
Warning:UserWarning: CUDA not available, using CPU
Solution: This is expected behavior. The model loads with map_location=torch.device('cpu') (microservice.py:73) for CPU inference.
Next steps
Quickstart guide
Make your first prediction request and understand the response format
Model architecture
Learn how the neural network and classifiers work together