Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Commit 
							
							·
						
						0689ee5
	
1
								Parent(s):
							
							b09d5e9
								
updated scritps
Browse files- static/script.js +146 -0
    	
        static/script.js
    CHANGED
    
    | @@ -2,9 +2,20 @@ document.addEventListener('DOMContentLoaded', () => { | |
| 2 | 
             
              const queryInput = document.getElementById('queryInput');
         | 
| 3 | 
             
              const askButton = document.getElementById('askButton');
         | 
| 4 | 
             
              const chatMessages = document.getElementById('chatMessages');
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 5 |  | 
| 6 | 
             
              // 💬 Conversation state management
         | 
| 7 | 
             
              let conversationHistory = [];
         | 
|  | |
|  | |
| 8 |  | 
| 9 | 
             
              function addUserMessage(content) {
         | 
| 10 | 
             
                conversationHistory.push({
         | 
| @@ -135,6 +146,106 @@ function formatAnswer(text) { | |
| 135 | 
             
                .join('');
         | 
| 136 | 
             
            }
         | 
| 137 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 138 | 
             
              // 🔍 Query handler
         | 
| 139 | 
             
              async function handleQuery() {
         | 
| 140 | 
             
                const query = queryInput.value.trim();
         | 
| @@ -187,6 +298,41 @@ function formatAnswer(text) { | |
| 187 | 
             
                if (e.key === 'Enter') handleQuery();
         | 
| 188 | 
             
              });
         | 
| 189 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 190 | 
             
              // Auto-resize input
         | 
| 191 | 
             
              queryInput.addEventListener('input', () => {
         | 
| 192 | 
             
                queryInput.style.height = 'auto';
         | 
|  | |
| 2 | 
             
              const queryInput = document.getElementById('queryInput');
         | 
| 3 | 
             
              const askButton = document.getElementById('askButton');
         | 
| 4 | 
             
              const chatMessages = document.getElementById('chatMessages');
         | 
| 5 | 
            +
              const attachFileBtn = document.getElementById('attachFileBtn');
         | 
| 6 | 
            +
              const fileInput = document.getElementById('fileInput');
         | 
| 7 | 
            +
              const ocrModal = document.getElementById('ocrModal');
         | 
| 8 | 
            +
              const closeOcrModal = document.getElementById('closeOcrModal');
         | 
| 9 | 
            +
              const useOcrText = document.getElementById('useOcrText');
         | 
| 10 | 
            +
              const queryWithOcr = document.getElementById('queryWithOcr');
         | 
| 11 | 
            +
              const cancelOcr = document.getElementById('cancelOcr');
         | 
| 12 | 
            +
              const ocrPreview = document.getElementById('ocrPreview');
         | 
| 13 | 
            +
              const ocrResults = document.getElementById('ocrResults');
         | 
| 14 |  | 
| 15 | 
             
              // 💬 Conversation state management
         | 
| 16 | 
             
              let conversationHistory = [];
         | 
| 17 | 
            +
              let currentOcrText = '';
         | 
| 18 | 
            +
              let currentOcrImage = null;
         | 
| 19 |  | 
| 20 | 
             
              function addUserMessage(content) {
         | 
| 21 | 
             
                conversationHistory.push({
         | 
|  | |
| 146 | 
             
                .join('');
         | 
| 147 | 
             
            }
         | 
| 148 |  | 
| 149 | 
            +
              // 📷 OCR Functions
         | 
| 150 | 
            +
              async function handleFileUpload(event) {
         | 
| 151 | 
            +
                const file = event.target.files[0];
         | 
| 152 | 
            +
                if (!file) return;
         | 
| 153 | 
            +
             | 
| 154 | 
            +
                // Validate file type
         | 
| 155 | 
            +
                if (!file.type.startsWith('image/')) {
         | 
| 156 | 
            +
                  alert('Please select an image file.');
         | 
| 157 | 
            +
                  return;
         | 
| 158 | 
            +
                }
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                // Show modal
         | 
| 161 | 
            +
                ocrModal.style.display = 'block';
         | 
| 162 | 
            +
                ocrPreview.innerHTML = `<img src="${URL.createObjectURL(file)}" alt="Preview" style="max-width: 100%; max-height: 300px;">`;
         | 
| 163 | 
            +
                ocrResults.innerHTML = '<div class="loading">Processing image...</div>';
         | 
| 164 | 
            +
             | 
| 165 | 
            +
                try {
         | 
| 166 | 
            +
                  // Extract text using OCR
         | 
| 167 | 
            +
                  const formData = new FormData();
         | 
| 168 | 
            +
                  formData.append('file', file);
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                  const response = await fetch('/ocr/extract-text/', {
         | 
| 171 | 
            +
                    method: 'POST',
         | 
| 172 | 
            +
                    body: formData
         | 
| 173 | 
            +
                  });
         | 
| 174 | 
            +
             | 
| 175 | 
            +
                  if (!response.ok) throw new Error(`Server returned ${response.status}`);
         | 
| 176 | 
            +
                  const data = await response.json();
         | 
| 177 | 
            +
             | 
| 178 | 
            +
                  if (data.success) {
         | 
| 179 | 
            +
                    currentOcrText = data.extracted_text;
         | 
| 180 | 
            +
                    currentOcrImage = file;
         | 
| 181 | 
            +
                    ocrResults.innerHTML = `
         | 
| 182 | 
            +
                      <div class="ocr-success">
         | 
| 183 | 
            +
                        <h4>Extracted Text:</h4>
         | 
| 184 | 
            +
                        <div class="extracted-text">${escapeHtml(data.extracted_text)}</div>
         | 
| 185 | 
            +
                      </div>
         | 
| 186 | 
            +
                    `;
         | 
| 187 | 
            +
                  } else {
         | 
| 188 | 
            +
                    ocrResults.innerHTML = `
         | 
| 189 | 
            +
                      <div class="ocr-error">
         | 
| 190 | 
            +
                        <h4>Error:</h4>
         | 
| 191 | 
            +
                        <p>${data.error || 'Failed to extract text'}</p>
         | 
| 192 | 
            +
                      </div>
         | 
| 193 | 
            +
                    `;
         | 
| 194 | 
            +
                  }
         | 
| 195 | 
            +
                } catch (error) {
         | 
| 196 | 
            +
                  ocrResults.innerHTML = `
         | 
| 197 | 
            +
                    <div class="ocr-error">
         | 
| 198 | 
            +
                      <h4>Error:</h4>
         | 
| 199 | 
            +
                      <p>Failed to process image: ${error.message}</p>
         | 
| 200 | 
            +
                    </div>
         | 
| 201 | 
            +
                  `;
         | 
| 202 | 
            +
                }
         | 
| 203 | 
            +
              }
         | 
| 204 | 
            +
             | 
| 205 | 
            +
              async function handleOcrQuery(query) {
         | 
| 206 | 
            +
                if (!currentOcrText) {
         | 
| 207 | 
            +
                  alert('No OCR text available. Please upload an image first.');
         | 
| 208 | 
            +
                  return;
         | 
| 209 | 
            +
                }
         | 
| 210 | 
            +
             | 
| 211 | 
            +
                // Add user message
         | 
| 212 | 
            +
                addUserMessage(`[Image Query] ${query}`);
         | 
| 213 | 
            +
                displayAllMessages();
         | 
| 214 | 
            +
             | 
| 215 | 
            +
                // Show loading
         | 
| 216 | 
            +
                const loadingMessage = renderLoadingMessage();
         | 
| 217 | 
            +
                chatMessages.innerHTML += loadingMessage;
         | 
| 218 | 
            +
                scrollToBottom();
         | 
| 219 | 
            +
             | 
| 220 | 
            +
                try {
         | 
| 221 | 
            +
                  const response = await fetch('/ocr/query/', {
         | 
| 222 | 
            +
                    method: 'POST',
         | 
| 223 | 
            +
                    headers: {
         | 
| 224 | 
            +
                      'Content-Type': 'application/json',
         | 
| 225 | 
            +
                    },
         | 
| 226 | 
            +
                    body: JSON.stringify({
         | 
| 227 | 
            +
                      query: query,
         | 
| 228 | 
            +
                      conversation_history: getHistory(),
         | 
| 229 | 
            +
                      extracted_text: currentOcrText
         | 
| 230 | 
            +
                    })
         | 
| 231 | 
            +
                  });
         | 
| 232 | 
            +
             | 
| 233 | 
            +
                  if (!response.ok) throw new Error(`Server returned ${response.status}`);
         | 
| 234 | 
            +
                  const data = await response.json();
         | 
| 235 | 
            +
             | 
| 236 | 
            +
                  // Remove loading and add response
         | 
| 237 | 
            +
                  chatMessages.innerHTML = chatMessages.innerHTML.replace(loadingMessage, '');
         | 
| 238 | 
            +
                  addAssistantMessage(data.response, data.source);
         | 
| 239 | 
            +
                  displayAllMessages();
         | 
| 240 | 
            +
                  ocrModal.style.display = 'none';
         | 
| 241 | 
            +
             | 
| 242 | 
            +
                } catch (error) {
         | 
| 243 | 
            +
                  chatMessages.innerHTML = chatMessages.innerHTML.replace(loadingMessage, '');
         | 
| 244 | 
            +
                  addAssistantMessage(`Failed to get response: ${error.message}`, 'Error');
         | 
| 245 | 
            +
                  displayAllMessages();
         | 
| 246 | 
            +
                }
         | 
| 247 | 
            +
              }
         | 
| 248 | 
            +
             | 
| 249 | 
             
              // 🔍 Query handler
         | 
| 250 | 
             
              async function handleQuery() {
         | 
| 251 | 
             
                const query = queryInput.value.trim();
         | 
|  | |
| 298 | 
             
                if (e.key === 'Enter') handleQuery();
         | 
| 299 | 
             
              });
         | 
| 300 |  | 
| 301 | 
            +
              // OCR functionality
         | 
| 302 | 
            +
              attachFileBtn.addEventListener('click', () => {
         | 
| 303 | 
            +
                fileInput.click();
         | 
| 304 | 
            +
              });
         | 
| 305 | 
            +
             | 
| 306 | 
            +
              fileInput.addEventListener('change', handleFileUpload);
         | 
| 307 | 
            +
             | 
| 308 | 
            +
              closeOcrModal.addEventListener('click', () => {
         | 
| 309 | 
            +
                ocrModal.style.display = 'none';
         | 
| 310 | 
            +
              });
         | 
| 311 | 
            +
             | 
| 312 | 
            +
              useOcrText.addEventListener('click', () => {
         | 
| 313 | 
            +
                queryInput.value = currentOcrText;
         | 
| 314 | 
            +
                ocrModal.style.display = 'none';
         | 
| 315 | 
            +
                queryInput.focus();
         | 
| 316 | 
            +
              });
         | 
| 317 | 
            +
             | 
| 318 | 
            +
              queryWithOcr.addEventListener('click', () => {
         | 
| 319 | 
            +
                const query = prompt('Enter your question about the image:');
         | 
| 320 | 
            +
                if (query) {
         | 
| 321 | 
            +
                  handleOcrQuery(query);
         | 
| 322 | 
            +
                }
         | 
| 323 | 
            +
              });
         | 
| 324 | 
            +
             | 
| 325 | 
            +
              cancelOcr.addEventListener('click', () => {
         | 
| 326 | 
            +
                ocrModal.style.display = 'none';
         | 
| 327 | 
            +
              });
         | 
| 328 | 
            +
             | 
| 329 | 
            +
              // Close modal when clicking outside
         | 
| 330 | 
            +
              window.addEventListener('click', (e) => {
         | 
| 331 | 
            +
                if (e.target === ocrModal) {
         | 
| 332 | 
            +
                  ocrModal.style.display = 'none';
         | 
| 333 | 
            +
                }
         | 
| 334 | 
            +
              });
         | 
| 335 | 
            +
             | 
| 336 | 
             
              // Auto-resize input
         | 
| 337 | 
             
              queryInput.addEventListener('input', () => {
         | 
| 338 | 
             
                queryInput.style.height = 'auto';
         |