File size: 3,510 Bytes
590afda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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">
    <title>AI Chat</title>
    <style>
        body {
            background-color: #121212;
            color: white;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .chat-container {
            display: flex;
            flex-direction: column;
            justify-content: flex-end;
            height: 100vh;
            padding: 20px;
        }
        .messages {
            flex-grow: 1;
            overflow-y: auto;
            margin-bottom: 20px;
            padding: 10px;
            background-color: #1e1e1e;
            border-radius: 8px;
            max-height: 80%;
        }
        .input-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .input-field {
            width: 80%;
            padding: 10px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 5px;
        }
        .send-button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .send-button:hover {
            background-color: #45a049;
        }
        .message {
            margin-bottom: 15px;
        }
        .user-message {
            text-align: right;
            color: #4CAF50;
        }
        .ai-message {
            text-align: left;
            color: #f1f1f1;
        }
    </style>
</head>
<body>

    <div class="chat-container">
        <div id="messages" class="messages">
            <!-- Messages will be displayed here -->
        </div>
        <div class="input-container">
            <input type="text" id="messageInput" class="input-field" placeholder="Type a message..." onkeydown="if(event.key === 'Enter'){sendMessage();}">
            <button id="sendButton" class="send-button" onclick="sendMessage()">Send</button>
        </div>
    </div>

    <script>
        async function sendMessage() {
            const inputField = document.getElementById("messageInput");
            const message = inputField.value.trim();
            if (message) {
                // Display user's message in the chat
                displayMessage(message, "user-message");

                // Clear the input field
                inputField.value = '';

                // Send the message to the backend and get the AI's response
                const response = await fetch(`/chat/${encodeURIComponent(message)}`);
                const data = await response.json();
                const aiResponse = data.response;

                // Display AI's response in the chat
                displayMessage(aiResponse, "ai-message");
            }
        }

        function displayMessage(message, className) {
            const messagesContainer = document.getElementById("messages");
            const messageElement = document.createElement("div");
            messageElement.classList.add("message", className);
            messageElement.textContent = message;
            messagesContainer.appendChild(messageElement);

            // Scroll to the bottom of the messages
            messagesContainer.scrollTop = messagesContainer.scrollHeight;
        }
    </script>

</body>
</html>