File size: 1,734 Bytes
3603ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from transformers import pipeline
import time

app = FastAPI(
    title="Sentiment Analysis API",
    description="Analyze text sentiment using transformers",
    version="1.0.0"
)

# Load model once at startup
print("Loading sentiment analysis model...")
sentiment_analyzer = pipeline(
    "sentiment-analysis",
    model="distilbert-base-uncased-finetuned-sst-2-english"
)
print("Model loaded!")

class TextRequest(BaseModel):
    text: str = Field(..., min_length=1, max_length=512, 
                     example="I love this product!")

class SentimentResponse(BaseModel):
    text: str
    sentiment: str
    confidence: float
    processing_time_ms: int

@app.get("/")
def root():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "service": "sentiment-api",
        "version": "1.0.0"
    }

@app.post("/analyze", response_model=SentimentResponse)
def analyze_sentiment(request: TextRequest):
    """
    Analyze sentiment of input text.
    Returns sentiment (POSITIVE/NEGATIVE) with confidence score.
    """
    start_time = time.time()
    
    try:
        # Run inference
        result = sentiment_analyzer(request.text)[0]
        
        processing_time = int((time.time() - start_time) * 1000)
        
        return SentimentResponse(
            text=request.text,
            sentiment=result['label'],
            confidence=round(result['score'], 4),
            processing_time_ms=processing_time
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/health")
def health():
    """Kubernetes-style health check"""
    return {"status": "ok"}