RVC-Boss / GPT-SoVITS

1 min voice data can also be used to train a good TTS model! (few shot voice cloning)
MIT License
35.76k stars 4.08k forks source link

使用fast_inference_分支的api_v2.py推理时,API会偶尔无故中止 #1016

Open kirbykirby opened 6 months ago

kirbykirby commented 6 months ago

写了个函数进行批量推理,API偶尔会显示Press any key to continue...然后无报错中止运行。 一开始我还以为是多线程的锅,但是改成单线程后API还是会偶尔中止。是哪里出错了?求各位大佬指点😥 配置: i5 13400F RTX 4060Ti 16G 32G DDR4

def send_request_and_save(text_file_path, max_retries=3, backoff_factor=0.5):
    attempts = 0
    success = False

    # 读取配置文件
    config = configparser.ConfigParser()
    config.read('配置文件.ini', encoding='utf-8')

    with open(text_file_path, 'r', encoding='utf-8') as text_file:
        lines = text_file.readlines()

    for line in lines:
        text = line.strip()
        attempts = 0
        success = False

        # 检查该行是否以'|'开头
        special_name = text.startswith("|")
        # 如果是,去除行首的'|'符号
        if special_name:
            text = text[1:]

        rand_temp = round(random.uniform(0.5, 1), 2)
        rand_speed = round(random.uniform(0.98, 1.06), 2)
        with open(f'{script_dir}/Users/{user_name}/{user_name}.json', 'r', encoding='utf-8') as f:
            json_data = json.load(f)

        refer_wav, refer_text = random.choice(list(json_data['ref_audio'].items()))
        refer_wav_path = f'{script_dir}/Users/{user_name}/{refer_wav}'
        data = {
            'text': text,
            'text_lang': 'auto',
            'ref_audio_path': refer_wav_path,
            'prompt_text': refer_text,
            'prompt_lang': 'auto',
            'temperature': rand_temp,
            'streaming_mode': True,
            'split_bucket': False,
            'batch_size': 20,
            'media_type': "ogg",
            'text_split_method': "cut3",
            'fragment_interval': 0.15,
            'speed_factor': rand_speed,
            'parallel_infer': True
        }

        headers = {'Content-Type': 'application/json'}

        script_audio_folder = f"{user_name}"
        os.makedirs(script_audio_folder, exist_ok=True)
        existing_files = [f for f in os.listdir(script_audio_folder) if f.endswith('.ogg')]

        while attempts < max_retries and not success:
            try:
                response = requests.post(f'{url}/tts', json=data, headers=headers)

                if response.status_code == 200:
                    success = True
                    file_suffix = '_' if special_name else ''
                    audio_file = os.path.join(script_audio_folder, f"{len(existing_files) + 1}{file_suffix}.ogg")

                    with open(audio_file, "wb") as file:
                        file.write(response.content)
                    time.sleep(0.3)
                    break
            except RequestException as e:
                attempts += 1
                time.sleep(backoff_factor * (2 ** attempts))  # 指数退避策略
            except Exception as e:
                # 可能处理其他类型的异常
                print(f"出现了未预料到的错误: {e}")
                break

            if not success:
                print(f"处理行'{line}'时失败")
RVC-Boss commented 6 months ago

@ChasonJiang

ChasonJiang commented 6 months ago

@kirbykirby 可能是ogg的问题,不好复现。你用别的格式看看会不会复现

kirbykirby commented 6 months ago

@kirbykirby 可能是ogg的问题,不好复现。你用别的格式看看会不会复现

改成wav格式没有问题。我怀疑是长句的问题,导致API中止的都是超过100字的句子。