-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: AI text to speech for proposal body (#244)
* feat: AI text to speech for proposal body * fix: add min proposal body length constraint * fix: return meaningful error message when proposal is not supported * fix: strip markdown before sending to speech AI * fix: fix error thrown when instantiating openAI with empty API KEY * fix: return a more precise error code * fix: fix invalid env var name --------- Co-authored-by: ChaituVR <[email protected]>
- Loading branch information
Showing
6 changed files
with
109 additions
and
16 deletions.
There are no files selected for viewing
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
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,50 @@ | ||
import OpenAI from 'openai'; | ||
import removeMd from 'remove-markdown'; | ||
import Cache from '../cache'; | ||
import { fetchProposal, Proposal } from '../../helpers/snapshot'; | ||
import { IStorage } from '../storage/types'; | ||
|
||
const MIN_BODY_LENGTH = 500; | ||
const MAX_BODY_LENGTH = 4096; | ||
|
||
export default class TextToSpeech extends Cache { | ||
proposal?: Proposal | null; | ||
openAi: OpenAI; | ||
|
||
constructor(id: string, storage: IStorage) { | ||
super(id, storage); | ||
this.filename = `snapshot-proposal-ai-tts-${this.id}.mp3`; | ||
this.openAi = new OpenAI({ apiKey: process.env.OPENAI_API_KEY || 'Missing key' }); | ||
} | ||
|
||
async isCacheable() { | ||
this.proposal = await fetchProposal(this.id); | ||
|
||
if (!this.proposal) { | ||
throw new Error('RECORD_NOT_FOUND'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
getContent = async () => { | ||
this.isCacheable(); | ||
const body = removeMd(this.proposal!.body); | ||
|
||
if (body.length < MIN_BODY_LENGTH || body.length > MAX_BODY_LENGTH) { | ||
throw new Error('UNSUPPORTED_PROPOSAL'); | ||
} | ||
|
||
try { | ||
const mp3 = await this.openAi.audio.speech.create({ | ||
model: 'tts-1', | ||
voice: 'alloy', | ||
input: body | ||
}); | ||
|
||
return Buffer.from(await mp3.arrayBuffer()); | ||
} catch (e: any) { | ||
throw e.error?.code ? new Error(e.error?.code.toUpperCase()) : e; | ||
} | ||
}; | ||
} |
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 |
---|---|---|
|
@@ -2352,6 +2352,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc" | ||
integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw== | ||
|
||
"@types/remove-markdown@^0.3.4": | ||
version "0.3.4" | ||
resolved "https://registry.yarnpkg.com/@types/remove-markdown/-/remove-markdown-0.3.4.tgz#b90c2e7117fbb30e51d5d849afac744715db4f91" | ||
integrity sha512-i753EH/p02bw7bLlpfS/4CV1rdikbGiLabWyVsAvsFid3cA5RNU1frG7JycgY+NSnFwtoGlElvZVceCytecTDA== | ||
|
||
"@types/semver@^7.5.0": | ||
version "7.5.3" | ||
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.3.tgz#9a726e116beb26c24f1ccd6850201e1246122e04" | ||
|
@@ -5973,6 +5978,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/rehackt/-/rehackt-0.0.3.tgz#1ea454620d4641db8342e2db44595cf0e7ac6aa0" | ||
integrity sha512-aBRHudKhOWwsTvCbSoinzq+Lej/7R8e8UoPvLZo5HirZIIBLGAgdG7SL9QpdcBoQ7+3QYPi3lRLknAzXBlhZ7g== | ||
|
||
remove-markdown@^0.5.0: | ||
version "0.5.0" | ||
resolved "https://registry.yarnpkg.com/remove-markdown/-/remove-markdown-0.5.0.tgz#a596264bbd60b9ceab2e2ae86e5789eee91aee32" | ||
integrity sha512-x917M80K97K5IN1L8lUvFehsfhR8cYjGQ/yAMRI9E7JIKivtl5Emo5iD13DhMr+VojzMCiYk8V2byNPwT/oapg== | ||
|
||
require-directory@^2.1.1: | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" | ||
|