Spaces:
Running on Zero
Running on Zero
Commit ·
6986444
1
Parent(s): e5ca5e0
Let Gradio start the server; attach the API under /tts-api
Browse files
README.md
CHANGED
|
@@ -17,11 +17,11 @@ short_description: Private text-to-speech API for 92 African languages
|
|
| 17 |
The [Africa TTS](https://huggingface.co/AfriSpeech/africa-tts) server by AfriSpeech on a free
|
| 18 |
Hugging Face Space. It is an API for one app, not a demo: speaking needs the secret key.
|
| 19 |
|
| 20 |
-
- `GET /health` and `GET /v1/languages` answer without a key.
|
| 21 |
-
- `POST /v1/tts` needs `Authorization: Bearer <AFRICA_TTS_API_KEY>`.
|
| 22 |
|
| 23 |
-
Free Spaces only run Gradio apps, so [app.py](app.py)
|
| 24 |
-
|
| 25 |
`deploy/huggingface-docker` does the same without Gradio.)
|
| 26 |
|
| 27 |
## Setting it up
|
|
@@ -33,9 +33,10 @@ Gradio page beside it. Speech is made on the CPU. (With a paid plan, the Docker
|
|
| 33 |
`AFRICA_TTS_API_KEY` with a long random value (e.g. `openssl rand -hex 24`).
|
| 34 |
3. Upload this folder's four files to the Space: `app.py`, `requirements.txt`,
|
| 35 |
`packages.txt` and `README.md`.
|
| 36 |
-
4. Wait for the build and the model download (about 10 minutes). The
|
| 37 |
-
`https://<owner>-<space-name>.hf.space`; `/health` answers once it is ready.
|
| 38 |
-
5. In your app, set that address and the key (Scribe: `SPEECH_API_URL` and
|
|
|
|
| 39 |
|
| 40 |
## Good to know
|
| 41 |
|
|
|
|
| 17 |
The [Africa TTS](https://huggingface.co/AfriSpeech/africa-tts) server by AfriSpeech on a free
|
| 18 |
Hugging Face Space. It is an API for one app, not a demo: speaking needs the secret key.
|
| 19 |
|
| 20 |
+
- `GET /tts-api/health` and `GET /tts-api/v1/languages` answer without a key.
|
| 21 |
+
- `POST /tts-api/v1/tts` needs `Authorization: Bearer <AFRICA_TTS_API_KEY>`.
|
| 22 |
|
| 23 |
+
Free Spaces only run Gradio apps, so [app.py](app.py) lets Gradio start the server and
|
| 24 |
+
attaches the ordinary API under `/tts-api`. Speech is made on the CPU. (With a paid plan, the Docker version in
|
| 25 |
`deploy/huggingface-docker` does the same without Gradio.)
|
| 26 |
|
| 27 |
## Setting it up
|
|
|
|
| 33 |
`AFRICA_TTS_API_KEY` with a long random value (e.g. `openssl rand -hex 24`).
|
| 34 |
3. Upload this folder's four files to the Space: `app.py`, `requirements.txt`,
|
| 35 |
`packages.txt` and `README.md`.
|
| 36 |
+
4. Wait for the build and the model download (about 10 minutes). The API's address
|
| 37 |
+
is `https://<owner>-<space-name>.hf.space/tts-api`; its `/health` answers once it is ready.
|
| 38 |
+
5. In your app, set that address (with `/tts-api`) and the key (Scribe: `SPEECH_API_URL` and
|
| 39 |
+
`SPEECH_API_KEY`).
|
| 40 |
|
| 41 |
## Good to know
|
| 42 |
|
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
"""Africa TTS API on a free Hugging Face Space (Gradio SDK).
|
| 2 |
|
| 3 |
-
Free Spaces only run Gradio apps, so
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
@@ -27,22 +30,35 @@ os.environ.setdefault("AFRICA_TTS_DEVICE", "cpu")
|
|
| 27 |
os.environ.setdefault("AFRICA_TTS_WORKERS", "1")
|
| 28 |
os.environ.setdefault("AFRICA_TTS_MAX_CHARS", "1000")
|
| 29 |
|
|
|
|
|
|
|
| 30 |
import gradio as gr # noqa: E402
|
| 31 |
-
import
|
| 32 |
|
| 33 |
from africa_tts.server.app import app as api # noqa: E402
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with gr.Blocks(title="Africa TTS API") as page:
|
| 36 |
gr.Markdown(
|
| 37 |
"# Africa TTS API\n"
|
| 38 |
"A private text-to-speech API for one app, built on "
|
| 39 |
"[Africa TTS](https://huggingface.co/AfriSpeech/africa-tts) by AfriSpeech "
|
| 40 |
"(CC-BY-NC-4.0). There is nothing to try here: requests need a secret key.\n\n"
|
| 41 |
-
"Is it running? See [/health](/health)."
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# The API's own routes come first; the page answers everything else.
|
| 45 |
-
app = gr.mount_gradio_app(api, page, path="/")
|
| 46 |
-
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""Africa TTS API on a free Hugging Face Space (Gradio SDK).
|
| 2 |
|
| 3 |
+
Free Spaces only run Gradio apps, so Gradio starts the server as usual and the
|
| 4 |
+
ordinary Africa TTS API is attached under /tts-api:
|
| 5 |
+
|
| 6 |
+
GET /tts-api/health
|
| 7 |
+
GET /tts-api/v1/languages
|
| 8 |
+
POST /tts-api/v1/tts (needs the key in the AFRICA_TTS_API_KEY Space secret)
|
| 9 |
"""
|
| 10 |
|
| 11 |
import os
|
|
|
|
| 30 |
os.environ.setdefault("AFRICA_TTS_WORKERS", "1")
|
| 31 |
os.environ.setdefault("AFRICA_TTS_MAX_CHARS", "1000")
|
| 32 |
|
| 33 |
+
import asyncio # noqa: E402
|
| 34 |
+
|
| 35 |
import gradio as gr # noqa: E402
|
| 36 |
+
from starlette.routing import Mount # noqa: E402
|
| 37 |
|
| 38 |
from africa_tts.server.app import app as api # noqa: E402
|
| 39 |
|
| 40 |
+
API_PATH = "/tts-api"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
async def _load_model():
|
| 44 |
+
# A mounted app's own startup never runs, so load the model (and download it) here.
|
| 45 |
+
await api.router.lifespan_context(api).__aenter__()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
asyncio.run(_load_model())
|
| 49 |
+
|
| 50 |
with gr.Blocks(title="Africa TTS API") as page:
|
| 51 |
gr.Markdown(
|
| 52 |
"# Africa TTS API\n"
|
| 53 |
"A private text-to-speech API for one app, built on "
|
| 54 |
"[Africa TTS](https://huggingface.co/AfriSpeech/africa-tts) by AfriSpeech "
|
| 55 |
"(CC-BY-NC-4.0). There is nothing to try here: requests need a secret key.\n\n"
|
| 56 |
+
f"Is it running? See [{API_PATH}/health]({API_PATH}/health)."
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
# Gradio picks the host and port the Space expects.
|
| 61 |
+
server, _, _ = page.launch(server_name="0.0.0.0", prevent_thread_lock=True)
|
| 62 |
+
# First in line, so none of Gradio's own routes can answer for the API.
|
| 63 |
+
server.router.routes.insert(0, Mount(API_PATH, app=api))
|
| 64 |
+
page.block_thread()
|