test-api-fix
This commit is contained in:
parent
28b23647e6
commit
c66a3a42b4
@ -33,10 +33,20 @@ class CosyVoiceService:
|
|||||||
if not self.connect():
|
if not self.connect():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
voices = self.client.predict(api_name="/refresh_sft_spk")
|
result = self.client.predict(api_name="/refresh_sft_spk")
|
||||||
# 过滤掉不需要的音色
|
|
||||||
filtered_voices = [voice for voice in voices if voice != '.ipynb_checkpoints']
|
# 处理返回的字典格式
|
||||||
return filtered_voices
|
if isinstance(result, dict) and 'choices' in result:
|
||||||
|
# 从choices中提取音色名称
|
||||||
|
voices = [choice[0] for choice in result['choices'] if choice[0] != '.ipynb_checkpoints']
|
||||||
|
return voices
|
||||||
|
elif isinstance(result, list):
|
||||||
|
# 直接是列表格式
|
||||||
|
return [voice for voice in result if voice != '.ipynb_checkpoints']
|
||||||
|
else:
|
||||||
|
logger.error(f"未知的音色列表格式: {result}")
|
||||||
|
return []
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取音色列表失败: {str(e)}")
|
logger.error(f"获取音色列表失败: {str(e)}")
|
||||||
return []
|
return []
|
||||||
@ -48,8 +58,17 @@ class CosyVoiceService:
|
|||||||
if not self.connect():
|
if not self.connect():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
audio_files = self.client.predict(api_name="/refresh_prompt_wav")
|
result = self.client.predict(api_name="/refresh_prompt_wav")
|
||||||
return audio_files
|
|
||||||
|
# 处理返回的字典格式
|
||||||
|
if isinstance(result, dict) and 'choices' in result:
|
||||||
|
audios = [choice[0] for choice in result['choices']]
|
||||||
|
return audios
|
||||||
|
elif isinstance(result, list):
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取参考音频列表失败: {str(e)}")
|
logger.error(f"获取参考音频列表失败: {str(e)}")
|
||||||
return []
|
return []
|
||||||
@ -65,7 +84,7 @@ class CosyVoiceService:
|
|||||||
prompt_wav=handle_file(audio_file_path),
|
prompt_wav=handle_file(audio_file_path),
|
||||||
api_name="/prompt_wav_recognition"
|
api_name="/prompt_wav_recognition"
|
||||||
)
|
)
|
||||||
return text
|
return text if isinstance(text, str) else ""
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"语音识别失败: {str(e)}")
|
logger.error(f"语音识别失败: {str(e)}")
|
||||||
return ""
|
return ""
|
||||||
@ -84,16 +103,41 @@ class CosyVoiceService:
|
|||||||
if not self.connect():
|
if not self.connect():
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
# 创建临时空音频文件用于占位
|
||||||
|
import tempfile
|
||||||
|
import wave
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# 创建一个短的静音音频作为占位符
|
||||||
|
temp_audio = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
|
||||||
|
with wave.open(temp_audio.name, 'w') as wav_file:
|
||||||
|
wav_file.setnchannels(1) # 单声道
|
||||||
|
wav_file.setsampwidth(2) # 16位
|
||||||
|
wav_file.setframerate(16000) # 16kHz采样率
|
||||||
|
# 写入很短的静音(0.1秒)
|
||||||
|
silence = np.zeros(1600, dtype=np.int16)
|
||||||
|
wav_file.writeframes(silence.tobytes())
|
||||||
|
|
||||||
result = self.client.predict(
|
result = self.client.predict(
|
||||||
tts_text=text,
|
tts_text=text,
|
||||||
mode_checkbox_group="预训练音色",
|
mode_checkbox_group="预训练音色",
|
||||||
sft_dropdown=voice,
|
sft_dropdown=voice,
|
||||||
seed=seed,
|
prompt_text="",
|
||||||
speed=speed,
|
prompt_wav_upload=handle_file(temp_audio.name),
|
||||||
stream="true" if stream else "false",
|
prompt_wav_record=handle_file(temp_audio.name),
|
||||||
|
instruct_text="",
|
||||||
|
seed=float(seed),
|
||||||
|
stream="True" if stream else "False",
|
||||||
|
speed=float(speed),
|
||||||
api_name="/generate_audio"
|
api_name="/generate_audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 清理临时文件
|
||||||
|
try:
|
||||||
|
os.unlink(temp_audio.name)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
# result是一个元组 [流式音频路径, 完整音频路径]
|
# result是一个元组 [流式音频路径, 完整音频路径]
|
||||||
if isinstance(result, (list, tuple)) and len(result) >= 2:
|
if isinstance(result, (list, tuple)) and len(result) >= 2:
|
||||||
return result[0], result[1]
|
return result[0], result[1]
|
||||||
@ -127,9 +171,14 @@ class CosyVoiceService:
|
|||||||
result = self.client.predict(
|
result = self.client.predict(
|
||||||
tts_text=text,
|
tts_text=text,
|
||||||
mode_checkbox_group="3s极速复刻",
|
mode_checkbox_group="3s极速复刻",
|
||||||
|
sft_dropdown="中文女",
|
||||||
prompt_text=reference_text,
|
prompt_text=reference_text,
|
||||||
prompt_wav_upload=handle_file(reference_audio_path),
|
prompt_wav_upload=handle_file(reference_audio_path),
|
||||||
seed=seed,
|
prompt_wav_record=handle_file(reference_audio_path),
|
||||||
|
instruct_text="",
|
||||||
|
seed=float(seed),
|
||||||
|
stream="False",
|
||||||
|
speed=1.0,
|
||||||
api_name="/generate_audio"
|
api_name="/generate_audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -154,14 +203,41 @@ class CosyVoiceService:
|
|||||||
if not self.connect():
|
if not self.connect():
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
# 创建临时空音频文件用于占位
|
||||||
|
import tempfile
|
||||||
|
import wave
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# 创建一个短的静音音频作为占位符
|
||||||
|
temp_audio = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
|
||||||
|
with wave.open(temp_audio.name, 'w') as wav_file:
|
||||||
|
wav_file.setnchannels(1) # 单声道
|
||||||
|
wav_file.setsampwidth(2) # 16位
|
||||||
|
wav_file.setframerate(16000) # 16kHz采样率
|
||||||
|
# 写入很短的静音(0.1秒)
|
||||||
|
silence = np.zeros(1600, dtype=np.int16)
|
||||||
|
wav_file.writeframes(silence.tobytes())
|
||||||
|
|
||||||
result = self.client.predict(
|
result = self.client.predict(
|
||||||
tts_text=text,
|
tts_text=text,
|
||||||
mode_checkbox_group="自然语言控制",
|
mode_checkbox_group="自然语言控制",
|
||||||
|
sft_dropdown="中文女",
|
||||||
|
prompt_text="",
|
||||||
|
prompt_wav_upload=handle_file(temp_audio.name),
|
||||||
|
prompt_wav_record=handle_file(temp_audio.name),
|
||||||
instruct_text=instruction,
|
instruct_text=instruction,
|
||||||
seed=seed,
|
seed=float(seed),
|
||||||
|
stream="False",
|
||||||
|
speed=1.0,
|
||||||
api_name="/generate_audio"
|
api_name="/generate_audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 清理临时文件
|
||||||
|
try:
|
||||||
|
os.unlink(temp_audio.name)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
if isinstance(result, (list, tuple)) and len(result) >= 2:
|
if isinstance(result, (list, tuple)) and len(result) >= 2:
|
||||||
return result[0], result[1]
|
return result[0], result[1]
|
||||||
else:
|
else:
|
||||||
@ -178,8 +254,20 @@ class CosyVoiceService:
|
|||||||
if not self.connect():
|
if not self.connect():
|
||||||
return 42
|
return 42
|
||||||
|
|
||||||
seed = self.client.predict(api_name="/generate_random_seed")
|
result = self.client.predict(api_name="/generate_random_seed")
|
||||||
return int(seed) if seed else 42
|
|
||||||
|
# 处理返回的字典格式
|
||||||
|
if isinstance(result, dict) and 'value' in result:
|
||||||
|
seed = int(result['value'])
|
||||||
|
elif isinstance(result, (int, float)):
|
||||||
|
seed = int(result)
|
||||||
|
elif isinstance(result, str) and result.isdigit():
|
||||||
|
seed = int(result)
|
||||||
|
else:
|
||||||
|
logger.warning(f"未知的随机种子格式: {result}")
|
||||||
|
seed = 42
|
||||||
|
|
||||||
|
return seed
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"生成随机种子失败: {str(e)}")
|
logger.error(f"生成随机种子失败: {str(e)}")
|
||||||
return 42
|
return 42
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user