File size: 6,535 Bytes
95636c5
 
 
 
 
 
d909d3b
 
95636c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d909d3b
 
 
 
 
 
 
 
 
95636c5
 
d909d3b
95636c5
d909d3b
95636c5
 
d909d3b
 
 
 
 
 
 
 
 
 
 
 
 
95636c5
 
 
 
 
 
 
d909d3b
95636c5
 
 
 
 
 
 
 
 
 
 
 
 
 
d909d3b
 
95636c5
 
 
 
 
 
 
 
 
d909d3b
 
 
 
 
 
 
 
 
 
 
 
95636c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d909d3b
95636c5
d909d3b
95636c5
d909d3b
95636c5
 
 
 
d909d3b
95636c5
d909d3b
95636c5
 
 
d909d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95636c5
d909d3b
95636c5
 
 
 
d909d3b
 
95636c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import logging
import os
import uuid
from contextlib import asynccontextmanager
from tempfile import NamedTemporaryFile

from azure.storage.blob import BlobServiceClient, generate_blob_sas, BlobSasPermissions
from datetime import datetime, timedelta
import torchaudio
from fastapi import BackgroundTasks, Depends, FastAPI, Header, HTTPException
from fastapi.security import APIKeyHeader
from pydantic import BaseModel

from inference import load_models, process_voice_conversion

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Global variables
models = None
API_KEY = os.getenv("API_KEY")

api_key_header = APIKeyHeader(name="Authorization", auto_error=False)


async def verify_api_key(authorization: str = Header(None)):
    if not authorization:
        logger.warning("No API key provided")
        raise HTTPException(status_code=401, detail="API key is missing")

    if authorization.startswith("Bearer "):
        token = authorization.replace("Bearer ", "")
    else:
        token = authorization

    if token != API_KEY:
        logger.warning("Invalid API key provided")
        raise HTTPException(status_code=401, detail="Invalid API key")

    return token


def get_azure_blob_client():
    account_name = os.getenv("AZURE_STORAGE_ACCOUNT_NAME", "getpoints")
    account_key = os.getenv("AZURE_STORAGE_KEY", "ts/PL1cr3X1F9JWgksAtqcWsQvPBK9UJ3BtNQBL98kYU17U3JxEiFI2vJrNDzmAyFRleOdRdoG03+ASt9RDnZA==")
    blob_endpoint = os.getenv("AZURE_BLOB_ENDPOINT", "https://getpoints.blob.core.windows.net/")
    blob_service_client = BlobServiceClient(
        account_url=blob_endpoint,
        credential=account_key
    )
    return blob_service_client


blob_client = get_azure_blob_client()

AZURE_CONTAINER_NAME = os.getenv("AZURE_CONTAINER_NAME", "seedvc-outputs")


async def ensure_container_exists():
    """Ensure the Azure container exists, create if it doesn't"""
    try:
        container_client = blob_client.get_container_client(AZURE_CONTAINER_NAME)
        container_client.get_container_properties()
        logger.info(f"Container '{AZURE_CONTAINER_NAME}' already exists")
    except Exception:
        try:
            blob_client.create_container(AZURE_CONTAINER_NAME)
            logger.info(f"Created container '{AZURE_CONTAINER_NAME}'")
        except Exception as e:
            logger.error(f"Failed to create container '{AZURE_CONTAINER_NAME}': {e}")
            raise


@asynccontextmanager
async def lifespan(app: FastAPI):
    global models
    logger.info("Loading Seed-VC model...")
    try:
        await ensure_container_exists()
        models = load_models()
        logger.info("Seed-VC model loaded successfully")
    except Exception as e:
        logger.error(f"Failed to load model: {e}")
        raise

    yield

    logger.info("Shutting down Seed-VC API")

app = FastAPI(title="Seed-VC API",
              lifespan=lifespan)

TARGET_VOICES = {
    "male": "examples/reference/s1p2.wav",
    "female": "examples/reference/s1p1.wav",
    "trump": "examples/reference/trump_0.wav",
}


class VoiceConversionRequest(BaseModel):
    source_audio_key: str
    target_voice: str


def download_blob_to_temp(blob_name):
    temp_file = NamedTemporaryFile(delete=False, suffix=".wav")
    blob_client_instance = blob_client.get_blob_client(
        container=AZURE_CONTAINER_NAME,
        blob=blob_name
    )
    with open(temp_file.name, "wb") as f:
        download_stream = blob_client_instance.download_blob()
        f.write(download_stream.readall())
    return temp_file.name


@app.post("/convert", dependencies=[Depends(verify_api_key)])
async def generate_speech(request: VoiceConversionRequest, background_tasks: BackgroundTasks):
    if not models:
        raise HTTPException(status_code=500, detail="Model not loaded")

    if request.target_voice not in TARGET_VOICES:
        raise HTTPException(
            status_code=400, detail=f"Target voice not supported. Choose from: {', '.join(TARGET_VOICES.keys())}")

    try:
        target_audio_path = TARGET_VOICES[request.target_voice]
        logger.info(
            f"Converting voice: {request.source_audio_key} to {request.target_voice}")

        # Generate a unique filename
        audio_id = str(uuid.uuid4())
        output_filename = f"{audio_id}.wav"
        local_path = f"/tmp/{output_filename}"

        logger.info("Downloading source audio from Azure Blob Storage")
        try:
            source_temp_path = download_blob_to_temp(request.source_audio_key)
        except Exception as e:
            logger.error(f"Failed to download source audio: {e}")
            raise HTTPException(
                status_code=404, detail="Source audio not found")

        vc_wave, sr = process_voice_conversion(
            models=models, source=source_temp_path, target_name=target_audio_path, output=None)

        os.unlink(source_temp_path)

        torchaudio.save(local_path, vc_wave, sr)

        # Upload to Azure Blob Storage
        blob_name = f"seedvc-outputs/{output_filename}"
        blob_client_instance = blob_client.get_blob_client(
            container=AZURE_CONTAINER_NAME,
            blob=blob_name
        )
        with open(local_path, "rb") as data:
            blob_client_instance.upload_blob(data, overwrite=True)

        # Generate SAS URL for temporary access
        sas_token = generate_blob_sas(
            account_name=blob_client.account_name,
            container_name=AZURE_CONTAINER_NAME,
            blob_name=blob_name,
            account_key=os.getenv("AZURE_STORAGE_KEY", "ts/PL1cr3X1F9JWgksAtqcWsQvPBK9UJ3BtNQBL98kYU17U3JxEiFI2vJrNDzmAyFRleOdRdoG03+ASt9RDnZA=="),
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1)
        )
        blob_url = f"{blob_client_instance.url}?{sas_token}"

        background_tasks.add_task(os.remove, local_path)

        return {
            "audio_url": blob_url,
            "blob_name": blob_name
        }
    except Exception as e:
        logger.error(f"Error in voice conversion: {e}")
        raise HTTPException(
            status_code=500, detail="Error in voice conversion")


@app.get("/voices", dependencies=[Depends(verify_api_key)])
async def list_voices():
    return {"voices": list(TARGET_VOICES.keys())}


@app.get("/health", dependencies=[Depends(verify_api_key)])
async def health_check():
    if models:
        return {"status": "healthy", "model": "loaded"}
    return {"status": "unhealthy", "model": "not loaded"}