Spaces:
Sleeping
Sleeping
File size: 10,034 Bytes
fbf40d5 b95ed7b fbf40d5 b95ed7b fbf40d5 b95ed7b fbf40d5 b95ed7b | 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | ---
title: SentimentAI
emoji: π
colorFrom: purple
colorTo: blue
sdk: docker
app_file: app.py
pinned: false
license: apache-2.0
short_description: "3-class sentiment analysis API β airzipm"
---
# π SentimentAI β RoBERTa Sentiment Analysis API
A production-grade, high-concurrency sentiment analysis API built on
[airzipm/sentiment-analysis-roberta](https://huggingface.co/airzipm/sentiment-analysis-roberta).
Classifies text into **Positive**, **Neutral**, or **Negative** with confidence scores.
---
## π Quick Start
### Live Demo
Open the Space URL in your browser β the frontend (`index.html`) loads automatically.
### Base URL
```
https://airzipm-sentimentai.hf.space
```
---
## π‘ API Endpoints
| Method | Path | Rate Limit | Description |
|--------|------|-----------|-------------|
| `GET` | `/` | β | Serve frontend `index.html` |
| `GET` | `/health` | β | Model readiness & server stats |
| `GET` | `/ping` | β | Lightweight liveness check |
| `POST` | `/analyze` | 30/min per IP | Single text sentiment analysis |
| `POST` | `/batch` | 10/min per IP | Batch analysis (up to 10 texts) |
| `GET` | `/docs` | β | Interactive Swagger UI |
| `GET` | `/redoc` | β | ReDoc API documentation |
---
## π Endpoint Reference
### `GET /ping`
Ultra-lightweight liveness probe. No inference. Always < 5ms.
Frontend pings this every 30 seconds to prevent the Space from sleeping.
**Response:**
```json
{
"pong": true,
"model_ready": true,
"t": 1717500000.123
}
```
---
### `GET /health`
Model readiness check with server statistics.
Always returns HTTP 200 β read the `status` field to check readiness.
**Response:**
```json
{
"status": "ok",
"model_loaded": true,
"device": "cpu",
"model_name": "airzipm/sentiment-analysis-roberta",
"uptime_s": 342.7,
"requests_served": 1284,
"version": "1.0.0"
}
```
| `status` value | Meaning |
|---------------|---------|
| `"ok"` | Model loaded, ready to serve |
| `"loading"` | Model is still initializing (cold start) |
| `"error"` | Model failed to load (check logs) |
---
### `POST /analyze`
Analyze sentiment of a single text. HuggingFace Inference API compatible.
**Rate limit:** 30 requests per IP per minute
**Request body:**
```json
{
"inputs": "This movie was absolutely amazing!"
}
```
**Successful response (200):**
```json
{
"label": "Positive",
"score": 0.973241,
"all_scores": [
{"label": "Positive", "score": 0.973241},
{"label": "Neutral", "score": 0.021034},
{"label": "Negative", "score": 0.005725}
],
"response_ms": 87
}
```
**Error responses:**
| Status | When | Body |
|--------|------|------|
| 400 | Text empty or > 2000 chars | `{"detail": "validation error..."}` |
| 429 | Rate limit hit | `{"detail": "Rate limit exceeded"}` |
| 503 | Model still loading | `{"detail": "Model is still loading..."}` |
| 500 | Unexpected error | `{"detail": "Inference failed..."}` |
---
### `POST /batch`
Analyze up to 10 texts in a single efficient request.
All texts are processed in one RoBERTa forward pass (padded batch).
**Rate limit:** 10 requests per IP per minute (batch is more expensive)
**Request body:**
```json
{
"inputs": [
"I loved this product!",
"The service was average.",
"Absolutely terrible experience."
]
}
```
**Successful response (200):**
```json
{
"results": [
{
"label": "Positive",
"score": 0.971203,
"all_scores": [
{"label": "Positive", "score": 0.971203},
{"label": "Neutral", "score": 0.022411},
{"label": "Negative", "score": 0.006386}
],
"response_ms": 134
},
{
"label": "Neutral",
"score": 0.683441,
"all_scores": [
{"label": "Neutral", "score": 0.683441},
{"label": "Positive", "score": 0.201233},
{"label": "Negative", "score": 0.115326}
],
"response_ms": 134
},
{
"label": "Negative",
"score": 0.941872,
"all_scores": [
{"label": "Negative", "score": 0.941872},
{"label": "Neutral", "score": 0.042311},
{"label": "Positive", "score": 0.015817}
],
"response_ms": 134
}
],
"batch_size": 3,
"total_ms": 148
}
```
---
## π₯οΈ Code Examples
### JavaScript (Fetch API)
```javascript
const API = "https://airzipm-sentimentai.hf.space";
// ββ Single analysis ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function analyze(text) {
const response = await fetch(`${API}/analyze`, {
method : "POST",
headers: { "Content-Type": "application/json" },
body : JSON.stringify({ inputs: text }),
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.detail || "Request failed");
}
return response.json();
// β { label: "Positive", score: 0.973, all_scores: [...], response_ms: 87 }
}
// ββ Batch analysis βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function analyzeBatch(texts) {
const response = await fetch(`${API}/batch`, {
method : "POST",
headers: { "Content-Type": "application/json" },
body : JSON.stringify({ inputs: texts }),
});
return response.json();
// β { results: [...], batch_size: 3, total_ms: 148 }
}
// ββ Keep-alive ping (prevents HF Space from sleeping) βββββββββββββββββββββ
setInterval(async () => {
const { model_ready } = await fetch(`${API}/ping`).then(r => r.json());
console.log("Model ready:", model_ready);
}, 30_000);
```
### Python (httpx)
```python
import httpx
API = "https://airzipm-sentimentai.hf.space"
# Single text
response = httpx.post(f"{API}/analyze", json={"inputs": "This is great!"})
result = response.json()
print(result["label"], result["score"]) # β Positive 0.973
# Batch
batch_resp = httpx.post(f"{API}/batch", json={
"inputs": ["Loved it!", "Meh.", "Terrible."]
})
for r in batch_resp.json()["results"]:
print(r["label"], r["score"])
```
### cURL
```bash
# Single analysis
curl -X POST "https://airzipm-sentimentai.hf.space/analyze" \
-H "Content-Type: application/json" \
-d '{"inputs": "This product completely exceeded my expectations!"}'
# Batch analysis
curl -X POST "https://airzipm-sentimentai.hf.space/batch" \
-H "Content-Type: application/json" \
-d '{"inputs": ["Amazing!", "It was okay.", "Terrible experience."]}'
# Health check
curl "https://airzipm-sentimentai.hf.space/health"
# Ping
curl "https://airzipm-sentimentai.hf.space/ping"
```
---
## β‘ Architecture & Concurrency
### How it handles multiple simultaneous users
```
User 1 β acquires semaphore slot 1 β running inference (~100ms)
User 2 β acquires semaphore slot 2 β running inference
User 3 β acquires semaphore slot 3 β running inference
User 4 β acquires semaphore slot 4 β running inference
User 5 β WAITS in asyncio queue (non-blocking β event loop stays free)
User 6 β WAITS in asyncio queue
...
User 20 β WAITS in asyncio queue
When User 1 finishes:
β releases slot
β User 5 immediately acquires it and begins inference
```
**No requests are dropped or rejected.** The asyncio event loop stays free to
accept new connections and serve `/ping` responses while inferences run.
### Component Breakdown
| Component | Role |
|-----------|------|
| **FastAPI** | Async request routing, Pydantic validation |
| **Gunicorn + 4 UvicornWorkers** | Multi-process concurrency (~100+ connections) |
| **asyncio.Semaphore(4)** | Caps simultaneous inferences to prevent OOM |
| **loop.run_in_executor** | Runs CPU-bound inference off the event loop |
| **slowapi** | Per-IP rate limiting (in-memory, no Redis) |
| **RoBERTa (loaded once)** | Weights in RAM at startup β never reloaded per request |
### Response Headers
Every response includes:
| Header | Value | Use |
|--------|-------|-----|
| `X-Response-Time` | `87ms` | Frontend displays this |
| `X-Model-Ready` | `true` / `false` | Frontend status bar |
| `Access-Control-Allow-Origin` | `*` | CORS β any frontend domain |
---
## π§ Local Development
```bash
# Clone and install
git clone https://huggingface.co/spaces/airzipm/sentimentai
cd sentimentai
pip install -r requirements.txt
# Run locally (single worker, port 7860)
python app.py
# Or with gunicorn (production-like, 4 workers)
gunicorn app:app \
--workers 4 \
--worker-class uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:7860 \
--timeout 120
# Visit: http://localhost:7860
# Swagger: http://localhost:7860/docs
```
---
## β οΈ Cold Start Note
This Space runs on the **free tier**, which means:
- The Space **sleeps** after ~5 minutes of inactivity
- The first request after sleeping triggers a **cold start** (20β50 seconds)
- The model is downloaded (~500MB) and loaded into RAM on first start
- The frontend handles this gracefully with a "warming up" status bar
---
## π License
Apache 2.0 β see [LICENSE](LICENSE)
Model: [airzipm/sentiment-analysis-roberta](https://huggingface.co/airzipm/sentiment-analysis-roberta)
---
<!--
OPTION A β Gradio SDK wrapper (simpler, no Dockerfile needed)
If you prefer not to use Docker, you can wrap FastAPI with Gradio:
README frontmatter:
sdk: gradio
sdk_version: "4.36.1"
Add to requirements.txt:
gradio==4.36.1
Add to bottom of app.py (before uvicorn.run):
import gradio as gr
gr_app = gr.Blocks(title="SentimentAI")
with gr_app:
gr.HTML("""
<h2>π SentimentAI API</h2>
<p>REST API is running. Visit <a href="/docs">/docs</a> for Swagger UI
or use the <a href="/">frontend</a>.</p>
""")
app = gr.mount_gradio_app(app, gr_app, path="/ui")
Tradeoff: Option A is simpler but has Gradio overhead.
Option B (Dockerfile, used above) is cleaner and production-grade.
--> |