File size: 4,300 Bytes
943c8bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f924031
 
943c8bf
 
 
 
 
 
f924031
 
943c8bf
 
f924031
943c8bf
 
 
 
 
f924031
 
943c8bf
 
f924031
943c8bf
 
 
f924031
 
 
 
943c8bf
 
 
 
 
f924031
 
 
 
 
 
 
 
 
 
 
 
 
 
943c8bf
 
 
 
f924031
 
 
943c8bf
f924031
 
 
 
 
 
 
 
 
 
943c8bf
 
 
f924031
 
943c8bf
 
 
 
f924031
943c8bf
f924031
943c8bf
 
 
 
 
f924031
 
 
 
 
 
 
943c8bf
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f7fb;
            margin: 0;
            padding: 0;
        }
        #chat-container {
            max-width: 500px;
            margin: auto;
            padding: 25px;
            border-radius: 15px;
            background-color: #fff;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            position: relative;
        }
        .btn-mic, .btn-send {
            background-color: #007bff;
            color: white;
            border-radius: 8px;
            padding: 10px 15px;
            border: none;
            cursor: pointer;
            font-size: 1.1em;
            transition: background-color 0.3s;
        }
        .btn-mic:hover, .btn-send:hover {
            background-color: #0056b3;
        }
    </style>
    <title>Voice-based ChatGPT</title>
</head>
<body>
    <div id="chat-container">
        <div class="input-group">
            <button id="mic-button" class="btn-mic"><i class="fas fa-microphone"></i></button>
            <button id="send-button" class="btn-send"><i class="fas fa-paper-plane"></i></button>
        </div>
    </div>

    <script>
        const synth = window.speechSynthesis;

        // Speech-to-Text function
        function startListening() {
            const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
            recognition.lang = "en-US";
            recognition.interimResults = false;
            recognition.onresult = (event) => {
                const transcript = event.results[0][0].transcript;
                sendMessage(transcript);
            };
            recognition.onerror = (event) => {
                console.error("Speech recognition error:", event.error);
                speak("Sorry, I couldn't understand you.");
            };
            recognition.start();
        }

        // Send message to server
        async function sendMessage(message) {
            if (!message.trim()) return;
            addMessage("User", message, "user-message");

            try {
                // Simulate bot typing
                const botMessage = "Bot is typing...";
                addMessage("Bot", botMessage, "bot-message");

                // Simulate a server response (replace with actual server request)
                const response = await new Promise(resolve => setTimeout(() => resolve({ response: "Hello! How can I assist you today?" }), 1000));
                const botMessageContent = response.response;

                addMessage("Bot", botMessageContent, "bot-message");
                speak(botMessageContent);
            } catch (error) {
                console.error("Error:", error);
                const errorMessage = "Sorry, something went wrong.";
                addMessage("Bot", errorMessage, "bot-message");
                speak(errorMessage);
            }
        }

        // Text-to-Speech function
        function speak(text) {
            const utterance = new SpeechSynthesisUtterance(text);
            utterance.lang = "en-US";
            utterance.pitch = 1;
            utterance.rate = 1;
            synth.speak(utterance);
        }

        // Add message to chat history
        function addMessage(sender, message, className) {
            const chatHistory = document.getElementById("chat-container");
            const messageElement = document.createElement("div");
            messageElement.className = `message ${className}`;
            messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
            chatHistory.appendChild(messageElement);
        }

        // Event Listeners
        document.getElementById("send-button").addEventListener("click", startListening);
        document.getElementById("mic-button").addEventListener("click", startListening);
    </script>
</body>
</html>