File size: 5,941 Bytes
9e84b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
911a1b5
9e84b3b
 
 
 
 
 
 
 
 
 
 
f9c8293
 
 
 
 
 
 
911a1b5
f9c8293
 
 
 
af76d38
 
7927f9b
af76d38
f9c8293
7927f9b
 
 
 
 
f9c8293
 
7927f9b
 
 
 
 
 
 
 
 
 
 
af76d38
7927f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
f9c8293
48e5c7d
f9c8293
9e84b3b
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!-- templates/index.html -->
<!DOCTYPE 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>