DHEIVER commited on
Commit
f2c6292
·
verified ·
1 Parent(s): 89bba7c

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +106 -71
index.js CHANGED
@@ -1,79 +1,114 @@
1
- import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
2
-
3
- // Since we will download the model from the Hugging Face Hub, we can skip the local model check
4
- env.allowLocalModels = false;
5
-
6
- // Reference the elements that we will need
7
- const status = document.getElementById('status');
8
- const fileUpload = document.getElementById('upload');
9
- const imageContainer = document.getElementById('container');
10
- const example = document.getElementById('example');
11
-
12
- const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
13
-
14
- // Create a new object detection pipeline
15
- status.textContent = 'Loading model...';
16
- const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
17
- status.textContent = 'Ready';
18
-
19
- example.addEventListener('click', (e) => {
20
- e.preventDefault();
21
- detect(EXAMPLE_URL);
22
- });
23
-
24
- fileUpload.addEventListener('change', function (e) {
25
- const file = e.target.files[0];
26
- if (!file) {
27
- return;
28
  }
29
 
30
- const reader = new FileReader();
31
-
32
- // Set up a callback when the file is loaded
33
- reader.onload = e2 => detect(e2.target.result);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- reader.readAsDataURL(file);
36
- });
 
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- // Detect objects in the image
40
- async function detect(img) {
41
- imageContainer.innerHTML = '';
42
- imageContainer.style.backgroundImage = `url(${img})`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- status.textContent = 'Analysing...';
45
- const output = await detector(img, {
46
- threshold: 0.5,
47
- percentage: true,
48
- });
49
- status.textContent = '';
50
- output.forEach(renderBox);
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
 
53
- // Render a bounding box and label on the image
54
- function renderBox({ box, label }) {
55
- const { xmax, xmin, ymax, ymin } = box;
56
-
57
- // Generate a random color for the box
58
- const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
59
-
60
- // Draw the box
61
- const boxElement = document.createElement('div');
62
- boxElement.className = 'bounding-box';
63
- Object.assign(boxElement.style, {
64
- borderColor: color,
65
- left: 100 * xmin + '%',
66
- top: 100 * ymin + '%',
67
- width: 100 * (xmax - xmin) + '%',
68
- height: 100 * (ymax - ymin) + '%',
69
- })
70
-
71
- // Draw label
72
- const labelElement = document.createElement('span');
73
- labelElement.textContent = label;
74
- labelElement.className = 'bounding-box-label';
75
- labelElement.style.backgroundColor = color;
76
-
77
- boxElement.appendChild(labelElement);
78
- imageContainer.appendChild(boxElement);
79
- }
 
1
+ class PDFAnalyzer {
2
+ constructor() {
3
+ this.fileInput = document.getElementById('file-upload');
4
+ this.loading = document.getElementById('loading');
5
+ this.resultsContainer = document.getElementById('analysis-results');
6
+
7
+ this.initializeEventListeners();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  }
9
 
10
+ initializeEventListeners() {
11
+ this.fileInput.addEventListener('change', (e) => this.handleFileChange(e));
12
+
13
+ // Drag and drop handlers
14
+ const uploadArea = document.querySelector('.upload-area');
15
+ uploadArea.addEventListener('dragover', (e) => {
16
+ e.preventDefault();
17
+ uploadArea.style.borderColor = 'var(--primary-color)';
18
+ });
19
+
20
+ uploadArea.addEventListener('dragleave', () => {
21
+ uploadArea.style.borderColor = '#d1d5db';
22
+ });
23
+
24
+ uploadArea.addEventListener('drop', (e) => {
25
+ e.preventDefault();
26
+ uploadArea.style.borderColor = '#d1d5db';
27
+ const files = Array.from(e.dataTransfer.files);
28
+ this.analyzeFiles(files);
29
+ });
30
+ }
31
 
32
+ async handleFileChange(e) {
33
+ const files = Array.from(e.target.files);
34
+ await this.analyzeFiles(files);
35
+ }
36
 
37
+ async analyzeFiles(files) {
38
+ this.loading.classList.remove('hidden');
39
+
40
+ for (const file of files) {
41
+ try {
42
+ const analysis = await this.analyzePDF(file);
43
+ this.displayResult(analysis);
44
+ } catch (error) {
45
+ this.displayResult({
46
+ name: file.name,
47
+ issues: [`Error analyzing file: ${error.message}`],
48
+ size: file.size,
49
+ timestamp: new Date().toISOString()
50
+ });
51
+ }
52
+ }
53
+
54
+ this.loading.classList.add('hidden');
55
+ }
56
 
57
+ async analyzePDF(file) {
58
+ return new Promise((resolve, reject) => {
59
+ const reader = new FileReader();
60
+
61
+ reader.onload = async (e) => {
62
+ const content = new Uint8Array(e.target.result);
63
+ const issues = [];
64
+
65
+ // Check file size (10MB limit)
66
+ if (file.size > 10000000) {
67
+ issues.push('File size exceeds 10MB');
68
+ }
69
+
70
+ // Check PDF header
71
+ const header = content.slice(0, 4);
72
+ const isPDF = String.fromCharCode(...header) === '%PDF';
73
+ if (!isPDF) {
74
+ issues.push('Invalid PDF format');
75
+ }
76
+
77
+ resolve({
78
+ name: file.name,
79
+ issues,
80
+ size: file.size,
81
+ timestamp: new Date().toISOString()
82
+ });
83
+ };
84
+
85
+ reader.onerror = () => reject(new Error('Failed to read file'));
86
+ reader.readAsArrayBuffer(file);
87
+ });
88
+ }
89
 
90
+ displayResult(result) {
91
+ const alert = document.createElement('div');
92
+ alert.className = `alert ${result.issues.length ? 'alert-error' : 'alert-success'}`;
93
+
94
+ alert.innerHTML = `
95
+ <h3>${result.name}</h3>
96
+ <p>Size: ${(result.size / 1024).toFixed(2)} KB</p>
97
+ ${result.issues.length ? `
98
+ <div class="issues">
99
+ <p>Issues found:</p>
100
+ <ul>
101
+ ${result.issues.map(issue => `<li>${issue}</li>`).join('')}
102
+ </ul>
103
+ </div>
104
+ ` : '<p>No issues found</p>'}
105
+ `;
106
+
107
+ this.resultsContainer.appendChild(alert);
108
+ }
109
  }
110
 
111
+ // Initialize the analyzer when the DOM is loaded
112
+ document.addEventListener('DOMContentLoaded', () => {
113
+ new PDFAnalyzer();
114
+ });