How to use IBM Cloud services with POST request in Python?

Asked

Viewed 231 times

10

I am trying to use IBM Cloud’s "Speech To Text" service in my Python application via POST requests with the package requests.

The problem is that I am confused about the URL that should be used and also the form of the request. I saw some tutorials on the internet in which the boy uses the following URL:

https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize

But when going on the part of Management service, the URL I get is this:

https://api.us-east.speech-to-text.watson.cloud.ibm.com/instances/<um código aqui>

My Python code is this:

import requests

api_url = "<Uma das URL's que coloquei acima na pergunta>"
key = "<Chave da API (a chave está correta)>"

with open("audio.wav", "rb") as file:
    audio = file.read()

response = requests.post(
    api_url,
    data = audio,
    headers = {"Content-Type": "audio/wav"},
    auth = ("apikey", key)
)

print(response.content) 

When I try to use the first URL I get this answer:

b'{"code":403, "error": "Forbidden"}'

And by using the second URL I get this:

b'{"code":404, "error": "Not Found"}'.

What I’m doing wrong in the requisition?

  • Before you ask me if I’ve tried using the package ibm_watson, yes I have tried, but I found it very complicated. Therefore, I preferred to use the package requests that I think is simpler.

1 answer

4


According to the documentation, you must use the endpoint /v1/recognize from the base URL you receive. This base URL you receive is the:

https://<region>.speech-to-text.watson.cloud.ibm.com/instances/<uuid>

I leave here an image indicating where to get this base URL.

Then your POST request will be for:

https://<region>.speech-to-text.watson.cloud.ibm.com/instances/<uuid>/v1/recognize

I think your mistake was not using the correct URL. Also remember to check if the API keys are correct. :)


With this, your Python code will look like this:

import requests

base_api_url = "https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/<uuid>"
key = "<secret>"

with open("audio.wav", "rb") as file:
    audio = file.read()

response = requests.post(
    f"{base_api_url}/v1/recognize",
    data = audio,
    headers = { "Content-Type": "audio/wav" },
    auth = ("apikey", key)
)

print(response.content.decode("utf-8"))

To request for a language model other than English, you must add the parameter language_customization_id at the URL, as per appears in the documentation.

To do this using the library requests, just use the option params, as well as appears in the documentation.

Thus:

import requests

base_api_url = "https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/<uuid>"
key = "<secret>"

with open("audio.wav", "rb") as file:
    audio = file.read()

response = requests.post(
    f"{base_api_url}/v1/recognize",
    # Aqui você passa os atributos da URL:
    params= { "language_customization_id": "<lang-code>" },
    data = audio,
    headers = { "Content-Type": "audio/wav" },
    auth = ("apikey", key)
)

print(response.content.decode("utf-8"))

However, I could not test, since apparently it is a feature that requires a different plan than mine (the free trial). I was getting an error 400. :)

  • Thank you very much guy, I was breaking my head a lot for this kk. In case it would be the same for the other services of IBM or just for this ?

  • Since I’m not familiar with Watson’s Apis, I can’t answer you for sure. However, I imagine that there is a yes standard, since it is the same service with different "sub-services", like the one from Speech to Text. :)

  • Luiz, I’m sorry but I’ll need to bother you just a little bit more because I don’t have much knowledge about kkk requests. I know there is a way to set the language using the parameter model, but how would I do it in the request ? For example, now he gives me the text in English and I want it in Portuguese.

  • 1

    @Jeanextreme002, I edited the answer. I think it is enough to send a parameter through the URL. As I put it in the edition, the library you’re using copper this use case in the documentation. :)

  • Thanks a lot for the help Luiz <3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.