Skip to content

Commit

Permalink
Merge pull request #2 from datopian/fix/improvement
Browse files Browse the repository at this point in the history
 IBM waston SDK dosn't support python2 so replaced with request lib
  • Loading branch information
anuveyatsu authored Oct 19, 2021
2 parents 6aa3284 + 699c865 commit 910385e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![CKAN](https://img.shields.io/badge/ckan-2.8-orange.svg?style=flat-square)](https://github.com/ckan/ckan/tree/2.8) [![CKAN](https://img.shields.io/badge/ckan-2.9-orange.svg?style=flat-square)](https://github.com/ckan/ckan/tree/2.9)

Note: This plugin is tested with CKAN 2.8 or later version but hasn't been tested on earlier version.

# ckanext-translate
This extension provides an REST API that translates the provided text into the given languages. At the moment, it uses [IBM Watson Language Translator](https://www.ibm.com/cloud/watson-language-translator) APIs on backend for translation. In the future, other third-part services can be integrated.
Expand Down
Empty file.
44 changes: 22 additions & 22 deletions ckanext/translate/logic/action.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import json
import requests
from ckan.common import config
import ckan.plugins.toolkit as tk
import ckanext.translate.logic.schema as schema
from ibm_watson import LanguageTranslatorV3, ApiException
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

IBM_URL = config.get('ckanext.translate.ibm_url')
IBM_API_KEY = config.get('ckanext.translate.ibm_key')

authenticator = IAMAuthenticator(IBM_API_KEY)
language_translator = LanguageTranslatorV3(
version='2018-05-01',
authenticator=authenticator
)
language_translator.set_service_url(IBM_URL)

def translate(context, data_dict):
ibm_url = config.get('ckanext.translate.ibm_url')
ibm_api_key = config.get('ckanext.translate.ibm_key')

tk.check_access("translate", context, data_dict)
data, errors = tk.navl_validate(
data_dict, schema.translate(), context)

translate_from = data_dict['from']
translate_to = data_dict['to']
translate_keys, translate_values = zip(*data["input"].items())

if errors:
raise tk.ValidationError(errors)

translate_keys, translate_values = zip(*data["input"].items())

translate_req_dict= {
"text": list(translate_values),
"source": data_dict['from'],
"target": data_dict['to'],
}

try:
translation = language_translator.translate(text =list(translate_values),
source = translate_from,
target = translate_to,
).get_result()
response = requests.post('{}/v3/translate?{}'.format(ibm_url, 'version=2018-05-01'),
auth=('apikey', ibm_api_key),
headers= {"Content-Type": "application/json"},
data=json.dumps(translate_req_dict)
)

except ApiException as ex:
raise tk.ValidationError({'message': ex.message})
response.raise_for_status()
except requests.HTTPError as e:
raise tk.ValidationError({'message': '%s' % e})

translated_dict = {}
for index, translated_item in enumerate(translation['translations']):
for index, translated_item in enumerate(response.json()['translations']):
translated_dict.update({ list(translate_keys)[index]: translated_item['translation'] })

return {"output" : translated_dict}
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
namespace_packages=['ckanext'],

install_requires=[
"ibm-watson>=5.3.0"

# CKAN extensions should not list dependencies here, but in a separate
# ``requirements.txt`` file.
Expand Down

0 comments on commit 910385e

Please sign in to comment.