macrdel commited on
Commit
92678a7
1 Parent(s): 023f2e5

add nginx grafana workflows

Browse files
.github/deploy.yml ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Deploy to Hugging Face Spaces
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ test-and-deploy:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout code
14
+ uses: actions/checkout@v2
15
+
16
+ - name: Set up Docker Buildx
17
+ uses: docker/setup-buildx-action@v1
18
+
19
+ - name: Login to DockerHub
20
+ uses: docker/login-action@v1
21
+ with:
22
+ username: ${{ secrets.DOCKER_USERNAME }}
23
+ password: ${{ secrets.DOCKER_PASSWORD }}
24
+
25
+ - name: Run tests
26
+ run: |
27
+ docker build --target test -t macrdel/sentiment-summarize-youtube-comments-backend:latest .
28
+ docker run --rm macrdel/sentiment-summarize-youtube-comments-backend:latest pytest /tests
29
+
30
+ - name: Build Docker image
31
+ if: success()
32
+ run: docker build -t macrdel/sentiment-summarize-youtube-comments-backend:latest .
33
+
34
+ - name: Push Docker image
35
+ if: success()
36
+ run: docker push macrdel/sentiment-summarize-youtube-comments-backend:latest
37
+
38
+ - name: Deploy to Hugging Face Spaces
39
+ if: success()
40
+ env:
41
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
42
+ HF_USER: ${{ secrets.HF_USER }}
43
+ run: git push --force https://$HF_USER:$HF_TOKEN@huggingface.co/spaces/$HF_USER/sentiment-summarize-youtube-comms main
44
+
Dockerfile CHANGED
@@ -4,7 +4,7 @@ WORKDIR /code
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
 
7
- RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  RUN useradd -m -u 1000 user
10
  USER user
@@ -15,4 +15,4 @@ WORKDIR $HOME/app
15
 
16
  COPY --chown=user . $HOME/app
17
 
18
- CMD ["uvicorn", "app.api:app", "--host", "0.0.0.0", "--port", "7860"]
 
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
 
7
+ RUN apt-get update && apt-get install -y nginx && apt-get clean && pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  RUN useradd -m -u 1000 user
10
  USER user
 
15
 
16
  COPY --chown=user . $HOME/app
17
 
18
+ CMD ["sh", "-c", "service nginx start && uvicorn app.main:app --host 0.0.0.0 --port 8000"]
app/api.py CHANGED
@@ -5,6 +5,7 @@ from fastapi import FastAPI
5
  from pydantic import BaseModel
6
  # from transformers import pipeline
7
  # import uvicorn
 
8
  import pandas as pd
9
  import os
10
 
@@ -15,6 +16,7 @@ SENT_API_URL = f"https://api-inference.huggingface.co/models/{config.sentiment_m
15
  SUM_API_URL = f"https://api-inference.huggingface.co/models/{config.sum_model}"
16
 
17
  app = FastAPI()
 
18
 
19
  class YouTubeUrl(BaseModel):
20
  url_video: str
@@ -40,6 +42,10 @@ def get_summarize():
40
  if f"{config.NAME_DATA}" in os.listdir(f"{config.PATH_DATA}"):
41
  data = pd.read_csv(f"{config.DATA_FILE}")
42
  return pipeline_summarize(data['text_comment'], headers, SUM_API_URL)
 
 
 
 
43
 
44
 
45
  #if __name__ == '__main__':
 
5
  from pydantic import BaseModel
6
  # from transformers import pipeline
7
  # import uvicorn
8
+ from prometheus_client import start_http_server, Summary
9
  import pandas as pd
10
  import os
11
 
 
16
  SUM_API_URL = f"https://api-inference.huggingface.co/models/{config.sum_model}"
17
 
18
  app = FastAPI()
19
+ REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
20
 
21
  class YouTubeUrl(BaseModel):
22
  url_video: str
 
42
  if f"{config.NAME_DATA}" in os.listdir(f"{config.PATH_DATA}"):
43
  data = pd.read_csv(f"{config.DATA_FILE}")
44
  return pipeline_summarize(data['text_comment'], headers, SUM_API_URL)
45
+
46
+ @app.lifespan("startup")
47
+ def startup_event():
48
+ start_http_server(8000)
49
 
50
 
51
  #if __name__ == '__main__':
config/nginx.conf ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ upstream backend {
2
+ server app:8000;
3
+ }
4
+
5
+ server {
6
+ listen 80;
7
+
8
+ location / {
9
+ proxy_pass http://backend;
10
+ }
11
+ }
docker-compose-ci.yml ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.7'
2
+
3
+ services:
4
+ app:
5
+ build: .
6
+ container_name: sentiment-summarize-youtube-comments
7
+ ports:
8
+ - "8000:8000"
9
+ depends_on:
10
+ - prometheus
11
+ networks:
12
+ - custom
13
+
14
+ nginx:
15
+ image: nginx:latest
16
+ container_name: nginx
17
+ ports:
18
+ - "80:80"
19
+ volumes:
20
+ - ./config/nginx.conf:/etc/nginx/nginx.conf:ro
21
+ depends_on:
22
+ - app
23
+ networks:
24
+ - custom
25
+
26
+ prometheus:
27
+ image: prom/prometheus
28
+ container_name: prometheus
29
+ volumes:
30
+ - ./prometheus.yml:/etc/prometheus/prometheus.yml
31
+ ports:
32
+ - "9090:9090"
33
+ networks:
34
+ - custom
35
+
36
+ grafana:
37
+ image: grafana/grafana
38
+ container_name: grafana
39
+ ports:
40
+ - "3000:3000"
41
+ environment:
42
+ - GF_SECURITY_ADMIN_PASSWORD=admin
43
+ depends_on:
44
+ - prometheus
45
+ networks:
46
+ - custom
47
+
48
+ networks:
49
+ custom:
50
+ driver: bridge
prometheus.yml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ global:
2
+ scrape_interval: 15s
3
+
4
+ scrape_configs:
5
+ - job_name: 'sentiment-summarize-youtube-comments'
6
+ static_configs:
7
+ - targets: ['app:8000']
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
 
tests/test_main.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi.testclient import TestClient
2
+ from app.api import app
3
+
4
+ client = TestClient(app)
5
+
6
+ def test_read_root():
7
+ response = client.get("/")
8
+ assert response.status_code == 200
9
+ assert response.json() == {'message': 'FastAPI+HuggingFace app sentiment + summarize YouTube comments'}
10
+
11
+ #def test_get_comments():
12
+ # response = client.post("/comments", json={"url_video" : "https://www.youtube.com/watch?v=ITEfXK2J3Gw"})
13
+ # assert response.status_code == 200