-
-
Notifications
You must be signed in to change notification settings - Fork 671
Improve Japanese TTS/STT UX and dependency handling #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TechNavii
wants to merge
3
commits into
Blaizzy:main
Choose a base branch
from
TechNavii:fix/tts-stt-ux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+466
−63
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,21 +7,73 @@ | |
|
|
||
| import argparse | ||
| import asyncio | ||
| import importlib | ||
| import io | ||
| import math | ||
| import os | ||
| import sys | ||
| import shutil | ||
| import time | ||
| from numbers import Real | ||
| from textwrap import dedent | ||
| from typing import Any, Dict, List, Optional | ||
| from urllib.parse import unquote | ||
|
|
||
| import soundfile as sf | ||
| import uvicorn | ||
| from fastapi import FastAPI, File, Form, HTTPException, Response, UploadFile | ||
| from fastapi.encoders import jsonable_encoder | ||
| from fastapi.middleware.cors import CORSMiddleware | ||
| from fastapi.responses import StreamingResponse | ||
| from fastapi.responses import HTMLResponse, StreamingResponse | ||
| from pydantic import BaseModel | ||
|
|
||
| from mlx_audio.utils import load_model | ||
|
|
||
|
|
||
| def configure_fugashi_dictionary(): | ||
| """ | ||
| Ensure fugashi (used by misaki Cutlet) always has a dictionary. | ||
|
|
||
| Prefer unidic_lite, fall back to unidic if present. | ||
| """ | ||
|
|
||
| dicdir = None | ||
| chosen_module = None | ||
| for module_name in ("unidic_lite", "unidic"): | ||
| try: | ||
| module = __import__(module_name) | ||
| dicdir = getattr(module, "DICDIR", None) | ||
| if dicdir and os.path.isdir(dicdir): | ||
| mecabrc_path = os.path.join(dicdir, "mecabrc") | ||
| if os.path.exists(mecabrc_path): | ||
| chosen_module = module | ||
| break | ||
| dicdir = None | ||
| except ImportError: | ||
| continue | ||
|
|
||
| if not dicdir: | ||
| return | ||
|
|
||
| # Ensure any code importing `unidic` sees a module whose DICDIR actually exists. | ||
| if chosen_module.__name__ == "unidic_lite": | ||
| try: | ||
| import unidic # type: ignore | ||
|
|
||
| unidic.DICDIR = dicdir # type: ignore[attr-defined] | ||
| except ImportError: | ||
| sys.modules["unidic"] = chosen_module | ||
|
|
||
| mecabrc_path = os.path.join(dicdir, "mecabrc") | ||
|
|
||
| os.environ["FUGASHI_DICDIR"] = dicdir | ||
| os.environ["FUGASHI_ARGS"] = f"-d {dicdir} -r {mecabrc_path}" | ||
| if os.path.exists(mecabrc_path): | ||
| os.environ["MECABRC"] = mecabrc_path | ||
|
|
||
|
|
||
| configure_fugashi_dictionary() | ||
|
|
||
| MLX_AUDIO_NUM_WORKERS = os.getenv("MLX_AUDIO_NUM_WORKERS", "2") | ||
|
|
||
|
|
||
|
|
@@ -51,6 +103,80 @@ async def get_available_models(self): | |
| app = FastAPI() | ||
|
|
||
|
|
||
| @app.get("/", response_class=HTMLResponse) | ||
| async def root(): | ||
| """ | ||
| Simple landing page so the root URL no longer returns a 404. The full GUI | ||
| lives in the Next.js project under ``mlx_audio/ui``. | ||
| """ | ||
|
|
||
| html = dedent( | ||
| """ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>MLX Audio Server</title> | ||
| <style> | ||
| body { | ||
| font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; | ||
| margin: 0; | ||
| padding: 2rem; | ||
| background: #0b1120; | ||
| color: #e2e8f0; | ||
| } | ||
| a { | ||
| color: #38bdf8; | ||
| text-decoration: none; | ||
| } | ||
| .card { | ||
| max-width: 720px; | ||
| margin: auto; | ||
| padding: 2rem; | ||
| border-radius: 1rem; | ||
| background: rgba(15, 23, 42, 0.85); | ||
| box-shadow: 0 20px 40px rgba(0, 0, 0, 0.35); | ||
| } | ||
| h1 { | ||
| margin-top: 0; | ||
| } | ||
| code { | ||
| background: rgba(15, 118, 110, 0.25); | ||
| padding: 0.2rem 0.4rem; | ||
| border-radius: 0.35rem; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <main class="card"> | ||
| <h1>MLX Audio API server is running</h1> | ||
| <p> | ||
| This endpoint exposes the FastAPI backend (OpenAI-compatible TTS / STT APIs). | ||
| Open <a href="/docs">/docs</a> for interactive API documentation | ||
| or <a href="/redoc">/redoc</a> for the ReDoc view. | ||
| </p> | ||
| <p> | ||
| Looking for the graphical interface? Start the Next.js app inside | ||
| <code>mlx_audio/ui</code>: | ||
| </p> | ||
| <pre><code>cd mlx_audio/ui | ||
| npm install | ||
| export NEXT_PUBLIC_API_BASE_URL=http://localhost | ||
| export NEXT_PUBLIC_API_PORT=8000 | ||
| npm run dev</code></pre> | ||
| <p> | ||
| Then open <a href="http://localhost:3000" target="_blank" rel="noreferrer"> | ||
| http://localhost:3000</a>. | ||
| </p> | ||
| </main> | ||
| </body> | ||
| </html> | ||
| """ | ||
| ) | ||
|
|
||
| return HTMLResponse(content=html) | ||
|
|
||
|
|
||
| def int_or_float(value): | ||
|
|
||
| try: | ||
|
|
@@ -104,6 +230,51 @@ def setup_cors(app: FastAPI, allowed_origins: List[str]): | |
| setup_cors(app, default_origins) | ||
|
|
||
|
|
||
| LANGUAGE_DEPENDENCIES = { | ||
| "j": ("misaki.ja", "pip install misaki[ja]"), | ||
| "z": ("misaki.zh", "pip install misaki[zh]"), | ||
| } | ||
|
|
||
|
|
||
| def ensure_tts_language_support(lang_code: Optional[str]): | ||
| """Ensure extra dependencies for specific languages are installed.""" | ||
| code = (lang_code or "").lower() | ||
| requirement = LANGUAGE_DEPENDENCIES.get(code) | ||
| if requirement is None: | ||
| return | ||
|
|
||
| module_name, install_hint = requirement | ||
| try: | ||
| importlib.import_module(module_name) | ||
| except ModuleNotFoundError as exc: | ||
| missing = module_name.split(".")[0] | ||
| raise HTTPException( | ||
| status_code=503, | ||
| detail=( | ||
| f"Language '{code}' requires `{install_hint}` " | ||
| f"(missing dependency: {missing})." | ||
| ), | ||
| ) from exc | ||
|
|
||
|
|
||
| def sanitize_jsonable(value: Any): | ||
| """ | ||
| Recursively sanitize any jsonable structure by replacing NaN/inf values | ||
| with ``None`` so the response can be serialized by the JSON encoder. | ||
| """ | ||
|
|
||
| if isinstance(value, dict): | ||
| return {k: sanitize_jsonable(v) for k, v in value.items()} | ||
| if isinstance(value, (list, tuple)): | ||
| return [sanitize_jsonable(v) for v in value] | ||
| if isinstance(value, Real) and not isinstance(value, bool): | ||
| number = float(value) | ||
| if math.isnan(number) or math.isinf(number): | ||
| return None | ||
| return number | ||
| return value | ||
|
|
||
|
|
||
| # Request schemas for OpenAI-compatible endpoints | ||
| class SpeechRequest(BaseModel): | ||
| model: str | ||
|
|
@@ -207,6 +378,7 @@ async def generate_audio(model, payload: SpeechRequest, verbose: bool = False): | |
| @app.post("/v1/audio/speech") | ||
| async def tts_speech(payload: SpeechRequest): | ||
| """Generate speech audio following the OpenAI text-to-speech API.""" | ||
| ensure_tts_language_support(payload.lang_code) | ||
| model = model_provider.load_model(payload.model) | ||
| return StreamingResponse( | ||
| generate_audio(model, payload), | ||
|
|
@@ -230,9 +402,13 @@ async def stt_transcriptions( | |
| sf.write(tmp_path, audio, sr) | ||
|
|
||
| stt_model = model_provider.load_model(model) | ||
| result = stt_model.generate(tmp_path) | ||
| generate_kwargs = {} | ||
| if language: | ||
| generate_kwargs["language"] = language | ||
| result = stt_model.generate(tmp_path, **generate_kwargs) | ||
| os.remove(tmp_path) | ||
| return result | ||
| response = sanitize_jsonable(jsonable_encoder(result)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it realistic to get inf/nan here? What is this protecting against? |
||
| return response | ||
|
|
||
|
|
||
| def main(): | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you push this down into the Kokoro model initialization/invocation instead? It seems like this is all Kokoro/misaki-specific code that doesn't apply for other models and doesn't affect the API.