

chore: get base url from the request url in gradio
@3e09701346300481134740bf1a86fd0a4ab4da4f
--- src/faster_whisper_server/gradio_app.py
+++ src/faster_whisper_server/gradio_app.py
... | ... | @@ -22,22 +22,34 @@ |
22 | 22 |
TIMEOUT_SECONDS = 180 |
23 | 23 |
TIMEOUT = httpx.Timeout(timeout=TIMEOUT_SECONDS) |
24 | 24 |
|
25 |
+# NOTE: `gr.Request` seems to be passed in as the last positional (not keyword) argument |
|
25 | 26 |
|
26 |
-def create_gradio_demo(config: Config) -> gr.Blocks: # noqa: C901, PLR0915 |
|
27 |
- base_url = f"http://{config.host}:{config.port}" |
|
28 |
- # TODO: test that auth works |
|
29 |
- http_client = httpx.AsyncClient( |
|
27 |
+ |
|
28 |
+def base_url_from_gradio_req(request: gr.Request) -> str: |
|
29 |
+ # NOTE: `request.request.url` seems to always have a path of "/gradio_api/queue/join" |
|
30 |
+ assert request.request is not None |
|
31 |
+ return f"{request.request.url.scheme}://{request.request.url.netloc}" |
|
32 |
+ |
|
33 |
+ |
|
34 |
+def http_client_from_gradio_req(request: gr.Request, config: Config) -> httpx.AsyncClient: |
|
35 |
+ base_url = base_url_from_gradio_req(request) |
|
36 |
+ return httpx.AsyncClient( |
|
30 | 37 |
base_url=base_url, |
31 | 38 |
timeout=TIMEOUT, |
32 |
- headers={"Authorization": f"Bearer {config.api_key}"} if config.api_key else {}, |
|
33 |
- ) |
|
34 |
- openai_client = AsyncOpenAI( |
|
35 |
- base_url=f"{base_url}/v1", api_key=config.api_key if config.api_key else "cant-be-empty" |
|
39 |
+ headers={"Authorization": f"Bearer {config.api_key}"} if config.api_key else None, |
|
36 | 40 |
) |
37 | 41 |
|
42 |
+ |
|
43 |
+def openai_client_from_gradio_req(request: gr.Request, config: Config) -> AsyncOpenAI: |
|
44 |
+ base_url = base_url_from_gradio_req(request) |
|
45 |
+ return AsyncOpenAI(base_url=f"{base_url}/v1", api_key=config.api_key if config.api_key else "cant-be-empty") |
|
46 |
+ |
|
47 |
+ |
|
48 |
+def create_gradio_demo(config: Config) -> gr.Blocks: # noqa: C901, PLR0915 |
|
38 | 49 |
async def whisper_handler( |
39 |
- file_path: str, model: str, task: Task, temperature: float, stream: bool |
|
50 |
+ file_path: str, model: str, task: Task, temperature: float, stream: bool, request: gr.Request |
|
40 | 51 |
) -> AsyncGenerator[str, None]: |
52 |
+ http_client = http_client_from_gradio_req(request, config) |
|
41 | 53 |
if task == Task.TRANSCRIBE: |
42 | 54 |
endpoint = TRANSCRIPTION_ENDPOINT |
43 | 55 |
elif task == Task.TRANSLATE: |
... | ... | @@ -45,13 +57,15 @@ |
45 | 57 |
|
46 | 58 |
if stream: |
47 | 59 |
previous_transcription = "" |
48 |
- async for transcription in streaming_audio_task(file_path, endpoint, temperature, model): |
|
60 |
+ async for transcription in streaming_audio_task(http_client, file_path, endpoint, temperature, model): |
|
49 | 61 |
previous_transcription += transcription |
50 | 62 |
yield previous_transcription |
51 | 63 |
else: |
52 |
- yield await audio_task(file_path, endpoint, temperature, model) |
|
64 |
+ yield await audio_task(http_client, file_path, endpoint, temperature, model) |
|
53 | 65 |
|
54 |
- async def audio_task(file_path: str, endpoint: str, temperature: float, model: str) -> str: |
|
66 |
+ async def audio_task( |
|
67 |
+ http_client: httpx.AsyncClient, file_path: str, endpoint: str, temperature: float, model: str |
|
68 |
+ ) -> str: |
|
55 | 69 |
with Path(file_path).open("rb") as file: # noqa: ASYNC230 |
56 | 70 |
response = await http_client.post( |
57 | 71 |
endpoint, |
... | ... | @@ -67,7 +81,7 @@ |
67 | 81 |
return response.text |
68 | 82 |
|
69 | 83 |
async def streaming_audio_task( |
70 |
- file_path: str, endpoint: str, temperature: float, model: str |
|
84 |
+ http_client: httpx.AsyncClient, file_path: str, endpoint: str, temperature: float, model: str |
|
71 | 85 |
) -> AsyncGenerator[str, None]: |
72 | 86 |
with Path(file_path).open("rb") as file: # noqa: ASYNC230 |
73 | 87 |
kwargs = { |
... | ... | @@ -83,7 +97,8 @@ |
83 | 97 |
async for event in event_source.aiter_sse(): |
84 | 98 |
yield event.data |
85 | 99 |
|
86 |
- async def update_whisper_model_dropdown() -> gr.Dropdown: |
|
100 |
+ async def update_whisper_model_dropdown(request: gr.Request) -> gr.Dropdown: |
|
101 |
+ openai_client = openai_client_from_gradio_req(request, config) |
|
87 | 102 |
models = (await openai_client.models.list()).data |
88 | 103 |
model_names: list[str] = [model.id for model in models] |
89 | 104 |
assert config.whisper.model in model_names |
... | ... | @@ -96,14 +111,16 @@ |
96 | 111 |
value=config.whisper.model, |
97 | 112 |
) |
98 | 113 |
|
99 |
- async def update_piper_voices_dropdown() -> gr.Dropdown: |
|
114 |
+ async def update_piper_voices_dropdown(request: gr.Request) -> gr.Dropdown: |
|
115 |
+ http_client = http_client_from_gradio_req(request, config) |
|
100 | 116 |
res = (await http_client.get("/v1/audio/speech/voices")).raise_for_status() |
101 | 117 |
piper_models = [PiperModel.model_validate(x) for x in res.json()] |
102 | 118 |
return gr.Dropdown(choices=[model.voice for model in piper_models], label="Voice", value=DEFAULT_VOICE) |
103 | 119 |
|
104 | 120 |
async def handle_audio_speech( |
105 |
- text: str, voice: str, response_format: str, speed: float, sample_rate: int | None |
|
121 |
+ text: str, voice: str, response_format: str, speed: float, sample_rate: int | None, request: gr.Request |
|
106 | 122 |
) -> Path: |
123 |
+ openai_client = openai_client_from_gradio_req(request, config) |
|
107 | 124 |
res = await openai_client.audio.speech.create( |
108 | 125 |
input=text, |
109 | 126 |
model="piper", |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?