File size: 4,876 Bytes
ee3932a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
	<title>AI Assist</title>
	<link rel="icon" type="image/png"  />
	<link rel="apple-touch-icon" type="image/png"  />
	<style>
		body {
			background-color: #ECE5DD;
			font-family: Arial, sans-serif;
			font-size: 14px;
			padding: 20px;
		}
		.container {
			display: flex;
			flex-direction: column;
			height: 700px;
			max-width: 700px;
			margin: 0 auto;
			background-color: #fff;
			border-radius: 10px;
			box-shadow: 0px 5px 10px rgba(0,0,0,0.1);
			overflow: hidden;
		}
		.header {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 60px;
			background-color:#0f3cc9 ;
			/* #075E54 */
			color: #fff;
			font-weight: bold;
			font-size: 20px;
		}
		.chat-area {
			flex-grow: 1;
			padding: 20px;
			overflow-y: auto;
		}
		.chat-area p {
			margin: 5px 0;
		}
		.message-box {
			display: flex;
			align-items: center;
			background-color: #F4F4F4;
			padding: 10px;
			border-top: 1px solid #ccc;
		}
		.message-box input {
			flex-grow: 1;
			border: none;
			padding: 10px;
			font-size: 14px;
			border-radius: 5px;
			background-color: #fff;
			margin-right: 10px;
		}
		.message-box button {
			background-color:#0f3cc9 ;
			/* #075E54 */
			color: #fff;
			border: none;
			padding: 10px;
			font-size: 14px;
			font-weight: bold;
			border-radius: 5px;
			cursor: pointer;
		}
		.user-message {
			display: flex;
			flex-direction: row-reverse;
			align-items: center;
			margin-bottom: 10px;
		}
		.bot-message {
			display: flex;
			align-items: center;
			margin-bottom: 10px;
		}
		.user-message p, .bot-message p {
			background-color:#ccd6f2;
			 /* #DCF8C6; */
			color: #000;
			padding: 10px;
			border-radius: 10px;
			max-width: 60%;
			word-wrap: break-word;
		}
		.user-message p {
			margin-right: 10px;
		}
		.bot-message p {
			margin-left: 10px;
		}

		.logoClass
		{
			width: 50px;
			height: 40px;
			align-self: center;
			margin-right: 10px;
			gap: 20px 20px; 
			background-repeat: no-repeat;
			background-size: cover;
			/* background-image: url({{url_for('static',filename='.png')}}); */
		}
	</style>
</head>
<body>
	<div class="container">
		<div class="header">
			<div> </div>
			<div>AI Assist - Powered by J-GPT</div> 
		</div>
		<div class="chat-area">
			<div class="bot-message">
				<p>Hello! How can I assist you today?</p>
			</div>
		</div>
		<div class="message-box">
			<input id="user-input" type="text" placeholder="Type your message..." onkeyup="return searchKeyPress(event)">
			<button id="send-button" onclick="sendMessage()">Send</button>
		</div>
	</div>

	<script>

		function searchKeyPress(event) {
					event.preventDefault();
					if (event.keyCode === 13) {
						document.getElementById("send-button").click();
					}
				};

		async function sendMessage() {
            var response="";
			// Get the user input
			var userInput = document.getElementById("user-input").value;

			// Create a new user message and add it to the chat area
			var userMessage = document.createElement("div");
			userMessage.classList.add("user-message");
			userMessage.innerHTML = '<p>' + userInput + '</p>';
			document.querySelector(".chat-area").appendChild(userMessage);

			// Clear the input field
			document.getElementById("user-input").value = "";

            

			// Generate a bot response
			var botResponse = await generateBotResponse(userInput);

			
		}

		async function generateBotResponse(userInput) {
			// Make a POST request to the chatbot API endpoint
		    //  response = await fetch('http://127.0.0.1:1111/post_json', {
			// 	method: 'POST',
			// 	headers: {
			// 		'Content-Type': 'application/json'
			// 	},
			// 	body: JSON.stringify({
			// 		query: userInput
			// 	})
			// });

            var xhr = new XMLHttpRequest();
            xhr.open("POST", '/post_json', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify({
                query: userInput
            }));

            xhr.onload = function() {
            
            console.log(this.responseText);
            var response = JSON.parse(this.responseText);
            console.log(response);

            // Create a new bot message and add it to the chat area
			var botMessage = document.createElement("div");
			botMessage.classList.add("bot-message");
			botMessage.innerHTML = '<p>' + response.botMessage + '</p>';
			document.querySelector(".chat-area").appendChild(botMessage);

			// Scroll to the bottom of the chat area
			document.querySelector('.chat-area').scrollTop = document.querySelector('.chat-area').scrollHeight;
            return response.botMessage;
            }

			// Parse the response JSON and return the bot message
            //console.log(data);
			//var data = await response.json();
            
			//return response.botMessage;
		}
	</script>
</body>
</html>