Skip to content

Commit b8bdcd4

Browse files
Fix ServeReferenceAudio to allow base64 reference data in json (#777)
* Update schema.py to fix ServeStreamResponse * Update schema.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 40665e1 commit b8bdcd4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tools/schema.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import base64
12
import os
23
import queue
34
from dataclasses import dataclass
45
from typing import Literal
56

67
import torch
7-
from pydantic import BaseModel, Field, conint, conlist
8+
from pydantic import BaseModel, Field, conint, conlist, model_validator
89
from pydantic.functional_validators import SkipValidation
910
from typing_extensions import Annotated
1011

@@ -140,6 +141,19 @@ class ServeReferenceAudio(BaseModel):
140141
audio: bytes
141142
text: str
142143

144+
@model_validator(mode="before")
145+
def decode_audio(cls, values):
146+
audio = values.get("audio")
147+
if (
148+
isinstance(audio, str) and len(audio) > 255
149+
): # Check if audio is a string (Base64)
150+
try:
151+
values["audio"] = base64.b64decode(audio)
152+
except Exception as e:
153+
# If the audio is not a valid base64 string, we will just ignore it and let the server handle it
154+
pass
155+
return values
156+
143157
def __repr__(self) -> str:
144158
return f"ServeReferenceAudio(text={self.text!r}, audio_size={len(self.audio)})"
145159

0 commit comments

Comments
 (0)