File size: 1,059 Bytes
0192a4b
17b81a5
0192a4b
 
 
 
194fb30
 
 
 
7597554
 
194fb30
 
 
 
 
 
0192a4b
 
 
 
 
 
 
 
 
194fb30
0192a4b
194fb30
 
 
 
0192a4b
 
 
 
 
 
 
 
194fb30
17b81a5
0192a4b
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
import requests
import time

class StatusError(Exception):
    pass

# Custom cache dictionary
cache = {}

def synthesis(text, speaker=None):
    if speaker is None:
        speaker = 1
    
    # Check if the result is already in the cache
    cache_key = (text, speaker)
    if cache_key in cache:
        return cache[cache_key]
    
    url = f"https://api.tts.quest/v3/voicevox/synthesis?text={text}&speaker={speaker}"
    resp = requests.get(url)
    resp.raise_for_status()
    data = resp.json()
    is_ready = check_audio_status(data["audioStatusUrl"])
    if not is_ready:
        raise StatusError("Unexpected status")

    resp = requests.get(data["mp3DownloadUrl"])
    audio_content = resp.content
    
    # Cache the result
    cache[cache_key] = audio_content
    
    return audio_content

def check_audio_status(url):
    is_ready = False
    while not is_ready:
        resp = requests.get(url)
        resp.raise_for_status()
        data = resp.json()
        is_ready = data["isAudioReady"]
        time.sleep(0.5)

    return is_ready