nagaananth commited on
Commit
eead084
·
1 Parent(s): 1b7a941

Level 2: Integrated real Sentiment Analysis model

Browse files
Files changed (3) hide show
  1. Dockerfile +5 -5
  2. main.py +13 -5
  3. requirements.txt +3 -1
Dockerfile CHANGED
@@ -1,15 +1,15 @@
1
- # Step 1: Use a lightweight Python base image
2
  FROM python:3.9-slim
3
 
4
- # Step 2: Set the working directory inside the container
5
  WORKDIR /app
6
 
7
- # Step 3: Copy your requirements and install them
8
  COPY requirements.txt .
9
  RUN pip install --no-cache-dir -r requirements.txt
10
 
11
- # Step 4: Copy the rest of your application code
 
 
 
12
  COPY . .
13
 
14
- # Step 5: Command to run the API using Uvicorn
15
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
 
1
  FROM python:3.9-slim
2
 
 
3
  WORKDIR /app
4
 
5
+ # Install dependencies first (to cache this layer)
6
  COPY requirements.txt .
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
9
+ # Pre-download the model so it's baked into the image
10
+ # This prevents the container from downloading it at runtime
11
+ RUN python -c "from transformers import pipeline; pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english')"
12
+
13
  COPY . .
14
 
 
15
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
main.py CHANGED
@@ -1,14 +1,22 @@
1
  from fastapi import FastAPI
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
5
  @app.get("/")
6
  def home():
7
- return {"status": "Mastered Level 1", "engine": "FastAPI", "mode": "Docker"}
8
 
9
  @app.post("/predict")
10
  def predict(data: dict):
11
- # This is where your model logic would go later
12
- features = data.get("features", [])
13
- prediction = sum(features) # Dummy logic: summing numbers
14
- return {"prediction": prediction}
 
 
 
 
1
  from fastapi import FastAPI
2
+ from transformers import pipeline
3
 
4
  app = FastAPI()
5
 
6
+ # Load the model once when the server starts
7
+ # This is "Sentiment Analysis" by default
8
+ classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
9
+
10
  @app.get("/")
11
  def home():
12
+ return {"status": "Level 2 Live", "model": "DistilBERT Sentiment"}
13
 
14
  @app.post("/predict")
15
  def predict(data: dict):
16
+ text = data.get("text", "")
17
+ if not text:
18
+ return {"error": "No text provided"}
19
+
20
+ # Run the model
21
+ result = classifier(text)
22
+ return {"input": text, "result": result[0]}
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  fastapi
2
- uvicorn
 
 
 
1
  fastapi
2
+ uvicorn
3
+ transformers
4
+ torch