Spaces:
Sleeping
Sleeping
# download model | |
import modules.hf as hf | |
# load models | |
import models.voice as voice | |
import models.whisper as whisper | |
voice.load() | |
voice.loadVoc() | |
#libs | |
import modules.register as register | |
from models.censor import Wash | |
import requests | |
import os | |
def download_audio(url, output_file): | |
""" | |
Downloads an audio file from the given URL and saves it locally. | |
If the file already exists, it returns the path without downloading again. | |
:param url: URL of the audio file | |
:param output_file: Path where the audio will be saved | |
:return: Path to the audio file | |
""" | |
if os.path.exists(output_file): | |
print(f"File already exists: {output_file}") | |
return output_file | |
try: | |
response = requests.get(url, stream=True) | |
response.raise_for_status() # Raise an HTTPError for bad responses (4xx and 5xx) | |
with open(output_file, 'wb') as file: | |
for chunk in response.iter_content(chunk_size=8192): | |
file.write(chunk) | |
print(f"Audio downloaded successfully: {output_file}") | |
return output_file | |
except requests.exceptions.RequestException as e: | |
print(f"Error downloading audio: {e}") | |
return None | |
# generate audio function | |
censorModel = Wash() | |
def generate_audio(key, text, censor=False, offset=0, speed=0.9, crossfade=0.1): | |
"""Generate audio from text""" | |
data = register.get_audio(key) | |
if(data["isOnline"] == "True"): | |
audio = download_audio(data["audio_path"], f'{key}.wav') | |
txt = data["transcription"].decode('utf-8') | |
print(txt) | |
audio, spectogram = voice.infer(audio, txt, text, remove_silence=True) | |
else: | |
audio, spectogram = voice.infer(data["audio_path"], data["transcription"], text, remove_silence=True, speed=speed, crossfade=crossfade) | |
if(censor): | |
audio = censorModel.process_audio(audio, offset) | |
return audio | |