Skip to content

Commit

Permalink
Fix JSON serialization issue with numpy.float32 values in voicevox se…
Browse files Browse the repository at this point in the history
…rver

This commit introduces a new utility function `convert_to_float` that recursively converts numpy.float32 values to Python's native float type. This change is necessary because numpy.float32 values are not serializable by Python's json library, which causes issues when these values need to be sent as JSON responses from the voicevox server.

The `convert_to_float` function is now applied to `accent_phrases` after they are generated by the engine, ensuring that all numeric values are in a format compatible with JSON serialization standards.

This update ensures that the server can handle serialization of voice properties without encountering errors due to data type compatibility issues with JSON.
  • Loading branch information
iory committed May 13, 2024
1 parent a5e9bf6 commit 1fa5313
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 3rdparty/voicevox/node_scripts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import Optional
import zipfile

import numpy as np
from fastapi import FastAPI
from fastapi import HTTPException
from fastapi.middleware.cors import CORSMiddleware
Expand Down Expand Up @@ -53,6 +54,7 @@
from voicevox_engine.model import Speaker
from voicevox_engine.model import SpeakerInfo
from voicevox_engine.model import SupportedDevicesInfo
from voicevox_engine.model import Mora
from voicevox_engine.morphing import \
synthesis_morphing_parameter as _synthesis_morphing_parameter
from voicevox_engine.morphing import synthesis_morphing
Expand All @@ -66,6 +68,21 @@
from voicevox_engine.utility import engine_root


def convert_to_float(obj):
"""
Recursively converts numpy.float32 values to Python float in an object's attributes.
"""
if isinstance(obj, list):
return [convert_to_float(item) for item in obj]
elif isinstance(obj, AccentPhrase) or isinstance(obj, Mora):
for attr, value in obj.__dict__.items():
setattr(obj, attr, convert_to_float(value))
return obj
elif isinstance(obj, np.float32):
return float(obj)
else:
return obj

def b64encode_str(s):
return base64.b64encode(s).decode("utf-8")

Expand Down Expand Up @@ -129,6 +146,7 @@ def audio_query(text: str, speaker: int, core_version: Optional[str] = None):
"""
engine = get_engine(core_version)
accent_phrases = engine.create_accent_phrases(text, speaker_id=speaker)
accent_phrases = [convert_to_float(ac) for ac in accent_phrases]
return AudioQuery(
accent_phrases=accent_phrases,
speedScale=1,
Expand Down

0 comments on commit 1fa5313

Please sign in to comment.