import io import requests from pydub import AudioSegment from .tts_utils import mix_background_music available_roles = {} def get_reecho_ai_name(token): url = 'https://v1.reecho.cn/api/tts/voice' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {token}' } r = requests.get(url, headers=headers).json() if r['status'] == 200: role_dict = {} for i in r['data']: info_data = [i['id'], i['metadata']['promptMP3StorageUrl'], b''] if i['metadata']['description'] == '': role_dict['{}'.format(i['name'])] = info_data else: role_dict['{}——{}'.format(i['name'], i['metadata']['description'])] = info_data return role_dict else: return {'获取角色列表失败': ['', '', b''], str(r): ['', '', b'']} def get_example_audio(role): try: if available_roles[role][2] != b'': return available_roles[role][2] audio_url = available_roles[role][1] audio_data = requests.get(audio_url).content available_roles[role][2] = audio_data return audio_data except Exception as e: return None def refresh_roles(reecho_api_key): global available_roles available_roles = get_reecho_ai_name(reecho_api_key) def get_reecho_ai_result(token, text, voiceId, randomness, stability_boost, 背景音乐, TTS_up, bg_up): url = 'https://v1.reecho.cn/api/tts/simple-generate' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {token}' } body = { 'model': 'reecho-neural-voice-001', 'randomness': randomness, 'stability_boost': stability_boost, 'voiceId': available_roles[voiceId][0], 'text': text } try: r = requests.post(url, headers=headers, json=body) if r.status_code != 200: return f'请求失败,HTTP状态码为{r.status_code}\n\n{r.text}', None, None r = r.json() mp3_data = requests.get(r['data']['audio']).content original_audio = AudioSegment.from_file(io.BytesIO(mp3_data), format="mp3") return None, *mix_background_music(original_audio, 背景音乐, TTS_up, bg_up) except Exception as e: return str(e), None, None