2020 padding : 2rem ;
2121 border-radius : 8px ;
2222 box-shadow : 0 4px 6px rgba (0 , 0 , 0 , 0.1 );
23- max-width : 600 px ;
23+ max-width : 800 px ;
2424 width : 100% ;
2525 }
2626 button {
4444 # apiResponse {
4545 margin-top : 1rem ;
4646 text-align : left;
47- white-space : pre-wrap;
47+ overflow-x : auto;
48+ }
49+ # formattedResponse {
50+ background-color : # f8f8f8 ;
51+ padding : 1rem ;
52+ border-radius : 4px ;
53+ margin-bottom : 1rem ;
54+ }
55+ # jsonResponse {
4856 background-color : # f8f8f8 ;
4957 padding : 1rem ;
5058 border-radius : 4px ;
@@ -60,10 +68,15 @@ <h1>OpenAI Audio</h1>
6068 < audio id ="audioPlayback " controls > </ audio >
6169 < textarea id ="prompt " placeholder ="Enter your prompt here "> </ textarea >
6270 < button id ="submitButton "> Submit to API</ button >
63- < div id ="apiResponse "> </ div >
71+ < div id ="apiResponse ">
72+ < div id ="formattedResponse "> </ div >
73+ < pre id ="jsonResponse "> </ pre >
74+ </ div >
6475 </ div >
6576
66- < script >
77+ < script type ="module ">
78+ import { marked } from "https://esm.run/marked" ;
79+
6780 let audioContext ;
6881 let recorder ;
6982 let audioChunks = [ ] ;
@@ -76,7 +89,8 @@ <h1>OpenAI Audio</h1>
7689 const audioPlayback = document . getElementById ( 'audioPlayback' ) ;
7790 const promptTextarea = document . getElementById ( 'prompt' ) ;
7891 const submitButton = document . getElementById ( 'submitButton' ) ;
79- const apiResponse = document . getElementById ( 'apiResponse' ) ;
92+ const formattedResponse = document . getElementById ( 'formattedResponse' ) ;
93+ const jsonResponse = document . getElementById ( 'jsonResponse' ) ;
8094
8195 recordButton . addEventListener ( 'click' , toggleRecording ) ;
8296 submitButton . addEventListener ( 'click' , submitToAPI ) ;
@@ -217,6 +231,9 @@ <h1>OpenAI Audio</h1>
217231 return ;
218232 }
219233
234+ submitButton . textContent = 'Processing prompt...' ;
235+ submitButton . disabled = true ;
236+
220237 const base64Audio = await blobToBase64 ( audioBlob ) ;
221238 const prompt = promptTextarea . value ;
222239
@@ -251,13 +268,48 @@ <h1>OpenAI Audio</h1>
251268 } ) ;
252269
253270 const data = await response . json ( ) ;
254- apiResponse . textContent = JSON . stringify ( data , null , 2 ) ;
271+ displayFormattedResponse ( data ) ;
255272 } catch ( error ) {
256273 console . error ( 'Error:' , error ) ;
257- apiResponse . textContent = `Error: ${ error . message } ` ;
274+ formattedResponse . innerHTML = marked ( `Error: ${ error . message } ` ) ;
275+ jsonResponse . textContent = '' ;
276+ } finally {
277+ submitButton . textContent = 'Submit to API' ;
278+ submitButton . disabled = false ;
258279 }
259280 }
260281
282+ function displayFormattedResponse ( data ) {
283+ let mdResponse = '' ;
284+
285+ // Display content from choices
286+ if ( data . choices && data . choices . length > 0 ) {
287+ const contents = data . choices . map ( choice => choice . message . content ) . join ( '\n\n' ) ;
288+ mdResponse += `### Response Content\n\n${ contents } \n\n` ;
289+ }
290+
291+ // Calculate and display token usage and cost
292+ if ( data . usage ) {
293+ const textTokens = data . usage . prompt_tokens_details . text_tokens || 0 ;
294+ const audioTokens = data . usage . prompt_tokens_details . audio_tokens || 0 ;
295+ const textCost = ( textTokens * 2.50 ) / 1000000 ;
296+ const audioCost = ( audioTokens * 100.00 ) / 1000000 ;
297+ const totalCostDollars = textCost + audioCost ;
298+ const totalCostCents = totalCostDollars * 100 ;
299+
300+ mdResponse += `### Token Usage and Cost\n\n` ;
301+ mdResponse += `- Text input tokens: ${ textTokens } \n` ;
302+ mdResponse += `- Audio input tokens: ${ audioTokens } \n` ;
303+ mdResponse += `- Total cost: ${ totalCostCents . toFixed ( 4 ) } cents\n\n` ;
304+ }
305+
306+ // Render markdown response
307+ formattedResponse . innerHTML = marked ( mdResponse ) ;
308+
309+ // Display full JSON response
310+ jsonResponse . textContent = JSON . stringify ( data , null , 2 ) ;
311+ }
312+
261313 function getAPIKey ( ) {
262314 let apiKey = localStorage . getItem ( 'openai_api_key' ) ;
263315 if ( ! apiKey ) {
@@ -279,4 +331,4 @@ <h1>OpenAI Audio</h1>
279331 }
280332 </ script >
281333</ body >
282- </ html >
334+ </ html >
0 commit comments