File size: 4,640 Bytes
47abf3e
 
 
 
 
 
 
 
41a6ecd
47abf3e
 
 
 
 
41a6ecd
 
 
 
 
 
 
47abf3e
 
 
 
 
 
16623fe
 
 
 
 
 
 
84e6d83
c759f7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16623fe
 
47abf3e
 
41a6ecd
47abf3e
16623fe
c6ecd8f
47abf3e
 
41a6ecd
47abf3e
 
41a6ecd
47abf3e
41a6ecd
16623fe
 
41a6ecd
16623fe
 
41a6ecd
16623fe
47abf3e
16623fe
 
41a6ecd
47abf3e
 
 
 
 
 
 
 
 
 
41a6ecd
16623fe
 
 
 
 
 
 
 
 
 
c759f7b
 
16623fe
 
 
 
 
47abf3e
41a6ecd
47abf3e
 
 
 
 
41a6ecd
 
4dc2adb
 
41a6ecd
 
47abf3e
 
c759f7b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from fastapi import FastAPI, HTTPException, Response, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel, Field
from typing import Optional
from vocify import generate_speech
import os
from fastapi.middleware.cors import CORSMiddleware

# Create necessary directories if they don't exist
os.makedirs("static", exist_ok=True)
os.makedirs("templates", exist_ok=True)

# Initialize FastAPI app
app = FastAPI(
    title="Pyxilabs._.Vocify",
    description="A Text-to-Speech API",
    swagger_ui_parameters={"favicon": "/static/icon.png"}
)

# Mount static files directory
app.mount("/static", StaticFiles(directory="static"), name="static")

# Initialize templates
templates = Jinja2Templates(directory="templates")

# Model and voice information structure
MODEL_INFO = {
    "Pyx r1-voice": {
        "name": "Pyx r1-voice",
        "created": "2024-12-12",
        "owner": "Pyxilabs AI Studio",
        "voices": {
            # Female Voices
            "seraphina": "XB0fDUnXU5powFXDhCwa",  # Elegant and sophisticated
            "isabella": "LcfcDJNUP1GQjkzn1xUU",   # Classic and timeless
            "astrid": "jsCqWAovK2LkecY7zXl4",     # Strong and Nordic-inspired
            "lila": "jBpfuIE2acCO8z3wKNLl",       # Playful and charming
            "elara": "z9fAnlkpzviPz146aGWa",       # Mystical and enchanting
            "evelyn": "oWAxZDx7w5VEj9dCyTzz",      # Graceful and refined

            # Male Voices
            "sebastian": "onwK4e9ZLuTAKqWW03F9",   # Strong and authoritative
            "finnian": "N2lVS1w4EtoT3dr4eOWO",     # Rugged and adventurous
            "theodore": "IKne3meq5aSn9XLyUdCD",    # Warm and friendly
            "magnus": "2EiwWnXFnvU5JabPnv8n",      # Bold and powerful
            "oliver": "CYw3kZ02Hs0563khs1Fj",      # Reliable and approachable
            "liam": "g5CIjZEefAph4nQFvHAz",        # Modern and confident
            "arthur": "SOYHLrjzK2X1ezoPC6cr",      # Classic and noble
            "elliot": "ZQe5CZNOzWyzPSCn5a3c",      # Sophisticated and calm
            "nathaniel": "bVMeCyTHy58xNoL34h3p",    # Wise and thoughtful

            # Neutral Voices
            "rowan": "D38z5RcWu1voky8WS1ja",       # Unisex, nature-inspired
            "avery": "zcAOhNBS3c14rBihAFp1",
        }
    }
}

# Pydantic model for speech request
class SpeechRequest(BaseModel):
    model: Optional[str] = Field(default="Pyx r1-voice")
    input: str = Field(..., max_length=5000)
    voice: str

# Endpoint to generate speech
@app.post("/v1/audio/speech")
@app.get("/v1/audio/speech")
async def create_speech(request: Request, speech_request: SpeechRequest):
    try:
        if speech_request.model != "Pyx r1-voice":
            raise HTTPException(status_code=400, detail="Invalid model. Only 'Pyx r1-voice' is supported.")
        
        if speech_request.voice not in MODEL_INFO["Pyx r1-voice"]["voices"]:
            raise HTTPException(status_code=400, detail="Invalid voice ID")
        
        voice_id = MODEL_INFO["Pyx r1-voice"]["voices"][speech_request.voice]
        
        result = generate_speech(
            model="eleven_multilingual_v2",
            voice=voice_id,
            input_text=speech_request.input
        )
        
        if isinstance(result, list):
            raise HTTPException(status_code=result[0], detail=result[1])
        
        return Response(content=result, media_type="audio/mpeg")
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Endpoint to get available models
@app.get("/v1/models")
async def get_models():
    models_response = []
    for model_id, model_data in MODEL_INFO.items():
        model_info = {
            "id": model_id,
            "name": model_data["name"],
            "created": model_data["created"],
            "owner": model_data["owner"],
            "vocals": [
                {"id": voice_name}
                for voice_name in model_data["voices"]
            ]
        }
        models_response.append(model_info)
    
    return {"models": models_response}

# Root endpoint to serve the HTML frontend
@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
    with open("templates/index.html", "r") as file:
        return HTMLResponse(content=file.read())

# Favicon endpoint
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
    return {"url": "/static/icon.png"}

# Run the application
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)