Skip to content

Instantly share code, notes, and snippets.

@gnh1201
Last active September 9, 2023 21:12
Show Gist options
  • Save gnh1201/2ef69f3e57828692e47297edc1c4bb26 to your computer and use it in GitHub Desktop.
Save gnh1201/2ef69f3e57828692e47297edc1c4bb26 to your computer and use it in GitHub Desktop.
IBM Cloud Language Translator in Mastodon (Not working yet)
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0] method=POST path=/api/v1/statuses/111036983556043247/translate format=html controller=Api::V1::Statuses::TranslationsController action=create status=500 error='NoMethodError: undefined method `model_name' for Request:Class
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0]
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0] NoMethodError (undefined method `model_name' for Request:Class
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0]
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0] app/controllers/api/v1/statuses/translations_controller.rb:14:in `create'
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0] app/controllers/concerns/localized.rb:11:in `set_locale'
Sep 9 20:44:08 gosomi bundle[782892]: [26a0d58f-e22c-4219-9dcd-6a04f953ced0] lib/mastodon/rack_middleware.rb:9:in `call'
# frozen_string_literal: true
class TranslationService::IBMCloud < TranslationService
def initialize(base_url, api_key)
super()
@base_url = base_url
@api_key = api_key
end
def translate(text, source_language, target_language)
puts 'translate()'
request(text, source_language, target_language).perform do |res|
case res.code
when 429
raise TooManyRequestsError
when 456
raise QuotaExceededError
when 200...300
transform_response(res.body_with_limit, source_language)
else
raise UnexpectedResponseError
end
end
end
private
def request(text, source_language, target_language)
puts 'request()'
encoded_api_key = Base64.strict_encode64("apikey:#{@api_key}")
body = Oj.dump(text: [text], model_id: "#{source_language}-#{target_language}")
req = Request.new(:post, "#{@base_url}/v3/translate?version=2018-05-01", body: body, allow_local: true)
req.add_headers('Content-Type': 'application/json')
req.add_headers('Authorization': "Basic #{encoded_api_key}")
req
end
def transform_response(str, source_language)
puts 'transform_response()'
data = Oj.load(str, mode: :strict)
raise UnexpectedResponseError unless data.is_a?(Hash)
Translation.new(
text: json.dig('translations', 0, 'translation'),
detected_source_language: source_language,
provider: 'IBM Cloud Language Translator'
)
end
end
# frozen_string_literal: true
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'StatsD'
inflect.acronym 'OEmbed'
inflect.acronym 'OStatus'
inflect.acronym 'ActivityPub'
inflect.acronym 'PubSubHubbub'
inflect.acronym 'ActivityStreams'
inflect.acronym 'JsonLd'
inflect.acronym 'NodeInfo'
inflect.acronym 'Ed25519'
inflect.acronym 'TOC'
inflect.acronym 'RSS'
inflect.acronym 'REST'
inflect.acronym 'URL'
inflect.acronym 'ASCII'
inflect.acronym 'DeepL'
inflect.acronym 'DSL'
inflect.acronym 'IBMCloud'
inflect.singular 'data', 'data'
end
# frozen_string_literal: true
class TranslationService
class Error < StandardError; end
class NotConfiguredError < Error; end
class TooManyRequestsError < Error; end
class QuotaExceededError < Error; end
class UnexpectedResponseError < Error; end
def self.configured
if ENV['DEEPL_API_KEY'].present?
TranslationService::DeepL.new(ENV.fetch('DEEPL_PLAN', 'free'), ENV['DEEPL_API_KEY'])
elsif ENV['LIBRE_TRANSLATE_ENDPOINT'].present?
TranslationService::LibreTranslate.new(ENV['LIBRE_TRANSLATE_ENDPOINT'], ENV['LIBRE_TRANSLATE_API_KEY'])
elsif ENV['IBMCLOUD_LT_API_KEY'].present?
puts 'TranslationService::IBMCloud.new()'
TranslationService::IBMCloud.new(ENV['IBMCLOUD_LT_ENDPOINT'], ENV['IBMCLOUD_LT_API_KEY'])
else
raise NotConfiguredError
end
end
def self.configured?
ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present? || ENV['IBMCLOUD_LT_API_KEY'].present?
end
def translate(_text, _source_language, _target_language)
raise NotImplementedError
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment