[IBM]Python for Data Science, AI & Development - Watson Speech to Text Translator

2021. 5. 11. 12:45Data science/Python

반응형

I have already used all these functions before. So I just had to go to my instance to get URL and API key information from there. Otherwise, you have to pass through creating new services. 

#you will need the following library 
!pip install ibm_watson wget

Speech to Text 

from ibm_watson import SpeechToTextV1 
import json
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

#Get your own url and apikey infromation from your instance
url_s2t = "https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/6...."
iam_apikey_s2t = "Cdg3..."
#create speech to text adaptor objext the parameters are the endpoint and api key
authenticator = IAMAuthenticator(iam_apikey_s2t)   ##API key paramaeter
s2t = SpeechToTextV1(authenticator=authenticator)  
s2t.set_service_url(url_s2t)
s2t


#downlad the audio file 
!wget -O PolynomialRegressionandPipelines.mp3  https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/PolynomialRegressionandPipelines.mp3

filename='PolynomialRegressionandPipelines.mp3'

with open(filename, mode="rb")  as wav:                 #mode : rb (to read), file type :wav
    response = s2t.recognize(audio=wav, content_type='audio/mp3')
    
response.result
from pandas import json_normalize

json_normalize(response.result['results'],"alternatives")

response

recognized_text=response.result['results'][0]["alternatives"][0]["transcript"]
type(recognized_text)
#str

Language Translator

from ibm_watson import LanguageTranslatorV3

url_lt='https://gateway.watsonplatform.net/language-translator/api'
apikey_lt='OIe.....'

#API requests require a version parameter that takes a date in the format version=YYYY-MM-DD
version_lt='2018-05-01'

authenticator = IAMAuthenticator(apikey_lt)
language_translator = LanguageTranslatorV3(version=version_lt,authenticator=authenticator)
language_translator.set_service_url(url_lt)
language_translator

from pandas import json_normalize

json_normalize(language_translator.list_identifiable_languages().get_result(), "languages")

#Now, We can use the method translate this will translate the text.
recognized_text=response.result['results'][0]["alternatives"][0]["transcript"]
type(recognized_text)

translation_response = language_translator.translate(\
    text=recognized_text, model_id='en-es')
translation_response

translation=translation_response.get_result()
translation
#{'translations': [{'translation': 'en este vídeo cubriremos la regresión polinómica y las tuberías '}],
# 'word_count': 10,
# 'character_count': 64}

spanish_translation =translation['translations'][0]['translation']
spanish_translation 
#'en este vídeo cubriremos la regresión polinómica y las tuberías '

translation_new = language_translator.translate(text=spanish_translation ,model_id='es-en').get_result()

translation_eng=translation_new['translations'][0]['translation']
translation_eng
#'in this video we will cover the polynomial regression and the pipes '

QUIZ

# Write your code below and press Shift+Enter to execute
French_translation = language_translator.translate(
    text=translation_eng, model_id='en-fr').get_result()

French_translation['translations'][0]['translation']

'Dans cette vidéo nous couvrons la régression polynomiale et les tuyaux '

반응형