1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Hono } from "npm:hono@3";
import { html } from "npm:hono@3/html";
import { cors } from 'npm:hono/cors';
import { OpenAI } from "npm:openai";
const app = new Hono();
const openai = new OpenAI(Deno.env.get("OPENAI_API_KEY"));
class TranscriptionService {
async transcribeAudio(audioFile) {
try {
const transcription = await openai.audio.transcriptions.create({
file: audioFile,
model: "whisper-1",
response_format: "text",
});
return transcription;
} catch (error) {
console.error('OpenAI API error:', error);
throw error;
}
}
}
export const transcriptionService = new TranscriptionService();
app.use('*', cors({
origin: '*',
allowMethods: ['GET', 'POST'],
allowHeaders: ['Content-Type'],
}));
app.get("/", async (c) => {
const resHtml = html`
<html>
<head>
<title>OpenAI Whisper Transcription Example</title>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style>
#recordButton {
transition: all 0.2s;
}
#recordButton:active {
transform: scale(0.95);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}
#recordButton.recording {
background-color: #ff0000;
}
</style>
</head>
<body>
<div class="container mx-auto py-8">
<h1 class="text-4xl font-bold mb-4">OpenAI Whisper Transcription Example</h1>
<button id="recordButton" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded mb-4">
Record Audio
</button>
<div class="mt-4">
<h2 class="text-xl font-bold mb-2">Transcription:</h2>
<div id="output" class="border border-gray-300 rounded p-4">
${c.req.query('transcription') || ''}
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const recordButton = document.getElementById('recordButton');
let recorder;
recordButton.addEventListener('mousedown', async () => {
if (!recorder) {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
recorder = new MediaRecorder(stream);
recorder.start();
recordButton.classList.add('recording');
}
});
recordButton.addEventListener('mouseup', async () => {
if (recorder) {
recorder.stop();
recordButton.classList.remove('recording');
const chunks = [];
recorder.ondataavailable = (e) => chunks.push(e.data);
recorder.onstop = async () => {
const blob = new Blob(chunks, { type: 'audio/wav' });
const formData = new FormData();
formData.append('audio', blob, 'recording.wav');
const response = await fetch('/transcribe', { method: 'POST', body: formData });
if (response.ok) {
const transcription = await response.text();
document.getElementById('output').textContent = transcription;
} else {
const errorMessage = await response.text();
document.getElementById('output').textContent = "Error:" + errorMessage;
}
recorder = null;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
yawnxyz-voice.web.val.run
v44
May 10, 2024