-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from harmony-one/voice-translate
Voice-translate Added raw demo (voice to synthetic voice)
- Loading branch information
Showing
9 changed files
with
613 additions
and
18 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import GcTextToSpeech, { type TextToSpeechClient } from '@google-cloud/text-to-speech' | ||
import config from '../config' | ||
import type { CredentialBody } from 'google-auth-library/build/src/auth/credentials' | ||
|
||
class GcTextToSpeechClient { | ||
private readonly _client: TextToSpeechClient | ||
constructor (credentials: CredentialBody) { | ||
this._client = new GcTextToSpeech.TextToSpeechClient({ credentials }) | ||
} | ||
|
||
async textToSpeech (text: string): Promise<string | Uint8Array | null | undefined> { | ||
const ssml = `<speak>${text}</speak>` | ||
|
||
const [response] = await this._client.synthesizeSpeech({ | ||
input: { ssml }, | ||
voice: { languageCode: 'en-US', ssmlGender: 'MALE' }, | ||
audioConfig: { audioEncoding: 'OGG_OPUS' } | ||
}) | ||
|
||
return response.audioContent | ||
} | ||
} | ||
|
||
const credentials = JSON.parse(Buffer.from(config.gc.credentials, 'base64').toString('utf-8')) | ||
|
||
export const gcTextToSpeedClient = new GcTextToSpeechClient(credentials) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pino from 'pino' | ||
import { InputFile } from 'grammy' | ||
import type { Logger } from 'pino' | ||
import type { BotPayments } from '../payment' | ||
import type { OnMessageContext, PayableBot } from '../types' | ||
import { gcTextToSpeedClient } from '../../google-cloud/gcTextToSpeechClient' | ||
|
||
export class TextToSpeechBot implements PayableBot { | ||
private readonly payments: BotPayments | ||
|
||
private readonly logger: Logger | ||
|
||
constructor (payments: BotPayments) { | ||
this.payments = payments | ||
this.logger = pino({ | ||
name: 'TextToSpeech', | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { colorize: true } | ||
} | ||
}) | ||
} | ||
|
||
public isSupportedEvent (ctx: OnMessageContext): boolean { | ||
return ctx.hasCommand('voice') | ||
} | ||
|
||
public getEstimatedPrice (ctx: OnMessageContext): number { | ||
const str = this.getTextFromMessage(ctx) | ||
return str.length * 0.005 | ||
} | ||
|
||
public getTextFromMessage (ctx: OnMessageContext): string { | ||
if (ctx.match?.toString()) { | ||
return ctx.match.toString() | ||
} | ||
|
||
return ctx.message.reply_to_message?.text ?? '' | ||
} | ||
|
||
public async onEvent (ctx: OnMessageContext): Promise<void> { | ||
if (ctx.hasCommand('voice')) { | ||
const text = this.getTextFromMessage(ctx) | ||
await this.onTextToSpeech(ctx, text) | ||
} | ||
} | ||
|
||
public async onTextToSpeech (ctx: OnMessageContext, message: string): Promise<void> { | ||
if (!message) { | ||
await ctx.reply('/voice command should contain text.') | ||
return | ||
} | ||
|
||
if (!ctx.chat?.id) { | ||
throw new Error('Internal error') | ||
} | ||
|
||
const progressMessage = await ctx.reply('Generating...') | ||
|
||
const voiceResult = await gcTextToSpeedClient.textToSpeech(message) | ||
|
||
if (!voiceResult) { | ||
await ctx.api.editMessageText(ctx.chat.id, progressMessage.message_id, 'An error occurred during the process of generating the message.') | ||
return | ||
} | ||
|
||
const inputFile = new InputFile(voiceResult) | ||
|
||
await ctx.api.deleteMessage(ctx.chat.id, progressMessage.message_id) | ||
await ctx.replyWithVoice(inputFile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import fs from 'fs' | ||
import pino from 'pino' | ||
import { InputFile } from 'grammy' | ||
import type { Logger } from 'pino' | ||
import { gcTextToSpeedClient } from '../../google-cloud/gcTextToSpeechClient' | ||
import type { BotPayments } from '../payment' | ||
import { speechToText } from '../open-ai/api/openAi' | ||
import type { OnMessageContext, PayableBot } from '../types' | ||
import config from '../../config' | ||
import { translator } from '../translate/deeplClient' | ||
|
||
export class VoiceTranslateBot implements PayableBot { | ||
private readonly payments: BotPayments | ||
|
||
private readonly logger: Logger | ||
|
||
constructor (payments: BotPayments) { | ||
this.payments = payments | ||
this.logger = pino({ | ||
name: 'VoiceTranslate', | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { colorize: true } | ||
} | ||
}) | ||
} | ||
|
||
public isSupportedEvent (ctx: OnMessageContext): boolean { | ||
const { voice, audio } = ctx.update.message | ||
|
||
if (!config.voiceTranslate.isEnabled) { | ||
return false | ||
} | ||
|
||
return (!!voice || !!audio) | ||
} | ||
|
||
public getEstimatedPrice (ctx: OnMessageContext): number { | ||
const { voice, audio } = ctx.update.message | ||
const seconds = (voice?.duration ?? audio?.duration) ?? 0 | ||
return seconds * 0.005 | ||
} | ||
|
||
public async onEvent (ctx: OnMessageContext): Promise<void> { | ||
const { voice, audio } = ctx.update.message | ||
|
||
if (!(!!voice || !!audio)) { | ||
return | ||
} | ||
|
||
const progressMessage = await ctx.reply('Generating...') | ||
|
||
if (!ctx.chat?.id) { | ||
throw Error('chat id is undefined') | ||
} | ||
|
||
const file = await ctx.getFile() | ||
const path = await file.download() | ||
|
||
let ext = 'ogg' | ||
|
||
if (file.file_path) { | ||
ext = file.file_path.split('.').pop() ?? ext | ||
} | ||
|
||
const filename = path + '.' + ext | ||
fs.renameSync(path, filename) | ||
|
||
const resultText = await speechToText(fs.createReadStream(filename)) | ||
fs.rmSync(filename) | ||
|
||
const translateResult = await translator.translateText(resultText, null, 'en-US') | ||
|
||
const voiceResult = await gcTextToSpeedClient.textToSpeech(translateResult.text) | ||
|
||
if (!voiceResult) { | ||
await ctx.reply('voice generation error') | ||
return | ||
} | ||
|
||
await ctx.api.deleteMessage(ctx.chat.id, progressMessage.message_id) | ||
|
||
const inputFile = new InputFile(voiceResult) | ||
|
||
await ctx.replyWithVoice(inputFile) | ||
} | ||
} |