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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<title>File Chat</title>
|
5 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.9.359/pdf.min.js"></script>
|
6 |
+
<script src="https://unpkg.com/[email protected]/dist/tesseract.min.js"></script>
|
7 |
+
<script src="https://unpkg.com/[email protected]/mammoth.browser.min.js"></script>
|
8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
|
9 |
+
<style>
|
10 |
+
body {
|
11 |
+
font-family: Arial, sans-serif;
|
12 |
+
margin: 0;
|
13 |
+
padding: 0;
|
14 |
+
background-color: #f5f5f5;
|
15 |
+
}
|
16 |
+
.container {
|
17 |
+
max-width: 800px;
|
18 |
+
margin: 0 auto;
|
19 |
+
padding: 20px;
|
20 |
+
}
|
21 |
+
h1 {
|
22 |
+
text-align: center;
|
23 |
+
color: #333;
|
24 |
+
}
|
25 |
+
.file-upload {
|
26 |
+
display: flex;
|
27 |
+
justify-content: center;
|
28 |
+
align-items: center;
|
29 |
+
margin-bottom: 20px;
|
30 |
+
}
|
31 |
+
.file-upload input[type="file"] {
|
32 |
+
display: none;
|
33 |
+
}
|
34 |
+
.file-upload label {
|
35 |
+
padding: 10px 20px;
|
36 |
+
background-color: #4CAF50;
|
37 |
+
color: white;
|
38 |
+
cursor: pointer;
|
39 |
+
border-radius: 4px;
|
40 |
+
}
|
41 |
+
.file-upload label:hover {
|
42 |
+
background-color: #45a049;
|
43 |
+
}
|
44 |
+
.text-block {
|
45 |
+
margin-bottom: 20px;
|
46 |
+
padding: 10px;
|
47 |
+
background-color: white;
|
48 |
+
border-radius: 4px;
|
49 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
50 |
+
}
|
51 |
+
.file-name {
|
52 |
+
font-weight: bold;
|
53 |
+
margin-bottom: 5px;
|
54 |
+
color: #333;
|
55 |
+
}
|
56 |
+
.loading {
|
57 |
+
margin-top: 10px;
|
58 |
+
font-style: italic;
|
59 |
+
color: #888;
|
60 |
+
}
|
61 |
+
.no-valid-text {
|
62 |
+
color: red;
|
63 |
+
font-weight: bold;
|
64 |
+
}
|
65 |
+
.remove-file {
|
66 |
+
margin-left: 10px;
|
67 |
+
color: red;
|
68 |
+
cursor: pointer;
|
69 |
+
}
|
70 |
+
.chat-container {
|
71 |
+
margin-top: 20px;
|
72 |
+
background-color: white;
|
73 |
+
border-radius: 4px;
|
74 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
75 |
+
}
|
76 |
+
.chat-header {
|
77 |
+
padding: 10px;
|
78 |
+
background-color: #4CAF50;
|
79 |
+
color: white;
|
80 |
+
border-top-left-radius: 4px;
|
81 |
+
border-top-right-radius: 4px;
|
82 |
+
}
|
83 |
+
.chat-history {
|
84 |
+
height: 300px;
|
85 |
+
overflow-y: scroll;
|
86 |
+
padding: 10px;
|
87 |
+
}
|
88 |
+
.user-message {
|
89 |
+
background-color: #e1f3fb;
|
90 |
+
padding: 10px;
|
91 |
+
margin-bottom: 10px;
|
92 |
+
border-radius: 4px;
|
93 |
+
}
|
94 |
+
.assistant-message {
|
95 |
+
background-color: #f0f0f0;
|
96 |
+
padding: 10px;
|
97 |
+
margin-bottom: 10px;
|
98 |
+
border-radius: 4px;
|
99 |
+
}
|
100 |
+
.chat-input {
|
101 |
+
display: flex;
|
102 |
+
align-items: center;
|
103 |
+
padding: 10px;
|
104 |
+
}
|
105 |
+
.chat-input input[type="text"] {
|
106 |
+
flex: 1;
|
107 |
+
padding: 8px;
|
108 |
+
border: 1px solid #ccc;
|
109 |
+
border-radius: 4px;
|
110 |
+
}
|
111 |
+
.chat-input button {
|
112 |
+
margin-left: 10px;
|
113 |
+
padding: 8px 16px;
|
114 |
+
background-color: #4CAF50;
|
115 |
+
color: white;
|
116 |
+
border: none;
|
117 |
+
border-radius: 4px;
|
118 |
+
cursor: pointer;
|
119 |
+
}
|
120 |
+
.chat-input button:hover {
|
121 |
+
background-color: #45a049;
|
122 |
+
}
|
123 |
+
</style>
|
124 |
+
</head>
|
125 |
+
<body>
|
126 |
+
<div class="container">
|
127 |
+
<h1>OpenCall.ai File Chat</h1> <div class="file-upload">
|
128 |
+
<input type="file" id="fileUpload" accept=".txt,.doc,.docx,.pdf,.html,.htm,.jpg,.jpeg,.png" multiple>
|
129 |
+
<label for="fileUpload"><i class="fas fa-file-upload"></i> Choose Files</label>
|
130 |
+
</div>
|
131 |
+
<div id="textContent"></div>
|
132 |
+
|
133 |
+
<div class="chat-container" style="display: none;">
|
134 |
+
<div class="chat-header">
|
135 |
+
<h2>Chat with AI</h2>
|
136 |
+
</div>
|
137 |
+
<div class="chat-history" id="chatHistory"></div>
|
138 |
+
<div class="chat-input">
|
139 |
+
<input type="text" id="userInput" placeholder="Enter your query">
|
140 |
+
<button id="sendButton"><i class="fas fa-paper-plane"></i> Send</button>
|
141 |
+
</div>
|
142 |
+
</div>
|
143 |
+
</div>
|
144 |
+
|
145 |
+
<script>
|
146 |
+
const fileUpload = document.getElementById('fileUpload');
|
147 |
+
const textContent = document.getElementById('textContent');
|
148 |
+
const chatContainer = document.querySelector('.chat-container');
|
149 |
+
const chatHistory = document.getElementById('chatHistory');
|
150 |
+
const userInput = document.getElementById('userInput');
|
151 |
+
const sendButton = document.getElementById('sendButton');
|
152 |
+
|
153 |
+
let extractedContent = '';
|
154 |
+
let chatMessages = [];
|
155 |
+
|
156 |
+
fileUpload.addEventListener('change', handleFileUpload);
|
157 |
+
sendButton.addEventListener('click', sendQuery);
|
158 |
+
|
159 |
+
function handleFileUpload(event) {
|
160 |
+
const files = event.target.files;
|
161 |
+
|
162 |
+
for (let i = 0; i < files.length; i++) {
|
163 |
+
const file = files[i];
|
164 |
+
const fileReader = new FileReader();
|
165 |
+
|
166 |
+
const textBlock = document.createElement('div');
|
167 |
+
textBlock.className = 'text-block';
|
168 |
+
textBlock.innerHTML = `
|
169 |
+
<div>
|
170 |
+
<span class="file-name">${file.name}</span>
|
171 |
+
<span class="remove-file" data-index="${textContent.children.length}"><i class="fas fa-times"></i></span>
|
172 |
+
</div>
|
173 |
+
<div class="text-content"></div>
|
174 |
+
<div class="loading">Loading...</div>
|
175 |
+
`;
|
176 |
+
textContent.appendChild(textBlock);
|
177 |
+
|
178 |
+
fileReader.onload = function() {
|
179 |
+
const content = fileReader.result;
|
180 |
+
|
181 |
+
if (file.type === 'application/pdf') {
|
182 |
+
extractTextFromPDF(content, textBlock);
|
183 |
+
} else if (file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
|
184 |
+
extractTextFromDocx(content, textBlock);
|
185 |
+
} else if (file.type.startsWith('image/')) {
|
186 |
+
extractTextFromImage(content, textBlock);
|
187 |
+
} else {
|
188 |
+
const extractedText = extractText(content);
|
189 |
+
displayText(extractedText, textBlock);
|
190 |
+
}
|
191 |
+
};
|
192 |
+
|
193 |
+
if (file.type === 'application/pdf') {
|
194 |
+
fileReader.readAsArrayBuffer(file);
|
195 |
+
} else if (file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
|
196 |
+
fileReader.readAsArrayBuffer(file);
|
197 |
+
} else if (file.type.startsWith('image/')) {
|
198 |
+
fileReader.readAsDataURL(file);
|
199 |
+
} else {
|
200 |
+
fileReader.readAsText(file);
|
201 |
+
}
|
202 |
+
}
|
203 |
+
|
204 |
+
chatContainer.style.display = 'block';
|
205 |
+
}
|
206 |
+
|
207 |
+
function extractTextFromPDF(content, textBlock) {
|
208 |
+
pdfjsLib.getDocument({ data: content }).promise.then(function(pdf) {
|
209 |
+
const numPages = pdf.numPages;
|
210 |
+
let extractedText = '';
|
211 |
+
|
212 |
+
const extractPageText = function(pageNum) {
|
213 |
+
pdf.getPage(pageNum).then(function(page) {
|
214 |
+
page.getTextContent().then(function(textContent) {
|
215 |
+
extractedText += textContent.items.map(item => item.str).join(' ');
|
216 |
+
|
217 |
+
if (pageNum < numPages) {
|
218 |
+
extractPageText(pageNum + 1);
|
219 |
+
} else {
|
220 |
+
displayText(extractedText, textBlock);
|
221 |
+
}
|
222 |
+
});
|
223 |
+
});
|
224 |
+
};
|
225 |
+
|
226 |
+
extractPageText(1);
|
227 |
+
}).catch(function(error) {
|
228 |
+
console.error('Error extracting text from PDF:', error);
|
229 |
+
displayText('Error extracting text from PDF. Please try again.', textBlock);
|
230 |
+
});
|
231 |
+
}
|
232 |
+
|
233 |
+
function extractTextFromDocx(content, textBlock) {
|
234 |
+
mammoth.extractRawText({ arrayBuffer: content })
|
235 |
+
.then(function(result) {
|
236 |
+
const extractedText = result.value;
|
237 |
+
displayText(extractedText, textBlock);
|
238 |
+
})
|
239 |
+
.catch(function(error) {
|
240 |
+
console.error('Error extracting text from DOCX:', error);
|
241 |
+
displayText('Error extracting text from DOCX. Please try again.', textBlock);
|
242 |
+
});
|
243 |
+
}
|
244 |
+
|
245 |
+
function extractTextFromImage(content, textBlock) {
|
246 |
+
Tesseract.recognize(content)
|
247 |
+
.then(function(result) {
|
248 |
+
const extractedText = result.data.text;
|
249 |
+
displayText(extractedText, textBlock);
|
250 |
+
})
|
251 |
+
.catch(function(error) {
|
252 |
+
console.error('Error extracting text from image:', error);
|
253 |
+
displayText('Error extracting text from image. Please try again.', textBlock);
|
254 |
+
});
|
255 |
+
}
|
256 |
+
|
257 |
+
function extractText(content) {
|
258 |
+
const lines = content.split('\n');
|
259 |
+
const extractedLines = lines.filter(line => line.trim() !== '');
|
260 |
+
return extractedLines.join('\n');
|
261 |
+
}
|
262 |
+
|
263 |
+
function displayText(text, textBlock) {
|
264 |
+
const textContent = textBlock.querySelector('.text-content');
|
265 |
+
const loadingIndicator = textBlock.querySelector('.loading');
|
266 |
+
textContent.textContent = text;
|
267 |
+
loadingIndicator.style.display = 'none';
|
268 |
+
|
269 |
+
const paragraphs = text.split('\n\n');
|
270 |
+
const validParagraphs = paragraphs.filter(isValidText);
|
271 |
+
|
272 |
+
if (validParagraphs.length === 0) {
|
273 |
+
textBlock.classList.add('no-valid-text');
|
274 |
+
textContent.textContent = 'No valid text found in the file.';
|
275 |
+
} else {
|
276 |
+
extractedContent += `${textBlock.querySelector('.file-name').textContent}\n${validParagraphs.join('\n\n')}\n\n`;
|
277 |
+
}
|
278 |
+
}
|
279 |
+
|
280 |
+
function isValidText(text) {
|
281 |
+
const letterRegex = /[a-zA-Z]/g;
|
282 |
+
const nonLetterRegex = /[^a-zA-Z]/g;
|
283 |
+
|
284 |
+
const letterCount = (text.match(letterRegex) || []).length;
|
285 |
+
const nonLetterCount = (text.match(nonLetterRegex) || []).length;
|
286 |
+
|
287 |
+
return letterCount > nonLetterCount;
|
288 |
+
}
|
289 |
+
|
290 |
+
function sendQuery() {
|
291 |
+
const query = userInput.value.trim();
|
292 |
+
if (query === '') return;
|
293 |
+
|
294 |
+
const userMessage = document.createElement('div');
|
295 |
+
userMessage.className = 'user-message';
|
296 |
+
userMessage.textContent = 'User: ' + query;
|
297 |
+
chatHistory.appendChild(userMessage);
|
298 |
+
|
299 |
+
chatMessages.push({ role: 'user', content: query });
|
300 |
+
|
301 |
+
const requestData = {
|
302 |
+
model: "gradientai/Llama-3-8B-Instruct-Gradient-1048k",
|
303 |
+
messages: [
|
304 |
+
{
|
305 |
+
role: "system",
|
306 |
+
content: extractedContent
|
307 |
+
},
|
308 |
+
...chatMessages,
|
309 |
+
{
|
310 |
+
role: "user",
|
311 |
+
content: "'Based on the above context, please answer the following query: " + query
|
312 |
+
}
|
313 |
+
],
|
314 |
+
max_tokens: 400,
|
315 |
+
top_p: 0.9,
|
316 |
+
temperature: 0.8
|
317 |
+
};
|
318 |
+
|
319 |
+
fetch('https://azure5.ngrok.app/v1/chat/completions', {
|
320 |
+
method: 'POST',
|
321 |
+
headers: {
|
322 |
+
'Content-Type': 'application/json'
|
323 |
+
},
|
324 |
+
body: JSON.stringify(requestData)
|
325 |
+
})
|
326 |
+
.then(response => response.json())
|
327 |
+
.then(data => {
|
328 |
+
const assistantMessage = document.createElement('div');
|
329 |
+
assistantMessage.className = 'assistant-message';
|
330 |
+
assistantMessage.textContent = 'Assistant: ' + data.choices[0].message.content;
|
331 |
+
chatHistory.appendChild(assistantMessage);
|
332 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
333 |
+
|
334 |
+
chatMessages.push({ role: 'assistant', content: data.choices[0].message.content });
|
335 |
+
})
|
336 |
+
.catch(error => {
|
337 |
+
console.error('Error:', error);
|
338 |
+
const errorMessage = document.createElement('div');
|
339 |
+
errorMessage.className = 'assistant-message';
|
340 |
+
errorMessage.textContent = 'Error: Failed to get response from the model.';
|
341 |
+
chatHistory.appendChild(errorMessage);
|
342 |
+
});
|
343 |
+
|
344 |
+
userInput.value = '';
|
345 |
+
}
|
346 |
+
|
347 |
+
textContent.addEventListener('click', function(event) {
|
348 |
+
if (event.target.classList.contains('remove-file')) {
|
349 |
+
const index = event.target.dataset.index;
|
350 |
+
textContent.removeChild(textContent.children[index]);
|
351 |
+
}
|
352 |
+
});
|
353 |
+
</script>
|
354 |
+
</body>
|
355 |
+
</html>
|