Team Ai
Apppublic

mandada/forced-alignment-server

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
test-alignment.js81 linesDownload Raw Back to src
1const axios = require('axios');2 3const API_BASE_URL = 'http://localhost:3000';4// Using a short, public audio file for a quick test.5const AUDIO_URL = 'https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav';6const TEXT = "Life moves pretty fast. If you don't stop and look around once in a while, you could miss it.";7 8/**9 * Polls the /alignment-status endpoint until the job is complete or fails.10 */11const pollStatus = async (payload) => {12  console.log('Polling for alignment status...');13  try {14    const response = await axios.post(`${API_BASE_URL}/alignment-status`, payload);15    const { status, result } = response.data;16 17    console.log(`Current status: ${status}`);18 19    if (status === 'completed') {20      console.log('\n✅ Alignment successful!\n');21      console.log('Final Result:');22      console.log(JSON.stringify(result, null, 2));23      return true; // Done24    }25    if (status === 'failed') {26      console.error('\n❌ Alignment failed.\n');27      console.error(response.data.error);28      return true; // Done29    }30    return false; // Not done, poll again31  } catch (error) {32    console.error('Error polling status:', error.response ? error.response.data : error.message);33    return true; // Stop polling on error34  }35};36 37/**38 * Main function to start the alignment test.39 */40const testAlignment = async () => {41  const payload = {42    audioUrl: AUDIO_URL,43    pdfText: TEXT,44  };45 46  try {47    console.log('Sending alignment request to the server...');48    const initialResponse = await axios.post(`${API_BASE_URL}/align`, payload);49    console.log('Server responded:', initialResponse.data);50 51    // If the result is already in the cache, print it and exit.52    if (initialResponse.status === 200 && initialResponse.data.status === 'cached') {53        console.log('\n✅ Result was already cached:\n');54        console.log(JSON.stringify(initialResponse.data.result, null, 2));55        return;56    }57 58    // If the job was just submitted, start polling for the result.59    if (initialResponse.status === 202) {60      // Use a recursive setTimeout to keep the process alive while polling61      const pollForCompletion = async () => {62        const isDone = await pollStatus(payload);63        if (!isDone) {64          setTimeout(pollForCompletion, 5000); // Poll again in 5 seconds65        }66      };67      await pollForCompletion();68      return;69    }70 71    // Handle any other unexpected responses.72    console.error('Unexpected initial response status:', initialResponse.status);73 74  } catch (error) {75    console.error('Error starting alignment job:', error.response ? error.response.data : error.message);76  }77};78 79// Run the test80testAlignment();81