Ancient Brain
  
 
Code viewer for World: Practical 2
const htmlContent = `
  <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Speech-to-Text</title>
    </head>
    <body>
      <input type="file" id="audioFileInput" accept=".mp3, .wav">
      <script src="your_script.js"></script>
    </body>
    </html>
`;

// Create a new document and set its content to the HTML
const newDocument = new DOMParser().parseFromString(htmlContent, 'text/html');

// Append the new document's body to the current document's body
document.body.appendChild(newDocument.body);

// Replace 'YOUR_API_KEY' with your actual Rev.ai API key
const apiKey = '02C_ac_fneC3eG3c8_xNokCR7HXwVyj2FJVMxO1evOHr02JtQ7SGOl7gBXW1Smn63vujWEdYpH-VoL8z8VBqWnwT5lWWw';

// Replace 'https://api.rev.ai/revspeech/v1beta/speech' with the Rev.ai API endpoint
const apiUrl = 'https://api.rev.ai/speechtotext/v1';

// Input for selecting the audio file in the browser
const audioFileInput = document.getElementById('audioFileInput');

audioFileInput.addEventListener('change', handleFileSelect);

function handleFileSelect(event) {
  const selectedFile = event.target.files[0];

  if (selectedFile) {
    // Read the selected audio file using FileReader
    const reader = new FileReader();

    reader.onload = function (e) {
      const audioFileBase64 = e.target.result.split(',')[1]; // Extract base64 data

      // Make a POST request to Rev.ai API
      fetch(apiUrl, {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${apiKey}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({media_url: 'https://www.youtube.com/watch?v=1RWXHI4mm6E&ab_channel=PizzaMusic'})
      })
        .then(response => {
          if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
          }
          return response.json();
        })
        .then(data => {
          // Handle the transcription data
          console.log(data);
        })
        .catch(error => {
          console.error('Fetch error:', error.message);
        });
    };

    // Read the file as data URL
    reader.readAsDataURL(selectedFile);
  }
}