'Uvicorn Server Error Output'
INFO:     Started server process [20584]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

INFO:     127.0.0.1:58723 - "GET / HTTP/1.1" 404 Not Found

Loading Zyphra/Zonos-v0.1-transformer model...
Zyphra/Zonos-v0.1-transformer model loaded successfully!
Recomputed speaker embedding
INFO:     127.0.0.1:58727 - "POST /generate_audio HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\routing.py", line 288, in handle
    await self.app(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\starlette\routing.py", line 73, in app
    response = await f(request)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\fastapi\routing.py", line 301, in app
    raw_response = await run_endpoint_function(
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\fastapi\routing.py", line 212, in run_endpoint_function
    return await dependant.call(**values)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\Uvicorn_Server.py", line 128, in create_audio
    result = generate_audio(request)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\Uvicorn_Server.py", line 106, in generate_audio        
    conditioning = selected_model.prepare_conditioning(cond_dict)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\zonos\model.py", line 209, in prepare_conditioning     
    self.prefix_conditioner(cond_dict),
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\torch\nn\modules\module.py", line 1749, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\torch\nn\modules\module.py", line 1760, in _call_impl
    return forward_call(*args, **kwargs)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\zonos\conditioning.py", line 309, in forward
    conds.append(conditioner(cond_dict.get(conditioner.name)))
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\torch\nn\modules\module.py", line 1749, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\env\lib\site-packages\torch\nn\modules\module.py", line 1760, in _call_impl
    return forward_call(*args, **kwargs)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\zonos\conditioning.py", line 49, in forward
    cond = self.apply_cond(*inputs)
  File "H:\Hot\Pookie\Poo\poo\api\zonos.git\app\zonos\conditioning.py", line 258, in apply_cond        
    assert x.shape[-1] == self.input_dim
AssertionError

'Uvicorn Server source code'
import os
import torch
import torchaudio
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse, StreamingResponse
from pydantic import BaseModel
from typing import Optional, List
from zonos.model import Zonos, DEFAULT_BACKBONE_CLS as ZonosBackbone
from zonos.conditioning import make_cond_dict, supported_language_codes
from zonos.utils import DEFAULT_DEVICE as device

CURRENT_MODEL_TYPE = None
CURRENT_MODEL = None

SPEAKER_EMBEDDING = None
SPEAKER_AUDIO_PATH = None


# Define a request model for audio generation
class GenerateAudioRequest(BaseModel):
    model_choice: str
    text: str
    language: str
    speaker_audio: Optional[str] = None
    prefix_audio: Optional[str] = None
    emotions: List[float]
    vq_single: float
    fmax: float
    pitch_std: float
    speaking_rate: float
    dnsmos_ovrl: float
    speaker_noised: bool
    cfg_scale: float
    min_p: float
    seed: int
    randomize_seed: bool
    unconditional_keys: List[str]


# Initialize FastAPI
app = FastAPI()


def load_model_if_needed(model_choice: str):
    global CURRENT_MODEL_TYPE, CURRENT_MODEL
    if CURRENT_MODEL_TYPE != model_choice:
        if CURRENT_MODEL is not None:
            del CURRENT_MODEL
            torch.cuda.empty_cache()
        print(f"Loading {model_choice} model...")
        CURRENT_MODEL = Zonos.from_pretrained(model_choice, device=device)
        CURRENT_MODEL.requires_grad_(False).eval()
        CURRENT_MODEL_TYPE = model_choice
        print(f"{model_choice} model loaded successfully!")
    return CURRENT_MODEL


def generate_audio(request: GenerateAudioRequest):
    selected_model = load_model_if_needed(request.model_choice)

    global SPEAKER_AUDIO_PATH, SPEAKER_EMBEDDING

    # Manage random seed
    seed = request.seed
    if request.randomize_seed:
        seed = torch.randint(0, 2 ** 32 - 1, (1,)).item()
    torch.manual_seed(seed)

    # Process speaker audio embedding
    if request.speaker_audio is not None:
        if request.speaker_audio != SPEAKER_AUDIO_PATH:
            print("Recomputed speaker embedding")
            wav, sr = torchaudio.load(request.speaker_audio)
            SPEAKER_EMBEDDING = selected_model.make_speaker_embedding(wav, sr)
            SPEAKER_EMBEDDING = SPEAKER_EMBEDDING.to(device, dtype=torch.bfloat16)
            SPEAKER_AUDIO_PATH = request.speaker_audio

    # Process prefix audio
    audio_prefix_codes = None
    if request.prefix_audio is not None:
        wav_prefix, sr_prefix = torchaudio.load(request.prefix_audio)
        wav_prefix = wav_prefix.mean(0, keepdim=True)
        wav_prefix = selected_model.autoencoder.preprocess(wav_prefix, sr_prefix)
        wav_prefix = wav_prefix.to(device, dtype=torch.float32)
        audio_prefix_codes = selected_model.autoencoder.encode(wav_prefix.unsqueeze(0))

    # Convert emotion values to tensor
    emotion_tensor = torch.tensor(request.emotions, device=device)

    vq_tensor = torch.tensor([request.vq_single] * 8, device=device).unsqueeze(0)

    cond_dict = make_cond_dict(
        text=request.text,
        language=request.language,
        speaker=SPEAKER_EMBEDDING,
        emotion=emotion_tensor,
        vqscore_8=vq_tensor,
        fmax=request.fmax,
        pitch_std=request.pitch_std,
        speaking_rate=request.speaking_rate,
        dnsmos_ovrl=request.dnsmos_ovrl,
        speaker_noised=request.speaker_noised,
        device=device,
        unconditional_keys=request.unconditional_keys,
    )
    conditioning = selected_model.prepare_conditioning(cond_dict)

    max_new_tokens = 86 * 30
    codes = selected_model.generate(
        prefix_conditioning=conditioning,
        audio_prefix_codes=audio_prefix_codes,
        max_new_tokens=max_new_tokens,
        cfg_scale=request.cfg_scale,
        batch_size=1,
        sampling_params=dict(min_p=request.min_p),
    )

    wav_out = selected_model.autoencoder.decode(codes).cpu().detach()
    sr_out = selected_model.autoencoder.sampling_rate
    if wav_out.dim() == 2 and wav_out.size(0) > 1:
        wav_out = wav_out[0:1, :]
        
    return {"sample_rate": sr_out, "audio": wav_out.squeeze().numpy().tolist(), "seed": seed}


@app.post("/generate_audio")
async def create_audio(request: GenerateAudioRequest):
    result = generate_audio(request)
    return JSONResponse(content=result)


@app.get("/supported_languages")
async def obtain_languages():
    return {"supported_languages": supported_language_codes}


if __name__ == "__main__":
    import uvicorn
    host = os.getenv("SERVER_NAME", "0.0.0.0")
    port = int(os.getenv("SERVER_PORT", 8000))
    uvicorn.run(app, host=host, port=port)

'C# usage used'
private readonly HttpClient MyClient = new HttpClient();
private readonly string baseUrl = "http://localhost:8000"; // Change if your server address is different

async Task TTS()
{
    // Construct your request object
    var request = new
    {
        model_choice = "Zyphra/Zonos-v0.1-transformer", // Replace with actual model name
        text = "Hello, this is a test.",
        language = "en-us",
        speaker_audio = RefAudioFile, // or provide path if required
        prefix_audio = RefAudioFile, // or provide path if required
        emotions = new float[] { 0.5f, 0.5f },
        vq_single = 0.5f,
        fmax = 4000.0f,
        pitch_std = 1.0f,
        speaking_rate = 1.0f,
        dnsmos_ovrl = 1.0f,
        speaker_noised = false,
        cfg_scale = 1.0f,
        min_p = 0.0f,
        seed = 42,
        randomize_seed = false,
        unconditional_keys = new string[] { }
    };

    // Serialize the request object into JSON
    var json = JsonConvert.SerializeObject(request);
    var content = new StringContent(json, Encoding.UTF8, "application/json");

    try
    {
        // Send a POST request
        var response = await client.PostAsync($"{baseUrl}/generate_audio", content);
        response.EnsureSuccessStatusCode();

        // Read the response content
        var responseString = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Response from server:");
        Console.WriteLine(responseString);
    }
    catch (HttpRequestException e)
    {
        Console.WriteLine("Error sending request:");
        Console.WriteLine(e.Message);
    }
}