Deploy backend
Browse files- .env.example +2 -0
- main.py +14 -4
.env.example
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
ELEVENLABS_API_KEY=
|
| 2 |
ELEVENLABS_VOICE_ID=
|
|
|
|
|
|
|
|
|
| 1 |
ELEVENLABS_API_KEY=
|
| 2 |
ELEVENLABS_VOICE_ID=
|
| 3 |
+
# Shared with the app (EXPO_PUBLIC_APP_KEY). Guards clone, rename and delete on the public Space.
|
| 4 |
+
APP_KEY=
|
main.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import io
|
|
|
|
| 2 |
import time
|
| 3 |
from collections import deque
|
| 4 |
|
| 5 |
import requests
|
| 6 |
import uvicorn
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
-
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
| 9 |
from fastapi.responses import Response
|
| 10 |
from fastapi.concurrency import run_in_threadpool
|
| 11 |
from PIL import Image, ImageOps, UnidentifiedImageError
|
|
@@ -180,7 +181,16 @@ async def voices():
|
|
| 180 |
return {"default": voice.default_voice_id(), "voices": await run_in_threadpool(voice.list_voices)}
|
| 181 |
|
| 182 |
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
async def clone_voice(audio: UploadFile = File(...), consent: str = Form("false"), name: str = Form("")):
|
| 185 |
"""Clone a trusted person's voice from a short recording. Needs an ElevenLabs plan with cloning."""
|
| 186 |
if consent != "true":
|
|
@@ -201,7 +211,7 @@ async def clone_voice(audio: UploadFile = File(...), consent: str = Form("false"
|
|
| 201 |
raise HTTPException(502, f"ElevenLabs unreachable: {e}")
|
| 202 |
|
| 203 |
|
| 204 |
-
@app.post("/voices/{voice_id}/name")
|
| 205 |
async def rename_voice(voice_id: str, name: str = Form(...)):
|
| 206 |
"""Rename a trusted (cloned) voice. The premade voices can't be renamed."""
|
| 207 |
name = name.strip()[: voice.NAME_MAX_CHARS]
|
|
@@ -215,7 +225,7 @@ async def rename_voice(voice_id: str, name: str = Form(...)):
|
|
| 215 |
raise HTTPException(502, f"ElevenLabs refused the rename: {e}")
|
| 216 |
|
| 217 |
|
| 218 |
-
@app.delete("/voices/{voice_id}")
|
| 219 |
async def delete_voice(voice_id: str):
|
| 220 |
"""Delete a trusted (cloned) voice. The premade voices can't be deleted."""
|
| 221 |
try:
|
|
|
|
| 1 |
import io
|
| 2 |
+
import os
|
| 3 |
import time
|
| 4 |
from collections import deque
|
| 5 |
|
| 6 |
import requests
|
| 7 |
import uvicorn
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
+
from fastapi import Depends, FastAPI, File, Form, Header, HTTPException, UploadFile
|
| 10 |
from fastapi.responses import Response
|
| 11 |
from fastapi.concurrency import run_in_threadpool
|
| 12 |
from PIL import Image, ImageOps, UnidentifiedImageError
|
|
|
|
| 181 |
return {"default": voice.default_voice_id(), "voices": await run_in_threadpool(voice.list_voices)}
|
| 182 |
|
| 183 |
|
| 184 |
+
# The Space is public, so changing the voices (clone, rename, delete) needs a key the app sends as
|
| 185 |
+
# X-App-Key (APP_KEY in .env and the Space's secrets, EXPO_PUBLIC_APP_KEY in app/.env). With no
|
| 186 |
+
# APP_KEY set, as on a laptop, everything is open.
|
| 187 |
+
def require_app_key(x_app_key: str | None = Header(None)):
|
| 188 |
+
expected = os.getenv("APP_KEY")
|
| 189 |
+
if expected and x_app_key != expected:
|
| 190 |
+
raise HTTPException(401, "this app isn't allowed to change voices")
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
@app.post("/voices/clone", dependencies=[Depends(require_app_key)])
|
| 194 |
async def clone_voice(audio: UploadFile = File(...), consent: str = Form("false"), name: str = Form("")):
|
| 195 |
"""Clone a trusted person's voice from a short recording. Needs an ElevenLabs plan with cloning."""
|
| 196 |
if consent != "true":
|
|
|
|
| 211 |
raise HTTPException(502, f"ElevenLabs unreachable: {e}")
|
| 212 |
|
| 213 |
|
| 214 |
+
@app.post("/voices/{voice_id}/name", dependencies=[Depends(require_app_key)])
|
| 215 |
async def rename_voice(voice_id: str, name: str = Form(...)):
|
| 216 |
"""Rename a trusted (cloned) voice. The premade voices can't be renamed."""
|
| 217 |
name = name.strip()[: voice.NAME_MAX_CHARS]
|
|
|
|
| 225 |
raise HTTPException(502, f"ElevenLabs refused the rename: {e}")
|
| 226 |
|
| 227 |
|
| 228 |
+
@app.delete("/voices/{voice_id}", dependencies=[Depends(require_app_key)])
|
| 229 |
async def delete_voice(voice_id: str):
|
| 230 |
"""Delete a trusted (cloned) voice. The premade voices can't be deleted."""
|
| 231 |
try:
|