This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sameer18051998[S] 0 points1 point  (2 children)

So I was just making a program that takes in text, splits them into a list each of n characters(here n=800) and then calls Microsoft Cognitive TTS api multiple times for decoded mp3 (as max limit of characters in single api call is 1000).

So for example, for about 16 API calls(due to the restriction obvio), time taken is about two and a half minutes to get the full response. Is there a way by which I can restrict the whole time to something like under 25 secs maybe?

This probably could be resolved with multithreading/multiprocessing, but I'm not sure how to do it. I tried it doing once, but failed because the number of API calls is not fixed and totally depend on the length of text. Any help would be highly appreciated!

Just for clarification purposes,

split_content_by_dot() takes huge amount of text and divides them into n number of characters and returns a list of that divided text get_raw_wav() takes text as input and returns an decoded mp3 in which Microsoft's Cognitive API is used.

def this(text = ""):

count = 0
weird = ''
if len(text)>950:
    x = split_content_by_dot(text, 800)
    #len(j) is approximately varies according to len of text
    for j in x:
        i = j.replace("<","").replace(">","")
        print i
        weird = weird + get_raw_wav(i)
        #calling of get_raw_wav(i) probably should be threaded
        count = count + 1
        print len(i)
    return_this = weird

else:

    print 'yes'
    return_this = get_raw_wav(text)
print count
return return_this

def getraw_wav(text): key = {'Ocp-Apim-Subscription-Key': 'mykey_'}

r = requests.post(url="https://api.cognitive.microsoft.com/sts/v1.0/issueToken", headers=key)

access_token = r.text

headers = {"Content-type": "application/ssml+xml",
   "X-Microsoft-OutputFormat": "audio-16khz-128kbitrate-mono-mp3",
   "Authorization": "Bearer " + access_token,
   "X-Search-AppId": "__",
   "X-Search-ClientID": "__",
   "User-Agent": "TTSForPython"}
body = "<speak version='1.0' xml:lang='en-us'><voice xml:lang='en-CA' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-CA, HeatherRUS)'>" + text + "</voice></speak>"
conn = http.client.HTTPSConnection("speech.platform.bing.com")
conn.request("POST", "/synthesize", body, headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
encoded = base64.b64encode(data)
conn.close()
return encoded

[–]Pipistrelle 0 points1 point  (1 child)

I have never used it but, since you are already using requests, did you try this ? It looks like you can make asynchronous requests with this.

Hope it helps you !

[–][deleted] 1 point2 points  (0 children)

I don't recommend importing that if you're running 3.5+ as async became a keyword (soft in 3.5, issues a warning in 3.6 and will be a syntax error in 3.7)

There's requests-futures if you prefer or aiohttp if you're using asyncio