Update core/app.py
Browse files- core/app.py +20 -0
core/app.py
CHANGED
|
@@ -10,6 +10,7 @@ from slowapi import Limiter
|
|
| 10 |
from slowapi.util import get_remote_address
|
| 11 |
from slowapi.errors import RateLimitExceeded
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 13 |
|
| 14 |
# Create necessary directories if they don't exist
|
| 15 |
os.makedirs("static", exist_ok=True)
|
|
@@ -81,6 +82,17 @@ class SpeechRequest(BaseModel):
|
|
| 81 |
input: str = Field(..., max_length=5000)
|
| 82 |
voice: str
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# Endpoint to generate speech
|
| 85 |
@app.post("/v1/audio/speech")
|
| 86 |
@app.get("/v1/audio/speech")
|
|
@@ -89,6 +101,13 @@ class SpeechRequest(BaseModel):
|
|
| 89 |
@limiter.limit(f"{RATE_LIMITS['rpd']}/day")
|
| 90 |
async def create_speech(request: Request, speech_request: SpeechRequest):
|
| 91 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if speech_request.model != "Pyx r1-voice":
|
| 93 |
raise HTTPException(status_code=400, detail="Invalid model. Only 'Pyx r1-voice' is supported.")
|
| 94 |
|
|
@@ -112,6 +131,7 @@ async def create_speech(request: Request, speech_request: SpeechRequest):
|
|
| 112 |
raise HTTPException(status_code=500, detail=str(e))
|
| 113 |
|
| 114 |
# Endpoint to get available models
|
|
|
|
| 115 |
@app.get("/v1/models")
|
| 116 |
async def get_models():
|
| 117 |
models_response = []
|
|
|
|
| 10 |
from slowapi.util import get_remote_address
|
| 11 |
from slowapi.errors import RateLimitExceeded
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
+
from langdetect import detect, LangDetectException
|
| 14 |
|
| 15 |
# Create necessary directories if they don't exist
|
| 16 |
os.makedirs("static", exist_ok=True)
|
|
|
|
| 82 |
input: str = Field(..., max_length=5000)
|
| 83 |
voice: str
|
| 84 |
|
| 85 |
+
# Allowed languages
|
| 86 |
+
ALLOWED_LANGUAGES = {"en", "ja", "zh", "id"} # Codes for English, Japanese, Chinese, Indonesian
|
| 87 |
+
|
| 88 |
+
# Function to validate language
|
| 89 |
+
def validate_language(text: str) -> bool:
|
| 90 |
+
try:
|
| 91 |
+
lang = detect(text)
|
| 92 |
+
return lang in ALLOWED_LANGUAGES
|
| 93 |
+
except LangDetectException:
|
| 94 |
+
return False
|
| 95 |
+
|
| 96 |
# Endpoint to generate speech
|
| 97 |
@app.post("/v1/audio/speech")
|
| 98 |
@app.get("/v1/audio/speech")
|
|
|
|
| 101 |
@limiter.limit(f"{RATE_LIMITS['rpd']}/day")
|
| 102 |
async def create_speech(request: Request, speech_request: SpeechRequest):
|
| 103 |
try:
|
| 104 |
+
# Validate input language
|
| 105 |
+
if not validate_language(speech_request.input):
|
| 106 |
+
raise HTTPException(
|
| 107 |
+
status_code=400,
|
| 108 |
+
detail="Input text must be in English, Japanese, Chinese, or Indonesian."
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
if speech_request.model != "Pyx r1-voice":
|
| 112 |
raise HTTPException(status_code=400, detail="Invalid model. Only 'Pyx r1-voice' is supported.")
|
| 113 |
|
|
|
|
| 131 |
raise HTTPException(status_code=500, detail=str(e))
|
| 132 |
|
| 133 |
# Endpoint to get available models
|
| 134 |
+
@app.get("/models")
|
| 135 |
@app.get("/v1/models")
|
| 136 |
async def get_models():
|
| 137 |
models_response = []
|