Skip to content

Commit 7008323

Browse files
committed
add monitoring, save new tweets in mongo db, add tests, prediction in worker in server
1 parent 5094ba2 commit 7008323

19 files changed

Lines changed: 636 additions & 314 deletions

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ FROM python:3.10-slim
22
RUN apt-get update && apt-get install -y procps
33
LABEL authors="shift"
44
WORKDIR /app
5-
COPY src .
65
COPY requirements.txt .
76
RUN pip install -r requirements.txt
7+
COPY src src/
8+
COPY scripts/entrypoint.sh .
9+
COPY supervisord.conf .
810
EXPOSE 5000
911
EXPOSE 5001
1012

11-
ENTRYPOINT "./entrypoint.sh"
13+
ENTRYPOINT ["./entrypoint.sh"]
-13.7 MB
Binary file not shown.
-4.5 MB
Binary file not shown.

data/tweets_test_train.csv

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
"0","1467810369","Mon Apr 06 22:19:45 PDT 2009","NO_QUERY","_TheSpecialOne_","@switchfoot http://twitpic.com/2y1zl - Awww, that's a bummer. You shoulda got David Carr of Third Day to do it. ;D"
2+
"4","1467822272","Mon Apr 06 22:22:45 PDT 2009","NO_QUERY","ersle","I LOVE @Health4UandPets u guys r the best!! "
23
"0","1467810672","Mon Apr 06 22:19:49 PDT 2009","NO_QUERY","scotthamilton","is upset that he can't update his Facebook by texting it... and might cry as a result School today also. Blah!"
4+
"4","1467822273","Mon Apr 06 22:22:45 PDT 2009","NO_QUERY","becca210","im meeting up with one of my besties tonight! Cant wait!! - GIRL TALK!!"
5+
"4","1467822287","Mon Apr 06 22:22:46 PDT 2009","NO_QUERY","katarinka","Being sick can be really cheap when it hurts too much to eat real food Plus, your friends make you soup"
36
"0","1467810917","Mon Apr 06 22:19:53 PDT 2009","NO_QUERY","mattycus","@Kenichan I dived many times for the ball. Managed to save 50% The rest go out of bounds"
47
"0","1467811184","Mon Apr 06 22:19:57 PDT 2009","NO_QUERY","ElleCTF","my whole body feels itchy and like its on fire "
58
"0","1467811193","Mon Apr 06 22:19:57 PDT 2009","NO_QUERY","Karoli","@nationwideclass no, it's not behaving at all. i'm mad. why am i here? because I can't see you all over there. "
9+
"4","1467822283","Mon Apr 06 22:22:46 PDT 2009","NO_QUERY","Wingman29","@DaRealSunisaKim Thanks for the Twitter add, Sunisa! I got to meet you once at a HIN show here in the DC area and you were a sweetheart. "
610
"0","1467811372","Mon Apr 06 22:20:00 PDT 2009","NO_QUERY","joy_wolf","@Kwesidei not the whole crew "
711
"0","1467811592","Mon Apr 06 22:20:03 PDT 2009","NO_QUERY","mybirch","Need a hug "
12+
"4","1467822391","Mon Apr 06 22:22:47 PDT 2009","NO_QUERY","ajarofalmonds","@ProductOfFear You can tell him that I just burst out laughing really loud because of that Thanks for making me come out of my sulk!"
813
"0","1467811594","Mon Apr 06 22:20:03 PDT 2009","NO_QUERY","coZZ","@LOLTrish hey long time no see! Yes.. Rains a bit ,only a bit LOL , I'm fine thanks , how's you ?"
914
"0","1467811795","Mon Apr 06 22:20:05 PDT 2009","NO_QUERY","2Hood4Hollywood","@Tatiana_K nope they didn't have it "
1015
"0","1467812025","Mon Apr 06 22:20:09 PDT 2009","NO_QUERY","mimismo","@twittera que me muera ? "
16+
"4","1467822293","Mon Apr 06 22:22:46 PDT 2009","NO_QUERY","_EmilyYoung","@LovesBrooklyn2 he has that effect on everyone "
1117
"0","1467812416","Mon Apr 06 22:20:16 PDT 2009","NO_QUERY","erinx3leannexo","spring break in plain city... it's snowing "
1218
"0","1467812579","Mon Apr 06 22:20:17 PDT 2009","NO_QUERY","pardonlauren","I just re-pierced my ears "
1319
"0","1467812723","Mon Apr 06 22:20:19 PDT 2009","NO_QUERY","TLeC","@caregiving I couldn't bear to watch it. And I thought the UA loss was embarrassing . . . . ."
20+
"4","1467822447","Mon Apr 06 22:22:51 PDT 2009","NO_QUERY","vmdavinci","@r_keith_hill Thans for your response. Ihad already find this answer "
21+
"4","1467822465","Mon Apr 06 22:22:48 PDT 2009","NO_QUERY","jessicavaliyi","@KeepinUpWKris I am so jealous, hope you had a great time in vegas! how did you like the ACM's?! LOVE YOUR SHOW!! "
1422
"0","1467812771","Mon Apr 06 22:20:19 PDT 2009","NO_QUERY","robrobbierobert","@octolinz16 It it counts, idk why I did either. you never talk to me anymore "
1523
"0","1467812784","Mon Apr 06 22:20:20 PDT 2009","NO_QUERY","bayofwolves","@smarrison i would've been the first, but i didn't have a gun. not really though, zac snyder's just a doucheclown."
24+
"4","1467822489","Mon Apr 06 22:22:49 PDT 2009","NO_QUERY","emmasaur28","@tommcfly ah, congrats mr fletcher for finally joining twitter "
1625
"0","1467812799","Mon Apr 06 22:20:20 PDT 2009","NO_QUERY","HairByJess","@iamjazzyfizzle I wish I got to watch it with you!! I miss you and @iamlilnicki how was the premiere?!"
1726
"0","1467812964","Mon Apr 06 22:20:22 PDT 2009","NO_QUERY","lovesongwriter","Hollis' death scene will hurt me severely to watch on film wry is directors cut not out now?"
18-
"0","1467813137","Mon Apr 06 22:20:25 PDT 2009","NO_QUERY","armotley","about to file taxes "
27+
"0","1467813137","Mon Apr 06 22:20:25 PDT 2009","NO_QUERY","armotley","about to file taxes "
28+
"4","1467822496","Mon Apr 06 22:22:49 PDT 2009","NO_QUERY","SherylBreuker","@e4VoIP I RESPONDED Stupid cat is helping me type. Forgive errors "

docker-compose.yml

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,76 @@ services:
44
container_name: sentimental_analyses_backend
55
ports:
66
- "5000:5000"
7-
- "5001:5001"
7+
- "5001:5001"
88
volumes:
9-
- ./mlflow:/app/mlruns/
10-
entrypoint: "./entrypoint.sh"
9+
- ./mlruns:/app/mlruns/
10+
- ./src:/app/src/
11+
stdin_open: true
12+
tty: true
13+
# entrypoint: "./entrypoint.sh"
1114
environment:
1215
- MLFLOW_TRACKING_URI=http://0.0.0.0:5001
1316
- HOST_DEPLOY = 0.0.0.0
1417

18+
prometheus:
19+
image: prom/prometheus:latest
20+
container_name: sentimental_analyses_prometheus
21+
ports:
22+
- "9090:9090"
23+
volumes:
24+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
25+
command:
26+
- --config.file=/etc/prometheus/prometheus.yml
27+
restart: unless-stopped
28+
29+
grafana:
30+
image: grafana/grafana:latest
31+
container_name: sentimental_analyses_grafana
32+
ports:
33+
- "3000:3000"
34+
depends_on:
35+
- loki
36+
volumes:
37+
- grafana-storage:/var/lib/grafana
38+
environment:
39+
- GF_SECURITY_ADMIN_PASSWORD=admin
40+
restart: unless-stopped
41+
42+
loki:
43+
image: grafana/loki:2.9.4
44+
container_name: sentimental_analyses_loki
45+
ports:
46+
- "3100:3100"
47+
command: -config.file=/etc/loki/local-config.yaml
48+
restart: unless-stopped
49+
50+
db:
51+
image: mongo:4.4
52+
container_name: sentimental_analyses_db
53+
ports:
54+
- "27017:27017"
55+
restart: unless-stopped
56+
57+
promtail:
58+
image: grafana/promtail:2.9.4
59+
container_name: sentimental_analyses_promtail
60+
volumes:
61+
- ./log:/var/log
62+
- ./promtail-config.yaml:/etc/promtail/config.yaml
63+
command: -config.file=/etc/promtail/config.yaml
64+
depends_on:
65+
- loki
66+
restart: unless-stopped
1567
nginx:
1668
image: nginx
1769
container_name: sentimental_analyses_nginx
1870
ports:
19-
- "80:80"
20-
- '443:443'
71+
- "81:80"
72+
- '444:443'
2173
volumes:
2274
- ./nginx.conf:/etc/nginx/conf.d/default.conf
2375
depends_on:
24-
- backend
76+
- backend
77+
78+
volumes:
79+
grafana-storage:

nginx.conf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11

2-
upstream backend {
3-
server backend:5000;
4-
}
5-
6-
upstream mlflow {
7-
server backend:5000;
8-
}
9-
102
server {
113
listen 80;
124
listen [::]:80;

prometheus.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
global:
2+
scrape_interval: 15s
3+
4+
scrape_configs:
5+
- job_name: 'fastapi-backend'
6+
static_configs:
7+
- targets: ['backend:5000']

promtail-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
server:
2+
http_listen_port: 9080
3+
grpc_listen_port: 0
4+
5+
positions:
6+
filename: /tmp/positions.yaml
7+
8+
clients:
9+
- url: http://loki:3100/loki/api/v1/push
10+
11+
scrape_configs:
12+
- job_name: system
13+
static_configs:
14+
- targets:
15+
- localhost
16+
labels:
17+
job: varlogs
18+
__path__: /var/log/*.log

scripts/entrypoint.sh

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ echo "start supervisor"
44
supervisord -c supervisord.conf
55

66
echo "start server"
7-
uvicorn server:app --host 0.0.0.0 --port 5000 --reload
7+
uvicorn src.server:app --host 0.0.0.0 --port 5000 --reload
88

src/mixins.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import mlflow
2+
import torch
3+
from tqdm import tqdm
4+
import gc
5+
import time
6+
import logging
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class TorchModelTrainMixin:
12+
"""
13+
Mixin to use with BaseModelABC
14+
"""
15+
16+
checkpoint: str = ""
17+
lr: float = 2e-5
18+
device: torch.device
19+
20+
def _train_batch(self, x, y):
21+
inputs = self.tokenizer(x, return_tensors="pt", truncation=True, padding=True)
22+
inputs.to(self.device)
23+
labels = y.to(self.device)
24+
self.optimizer.zero_grad()
25+
outputs = self.model(**inputs)
26+
try:
27+
loss = self.criterion(outputs.logits, labels)
28+
except AttributeError:
29+
loss = self.criterion(outputs, labels)
30+
loss.backward()
31+
self.optimizer.step()
32+
logger.info(loss.item())
33+
mlflow.log_metric("loss", loss.item())
34+
mlflow.log_metric("time", time.time())
35+
del inputs, labels, outputs, loss
36+
gc.collect()
37+
if torch.backends.mps.is_available():
38+
torch.mps.empty_cache()
39+
time.sleep(0.2)
40+
41+
def train(self):
42+
self.init_mlflow()
43+
self.model.train()
44+
self.model.to(self.device)
45+
for epoch in tqdm(range(self.epoch)):
46+
for tweets, labels in tqdm(self.dataloader):
47+
try:
48+
self._train_batch(tweets, labels.float())
49+
except RuntimeError as e:
50+
logger.error(e)
51+
del tweets, labels, self.optimizer
52+
gc.collect()
53+
if torch.backends.mps.is_available():
54+
torch.mps.empty_cache()
55+
time.sleep(0.2)
56+
self.save()
57+
self.load_checkpoint()
58+
self.model.to(self.device)
59+
self.model.train()
60+
continue
61+
if torch.backends.mps.is_available():
62+
logger.info(
63+
f"MPS allocated memory: {torch.mps.driver_allocated_memory()}"
64+
)
65+
if torch.cuda.is_available():
66+
logger.info(
67+
f"CUDA allocated memory: {torch.cuda.memory_allocated()}"
68+
)
69+
self.scheduler.step()
70+
self.save()
71+
super().train()
72+
73+
74+
class SklearnModelTrainMixin:
75+
"""
76+
Mixin to use with BaseModelABC
77+
"""
78+
79+
def train(self):
80+
self.model.fit(self.x_train, self.y_train)
81+
super().train()

0 commit comments

Comments
 (0)