srivatsavdamaraju commited on
Commit
57c9567
1 Parent(s): baa1a8a

Create index2.html

Browse files
Files changed (1) hide show
  1. index2.html +184 -0
index2.html ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Chatbot with Text-to-Speech</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ margin: 0;
11
+ padding: 0;
12
+ background-color: #f5f5f5;
13
+ }
14
+
15
+ #chat-container {
16
+ position: fixed;
17
+ bottom: 20px;
18
+ right: 20px;
19
+ width: 350px;
20
+ height: 500px;
21
+ background-color: #fff;
22
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
23
+ border-radius: 8px;
24
+ overflow: hidden;
25
+ z-index: 1000; /* Ensure it's on top */
26
+ }
27
+
28
+ #chat-header {
29
+ background-color: #007bff;
30
+ color: white;
31
+ padding: 10px;
32
+ text-align: center;
33
+ }
34
+
35
+ #chat-body {
36
+ padding: 10px;
37
+ height: calc(100% - 100px);
38
+ overflow-y: auto;
39
+ background-color: #fafafa;
40
+ }
41
+
42
+ #chat-footer {
43
+ padding: 10px;
44
+ background-color: #f1f1f1;
45
+ display: flex;
46
+ justify-content: space-between;
47
+ }
48
+
49
+ input[type="text"] {
50
+ width: 80%;
51
+ padding: 8px;
52
+ border-radius: 5px;
53
+ border: 1px solid #ccc;
54
+ }
55
+
56
+ button {
57
+ padding: 8px 16px;
58
+ border: none;
59
+ background-color: #007bff;
60
+ color: white;
61
+ border-radius: 5px;
62
+ cursor: pointer;
63
+ }
64
+
65
+ button:hover {
66
+ background-color: #0056b3;
67
+ }
68
+ </style>
69
+ </head>
70
+ <body>
71
+
72
+ <!-- Chatbot Container -->
73
+ <div id="chat-container">
74
+ <div id="chat-header">Chatbot</div>
75
+ <div id="chat-body"></div>
76
+ <div id="chat-footer">
77
+ <input type="text" id="user-input" placeholder="Type your message..." />
78
+ <button id="send-btn">Send</button>
79
+ </div>
80
+ </div>
81
+
82
+ <script type="module">
83
+ import Chatbot from 'https://cdn.jsdelivr.net/npm/flowise-embed/dist/web.js';
84
+
85
+ let latestBotResponse = '';
86
+ let lastPlayedMessage = '';
87
+ let audio = null; // Store the audio instance to stop playback
88
+
89
+ // Initialize the chatbot
90
+ Chatbot.init({
91
+ chatflowid: '7db12b23-ad83-4da7-9f8d-d0d8e20477d2',
92
+ apiHost: 'https://srivatsavdamaraju-flowiseollama.hf.space',
93
+ observersConfig: {
94
+ observeMessages: (messages) => {
95
+ // Get the latest bot response
96
+ const botMessages = messages.filter(msg => msg.type === 'apiMessage');
97
+ if (botMessages.length > 0) {
98
+ const newBotResponse = botMessages[botMessages.length - 1].message;
99
+
100
+ // Trigger TTS only for new responses
101
+ if (newBotResponse !== latestBotResponse) {
102
+ latestBotResponse = newBotResponse;
103
+ playTTS();
104
+ }
105
+ }
106
+
107
+ // Render messages in the chat
108
+ renderMessages(messages);
109
+ },
110
+ },
111
+ });
112
+
113
+ // Function to render messages in the chat window
114
+ function renderMessages(messages) {
115
+ const chatBody = document.getElementById('chat-body');
116
+ chatBody.innerHTML = '';
117
+
118
+ messages.forEach(message => {
119
+ const messageDiv = document.createElement('div');
120
+ messageDiv.textContent = message.message;
121
+ chatBody.appendChild(messageDiv);
122
+ });
123
+
124
+ chatBody.scrollTop = chatBody.scrollHeight;
125
+ }
126
+
127
+ // Function to trigger TTS API and play audio
128
+ async function playTTS() {
129
+ if (!latestBotResponse || latestBotResponse === lastPlayedMessage) {
130
+ return; // Prevent duplicate audio playback
131
+ }
132
+
133
+ const data = {
134
+ text: latestBotResponse,
135
+ target_language: "english",
136
+ tts_language: "en",
137
+ tts_gender: "female",
138
+ tts_rate: "+0%",
139
+ tts_volume: "+0%",
140
+ tts_pitch: "+0Hz",
141
+ chatgpt_api_key: "sk-proj-at87SxqXJuEy0T0ExteiT3BlbkFJ2C9wS9z9wue4eucYSEKb"
142
+ };
143
+
144
+ try {
145
+ const response = await fetch('https://api-tts-marco.gov-cloud.ai/tts', {
146
+ method: 'POST',
147
+ headers: { 'Content-Type': 'application/json' },
148
+ body: JSON.stringify(data)
149
+ });
150
+
151
+ if (response.ok) {
152
+ const audioData = await response.arrayBuffer();
153
+ const audioBlob = new Blob([audioData], { type: 'audio/mpeg' });
154
+ const audioUrl = URL.createObjectURL(audioBlob);
155
+
156
+ // Stop previous audio if playing
157
+ if (audio) {
158
+ audio.pause();
159
+ audio.currentTime = 0;
160
+ }
161
+
162
+ // Play new audio
163
+ audio = new Audio(audioUrl);
164
+ audio.play();
165
+ lastPlayedMessage = latestBotResponse; // Update the last played message
166
+ } else {
167
+ console.error('TTS request failed:', response.statusText);
168
+ }
169
+ } catch (error) {
170
+ console.error('TTS Error:', error);
171
+ }
172
+ }
173
+
174
+ // Event listener for the Escape key to stop audio playback
175
+ document.addEventListener('keydown', (event) => {
176
+ if (event.key === 'Escape' && audio) {
177
+ audio.pause();
178
+ audio.currentTime = 0; // Reset audio to start
179
+ }
180
+ });
181
+ </script>
182
+
183
+ </body>
184
+ </html>