-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrated.html
215 lines (190 loc) · 7.44 KB
/
integrated.html
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
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<title>diskusai</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 400px;
display: none; /* Hide the container initially */
}
.chat-container {
height: 300px;
overflow-y: scroll;
border: 1px solid #ccc;
padding: 10px;
}
.message {
word-wrap: break-word;
padding: 5px;
border-radius: 5px;
margin-bottom: 10px;
}
.user-message {
text-align: right;
background-color: #e9ecef;
padding: 10px;
}
.response-message {
text-align: left;
background-color: #d1ecf1;
padding: 10px;
white-space: pre-line;
}
.response-message p {
margin-bottom: 5px;
}
.home-button {
position: absolute;
top: 10px;
left: 10px;
}
/* Style for dropdown suggestions */
.dropdown-menu.show {
top: calc(100% + 10px) !important;
left: 0 !important;
right: auto !important;
width: 100% !important;
margin-top: 0;
}
.dropdown-menu .dropdown-item {
white-space: normal;
}
</style>
</head>
<body>
<div class="container">
<a href="/" class="home-button btn btn-primary">Home</a>
<h1 class="text-center">diskus<span style="color: blue;">AI</span></h1>
<div class="chat-container" id="chatContainer"></div>
<div class="input-group mb-3">
<input type="text" class="form-control" id="messageQueryInput" placeholder="Type a message or enter your query">
<div class="input-group-append">
<button class="btn btn-primary" type="button" id="sendButton">Send</button>
</div>
</div>
<div id="suggestions" class="dropdown-menu dropdown-menu-right" aria-labelledby="messageQueryInput"></div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
var recommendationSent = false;
// Function to add a message to the chat container
function addMessage(message, isUser) {
var chatContainer = document.getElementById("chatContainer");
var messageClass = isUser ? "user-message" : "response-message";
var messageElement = document.createElement("div");
messageElement.className = "message " + messageClass;
messageElement.innerHTML = message;
chatContainer.appendChild(messageElement);
// Scroll to the bottom of the chat container
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Function to simulate typing animation
function showTypingAnimation() {
var chatContainer = document.getElementById("chatContainer");
var typingElement = document.createElement("div");
typingElement.className = "message response-message";
typingElement.textContent = "Typing...";
chatContainer.appendChild(typingElement);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Add initial greeting and recommendation message
addMessage("Halo kak, ada yang bisa saya bantu?", false);
recommendationSent = true;
// Send button click event
$("#sendButton").click(function() {
var messageInput = $("#messageQueryInput");
var message = messageInput.val().trim();
if (message !== "") {
addMessage(message, true);
// Show typing animation
showTypingAnimation();
// Simulate delay before displaying response
setTimeout(function() {
// Show recommendation message
if (recommendationSent) {
addMessage("Kakak dapat menyelesaikan masalah tersebut dengan cek ini ya.", false);
recommendationSent = false;
}
// Send the message as a POST request to the backend API endpoint
$.ajax({
url: "https://chatbot-serving-integrated-shx4zogvqq-et.a.run.app/predict",
type: "POST",
data: JSON.stringify({ question: message }),
contentType: "application/json",
success: function(response) {
// Remove typing animation
$(".message.response-message:contains('Typing...')").remove();
if (response.combined_paragraph !== "") {
// Split the combined paragraph into separate paragraphs
var paragraphs = response.combined_paragraph.split("<hr>");
// Add each paragraph as a separate message
for (var i = 0; i < paragraphs.length; i++) {
addMessage(paragraphs[i], false);
}
// Add a follow-up message
addMessage("Ada yang bisa kami bantu lagi?", false);
} else {
// Display an error message
addMessage("Tidak ditemukan modul yang relevan.", false);
}
},
error: function() {
// Remove typing animation
$(".message.response-message:contains('Typing...')").remove();
// Display an error message
addMessage("Terjadi kesalahan.", false);
}
});
}, 1500);
// Clear the message/query input
messageQueryInput.val("");
}
});
var typingTimer;
var doneTypingInterval = 500;
var input = $('#messageQueryInput');
var apiUrl = 'https://searchbar-serving-shx4zogvqq-et.a.run.app/suggest'; // Modify this URL to match your backend API endpoint
input.on('input', function() {
clearTimeout(typingTimer);
typingTimer = setTimeout(fetchSuggestions, doneTypingInterval);
});
function fetchSuggestions() {
var query = input.val();
$.ajax({
url: apiUrl,
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
data: { query: query }, // No need to convert data to x-www-form-urlencoded format, jQuery will handle it automatically
success: function(data) {
var suggestions = data.suggestions;
var dropdown = $('#suggestions');
dropdown.empty();
if (suggestions.length > 0) {
suggestions.forEach(function(suggestion) {
dropdown.append('<button class="dropdown-item">' + suggestion + '</button>');
});
dropdown.addClass('show');
} else {
dropdown.removeClass('show');
}
}
});
}
// Handle click event on dropdown items
$('#suggestions').on('click', '.dropdown-item', function() {
var selectedSuggestion = $(this).text();
input.val(selectedSuggestion);
$('#suggestions').removeClass('show');
});
// Show the containers with fadeIn animations
$(".container").fadeIn(1000);
});
</script>
</body>
</html>