-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (75 loc) · 2.73 KB
/
Copy pathmain.py
File metadata and controls
87 lines (75 loc) · 2.73 KB
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
import logging
import os
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI, status
from fastapi.responses import RedirectResponse
from app.core.config import executor, settings
from app.services.analyzer import analyzer_service
from app.routes.api import router
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager to handle startup and shutdown events."""
logging.basicConfig(
level=settings.LOG_LEVEL,
format=settings.LOG_FORMAT,
)
logger.info("Loading models...")
analyzer_service.load_models()
logger.info("Models loaded successfully!")
yield
logger.info("Shutting down...")
analyzer_service.unload_models()
executor.shutdown(wait=True)
app = FastAPI(
title="Sentiment Analysis API",
description="""
## Advanced NLP Sentiment Analysis API
This API provides comprehensive text analysis capabilities including:
* **Sentiment Analysis** - Detect positive, negative, or neutral sentiment
* **Emotion Detection** - Identify emotions like joy, sadness, anger, fear, etc.
* **Hate Speech Detection** - Flag potentially harmful or offensive content
* **Irony Detection** - Detect sarcastic or ironic statements
* **Named Entity Recognition (NER)** - Extract entities like persons, organizations, locations
* **Part-of-Speech (POS) Tagging** - Identify grammatical components
* **Targeted Sentiment Analysis** - Analyze sentiment towards specific entities
### Features
- 🚀 Fast and scalable inference using thread pool executors
- 🧠 Multiple pre-trained NLP models
- 🌍 Multi-language support (Spanish by default, configurable)
- ⚡ Async API with automatic model loading/unloading
- 📊 Comprehensive analysis results
### Usage
Send a POST request to `/analyze` with your text and configuration options.
Check `/health` endpoint to verify service status.
""",
version="1.0.0",
contact={
"name": "API Support",
"email": "gonzalozanelli1+support@gmail.com",
},
license_info={
"name": "GPLv3 License",
"url": "https://opensource.org/licenses/GPL-3.0",
},
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json",
lifespan=lifespan,
)
app.include_router(router)
@app.get("/", include_in_schema=False)
async def root():
"""Redirect root to API documentation."""
return RedirectResponse(url="/docs", status_code=status.HTTP_308_PERMANENT_REDIRECT)
if __name__ == "__main__":
workers = os.cpu_count() or 1
logger.info("Starting uvicorn with %s workers", workers)
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
workers=1,
reload=settings.ENVIRONMENT == "development"
)