-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/docs: deprecate
v2
and introduce v3
The `v3` route has larger scaling capability. We no longer expose our batching capability at the Python level. Instead, we let `FastAPI` handle batching via multithreading. This works because `ctranslate2` drops the GIL when `translate_iterable` is called, therefore the entire operation can be asynchronous. Now batching is simplified to just making multiple API requests.
- Loading branch information
1 parent
cca5e2d
commit 42c0a17
Showing
13 changed files
with
66 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from server.api.v2 import v2 as v2 | ||
from server.api.v3 import v3 as v3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
from typing import Annotated, Generator | ||
from asyncio import gather | ||
|
||
from fastapi import Depends | ||
from starlette.responses import StreamingResponse | ||
from starlette.responses import PlainTextResponse | ||
|
||
from server.api.v2 import v2 | ||
from server.dependencies import translation | ||
from server.features import Translator | ||
from server.schemas.v1 import Translation | ||
|
||
|
||
@v2.post('/translate') | ||
def translate(result: Annotated[Generator[str, None, None], Depends(translation)]): | ||
@v2.post('/translate', deprecated=True) | ||
async def translate(request: Translation) -> PlainTextResponse: | ||
""" | ||
Summary | ||
------- | ||
the `/translate` route translates an input from a source language to a target language | ||
""" | ||
return StreamingResponse(result, media_type='text/event-stream') | ||
results = await gather( | ||
*(Translator.translate(line, request.source, request.target) for line in request.text.splitlines() if line) | ||
) | ||
|
||
return PlainTextResponse('\n'.join(results)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from fastapi import APIRouter | ||
|
||
v3 = APIRouter(prefix='/v3', tags=["v3"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Literal | ||
|
||
from starlette.responses import PlainTextResponse | ||
|
||
from server.api.v3 import v3 | ||
|
||
|
||
@v3.get('/', response_model=Literal['Welcome to v3 of the API!']) | ||
def index(): | ||
""" | ||
Summary | ||
------- | ||
the `/` route | ||
""" | ||
return PlainTextResponse('Welcome to v2 of the API!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from server.api.v3 import v3 | ||
from server.features import Translator | ||
from server.schemas.v1 import Translated, Translation | ||
|
||
|
||
@v3.post('/translate') | ||
async def translate(request: Translation) -> Translated: | ||
""" | ||
Summary | ||
------- | ||
the `/translate` route translates an input from a source language to a target language | ||
""" | ||
return Translated(result=await Translator.translate(request.text, request.source, request.target)) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters