Skip to content

Commit

Permalink
Merge pull request #336 from harmony-one/update-translate-module
Browse files Browse the repository at this point in the history
Migrated translation module to google cloud service
  • Loading branch information
theofandrich authored Oct 10, 2023
2 parents 585d2be + 569c175 commit e165b90
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 31 deletions.
115 changes: 115 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"dependencies": {
"@elastic/elasticsearch": "^8.9.0",
"@google-cloud/text-to-speech": "^5.0.1",
"@google-cloud/translate": "^8.0.2",
"@grammyjs/auto-chat-action": "^0.1.1",
"@grammyjs/auto-retry": "^1.1.1",
"@grammyjs/conversations": "^1.1.2",
Expand Down
34 changes: 34 additions & 0 deletions src/google-cloud/gcTranslateClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import config from '../config'
import type { CredentialBody } from 'google-auth-library/build/src/auth/credentials'
import type { google } from '@google-cloud/text-to-speech/build/protos/protos'
import { v2 } from '@google-cloud/translate'
import { type LanguageResult } from '@google-cloud/translate/build/src/v2'

export interface TextToSpeechParams {
text: string
languageCode: string
ssmlGender?: google.cloud.texttospeech.v1.SsmlVoiceGender | keyof typeof google.cloud.texttospeech.v1.SsmlVoiceGender | null
voiceName?: string | null
}

class GcTextToSpeechClient {
private readonly _client: v2.Translate
constructor (credentials: CredentialBody) {
this._client = new v2.Translate({ credentials })
}

async translate (text: string, targetLang: string): Promise<string> {
const [strList] = await this._client.translate([text], targetLang)

return strList[0]
}

async getLanguageList (): Promise<LanguageResult[]> {
const [languages] = await this._client.getLanguages()
return languages
}
}

const credentials = JSON.parse(Buffer.from(config.gc.credentials, 'base64').toString('utf-8'))

export const gcTranslateClient = new GcTextToSpeechClient(credentials)
19 changes: 19 additions & 0 deletions src/google-cloud/sandbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { gcTranslateClient } from './gcTranslateClient'

async function main (): Promise<void> {
const client = gcTranslateClient
const languageList = await client.getLanguageList()

console.log('### languageList', languageList.length)

const codes = languageList.map(lang => lang.code)
console.log('### codes', JSON.stringify(codes))
// for (const lang of languageList) {
// console.log(lang)
// }

const r = await client.translate('你好', 'ru')
console.log('### r', r)
}

main().then(() => { console.log('### finish') }).catch(console.log)
39 changes: 8 additions & 31 deletions src/modules/translate/TranslateBot.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { type OnMessageContext, type PayableBot, type RefundCallback, RequestState } from '../types'
import pino, { type Logger } from 'pino'
import { mapToTargetLang, translator } from './deeplClient'
import { now } from '../../utils/perf'
import { gcTranslateClient } from '../../google-cloud/gcTranslateClient'

enum SupportedCommands {
Translate = 'translate',
TranslateStop = 'translatestop'
}

const SupportedLangCommands = ['bg', 'cs', 'da', 'de', 'el', 'es', 'et', 'fi', 'fr', 'hu', 'id', 'it', 'ja', 'ko', 'lt', 'lv', 'nb', 'nl', 'pl', 'ro', 'ru', 'sk', 'sl', 'sv', 'tr', 'uk', 'zh', 'en', 'pt']
// const SupportedLangCommands = ['bg', 'cs', 'da', 'de', 'el', 'es', 'et', 'fi', 'fr', 'hu', 'id', 'it', 'ja', 'ko', 'lt', 'lv', 'nb', 'nl', 'pl', 'ro', 'ru', 'sk', 'sl', 'sv', 'tr', 'uk', 'zh', 'en', 'pt']
const SupportedLangCommands = ['af', 'sq', 'am', 'ar', 'hy', 'as', 'ay', 'az', 'bm', 'eu', 'be', 'bn', 'bho', 'bs', 'bg', 'ca', 'ceb', 'ny', 'zh', 'zh-TW', 'co', 'hr', 'cs', 'da', 'dv', 'doi', 'nl', 'en', 'eo', 'et', 'ee', 'tl', 'fi', 'fr', 'fy', 'gl', 'lg', 'ka', 'de', 'el', 'gn', 'gu', 'ht', 'ha', 'haw', 'iw', 'hi', 'hmn', 'hu', 'is', 'ig', 'ilo', 'id', 'ga', 'it', 'ja', 'jw', 'kn', 'kk', 'km', 'rw', 'gom', 'ko', 'kri', 'ku', 'ckb', 'ky', 'lo', 'la', 'lv', 'ln', 'lt', 'lb', 'mk', 'mai', 'mg', 'ms', 'ml', 'mt', 'mi', 'mr', 'mni-Mtei', 'lus', 'mn', 'my', 'ne', 'nso', 'no', 'or', 'om', 'ps', 'fa', 'pl', 'pt', 'pa', 'qu', 'ro', 'ru', 'sm', 'sa', 'gd', 'sr', 'st', 'sn', 'sd', 'si', 'sk', 'sl', 'so', 'es', 'su', 'sw', 'sv', 'tg', 'ta', 'tt', 'te', 'th', 'ti', 'ts', 'tr', 'tk', 'ak', 'uk', 'ur', 'ug', 'uz', 'vi', 'cy', 'xh', 'yi', 'yo', 'zu', 'he', 'jv', 'zh-CN']

export class TranslateBot implements PayableBot {
public readonly module = 'TranslateBot'
Expand Down Expand Up @@ -167,21 +168,14 @@ To disable translation, use the command /translatestop.`)
}

public async translateMessage (ctx: OnMessageContext, message: string, targetLangCode: string): Promise<void> {
const targetLanguage = mapToTargetLang(targetLangCode)
const result = await gcTranslateClient.translate(message, targetLangCode)

if (targetLanguage === null) {
await ctx.reply(`Unsupported language: ${targetLangCode}`)
return
}

const result = await translator.translateText(message, null, targetLanguage)

if (!result.text) {
if (!result) {
await ctx.reply('Unexpected error')
return
}

await ctx.reply(result.text)
await ctx.reply(result)
}

public async onTranslatePlainText (ctx: OnMessageContext): Promise<void> {
Expand All @@ -201,25 +195,8 @@ To disable translation, use the command /translatestop.`)

const translateResults: string[] = []
for (const targetLangCode of targetLanguages) {
const targetLanguage = mapToTargetLang(targetLangCode)

if (targetLanguage === null) {
translateResults.push(`Unsupported language: ${targetLangCode}`)
continue
}

const result = await translator.translateText(message, null, targetLanguage)
if (result.detectedSourceLang !== targetLangCode) {
translateResults.push(result.text)
}
// =======
// // can't detect original language
// if (completion01.completion === 'unknown') {
// await ctx.api.deleteMessage(ctx.chat.id, progressMessage.message_id)
// ctx.transient.analytics.actualResponseTime = now()
// ctx.transient.analytics.sessionState = SessionState.Success
// return
// >>>>>>> master
const result = await gcTranslateClient.translate(message, targetLangCode)
translateResults.push(result)
}

if (translateResults.length === 0) {
Expand Down

0 comments on commit e165b90

Please sign in to comment.