Skip to content

Commit

Permalink
feat: add reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Sep 19, 2023
1 parent 7c0122d commit d92f00f
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 157 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ capgen/__init__.py

*.md
*.pyc
*.toml
Dockerfile*

.github/
tests/
.vscode/
.venv/
.pytest_cache/
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: [main]
paths:
- .github/workflows/build.yml
- Caddyfile
- supervisord.conf
- Dockerfile.build
- pyproject.toml
- poetry.lock
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/warmer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
steps:
- name: cURL Swagger
run: |
[ ! "$(curl https://$GITHUB_REPOSITORY_OWNER-$REPOSITORY_NAME.hf.space/docs)" = "{\"detail\":\"Not Found\"}" ]
[ ! "$(curl https://$GITHUB_REPOSITORY_OWNER-$REPOSITORY_NAME.hf.space/api/docs)" = "{\"detail\":\"Not Found\"}" ]
- name: cURL the API
run: |
[ "$(curl https://$GITHUB_REPOSITORY_OWNER-$REPOSITORY_NAME.hf.space/v1/)" = "\"Welcome to v1 of the API!"\" ]
[ "$(curl https://$GITHUB_REPOSITORY_OWNER-$REPOSITORY_NAME.hf.space/api/v1/)" = "Welcome to v1 of the API!" ]
9 changes: 9 additions & 0 deletions Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:{$APP_PORT} {
handle_path /api/* {
reverse_proxy http://localhost:{$SERVER_PORT} {
transport http {
versions h2c
}
}
}
}
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM ghcr.io/winstxnhdw/capgen:main

ENV SERVER_PORT 7860
ENV SERVER_PORT 5000
ENV APP_PORT 7860
ENV OMP_NUM_THREADS 4

EXPOSE $SERVER_PORT
EXPOSE $APP_PORT
7 changes: 5 additions & 2 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ RUN apt update && \

FROM python:slim

RUN useradd -m -u 1000 user
RUN apt update && \
apt install -y supervisor caddy && \
rm -rf /var/lib/apt/lists/* && \
useradd -m -u 1000 user

ENV HOME=/home/user
ENV PYTHONUNBUFFERED=1
Expand All @@ -29,4 +32,4 @@ WORKDIR $HOME/app
COPY --from=builder --chown=user /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY . $HOME/app

CMD ["python", "main.py"]
CMD ["supervisord", "-c", "supervisord.conf"]
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ A fast CPU-first video/audio transcriber for generating caption files with [Whis
Simply cURL the endpoint like in the following. Currently, the only available caption format is `srt`.

```bash
curl "https://winstxnhdw-CapGen.hf.space/v1/transcribe?caption_format=$CAPTION_FORMAT" \
curl "https://winstxnhdw-CapGen.hf.space/api/v1/transcribe?caption_format=$CAPTION_FORMAT" \
-F "request=@$AUDIO_FILE_PATH"
```

You can also redirect the output to a file.

```bash
curl "https://winstxnhdw-CapGen.hf.space/v1/transcribe" \
curl "https://winstxnhdw-CapGen.hf.space/api/v1/transcribe" \
-F "request=@$AUDIO_FILE_PATH" | jq -r ".result" > result.srt
```

Expand Down Expand Up @@ -67,3 +67,18 @@ required:
-c, --caption the chosen caption file format
-o, --output the output file path
```

## Development

You can install the required dependencies for your editor with the following.

```bash
poetry install
```

You can spin the server up locally with the following. You can access the Swagger UI at [localhost:7860/api/docs](http://localhost:7860/api/docs).

```bash
docker build -f Dockerfile.build -t capgen .
docker run --rm -e SERVER_PORT=5000 -e APP_PORT=7860 -p 7860:7860 capgen
```
2 changes: 1 addition & 1 deletion capgen/transcriber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Transcriber:
base_options = TranscriberOptions(
model_size_or_path='guillaumekln/faster-whisper-large-v2',
compute_type='auto',
num_workers=4,
num_workers=2,
)

model = WhisperModel(**base_options, device='cpu')
Expand Down
336 changes: 193 additions & 143 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def initialise() -> Framework:
------
app (Framework) : an extended FastAPI instance
"""
app = Framework()
app = Framework(root_path='/api')
app.initialise_routes(join('server', 'api'))
app.include_router(v1)
app.add_middleware(
Expand Down
6 changes: 4 additions & 2 deletions server/api/v1/index.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Literal

from fastapi.responses import PlainTextResponse

from server.api.v1 import v1


@v1.get('/', response_model=Literal['Welcome to v1 of the API!'])
async def index():
def index():
"""
Summary
-------
the `/` route
"""
return 'Welcome to v1 of the API!'
return PlainTextResponse('Welcome to v1 of the API!')
2 changes: 1 addition & 1 deletion server/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def __init__(self, default_port: int = 49494):
self.accesslog = '-'
self.use_reloader = True
self.worker_class = 'uvloop'
self.workers = 4
self.workers = 2

super().__init__()
29 changes: 29 additions & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[supervisord]
nodaemon=true

[program:server]
command=python main.py
priority=1
startsecs=10
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_maxbytes=0
stderr_maxbytes=0
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
autostart=true
autorestart=true
process_name=%(program_name)s_%(process_num)02d

[program:caddy]
command=caddy run --config Caddyfile --adapter caddyfile
priority=2
startsecs=10
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_maxbytes=0
stderr_maxbytes=0
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
autostart=true
autorestart=true

0 comments on commit d92f00f

Please sign in to comment.