This is a sentiment analysis API built with Flask. It uses a logistic regression model trained on a cleaned version of the Sentiment140 dataset to classify Spanish input text as positive, negative, or neutral. The API also provides actionable recommendations based on the sentiment.
- Clean and prepare raw tweets using
utils/clean_data.py - Train a logistic regression model with TF-IDF vectorization
- Predict sentiment using a
/predictendpoint - Access model evaluation metrics via a
/metricsendpoint - Modular structure with scripts and models organized in folders
- Ready for local development with
pyenv
- Python 3.10.13 (recommended via
pyenv) pippackage manager
If you're using pyenv, the project includes a .python-version file:
pyenv install 3.10.13 # if not already installed
pyenv local 3.10.13- Clone the repository:
git clone https://github.com/JhymerMartinez/sentiment_analyzer.git
cd sentiment_analyzer- Create and activate a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt-
Download the original Sentiment140 dataset from here.
- File:
training.1600000.processed.noemoticon.csv - Place it inside the
data/folder.
- File:
-
Run the data cleaning script:
python utils/clean_data.pyThis will output a cleaned_dataset.csv file to the data/ directory.
Run the training script to train and save the model along with evaluation metrics:
python utils/train_model.pyThis will generate:
models/sentiment_model.pkl→ trained modelmetrics/model_metrics.json→ evaluation metrics
Start the Flask server:
python app.pyThe API will be available at: http://127.0.0.1:5000
Send text input and get back a sentiment prediction with a recommendation.
Example:
curl -X POST http://127.0.0.1:5000/predict \
-H "Content-Type: application/json" \
-d '{"text": "I like this product"}'Response:
{
"sentiment": "positive",
"recommendation": "Keep reinforcing what you're doing well."
}Fetch the stored model evaluation metrics:
curl http://127.0.0.1:5000/metricsExample Response:
{
"accuracy": 0.797,
"macro avg": {
"precision": 0.797,
"recall": 0.797,
"f1-score": 0.797
},
"weighted avg": {
"precision": 0.797,
"recall": 0.797,
"f1-score": 0.797
}
}sentiment_analyzer/
│
├── app.py
├── requirements.txt
├── .python-version
│
├── utils/
│ ├── clean_data.py
│ └── train_model.py
│
├── data/
│ ├── training.1600000.processed.noemoticon.csv
│ └── cleaned_dataset.csv
│
├── models/
│ └── sentiment_model.pkl
│
└── metrics/
└── model_metrics.json
- Make sure
data/,models/, andmetrics/directories exist before running the scripts. - The files
sentiment_model.pkl,model_metrics.json, andcleaned_dataset.csvare excluded from version control (see.gitignore).
MIT License