Spaces:
Sleeping
Sleeping
| <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; | |
| } | |
| #chat-history { | |
| height: 400px; | |
| overflow-y: auto; | |
| padding: 15px; | |
| background: linear-gradient(135deg, #f3f4f8 0%, #d3e0f0 100%); | |
| border-radius: 15px; | |
| box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1); | |
| margin-bottom: 15px; | |
| } | |
| .message { | |
| padding: 12px 15px; | |
| margin: 10px 0; | |
| border-radius: 20px; | |
| font-size: 0.9em; | |
| line-height: 1.4em; | |
| position: relative; | |
| } | |
| .user-message { | |
| background: linear-gradient(135deg, #c9e6f5 0%, #91c9e6 100%); | |
| color: #006c8e; | |
| margin-left: auto; | |
| border-radius: 20px 20px 0 20px; | |
| } | |
| .bot-message { | |
| background: linear-gradient(135deg, #fce0e0 0%, #85cfb6 100%); | |
| color: #8b3e40; | |
| margin-right: auto; | |
| border-radius: 20px 20px 20px 0; | |
| } | |
| .user-icon, .bot-icon { | |
| margin-right: 10px; | |
| font-size: 1.2em; | |
| } | |
| #user-input { | |
| border: 1px solid #ddd; | |
| border-radius: 8px; | |
| resize: none; | |
| padding: 12px; | |
| font-size: 1em; | |
| width: calc(10% - 48px); | |
| } | |
| #user-input:focus { | |
| border-color: #007bff; | |
| outline: none; | |
| } | |
| .btn-send, .btn-mic { | |
| 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-send:hover, .btn-mic:hover { | |
| background-color: #0056b3; | |
| } | |
| #language-selector { | |
| margin-bottom: 10px; | |
| } | |
| </style> | |
| <title>Multilanguage Voice Chatbot</title> | |
| </head> | |
| <body> | |
| <div id="chat-container"> | |
| <select id="language-selector" class="form-control"> | |
| <option value="en-US" selected>English</option> | |
| <option value="hi-IN">Hindi</option> | |
| <option value="es-ES">Spanish</option> | |
| <option value="fr-FR">French</option> | |
| <option value="te-IN">Telugu</option> | |
| </select> | |
| <div id="chat-history"></div> | |
| <div class="input-group"> | |
| <textarea id="user-input" class="form-control" rows="1" placeholder="Type or speak your message..." aria-label="Message input"></textarea> | |
| <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 chatHistoryArray = []; | |
| const synth = window.speechSynthesis; | |
| let currentLanguage = "en-US"; // Default language | |
| // Handle language selection | |
| document.getElementById("language-selector").addEventListener("change", (event) => { | |
| currentLanguage = event.target.value; | |
| }); | |
| // Add message to chat history | |
| function addMessage(sender, message, className) { | |
| const chatHistory = document.getElementById("chat-history"); | |
| const messageElement = document.createElement("div"); | |
| messageElement.className = `message ${className}`; | |
| messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`; | |
| chatHistory.appendChild(messageElement); | |
| chatHistory.scrollTop = chatHistory.scrollHeight; | |
| } | |
| // Send message to server | |
| async function sendMessage() { | |
| const input = document.getElementById("user-input"); | |
| const message = input.value.trim(); | |
| if (!message) return; | |
| addMessage("User", message, "user-message"); | |
| chatHistoryArray.push({ sender: "User", message }); | |
| input.value = ""; | |
| // Simulate bot typing | |
| const botTyping = document.createElement("div"); | |
| botTyping.className = "typing-indicator"; | |
| botTyping.textContent = "Bot is typing..."; | |
| document.getElementById("chat-history").appendChild(botTyping); | |
| try { | |
| const response = await fetch("/chat/", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ message, language: currentLanguage }) | |
| }); | |
| const data = await response.json(); | |
| botTyping.remove(); | |
| const botMessage = data.response; | |
| addMessage("Bot", botMessage, "bot-message"); | |
| chatHistoryArray.push({ sender: "Bot", message: botMessage }); | |
| speak(botMessage, currentLanguage); | |
| } catch (error) { | |
| botTyping.remove(); | |
| console.error("Error:", error); | |
| const errorMessage = "Sorry, something went wrong."; | |
| addMessage("Bot", errorMessage, "bot-message"); | |
| speak(errorMessage, currentLanguage); | |
| } | |
| } | |
| // Text-to-Speech function | |
| function speak(text, lang) { | |
| const utterance = new SpeechSynthesisUtterance(text); | |
| utterance.lang = lang; | |
| utterance.pitch = 1; | |
| utterance.rate = 1; | |
| synth.speak(utterance); | |
| } | |
| // Speech-to-Text function | |
| function startListening() { | |
| const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); | |
| recognition.lang = currentLanguage; | |
| recognition.interimResults = false; | |
| recognition.onresult = (event) => { | |
| const transcript = event.results[0][0].transcript; | |
| document.getElementById("user-input").value = transcript; | |
| sendMessage(); | |
| }; | |
| recognition.onerror = (event) => { | |
| console.error("Speech recognition error:", event.error); | |
| addMessage("Bot", "Sorry, I couldn't understand you.", "bot-message"); | |
| speak("Sorry, I couldn't understand you.", currentLanguage); | |
| }; | |
| recognition.start(); | |
| } | |
| // Event Listeners | |
| document.getElementById("send-button").addEventListener("click", sendMessage); | |
| document.getElementById("mic-button").addEventListener("click", startListening); | |
| </script> | |
| </body> | |
| </html> | |