File size: 1,536 Bytes
7651129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', () => {
    const convertButton = document.getElementById('convertButton');
    const inputText = document.getElementById('inputText');
    const statusDiv = document.getElementById('status');
    const downloadLink = document.getElementById('downloadLink');
    const audioPlayer = document.getElementById('audioPlayer');

    convertButton.addEventListener('click', async () => {
        const text = inputText.value;
        statusDiv.textContent = 'Processing...';
        downloadLink.style.display = 'none';
        audioPlayer.style.display = 'none';

        try {
            const response = await fetch('http://localhost:5000/text-to-speech/', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ text: text }),
            });

            const data = await response.json();

            if (data.status === 'success') {
                statusDiv.textContent = 'Speech generated successfully!';
                downloadLink.href = 'http://localhost:5000' + data.url;
                downloadLink.style.display = 'block';
                audioPlayer.src = 'http://localhost:5000' + data.url;
                audioPlayer.style.display = 'block';
            } else {
                statusDiv.textContent = `Error: ${data.message}`;
            }
        } catch (error) {
            statusDiv.textContent = `Network error: ${error}`;
        }
    });
});