mapluisch / OpenAI-Text-To-Speech-for-Unity

Implementation of OpenAI's Text-To-Speech in Unity. Synthesize any text and play it via any AudioSource.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Too many reaquest!

xunxun opened this issue · comments

Failed to get audio data from OpenAI.
UnityEngine.Debug:LogError (object)
TTSManager/d__6:MoveNext () (at Assets/TTS/Scripts/TTSManager.cs:28)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<byte[]>:SetResult (byte[]) OpenAIWrapper/<RequestTextToSpeech>d__6:MoveNext () (at Assets/TTS/Scripts/OpenAIWrapper.cs:62) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<byte[]>:SetResult (byte[])
OpenAIWrapper/d__5:MoveNext () (at Assets/TTS/Scripts/OpenAIWrapper.cs:54)
UnityEngine.UnitySynchronizationContext:ExecuteTasks ()

My API key is new and has full quota!

Error: TooManyRequests

Hey xunxun,

thanks for reaching out - give me a sec and I'll see if I can replicate the error. Will get back to you!

Ok, so it works fine on my end with a fresh API key.

Just to be sure, in case you've restricted your API key - it has access to /v1/audio (incl. /v1/audio/speech)?

Also, if you've recently increased your quota, it may take a couple minutes on OpenAI's backend to reflect within API calls.

Please check again in a couple minutes, and please check back with me if it still doesn't work then :)

hello i have the same problem

Hi Mehdi,

could you please check if your API key has access to the /v1/audio endpoint and / or if a normal curl request would work with your API key?

Does a direct curl to the API work on your end (when you use your OpenAI API key)?

curl -X POST "https://api.openai.com/v1/audio/speech" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer YOUR_OPENAI_API_KEY" \
     --data '{
       "model": "tts-1", 
       "input": "Hello World", 
       "voice": "alloy", 
       "response_format": "mp3", 
       "speed": 1.0
     }' \
     --output response.mp3

If it starts to download a response.mp3 which contains Hello World synthesized by alloy, please let me know.

when i click in 'https://api.openai.com/v1/audio/speech' he give me this error {
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

Yep, that's because you're not supposed to access the API endpoint via a browser - you have to transmit your API key and the params you want to use as headers and body via POST to OpenAI, e.g. via curl, Postman, etc. :)

You can try the curl command I mentioned earlier. I don't know if you're using Windows, macOS, or Linux, but for all three it's pretty much the same (you just use a different terminal, i.e. cmd / Terminal).

i dont know what i wiill do and i use windows can you please help and just a question this plugin work for andoird and ios

Okay, no worries, I will guide you through it.

Copy & paste this command into some text editor, like Notepad for example:

curl -X POST "https://api.openai.com/v1/audio/speech" ^
     -H "Content-Type: application/json" ^
     -H "Authorization: Bearer YOUR_OPENAI_API_KEY" ^
     --data "{""model"": ""tts-1"", ""input"": ""Hello World"", ""voice"": ""alloy"", ""response_format"": ""mp3"", ""speed"": 1.0}" ^
     --output response.mp3

Where it says YOUR_OPENAI_API_KEY, you put your API key as is (do not add brackets, quotation marks, ...).

Once you've changed that part, you copy the entire command.

Press Win+R on your keyboard and enter cmd.exe to open up the "Terminal" on Windows.

Paste the command that you've copied and press enter - it should now ask OpenAI to create a .mp3 file which contains the words "Hello World" synthesized as audio.

Once the download is completed, open the .mp3 file (which is called response.mp3) with any audio player of your choice and let me know what you hear.

To your second question, I haven't tried myself but it should work as I've made sure to use as little third party libraries as possible.

i open the resoponse.mp3 file and it's not working I cant hear nothing file not working
curl -X POST "https://api.openai.com/v1/audio/speech" ^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer your api key" ^
--data "{""model"": ""tts-1"", ""input"": ""Hello World"", ""voice"": ""alloy"", ""response_format"": ""mp3"", ""speed"": 1.0}" ^
--output response.mp3

If that's the case, then your API key either doesn't have access to v1/audio or does not have enough quota.

The problem is neither my project nor OpenAI's API - please create a new API key and, if you use it with restricted access, make sure that it has access to v1/audio. You can set this when creating a key with restricted access.

Screenshot 2024-04-05 at 17 20 17

and i add the new key into unity openAI prefab and i delete the response.mp3 and update the key from the note pad press into windows+R and when I add the code and click enter I still have the same problem when I try to open the response.mp3 it's not working
please if you have time we can make a meeting to help fix this problem I will create a meet and send you the link

Yep, that looks good (for your own safety I will remove your screenshot though).

You have enough quota as well?

what is mean quota

In your usage dashboard, you can see whether you have enough "money" left over in your account balance: https://platform.openai.com/usage

Your balance has to be > $0 right here: https://platform.openai.com/account/billing/overview

And your limit for the tts-1 model has to be > 0 RPM: https://platform.openai.com/account/limits

Okay, so to wrap it up; if this project does not work on your end, please ensure that:

  • your API key is valid
  • your API key has access to /v1/audio
  • you have enough quota / high enough limits / available funds on your OpenAI account
  • a basic curl to the API endpoint results in a working output mp3

I'm always happy to help, but it's impossible for me to debug things that are not within my scope.

Also, please refrain from asking me (and especially other strangers) for personal remote sessions; it's just a gateway for heavy misuse if you end up asking the wrong people. Don't share your API keys with strangers either.

If you have problems with my implementation or adjacent things that I can help you with, please feel free to open up issues. But before doing so, please ensure that neither your API key, nor OpenAI's backend are at fault.

This one here is closed for now :)