Spaces:
Sleeping
Sleeping
File size: 7,346 Bytes
abab187 03fce4f 387ed32 03fce4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
---
license: apache-2.0
title: NegaBot
sdk: docker
emoji: π
colorFrom: blue
colorTo: pink
short_description: 'Product Negative Reviews detection & Analsis API. '
---
# NegaBot API
**Tweet Sentiment Classification using SmolLM 360M V2 Model**
NegaBot is a sentiment analysis API that detects positive and negative sentiment in tweets, particularly focusing on product criticism detection. Built with FastAPI and the `jatinmehra/NegaBot-Product-Criticism-Catcher` model.
## Features
- **Advanced AI Model**: Uses fine-tuned SmolLM 360M V2 for accurate sentiment classification; Trained on real tweets data and can detect negative comments with sarcasm.
- **Fast API**: RESTful API built with FastAPI for high-performance predictions
- **Data Logging**: SQLite database for storing and analyzing predictions
- **Batch Processing**: Support for single and batch predictions
- **Built-in Dashboard**: HTML analytics dashboard with charts
- **Data Export**: Download predictions as CSV or JSON
## Quick Start
1. **Install Dependencies**
```bash
pip install -r requirements.txt
```
2. **Start the API**
```bash
uvicorn api:app --host 0.0.0.0 --port 8000
```
3. **Access the Services**
- API Documentation: http://localhost:8000/docs
- Analytics Dashboard: http://localhost:8000/dashboard
## Usage Examples
### API Usage
#### Single Prediction
```bash
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{"text": "This product is amazing! Best purchase ever!"}'
```
#### Batch Prediction
```bash
curl -X POST "http://localhost:8000/batch_predict" \
-H "Content-Type: application/json" \
-d '{
"tweets": [
"Amazing product, highly recommend!",
"Terrible quality, waste of money",
"Its okay, nothing special"
]
}'
```
#### Python Client Example
```python
import requests
# Single prediction
response = requests.post(
"http://localhost:8000/predict",
json={"text": "This product broke after one week!"}
)
result = response.json()
print(f"Sentiment: {result['sentiment']} (Confidence: {result['confidence']:.2%})")
# Batch prediction
response = requests.post(
"http://localhost:8000/batch_predict",
json={
"tweets": [
"Love this product!",
"Terrible experience",
"Pretty decent quality"
]
}
)
results = response.json()
for result in results['results']:
print(f"'{result['text']}' -> {result['sentiment']}")
```
### Model Usage (Direct)
```python
from model import NegaBotModel
# Initialize model
model = NegaBotModel()
# Single prediction
result = model.predict("This product is awful and broke within a week!")
print(f"Sentiment: {result['sentiment']}")
print(f"Confidence: {result['confidence']:.2%}")
print(f"Probabilities: {result['probabilities']}")
# Batch prediction
texts = [
"Amazing quality, highly recommend!",
"Terrible customer service",
"Pretty good value for money"
]
results = model.batch_predict(texts)
for result in results:
print(f"{result['text']} -> {result['sentiment']}")
```
## API Endpoints
| Endpoint | Method | Description |
|----------|--------|-------------|
| `/` | GET | API information and available endpoints |
| `/health` | GET | Health check and model status |
| `/predict` | POST | Single tweet sentiment prediction |
| `/batch_predict` | POST | Batch tweet sentiment prediction |
| `/stats` | GET | Prediction statistics and analytics |
| `/dashboard` | GET | HTML analytics dashboard |
| `/dashboard/data` | GET | Dashboard data as JSON |
| `/download/predictions.csv` | GET | Download predictions as CSV |
| `/download/predictions.json` | GET | Download predictions as JSON |
### Request/Response Schemas
#### Predict Request
```json
{
"text": "string (1-1000 chars)",
"metadata": {
"optional": "metadata object"
}
}
```
#### Predict Response
```json
{
"text": "input text",
"sentiment": "Positive|Negative",
"confidence": 0.95,
"predicted_class": 0,
"probabilities": {
"positive": 0.95,
"negative": 0.05
},
"timestamp": "2024-01-01T12:00:00"
}
```
## Dashboard Features
The built-in analytics dashboard provides:
- **Real-time Metrics**: Total predictions, sentiment distribution, average confidence
- **Interactive Charts**: Pie charts showing sentiment distribution
- **Recent Predictions**: View latest prediction results
- **Data Export**: Download prediction data as CSV or JSON
- **Auto-refresh**: View updated statistics as new predictions are made
## Testing
Test the API using the interactive documentation at http://localhost:8000/docs or use curl commands as shown in the usage examples above.
## Project Structure
```
NegaBot-API/
βββ api.py # FastAPI application
βββ model.py # NegaBot model wrapper
βββ database.py # SQLite database and logging
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration
βββ README.md # This file
βββ negabot_predictions.db # Database (created at runtime)
```
## Configuration
The API runs on port 8000 by default. You can modify the host and port by updating the uvicorn command:
```bash
uvicorn api:app --host 127.0.0.1 --port 8080
```
## Model Information
- **Model**: `jatinmehra/NegaBot-Product-Criticism-Catcher`
- **Base Architecture**: SmolLM 360M V2
- **Task**: Binary sentiment classification
- **Classes**:
- 0: Positive sentiment
- 1: Negative sentiment (criticism/complaints)
- **Input**: Text (max 512 tokens)
- **Output**: Sentiment label + confidence scores
### Performance Considerations
- **Memory Requirements**: Model requires ~2GB RAM minimum
- **API Scaling**: Use multiple worker processes with Gunicorn for production
- **Database**: Current SQLite setup is suitable for development and small-scale production
## Logging and Monitoring
### Database Schema
```sql
CREATE TABLE predictions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
sentiment TEXT NOT NULL,
confidence REAL NOT NULL,
predicted_class INTEGER NOT NULL,
timestamp TEXT NOT NULL,
metadata TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
```
### Log Files
- Application logs: Console output
- Prediction logs: SQLite database
- Access logs: Uvicorn/Gunicorn logs
## Contributing
1. Fork the repository
2. Create a feature branch
3. Add tests for new features
4. Ensure all tests pass
5. Submit a pull request
## License
This project is licensed under the Apache-2.0 License - see the [LICENSE](LICENSE) file for details.
## Troubleshooting
### Common Issues
1. **Model Loading Errors**
- Ensure internet connection for downloading the model
- Check disk space (model is ~1.5GB)
- Verify transformers library version
2. **Port Conflicts**
- Change ports using command line arguments
- Check if port 8000 is already in use
3. **Database Permissions**
- Ensure write permissions in the project directory
- Check SQLite installation
4. **Memory Issues**
- Model requires ~2GB RAM minimum
- Consider using CPU-only inference for smaller systems
---
**Built with FastAPI and the powerful NegaBot model.**
Model used in this app-https://github.com/Jatin-Mehra119/NegaBot-Product-Criticism-Catcher |