def test_unbabelAPI(self): api = UnbabelApi( username=TestConfig.UNBABEL_SANDBOX_USERNAME, api_key=TestConfig.UNBABEL_SANDBOX_KEY, sandbox=True, ) response = api.post_translations( text='Test translation', target_language='es', source_language='en', ) # Testing response fields self.assertEqual(response.status, 'new') self.assertTrue(response.uid) self.assertEqual(response.target_language, 'es') self.assertEqual(response.source_language, 'en') # Testing unused fields consistency self.assertIsNone(response.translation) self.assertIsNone(response.client) self.assertIsNone(response.balance) self.assertIsNone(response.order_number) self.assertIsNone(response.origin) self.assertIsNone(response.price_plan) self.assertIsNone(response.topics) self.assertTrue(response.price) self.assertEqual(response.translators, []) # Testing formats self.assertEqual(response.text_format, 'text') self.assertIn('Test translation', response.text)
def api(self): if not hasattr(self, '_api'): self._api = UnbabelApi(username = self.UNBABEL_TEST_USERNAME, api_key = self.UNBABEL_TEST_API_KEY, sandbox = True) self._api.api_url = self.UNBABEL_TEST_API_URL return self._api
def unbabel_api_settings(): username = current_app.config['UNBABEL_USERNAME'] api_key = current_app.config['UNBABEL_KEY'] sandbox = current_app.config['UNBABEL_SANDBOX'] # Set up account return UnbabelApi(username=username, api_key=api_key, sandbox=sandbox)
def setup(): unbabel_username = '******' unbabel_api_key = '339c3c0f3bb97c6a097c33f42122b32b74d032b5' in_test_mode = 'true' uapi = UnbabelApi(unbabel_username, unbabel_api_key, sandbox=in_test_mode) return uapi
def test_api_unauthorized_call(self): api = self.api self._api = UnbabelApi(username='******', api_key='fake_api_key') pairs = self.api.get_language_pairs() self.assertIsInstance(pairs, list, 'Got something that is not a list') self._api = api
def test_api_unauthorized_call(self, m): m.get("/tapi/v2/language_pair/", status_code=401, json={ "code": "401", "error": "Unauthorized" }) api = self.api self._api = UnbabelApi(username='******', api_key='fake_api_key') pairs = self.api.get_language_pairs() self.assertIsInstance(pairs, list, 'Got something that is not a list') self._api = api
def translate_report(report_id): report = Report.query.get(report_id) api = UnbabelApi(username=app.config['UNBABEL_USERNAME'], api_key=app.config['UNBABEL_API_KEY'], sandbox=True) callback_url = '{}/translate_report_callback/{}'.format( app.config['ROOT_URL'], report_id) api.post_translations(text=report.original_text, source_language=report.original_language, target_language='en', callback_url=callback_url)
def api(self): if not hasattr(self, '_api'): self._api = UnbabelApi(self.user, self.key) return self._api
import os from unbabel.api import UnbabelApi from config import load_dotenv uapi = UnbabelApi(os.environ['USERNAME'], os.environ['API_KEY'], sandbox=True) def post_translation(text): """HTTP request to the translation endpoint. :param text: text to be translated :return: dict with the uid """ return uapi.post_translations( text=text, source_language='en', target_language='es' ) def get_translation(uid): """Check the translation status. :param uid: uid from the text to be translated :return: dict with status and translatedText (if the translation is completed) """ return uapi.get_translation(uid)
# !/usr/bin/python # -*- coding: utf-8 -*- from sqlalchemy import or_ from app import make_celery from app.home.models import Translation from unbabel.api import UnbabelApi from database import db from config import Config celery = make_celery() api = UnbabelApi( username=Config.UNBABEL_SANDBOX_USERNAME, api_key=Config.UNBABEL_SANDBOX_KEY, sandbox=True, ) @celery.task def send_request(source_text, source_language, target_language): """ Translate a text using Unbabel API. :param source_text: Text submitted by the user. :param source_language: Original language. :param target_language: Target language. :return: The response text from Unbabel. """ response = api.post_translations( text=source_text, target_language=target_language,
import os import json import threading import shinarnews.db as db from shinarnews.Story import Story from unbabel.api import UnbabelApi import logging log = logging.getLogger() log.setLevel(logging.INFO) TRANSLATION_CHECKING_INTERVAL = 20 api = UnbabelApi(username=os.environ.get('UNBABEL_USERNAME'), api_key=os.environ.get('UNBABEL_API_KEY'), sandbox=True) def request_translate_story(story, langs): """ Requests a story to be translated Args: story (:obj:`Story`) langs (list of str) """ for lang in langs: if db.has_translation_in_process(story.id, lang): continue try: translation = api.post_mt_translations(story.title,
from flask import Flask, render_template, request from flask_sqlalchemy import SQLAlchemy import json from unbabel.api import UnbabelApi from models import db with open('config.json') as f: config = json.load(f) app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = config["postgres_url"] db = SQLAlchemy(app) db.init_app(app) api = UnbabelApi(username=config["unbabel_api_name"], api_key=config["unbabel_api_key"], sandbox=True) from models import Job @app.route('/', methods=["GET", "POST"]) def index(): error = "" if request.method == 'POST': text = request.form['text'] api.post_translations(text=text, target_language="es", source_language="en", callback_url="http://MISSING/translation/done") try:
from flask import Flask from flask import render_template from flask import request from unbabel.api import UnbabelApi, Translation from celery import Celery import redis import json from collections import OrderedDict app = Flask(__name__) app.config['SECRET_KEY'] = 'top-secret!' api = UnbabelApi(username='******', api_key='9db71b322d43a6ac0f681784ebdcc6409bb83359', sandbox=True) # Celery configuration app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0' app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0' celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) db = redis.Redis("localhost", 6379) @celery.task def get_async_translation(id): with app.app_context(): pipe = db.pipeline() trans = Translation() trans = api.get_translation(id) if trans.uid: