Skip to content

Commit

Permalink
update cors pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedir Zadniprovskyi authored and fedirz committed Aug 10, 2024
1 parent a2aafe4 commit 7691978
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 7 additions & 0 deletions faster_whisper_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ class Config(BaseSettings):
log_level: str = "info"
host: str = Field(alias="UVICORN_HOST", default="0.0.0.0")
port: int = Field(alias="UVICORN_PORT", default=8000)
allow_origins: list[str] | None = None
"""
https://docs.pydantic.dev/latest/concepts/pydantic_settings/#parsing-environment-variable-values
Usage:
`export ALLOW_ORIGINS='["http://localhost:3000", "http://localhost:3001"]'`
`export ALLOW_ORIGINS='["*"]'`
"""

enable_ui: bool = True
"""
Expand Down
7 changes: 3 additions & 4 deletions faster_whisper_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
WebSocket,
WebSocketDisconnect,
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from fastapi.websockets import WebSocketState
from faster_whisper import WhisperModel
Expand Down Expand Up @@ -77,18 +78,16 @@ def load_model(model_name: str) -> WhisperModel:

app = FastAPI()

cors_origins = os.environ.get("CORS_ORIGINS", "").split(",")
if cors_origins:
if config.allow_origins is not None:
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins,
allow_origins=config.allow_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)



@app.get("/health")
def health() -> Response:
return Response(status_code=200, content="OK")
Expand Down

0 comments on commit 7691978

Please sign in to comment.