File size: 3,141 Bytes
ab550cf
4c56550
ab550cf
4c56550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab550cf
 
 
4c56550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab550cf
4c56550
 
6603906
4c56550
 
 
ab550cf
 
5e5276d
ab550cf
 
4c56550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab550cf
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
    <title>AI Chat</title>
</head>
<body>
    <div id="chat-container"></div>
    
    <input type="text" id="text-input" placeholder="Type your message here">
    <button onclick="sendMessage(document.getElementById('text-input').value)">Send Text</button>
    <button onclick="startRecording()">Start Recording</button>
    
    <audio id="ai-voice" controls style="display: none;"></audio>
    
    <script>
        let recognition = new webkitSpeechRecognition();
        recognition.lang = 'en-US';
        recognition.continuous = false;
        
        function startRecording() {
            recognition.start();
            recognition.onresult = function(event) {
                let userMessage = event.results[0][0].transcript;
                sendMessage(userMessage);

                // Stop recording after successful transcription
                recognition.stop();
            }
            
            recognition.onspeechend = function() {
                recognition.stop();
            }
            
            recognition.onerror = function(event) {
                console.error('Speech recognition error:', event.error);
                recognition.stop();
            }
        }
        
        async function sendMessage(userMessage) {
            if (userMessage.trim() === '') {
                alert("Please enter a message.");
                return;
            }
            
            document.getElementById("chat-container").innerHTML += "<p>User: " + userMessage + "</p>";
            
            try {
                const response = await fetch('/chat', {
                    method: 'POST',
                    body: new URLSearchParams({ user_input: userMessage }),
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                });
                

                const data = await response.text();
                document.getElementById("chat-container").innerHTML += "<p>AI: " + data + "</p>";

                // Convert text response to voice
                let ap = 'hf';
                let a = '_jOgmLjsGmnjFcJnoStasSfaKgjzZMiDXfh';


                const voiceResponse = await fetch('https://api-inference.huggingface.co/models/facebook/fastspeech2-en-ljspeech', {
                method: 'POST',
                headers: {
                           'Authorization': `Bearer ${ap}${a}`,
                           'Content-type': 'application/json'
                  },
              body: JSON.stringify({ "inputs": data })
                 });

                const audioData = await voiceResponse.arrayBuffer();
                const audioContext = new AudioContext();
                const audioBuffer = await audioContext.decodeAudioData(audioData);
                const source = audioContext.createBufferSource();
                source.buffer = audioBuffer;
                source.connect(audioContext.destination);
                source.start();
                
            } catch (error) {
                console.error("Error:", error);
            }
        }
    </script>
</body>
</html>