Syed Arfan commited on
Commit
3603ded
·
1 Parent(s): ca75e40

Add Docker containerization support

Browse files

- Create Dockerfile with Python 3.11 base image
- Add .dockerignore to exclude unnecessary files
- Configure FastAPI app for containerized deployment
- Add sentiment analysis endpoint with DistilBERT

Files changed (4) hide show
  1. .dockerignore +36 -0
  2. Dockerfile +24 -0
  3. requirements.txt +7 -0
  4. src/main.py +65 -0
.dockerignore ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ *.pyd
6
+ .Python
7
+
8
+ # Virtual environment
9
+ .venv/
10
+ venv/
11
+ ENV/
12
+
13
+ # Git
14
+ .git/
15
+ .gitignore
16
+
17
+ # IDE
18
+ .vscode/
19
+ .idea/
20
+ *.swp
21
+ *.swo
22
+
23
+ # OS
24
+ .DS_Store
25
+ Thumbs.db
26
+
27
+ # Logs
28
+ *.log
29
+
30
+ # Test
31
+ .pytest_cache/
32
+ .coverage
33
+
34
+ # Documentation (not needed in container)
35
+ README.md
36
+ docs/
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base image with Python pre-installed
2
+ FROM python:3.11-slim
3
+
4
+ # Set working directory inside container
5
+ WORKDIR /app
6
+
7
+ # Copy requirements first (for layer caching)
8
+ COPY requirements.txt .
9
+
10
+ # Install Python dependencies
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy application code
14
+ COPY src/ ./src/
15
+
16
+ # Document the port (informational)
17
+ EXPOSE 8000
18
+
19
+ # Health check for production monitoring
20
+ HEALTHCHECK --interval=30s --timeout=3s --start-period=40s \
21
+ CMD curl -f http://localhost:8000/health || exit 1
22
+
23
+ # Command to run when container starts
24
+ CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
requirements.txt CHANGED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.115.5
2
+ uvicorn[standard]==0.32.1
3
+ transformers==4.46.3
4
+ torch==2.5.1
5
+ pydantic==2.10.3
6
+ pytest==8.3.4
7
+ httpx==0.28.1
src/main.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel, Field
3
+ from transformers import pipeline
4
+ import time
5
+
6
+ app = FastAPI(
7
+ title="Sentiment Analysis API",
8
+ description="Analyze text sentiment using transformers",
9
+ version="1.0.0"
10
+ )
11
+
12
+ # Load model once at startup
13
+ print("Loading sentiment analysis model...")
14
+ sentiment_analyzer = pipeline(
15
+ "sentiment-analysis",
16
+ model="distilbert-base-uncased-finetuned-sst-2-english"
17
+ )
18
+ print("Model loaded!")
19
+
20
+ class TextRequest(BaseModel):
21
+ text: str = Field(..., min_length=1, max_length=512,
22
+ example="I love this product!")
23
+
24
+ class SentimentResponse(BaseModel):
25
+ text: str
26
+ sentiment: str
27
+ confidence: float
28
+ processing_time_ms: int
29
+
30
+ @app.get("/")
31
+ def root():
32
+ """Health check endpoint"""
33
+ return {
34
+ "status": "healthy",
35
+ "service": "sentiment-api",
36
+ "version": "1.0.0"
37
+ }
38
+
39
+ @app.post("/analyze", response_model=SentimentResponse)
40
+ def analyze_sentiment(request: TextRequest):
41
+ """
42
+ Analyze sentiment of input text.
43
+ Returns sentiment (POSITIVE/NEGATIVE) with confidence score.
44
+ """
45
+ start_time = time.time()
46
+
47
+ try:
48
+ # Run inference
49
+ result = sentiment_analyzer(request.text)[0]
50
+
51
+ processing_time = int((time.time() - start_time) * 1000)
52
+
53
+ return SentimentResponse(
54
+ text=request.text,
55
+ sentiment=result['label'],
56
+ confidence=round(result['score'], 4),
57
+ processing_time_ms=processing_time
58
+ )
59
+ except Exception as e:
60
+ raise HTTPException(status_code=500, detail=str(e))
61
+
62
+ @app.get("/health")
63
+ def health():
64
+ """Kubernetes-style health check"""
65
+ return {"status": "ok"}