-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
165 lines (147 loc) · 5.09 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>PolicyPro Chatbot</title>
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
}
#chat-container {
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
width: 800px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
#chat-history {
height: 500px;
overflow-y: scroll;
}
.user-message {
text-align: right;
padding: 10px;
background-color: #f0f0f0;
border-radius: 10px;
margin-bottom: 5px;
}
.bot-message {
text-align: left;
padding: 10px;
background-color: #e0f0e0;
border-radius: 10px;
margin-bottom: 5px;
}
form {
display: flex;
}
input {
flex-grow: 1;
margin-right: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button[type="submit"],
button[type="button"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
}
button[type="button"] {
background-color: #3f51b5;
margin-left: 10px;
}
#loader {
display: none;
/* Hide by default */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div id="chat-container">
<h1>PolicyPro ChatBot</h1>
<div id="chat-history"></div>
<form id="chat-form">
<input type="text" id="user-input" placeholder="Enter your message">
<button type="submit">Send</button>
<button type="button" id="download-btn">Download PDF</button>
</form>
</div>
<div id="loader">
<img src="loader.gif" width="150px" alt="Loading...">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
<script>
const chatHistory = document.getElementById('chat-history');
const userInput = document.getElementById('user-input');
const form = document.getElementById('chat-form');
const downloadBtn = document.getElementById('download-btn');
async function sendMessage() {
const userMessage = userInput.value;
userInput.value = ''; // Clear input field
console.log(userMessage);
try {
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
userInput: userMessage,
}),
});
const data = await response.json();
console.log(data);
const botMessage = data.response;
console.log(botMessage);
// Add chat message to the chat history
chatHistory.innerHTML += `<div class="user-message">${userMessage}</div>`;
chatHistory.innerHTML += `<div class="bot-message">${botMessage}</div>`;
// Scroll to the bottom of the chat history
chatHistory.scrollTop = chatHistory.scrollHeight;
} catch (error) {
console.error('Error:', error);
// Handle errors gracefully, e.g., display an error message to the user
}
}
form.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent form submission
const loader = document.getElementById('loader');
loader.style.display = 'block'; // Show the loader
sendMessage().finally(() => {
loader.style.display = 'none'; // Hide the loader after the message is sent
});
});
// Function to download PDF
downloadBtn.addEventListener('click', async() => {
const chatContent = chatHistory.innerHTML;
const response = await fetch(`/download-pdf?chatHistory=${encodeURIComponent(chatContent)}`);
const pdfBlob = await response.blob();
const url = window.URL.createObjectURL(pdfBlob);
const a = document.createElement('a');
a.href = url;
a.download = 'chat_history.pdf';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
});
</script>
</body>
</html>