Spaces:
Sleeping
Sleeping
<!-- templates/index.html --> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>MedChat - Your Second Opinion</title> | |
<style> | |
body { | |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | |
margin: 0; | |
padding: 0; | |
background: linear-gradient(135deg, #e0f7fa, #80deea); | |
height: 100vh; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
} | |
.container { | |
width: 90%; | |
max-width: 600px; | |
background: #fff; | |
border-radius: 15px; | |
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); | |
overflow: hidden; | |
} | |
.header { | |
background: #0288d1; | |
color: white; | |
padding: 15px; | |
text-align: center; | |
font-size: 24px; | |
} | |
.chat-box { | |
height: 400px; | |
overflow-y: auto; | |
padding: 20px; | |
background: #f5f5f5; | |
} | |
.message { | |
margin: 10px 0; | |
padding: 10px 15px; | |
border-radius: 10px; | |
max-width: 80%; | |
word-wrap: break-word; | |
} | |
.user-message { | |
background: #0288d1; | |
color: white; | |
margin-left: auto; | |
text-align: right; | |
} | |
.bot-message { | |
background: #e0e0e0; | |
color: #333; | |
} | |
.input-area { | |
display: flex; | |
padding: 15px; | |
border-top: 1px solid #ddd; | |
background: #fff; | |
} | |
.input-area input { | |
flex: 1; | |
padding: 10px; | |
border: 1px solid #ccc; | |
border-radius: 5px 0 0 5px; | |
outline: none; | |
} | |
.input-area button { | |
padding: 10px 20px; | |
background: #0288d1; | |
color: white; | |
border: none; | |
border-radius: 0 5px 5px 0; | |
cursor: pointer; | |
transition: background 0.3s; | |
} | |
.input-area button:hover { | |
background: #0277bd; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="header">MedChat - Your Second Opinion</div> | |
<div class="chat-box" id="chatBox"></div> | |
<div class="input-area"> | |
<input type="text" id="userInput" placeholder="Ask about medications or studies..."> | |
<button onclick="sendMessage()">Send</button> | |
</div> | |
</div> | |
<script> | |
const chatBox = document.getElementById("chatBox"); | |
const userInput = document.getElementById("userInput"); | |
function addMessage(text, isUser) { | |
const msgDiv = document.createElement("div"); | |
msgDiv.className = `message ${isUser ? "user-message" : "bot-message"}`; | |
msgDiv.textContent = text; // Use textContent for streaming | |
chatBox.appendChild(msgDiv); | |
chatBox.scrollTop = chatBox.scrollHeight; | |
} | |
async function sendMessage() { | |
const message = userInput.value.trim(); | |
if (!message) return; | |
addMessage(message, true); | |
userInput.value = ""; | |
try { | |
// Send POST request to /chat with message in body | |
const postResponse = await fetch('/chat', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ message: message }) | |
}); | |
if (!postResponse.ok) { | |
throw new Error(`HTTP error! status: ${postResponse.status}`); | |
} | |
// Stream response using the response body (SSE) | |
const reader = postResponse.body.getReader(); | |
const decoder = new TextDecoder(); | |
let botResponse = ""; | |
function readStream() { | |
reader.read().then(({ done, value }) => { | |
if (done) { | |
addMessage(botResponse, false); | |
return; | |
} | |
const text = decoder.decode(value); | |
const lines = text.split('\n'); | |
for (const line of lines) { | |
if (line.startsWith('data: ')) { | |
const data = line.replace('data: ', ''); | |
if (data === '[END]') { | |
reader.cancel(); | |
addMessage(botResponse, false); | |
return; | |
} else { | |
botResponse += data; | |
// Update chat box incrementally for streaming | |
const lastBotMsg = chatBox.lastElementChild; | |
if (lastBotMsg && lastBotMsg.className.includes("bot-message")) { | |
lastBotMsg.textContent = botResponse; | |
} | |
} | |
} | |
} | |
readStream(); | |
}).catch(error => { | |
addMessage(`Error: Could not connect to server. Details: ${error.message}`, false); | |
}); | |
} | |
readStream(); | |
} catch (error) { | |
addMessage(`Error: Could not connect to server. Details: {error.message}`, false); | |
} | |
} | |
userInput.addEventListener("keypress", (e) => { | |
if (e.key === "Enter") sendMessage(); | |
}); | |
</script> | |
</body> | |
</html> |