This article is the 16th day article of IBM Cloud Advent Calendar 2020.
IBM Cloud AI service Watson can be called with REST API. You can also use SDK in various major languages.
This article describes how to use IBM Watson services from Python using the SDK. This article has been tested based on the following versions: v5.0.0
Please note that the usage changes depending on the version.
In v5.0.0, the following API calls can be made.
--Has an IBM Cloud account --Service created (Language Translator in the example below)
--If you don't know how to do it, please refer to here.
--There is an execution environment of python 3.5 or higher
Make sure that you can check the API KEY and URL of the service you created. If you don't know how to do it, please refer to here: Get Service Credentials (https://qiita.com/nishikyon/items/9b8f697db7ad0a693839#2-%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9%E3%81%AE%E8%B3%87%E6%A0%BC%E6%83%85%E5%A0%B1%E5%8F%96%E5%BE%97)
First of all, I will introduce how to find out how to use it in general regardless of the language. How to use the SDKs for various major languages is actually in the API documentation on IBM Cloud!
In the API & SDK Reference Library category [AI / Machine Learning](https://cloud.ibm.com/docs?tab=api-docs & category = ai), there are links to the API & SDK documentation for each service.
In this article, we will use Language Translator as an example, so please create a service for Language Translator.
This time, we will use Language Translator, so click ** Language Translator ** from API & SDK Reference Library Category ** AI / Machine Learning **.
This time it's Python, so click "Python". By the way, when I checked on 2020/12/15, Curl, Java, Node, Python, Go, .NET, Ruby, Swift, Unity were in the tabs.
For example, if it is "Introduction", the installation method with pip is described. You can copy and execute it.
For example, if Method "Translate", the definition and sample code of the selected language will be displayed on the right side. You can copy and paste the sample code and execute it.
However, note that you need to replace {apikey}
and {url}
with those of your service. Replace it with the one prepared in 1. Preparation.
For those who have drawn "2. Basics of using Watson SDK, the document is in English, but ...", I will explain how to use it. However, you can't understand the use of detailed SDK without looking at the SDK reference, so if you get a feel for it, please check the SDK reference for details. Code is a common language, isn't it?
Here, Language Translator is used as an example.
You can install it with the following pip command.
pip install --upgrade ibm-watson
Almost a copy of the sample code here (https://cloud.ibm.com/apidocs/language-translator?code=python#authentication).
In the sample code of here, for example, '{apikey}'
is written, it is better to write '{xxxxxxxxx}'
(xxxxxxxxx is your apikey) in the code. It's fine to do it. It's hard to understand, but ** {} ** is unnecessary. Replace with {apikey}. The code below was first defined as a variable without the ** {} ** description.
APIKEY='Enter your API KEY'
URL='Enter your URL'
VERSION='2018-05-01' #Please enter the version you want to use
from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator(APIKEY)
language_translator = LanguageTranslatorV3(
version=VERSION,
authenticator=authenticator
)
language_translator.set_service_url(URL)
For the above VERSION, click "Versioning" on the right side of API Document, and the version assumed in the document is described, so I think it is better to use that value.
Language Translator is a translation service, so let's call the translation method "Translate".
For the Method name of the calling method, the contents of the parameters, etc., click Basic 3: Subject to know how to use from the menu on the left. , click the content you want to know from the right side of API Document and it will be displayed with the sample code.
Write the following code after the 3.2 Create Instance code.
Since model_id ='en-es'
, it is a translation from English (en) to Spanish (es).
translation = language_translator.translate(
text='Hello, how are you today?',
model_id='en-es').get_result()
What is returned by get_result () in the return value of language_translator.translate () is written in the middle column of API documentation.
Also, the whole return value can be found in the API document under Data Handling
→ Response details
. In addition to get_result (), it is possible to get Header information and HTTP Status Code.
Let's view the results. Indented for easy viewing:
import json
print(json.dumps(translation, indent=2, ensure_ascii=False))
What you see:
{
"translations": [
{
"translation": "Hola, ¿cómo estás hoy?"
}
],
"word_count": 7,
"character_count": 25
}
If you want only the translation result, specify as follows (it will be a dictionary type):
print (translation[ "translations"][0]["translation"])
What you see:
Hola, ¿cómo estás hoy?
that's all.
If you suppress the points of usage, I think that Code can be easily applied even if the API document is in English, so please try it with other APIs!
Recommended Posts