Fedir Zadniprovskyi commited on
Commit
01b8eeb
·
1 Parent(s): c0a38da

chore: gradio use openai client to list models

Browse files
faster_whisper_server/gradio_app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import gradio as gr
5
  import httpx
6
  from httpx_sse import connect_sse
 
7
 
8
  from faster_whisper_server.config import Config, Task
9
 
@@ -16,6 +17,7 @@ def create_gradio_demo(config: Config) -> gr.Blocks:
16
  port = int(os.getenv("UVICORN_PORT", "8000"))
17
  # NOTE: worth looking into generated clients
18
  http_client = httpx.Client(base_url=f"http://{host}:{port}", timeout=None)
 
19
 
20
  def handler(file_path: str, model: str, task: Task, temperature: float, stream: bool) -> Generator[str, None, None]:
21
  if stream:
@@ -65,16 +67,15 @@ def create_gradio_demo(config: Config) -> gr.Blocks:
65
  yield event.data
66
 
67
  def update_model_dropdown() -> gr.Dropdown:
68
- res = http_client.get("/v1/models")
69
- res_data = res.json()
70
- models: list[str] = [model["id"] for model in res_data["data"]]
71
- assert config.whisper.model in models
72
- recommended_models = {model for model in models if model.startswith("Systran")}
73
- other_models = [model for model in models if model not in recommended_models]
74
- models = list(recommended_models) + other_models
75
  return gr.Dropdown(
76
  # no idea why it's complaining
77
- choices=models, # pyright: ignore[reportArgumentType]
78
  label="Model",
79
  value=config.whisper.model,
80
  )
 
4
  import gradio as gr
5
  import httpx
6
  from httpx_sse import connect_sse
7
+ from openai import OpenAI
8
 
9
  from faster_whisper_server.config import Config, Task
10
 
 
17
  port = int(os.getenv("UVICORN_PORT", "8000"))
18
  # NOTE: worth looking into generated clients
19
  http_client = httpx.Client(base_url=f"http://{host}:{port}", timeout=None)
20
+ openai_client = OpenAI(base_url=f"http://{host}:{port}/v1", api_key="cant-be-empty")
21
 
22
  def handler(file_path: str, model: str, task: Task, temperature: float, stream: bool) -> Generator[str, None, None]:
23
  if stream:
 
67
  yield event.data
68
 
69
  def update_model_dropdown() -> gr.Dropdown:
70
+ models = openai_client.models.list().data
71
+ model_names: list[str] = [model.id for model in models]
72
+ assert config.whisper.model in model_names
73
+ recommended_models = {model for model in model_names if model.startswith("Systran")}
74
+ other_models = [model for model in model_names if model not in recommended_models]
75
+ model_names = list(recommended_models) + other_models
 
76
  return gr.Dropdown(
77
  # no idea why it's complaining
78
+ choices=model_names, # pyright: ignore[reportArgumentType]
79
  label="Model",
80
  value=config.whisper.model,
81
  )