File size: 1,959 Bytes
1fe2f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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