|
1 |
| -import { serve } from "https://deno.land/[email protected]/http/server.ts"; |
2 |
| -import OpenAI, { toFile } from "https://deno.land/x/[email protected]/mod.ts"; |
| 1 | +import { serve } from 'https://deno.land/std/http/server.ts'; |
| 2 | +import { multiParser } from 'https://deno.land/x/[email protected]/mod.ts'; |
| 3 | +import OpenAI, { toFile } from 'https://deno.land/x/[email protected]/mod.ts'; |
3 | 4 |
|
4 |
| -import { corsHeaders } from "../common/cors.ts"; |
5 |
| -import { supabaseClient } from "../common/supabaseClient.ts"; |
| 5 | +import { corsHeaders } from '../common/cors.ts'; |
| 6 | +import { supabaseClient } from '../common/supabaseClient.ts'; |
6 | 7 |
|
7 | 8 | const processAudio = async (req: Request) => {
|
8 |
| - |
9 |
| - if (req.method !== "POST") { |
10 |
| - return new Response("Method Not Allowed", { status: 405 }); |
11 |
| - } |
| 9 | + if (req.method !== 'POST') { |
| 10 | + return new Response('Method Not Allowed', { status: 405 }); |
| 11 | + } |
12 | 12 |
|
13 |
| - const supabase = supabaseClient(req); |
14 |
| - const openaiClient = new OpenAI({ |
15 |
| - apiKey: Deno.env.get("OPENAI_API_KEY"), |
16 |
| - }); |
| 13 | + const supabase = supabaseClient(req); |
| 14 | + const openaiClient = new OpenAI({ |
| 15 | + apiKey: Deno.env.get('OPENAI_API_KEY'), |
| 16 | + }); |
17 | 17 |
|
18 |
| - // Validate Content-Type |
19 |
| - const contentType = req.headers.get("Content-Type") || ""; |
20 |
| - if (!contentType.includes("audio/wav") && !contentType.includes("audio/x-wav")) { |
21 |
| - return new Response("Unsupported Media Type", { status: 415 }); |
22 |
| - } |
| 18 | + const contentType = req.headers.get('Content-Type') || ''; |
| 19 | + let arrayBuffer: ArrayBuffer; |
| 20 | + let filenameTimestamp = `audio_${Date.now()}.wav`; |
23 | 21 |
|
24 |
| - const arrayBuffer = await req.arrayBuffer(); |
| 22 | + if (contentType.includes('multipart/form-data')) { |
| 23 | + const form = await multiParser(req); |
| 24 | + if (!form || !form.files || !form.files.file) { |
| 25 | + return new Response('File not found in form', { |
| 26 | + status: 400, |
| 27 | + headers: corsHeaders, |
| 28 | + }); |
| 29 | + } |
| 30 | + console.log('Form:', form); |
| 31 | + const file = form.files.file; |
| 32 | + arrayBuffer = file.content.buffer; |
| 33 | + filenameTimestamp = file.filename || filenameTimestamp; |
| 34 | + } else { |
| 35 | + arrayBuffer = await req.arrayBuffer(); |
| 36 | + } |
25 | 37 |
|
26 |
| - let transcript: string; |
27 |
| - let embeddings: any; |
28 |
| - try { |
29 |
| - const filenameTimestamp = `adeus_wav_${Date.now()}.wav`; |
30 |
| - const wavFile = await toFile(arrayBuffer, filenameTimestamp); |
| 38 | + let transcript: string; |
| 39 | + let embeddings: any; |
| 40 | + try { |
| 41 | + const filenameTimestamp = `adeus_wav_${Date.now()}.wav`; |
| 42 | + const wavFile = await toFile(arrayBuffer, filenameTimestamp); |
| 43 | + console.log(typeof wavFile, wavFile); |
31 | 44 |
|
32 |
| - // const { data, error } = await supabase.storage |
33 |
| - // .from("test") |
34 |
| - // .upload(filenameTimestamp, wavFile); |
| 45 | + // const { data, error } = await supabase.storage |
| 46 | + // .from("test") |
| 47 | + // .upload(filenameTimestamp, wavFile); |
35 | 48 |
|
36 |
| - // if (error) { |
37 |
| - // console.error("Error uploading file:", error); |
38 |
| - // } |
| 49 | + // if (error) { |
| 50 | + // console.error("Error uploading file:", error); |
| 51 | + // } |
39 | 52 |
|
40 |
| - const transcriptResponse = await openaiClient.audio.transcriptions.create({ |
41 |
| - file: await toFile(wavFile, filenameTimestamp), |
42 |
| - model: "whisper-1", |
43 |
| - prompt: |
44 |
| - 'If this audio file does not contain any speech, please return "None"', |
45 |
| - }); |
46 |
| - transcript = transcriptResponse.text; |
47 |
| - let transcriptLowered = transcript.toLowerCase(); |
48 |
| - // ("thank" in transcriptLowered && |
49 |
| - // "watch" in transcriptLowered && |
50 |
| - // "video" in transcriptLowered) |
51 |
| - if ( |
52 |
| - transcript == "None" || |
53 |
| - transcript == "" || |
54 |
| - transcript == null || |
55 |
| - (transcriptLowered.includes("thank") && |
56 |
| - transcriptLowered.includes("watch")) |
57 |
| - ) { |
58 |
| - return new Response(JSON.stringify({ message: "No transcript found." }), { |
59 |
| - headers: { ...corsHeaders, "Content-Type": "application/json" }, |
60 |
| - status: 200, |
61 |
| - }); |
62 |
| - } |
| 53 | + const transcriptResponse = |
| 54 | + await openaiClient.audio.transcriptions.create({ |
| 55 | + file: wavFile, |
| 56 | + model: 'whisper-1', |
| 57 | + prompt: 'If this audio file does not contain any speech, please return "None"', |
| 58 | + }); |
| 59 | + transcript = transcriptResponse.text; |
| 60 | + let transcriptLowered = transcript.toLowerCase(); |
| 61 | + // ("thank" in transcriptLowered && |
| 62 | + // "watch" in transcriptLowered && |
| 63 | + // "video" in transcriptLowered) |
| 64 | + if ( |
| 65 | + transcript == 'None' || |
| 66 | + transcript == '' || |
| 67 | + transcript == null || |
| 68 | + (transcriptLowered.includes('thank') && |
| 69 | + transcriptLowered.includes('watch')) |
| 70 | + ) { |
| 71 | + return new Response( |
| 72 | + JSON.stringify({ message: 'No transcript found.' }), |
| 73 | + { |
| 74 | + headers: { |
| 75 | + ...corsHeaders, |
| 76 | + 'Content-Type': 'application/json', |
| 77 | + }, |
| 78 | + status: 200, |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
63 | 82 |
|
64 |
| - console.log("Transcript:", transcript); |
| 83 | + console.log('Transcript:', transcript); |
65 | 84 |
|
66 |
| - const embeddingsResponse = await openaiClient.embeddings.create({ |
67 |
| - model: "text-embedding-ada-002", |
68 |
| - input: transcript.replace(/\n/g, " ").replace(/\s{2,}/g, " "), |
69 |
| - }); |
70 |
| - embeddings = embeddingsResponse.data[0].embedding; |
71 |
| - console.log("Embeddings:", embeddings); |
| 85 | + const embeddingsResponse = await openaiClient.embeddings.create({ |
| 86 | + model: 'text-embedding-ada-002', |
| 87 | + input: transcript.replace(/\n/g, ' ').replace(/\s{2,}/g, ' '), |
| 88 | + }); |
| 89 | + embeddings = embeddingsResponse.data[0].embedding; |
| 90 | + console.log('Embeddings:', embeddings); |
72 | 91 |
|
73 |
| - const { data, error } = await supabase |
74 |
| - .from("records") |
75 |
| - .insert({ raw_text: transcript, embeddings: embeddings }); |
| 92 | + const { data, error } = await supabase |
| 93 | + .from('records') |
| 94 | + .insert({ raw_text: transcript, embeddings: embeddings }); |
76 | 95 |
|
77 |
| - if (error) { |
78 |
| - console.error("Error inserting record:", error); |
| 96 | + if (error) { |
| 97 | + console.error('Error inserting record:', error); |
| 98 | + } |
| 99 | + } catch (error) { |
| 100 | + console.error('Transcription error:', error); |
| 101 | + return new Response(JSON.stringify({ error: error.message }), { |
| 102 | + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, |
| 103 | + status: 500, |
| 104 | + }); |
79 | 105 | }
|
80 |
| - } catch (error) { |
81 |
| - console.error("Transcription error:", error); |
82 |
| - return new Response(JSON.stringify({ error: error.message }), { |
83 |
| - headers: { ...corsHeaders, "Content-Type": "application/json" }, |
84 |
| - status: 500, |
85 |
| - }); |
86 |
| - } |
87 | 106 |
|
88 |
| - return new Response( |
89 |
| - JSON.stringify({ message: "Audio transcribed successfully.", transcript }), |
90 |
| - { |
91 |
| - headers: { ...corsHeaders, "Content-Type": "application/json" }, |
92 |
| - status: 200, |
93 |
| - } |
94 |
| - ); |
| 107 | + return new Response( |
| 108 | + JSON.stringify({ |
| 109 | + message: 'Audio transcribed successfully.', |
| 110 | + transcript, |
| 111 | + }), |
| 112 | + { |
| 113 | + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, |
| 114 | + status: 200, |
| 115 | + } |
| 116 | + ); |
95 | 117 | };
|
96 | 118 |
|
97 | 119 | serve(processAudio);
|
0 commit comments