From b9d657b657bf3c80f83939a080d4352132359b9d Mon Sep 17 00:00:00 2001 From: Gabriel Chua Date: Mon, 30 Sep 2024 22:47:47 +0800 Subject: [PATCH] fix meloTTS --- app.py | 4 ++++ utils.py | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 900d336..2611345 100644 --- a/app.py +++ b/app.py @@ -117,6 +117,7 @@ def generate_podcast( raise gr.Error( "The total content is too long. Please ensure the combined text from PDFs and URL is fewer than ~100,000 characters." ) + # Modify the system prompt based on the user input modified_system_prompt = SYSTEM_PROMPT @@ -156,6 +157,9 @@ def generate_podcast( transcript += speaker + "\n\n" total_characters += len(line.text) + if not use_advanced_audio: + LANGUAGE_MAPPING = MELO_TTS_LANGUAGE_MAPPING + # Get audio file path audio_file_path = generate_podcast_audio( line.text, line.speaker, LANGUAGE_MAPPING[language], use_advanced_audio diff --git a/utils.py b/utils.py index 4a56847..0d49815 100644 --- a/utils.py +++ b/utils.py @@ -9,6 +9,7 @@ import os import requests +import time from gradio_client import Client from openai import OpenAI from pydantic import ValidationError @@ -102,11 +103,17 @@ def generate_podcast_audio(text: str, speaker: str, language: str, use_advanced_ speed = 1.1 # Generate audio - result = hf_client.predict( - text=text, - language=language, - speaker=accent, - speed=speed, - api_name="/synthesize", - ) - return result + for attempt in range(3): + try: + result = hf_client.predict( + text=text, + language=language, + speaker=accent, + speed=speed, + api_name="/synthesize", + ) + return result + except Exception as e: + if attempt == 2: # Last attempt + raise # Re-raise the last exception if all attempts fail + time.sleep(1) # Wait for 1 second before retrying