baobabstack-code commited on
Commit
e5ca5e0
·
1 Parent(s): 88d9118

Serve the Africa TTS API, with a key, beside a small Gradio page

Browse files
Files changed (4) hide show
  1. README.md +38 -5
  2. app.py +48 -0
  3. packages.txt +1 -0
  4. requirements.txt +2 -0
README.md CHANGED
@@ -1,13 +1,46 @@
1
  ---
2
- title: Africa Tts Api
3
- emoji: 📈
4
  colorFrom: green
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.32.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Africa TTS API
3
+ emoji: 🗣️
4
  colorFrom: green
5
+ colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.32.0
8
+ python_version: "3.11"
9
  app_file: app.py
10
  pinned: false
11
+ license: cc-by-nc-4.0
12
+ short_description: Private text-to-speech API for 92 African languages
13
  ---
14
 
15
+ # Africa TTS API
16
+
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) serves the ordinary API with a small
24
+ Gradio page beside it. 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
28
+
29
+ 1. Create a Space: SDK **Gradio**, template **Blank**, the free hardware, visibility
30
+ **Public**. A private Space asks for a Hugging Face token, which the app doesn't send;
31
+ the secret key still keeps everyone else out.
32
+ 2. In the Space's **Settings → Variables and secrets**, add a **secret** named
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 address is
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 `SPEECH_API_KEY`).
39
+
40
+ ## Good to know
41
+
42
+ - A free Space sleeps after a while without requests. The next request wakes it, which takes
43
+ a few minutes (it downloads the 440 MB model again); until then the app says the speech
44
+ server can't be reached.
45
+ - One request is spoken at a time and others queue.
46
+ - The model and voices are CC-BY-NC-4.0: non-commercial use only, with credit to AfriSpeech.
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Africa TTS API on a free Hugging Face Space (Gradio SDK).
2
+
3
+ Free Spaces only run Gradio apps, so this serves the ordinary Africa TTS API
4
+ (/health, /v1/languages, /v1/tts) with a small Gradio page mounted beside it.
5
+ Every /v1/tts request needs the key in the AFRICA_TTS_API_KEY Space secret.
6
+ """
7
+
8
+ import os
9
+
10
+ try:
11
+ # ZeroGPU Spaces expect this before PyTorch loads, and at least one GPU function.
12
+ # Speech is made on the CPU; the function below is never called.
13
+ import spaces
14
+
15
+ @spaces.GPU
16
+ def _unused_gpu_function():
17
+ return None
18
+ except ImportError: # not on a ZeroGPU Space
19
+ pass
20
+
21
+ if not os.environ.get("AFRICA_TTS_API_KEY"):
22
+ # Without a key the server would let anyone in.
23
+ raise SystemExit("Add AFRICA_TTS_API_KEY as a Space secret first (Settings → Variables and secrets).")
24
+
25
+ os.environ.setdefault("AFRICA_TTS_DEVICE", "cpu")
26
+ # One request is spoken at a time; the rest queue.
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 uvicorn # noqa: E402
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
+ uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "7860")))
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ espeak-ng
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cpu
2
+ africa-tts[torch]==0.5.0