File size: 1,616 Bytes
82a5961
 
 
 
 
 
 
 
 
 
 
 
 
 
4ea119b
82a5961
 
 
 
 
 
 
4ea119b
 
 
 
82a5961
4ea119b
 
 
 
 
 
 
 
 
 
82a5961
 
 
 
 
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
39
40
41
42
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:80/text-to-speech/', { // Updated port to 80
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ text: text }),
            });

            if (!response.ok) {
                const errorData = await response.json();
                statusDiv.textContent = `Error: ${errorData.detail || response.statusText}`;
                return;
            }

            const blob = await response.blob();
            const audioUrl = URL.createObjectURL(blob);

            statusDiv.textContent = 'Speech generated successfully!';
            downloadLink.href = audioUrl;
            downloadLink.style.display = 'block';
            audioPlayer.src = audioUrl;
            audioPlayer.style.display = 'block';

        } catch (error) {
            statusDiv.textContent = `Network error: ${error}`;
        }
    });
});