Skip to content

Commit

Permalink
text to speech api πŸ”Š
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwit-y committed Nov 11, 2024
1 parent c1198bd commit a0f5601
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
sound
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"zod-validation-error": "npm:zod-validation-error@^3.4.0"
},
"tasks": {
"start": "deno run --allow-env --allow-net main.ts",
"start": "deno run --allow-env --allow-net --allow-write main.ts",
"watch": "deno run --allow-net --watch main.ts"
},
"compilerOptions": {
Expand Down
17 changes: 16 additions & 1 deletion libs/api/openai.api.ts β†’ libs/api/openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { injectable } from "inversify";
import { injectable } from "inversify";
import OpenAI from "openai";
import { Env } from "../utils/config/index.ts";
// import path from "node:path";

// import { Buffer } from "node:buffer";
// import fs from "node:fs";

const openai = new OpenAI({
apiKey: Env.openaiApiKey,
Expand Down Expand Up @@ -32,4 +36,15 @@ export class OpenAIAPI {
},
});
}

public async speech(text: string) {
const res = await openai.audio.speech.create({
model: "tts-1",
voice: "alloy",
input: text,
});

return res
// console.log(mp3);
}
}
8 changes: 8 additions & 0 deletions libs/mod/vocabulary/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const vocabularyRoutes = new Hono()
const srv = container.get<IVocabularyService>(Instances.VocabularyService);
const res = await srv.insert(body.word);
return c.json(res);
})
.get("/speech", async (c) => {
const text = await c.req.query("text");
if(!text) return c.json({ error: "text is required" }, 400);

const srv = container.get<IVocabularyService>(Instances.VocabularyService);
const res = await srv.speech(text);
return c.json(res);
});

export default vocabularyRoutes;
18 changes: 15 additions & 3 deletions libs/mod/vocabulary/service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { transform } from './../../utils/common/transform.ts';
import { transform } from "./../../utils/common/transform.ts";
import { injectable, inject } from "inversify";
import type { IVocabularyRepository } from "./repository.ts";
import { OpenAIAPI } from "../../api/openai.api.ts";

import "reflect-metadata";
import type { OpenAIAPI } from "../../api/openai.ts";

import fs from "node:fs";
import path from "node:path";
import { Buffer } from "node:buffer";

export interface IVocabularyService {
insert(word: string): Promise<any>;
speech(text: string): Promise<any>;
}

@injectable()
Expand All @@ -22,6 +27,13 @@ export class VocabularyService implements IVocabularyService {
this._repo = repo;
this._openai = openai;
}
public async speech(text: string): Promise<any> {
const res = await this._openai.speech(text);

const speechFile = path.resolve(`./sound/${text}.mp3`);
const buffer = Buffer.from(await res.arrayBuffer());
await fs.promises.writeFile(speechFile, buffer);
}

// public async auth() {
// const res = await this._repo.auth();
Expand All @@ -40,7 +52,7 @@ export class VocabularyService implements IVocabularyService {
console.log(`call openai: ${word}`);
const res = await this._openai.translate(word);
const content = res.choices[0].message.content ?? "";
const {thai, english, example, type, remark} = transform(content);
const { thai, english, example, type, remark } = transform(content);
// console.log("==============")
// console.log(content)
// // console.log(data);
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/config/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
VocabularyRepository,
type IVocabularyRepository,
} from "../../mod/vocabulary/repository.ts";
import { OpenAIAPI } from "../../api/openai.api.ts";
import {
VocabularyService,
type IVocabularyService,
Expand All @@ -14,6 +13,7 @@ import {
} from "../../mod/auth/repository.ts";
import { UserService, type IUserService } from "../../mod/auth/service.ts";
import { SupabaseDB, type ISupabaseDB } from "../db/index.ts";
import { OpenAIAPI } from "../../api/openai.ts";

enum Instances {
SupabaseDB = "SupabaseDB",
Expand Down
20 changes: 20 additions & 0 deletions test/openai.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { assertEquals } from "jsr:@std/assert";
import { OpenAIAPI } from "../libs/api/openai.ts";

// Deno.test("openai", async () => {
// const openai = new OpenAIAPI();
// await openai.speech('beer')

// assertEquals(1, 1);
// });

Deno.test({
name: "leaky test",
async fn() {
const openai = new OpenAIAPI();
await openai.speech("beer");

assertEquals(1, 1);
},
sanitizeResources: false,
});
File renamed without changes.

0 comments on commit a0f5601

Please sign in to comment.