Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +55 -0
- Accessibility Standards/Accessibility Remediation service list template 1.docx +0 -0
- Accessibility Standards/Document Accessibility Matrix_Word.docx +0 -0
- Accessibility Standards/Protected.docx +0 -0
- Accessibility Standards/Test_Document_with_Heading_Issues.docx +0 -0
- Dockerfile +19 -0
- FRONTEND_INTEGRATION.md +297 -0
- README.md +25 -0
- SHADOW_DEBUG.md +36 -0
- SHADOW_REMOVAL_COMPLETED.md +92 -0
- TESTING_GUIDE.md +402 -0
- api/batch-download.js +121 -0
- api/batch-upload.js +249 -0
- api/cors-test.js +16 -0
- api/download-document.js +298 -0
- api/reports.js +178 -0
- api/session.js +61 -0
- api/upload-document.js +268 -0
- api/upload-powerpoint.js +84 -0
- check-shadows.js +115 -0
- debug-detection.js +120 -0
- docs/batch-processing.html +329 -0
- docs/remediate-example.html +67 -0
- lib/cors-middleware.js +43 -0
- lib/pptx-analyzer.js +134 -0
- lib/session-manager.js +174 -0
- local-test-color-contrast.js +30 -0
- package-lock.json +204 -0
- package.json +13 -0
- python-server/.env.example +23 -0
- python-server/.gitignore +3 -0
- python-server/QUICKSTART.md +221 -0
- python-server/TESTING_READY.md +167 -0
- python-server/app.py +14 -0
- python-server/color_contrast.py +752 -0
- python-server/last_report.json +56 -0
- python-server/local_vision.py +377 -0
- python-server/output/remediated-test1.pptx +3 -0
- python-server/output/remediated-test2.pptx +3 -0
- python-server/requirements.txt +23 -0
- python-server/server2.py +1421 -0
- python-server/server_backup.py +304 -0
- python-server/server_output.log +0 -0
- python-server/uploads/17-Inquiry_Methods.ppt +3 -0
- python-server/uploads/17-Testing_Methods.ppt +3 -0
- python-server/uploads/6-presentation-bottomrow.pptx +3 -0
- python-server/uploads/Accessibility_Chatbot_Spike_Presentation.pptx +0 -0
- python-server/uploads/COMP - 5620 UID Chapter 12 presentation-1-1-1.pptx +3 -0
- python-server/uploads/Group 9- Chapter 13 Presentation.pptx +3 -0
- python-server/uploads/Group1_Chap11_V1_AB.pptx +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-server/output/remediated-test1.pptx filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
python-server/output/remediated-test2.pptx filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
python-server/uploads/17-Inquiry_Methods.ppt filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
python-server/uploads/17-Testing_Methods.ppt filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
python-server/uploads/6-presentation-bottomrow.pptx filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
python-server/uploads/COMP[[:space:]]-[[:space:]]5620[[:space:]]UID[[:space:]]Chapter[[:space:]]12[[:space:]]presentation-1-1-1.pptx filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
python-server/uploads/Group[[:space:]]9-[[:space:]]Chapter[[:space:]]13[[:space:]]Presentation.pptx filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
python-server/uploads/Group1_Chap11_V1_AB.pptx filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
python-server/uploads/Lec7.pptx filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
python-server/uploads/Lec8.pptx filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
python-server/uploads/PHIL_1020_Week10_102025.pptx filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
python-server/uploads/PHIL_1020_Week10_102225.pptx filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
python-server/uploads/PHIL_1020_Week10_102425.pptx filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
python-server/uploads/test1.pptx filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
python-server/uploads/test2.pptx filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
python-server/uploads/UI[[:space:]]Final[[:space:]]Presentation.pptx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
venv/Lib/site-packages/__pycache__/typing_extensions.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
venv/Lib/site-packages/anyio/_backends/__pycache__/_asyncio.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
venv/Lib/site-packages/click/__pycache__/core.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
venv/Lib/site-packages/fastapi/__pycache__/routing.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
venv/Lib/site-packages/idna/__pycache__/idnadata.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
venv/Lib/site-packages/idna/__pycache__/uts46data.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
venv/Lib/site-packages/pip/_vendor/__pycache__/typing_extensions.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
venv/Lib/site-packages/pip/_vendor/chardet/__pycache__/langrussianmodel.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
venv/Lib/site-packages/pip/_vendor/distlib/t64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
venv/Lib/site-packages/pip/_vendor/distlib/t64.exe filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
venv/Lib/site-packages/pip/_vendor/distlib/w64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
venv/Lib/site-packages/pip/_vendor/distlib/w64.exe filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
venv/Lib/site-packages/pip/_vendor/idna/__pycache__/uts46data.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
venv/Lib/site-packages/pip/_vendor/pkg_resources/__pycache__/__init__.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
venv/Lib/site-packages/pip/_vendor/pyparsing/__pycache__/core.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
venv/Lib/site-packages/pip/_vendor/rich/__pycache__/_emoji_codes.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
venv/Lib/site-packages/pip/_vendor/rich/__pycache__/console.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
venv/Lib/site-packages/pkg_resources/__pycache__/__init__.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
venv/Lib/site-packages/pkg_resources/_vendor/more_itertools/__pycache__/more.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
venv/Lib/site-packages/pkg_resources/_vendor/pyparsing/__pycache__/core.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
venv/Lib/site-packages/pydantic/__pycache__/json_schema.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
venv/Lib/site-packages/pydantic/__pycache__/types.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
venv/Lib/site-packages/pydantic/_internal/__pycache__/_generate_schema.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
venv/Lib/site-packages/pydantic_core/__pycache__/core_schema.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
venv/Lib/site-packages/pydantic_core/_pydantic_core.cp311-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
venv/Lib/site-packages/setuptools/_vendor/__pycache__/typing_extensions.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
venv/Lib/site-packages/setuptools/_vendor/more_itertools/__pycache__/more.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
venv/Lib/site-packages/setuptools/_vendor/pyparsing/__pycache__/core.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
venv/Lib/site-packages/setuptools/cli-arm64.exe filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
venv/Lib/site-packages/setuptools/command/__pycache__/easy_install.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
venv/Lib/site-packages/setuptools/config/_validate_pyproject/__pycache__/fastjsonschema_validations.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
venv/Lib/site-packages/setuptools/gui-arm64.exe filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
venv/Scripts/fastapi.exe filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
venv/Scripts/pip.exe filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
venv/Scripts/pip3.11.exe filter=lfs diff=lfs merge=lfs -text
|
| 52 |
+
venv/Scripts/pip3.exe filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
venv/Scripts/python.exe filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
venv/Scripts/pythonw.exe filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
venv/Scripts/uvicorn.exe filter=lfs diff=lfs merge=lfs -text
|
Accessibility Standards/Accessibility Remediation service list template 1.docx
ADDED
|
Binary file (42.3 kB). View file
|
|
|
Accessibility Standards/Document Accessibility Matrix_Word.docx
ADDED
|
Binary file (38.6 kB). View file
|
|
|
Accessibility Standards/Protected.docx
ADDED
|
Binary file (13.5 kB). View file
|
|
|
Accessibility Standards/Test_Document_with_Heading_Issues.docx
ADDED
|
Binary file (36.8 kB). View file
|
|
|
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy requirements and install dependencies
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
# Copy the entire backend
|
| 10 |
+
COPY python-server/ ./python-server/
|
| 11 |
+
|
| 12 |
+
# Set working directory to python-server
|
| 13 |
+
WORKDIR /app/python-server
|
| 14 |
+
|
| 15 |
+
# Expose port (HF Spaces uses 7860)
|
| 16 |
+
EXPOSE 7860
|
| 17 |
+
|
| 18 |
+
# Start the app
|
| 19 |
+
CMD ["uvicorn", "server2:app", "--host", "0.0.0.0", "--port", "7860"]
|
FRONTEND_INTEGRATION.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Frontend Integration Guide - Session-Based Batch Processing
|
| 2 |
+
|
| 3 |
+
## 🚀 NEW ENDPOINTS AVAILABLE
|
| 4 |
+
|
| 5 |
+
### 1. **Session Management** - `/api/session`
|
| 6 |
+
**Purpose**: Initialize and maintain user sessions for temporary file storage
|
| 7 |
+
|
| 8 |
+
```javascript
|
| 9 |
+
// Initialize session when user opens the app
|
| 10 |
+
POST /api/session
|
| 11 |
+
Response: { sessionId: "1762145344331-h6evl2etm", success: true }
|
| 12 |
+
|
| 13 |
+
// Keep session alive (call every 5 minutes while user is active)
|
| 14 |
+
POST /api/session
|
| 15 |
+
Headers: { "X-Session-ID": "session-id-here" }
|
| 16 |
+
Response: { success: true, message: "Session refreshed" }
|
| 17 |
+
|
| 18 |
+
// Get session info and existing batches
|
| 19 |
+
GET /api/session?sessionId=session-id-here
|
| 20 |
+
Response: {
|
| 21 |
+
sessionId: "...",
|
| 22 |
+
files: [...],
|
| 23 |
+
batches: [...],
|
| 24 |
+
expiresIn: "1 hour from last activity"
|
| 25 |
+
}
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
### 2. **Batch Upload** - `/api/batch-upload`
|
| 29 |
+
**Purpose**: Upload and process multiple DOCX files at once (up to 10 files)
|
| 30 |
+
|
| 31 |
+
```javascript
|
| 32 |
+
// Upload multiple files
|
| 33 |
+
POST /api/batch-upload
|
| 34 |
+
Headers: { "X-Session-ID": "session-id-here" }
|
| 35 |
+
Body: FormData with multiple files
|
| 36 |
+
|
| 37 |
+
Response: {
|
| 38 |
+
sessionId: "session-id-here",
|
| 39 |
+
batchId: 1762145344343,
|
| 40 |
+
summary: {
|
| 41 |
+
totalFiles: 5,
|
| 42 |
+
successful: 4,
|
| 43 |
+
failed: 1
|
| 44 |
+
},
|
| 45 |
+
results: [
|
| 46 |
+
{
|
| 47 |
+
fileIndex: 1,
|
| 48 |
+
filename: "document1.docx",
|
| 49 |
+
success: true,
|
| 50 |
+
reportId: "report-123",
|
| 51 |
+
summary: { flagged: 2, fixed: 1 },
|
| 52 |
+
details: { ... }
|
| 53 |
+
},
|
| 54 |
+
// ... more files
|
| 55 |
+
],
|
| 56 |
+
expiresIn: "1 hour"
|
| 57 |
+
}
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### 3. **Batch Download** - `/api/batch-download`
|
| 61 |
+
**Purpose**: Download all remediated files as a ZIP
|
| 62 |
+
|
| 63 |
+
```javascript
|
| 64 |
+
// Download remediated files
|
| 65 |
+
GET /api/batch-download?batchId=1762145344343&sessionId=session-id-here
|
| 66 |
+
Response: ZIP file containing all remediated documents
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
|
| 71 |
+
## 📋 FRONTEND IMPLEMENTATION CHECKLIST
|
| 72 |
+
|
| 73 |
+
### Step 1: **Session Initialization** (Required)
|
| 74 |
+
```javascript
|
| 75 |
+
class AccessibilityChecker {
|
| 76 |
+
constructor() {
|
| 77 |
+
this.sessionId = null;
|
| 78 |
+
this.heartbeatInterval = null;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
async initializeSession() {
|
| 82 |
+
try {
|
| 83 |
+
const response = await fetch('/api/session', {
|
| 84 |
+
method: 'POST',
|
| 85 |
+
headers: { 'Content-Type': 'application/json' }
|
| 86 |
+
});
|
| 87 |
+
const data = await response.json();
|
| 88 |
+
this.sessionId = data.sessionId;
|
| 89 |
+
|
| 90 |
+
// Start heartbeat to keep session alive
|
| 91 |
+
this.startHeartbeat();
|
| 92 |
+
|
| 93 |
+
return this.sessionId;
|
| 94 |
+
} catch (error) {
|
| 95 |
+
console.error('Session initialization failed:', error);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
startHeartbeat() {
|
| 100 |
+
// Send heartbeat every 5 minutes while user is active
|
| 101 |
+
this.heartbeatInterval = setInterval(async () => {
|
| 102 |
+
if (this.sessionId) {
|
| 103 |
+
try {
|
| 104 |
+
await fetch('/api/session', {
|
| 105 |
+
method: 'POST',
|
| 106 |
+
headers: {
|
| 107 |
+
'Content-Type': 'application/json',
|
| 108 |
+
'X-Session-ID': this.sessionId
|
| 109 |
+
}
|
| 110 |
+
});
|
| 111 |
+
} catch (error) {
|
| 112 |
+
console.warn('Heartbeat failed:', error);
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}, 5 * 60 * 1000); // 5 minutes
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
cleanup() {
|
| 119 |
+
if (this.heartbeatInterval) {
|
| 120 |
+
clearInterval(this.heartbeatInterval);
|
| 121 |
+
}
|
| 122 |
+
// Note: Server will auto-cleanup files after 1 hour
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
// Initialize when app loads
|
| 127 |
+
const checker = new AccessibilityChecker();
|
| 128 |
+
checker.initializeSession();
|
| 129 |
+
|
| 130 |
+
// Cleanup when user leaves
|
| 131 |
+
window.addEventListener('beforeunload', () => checker.cleanup());
|
| 132 |
+
```
|
| 133 |
+
|
| 134 |
+
### Step 2: **Multi-File Upload UI** (Recommended)
|
| 135 |
+
```javascript
|
| 136 |
+
async function uploadMultipleFiles(files) {
|
| 137 |
+
if (!checker.sessionId) {
|
| 138 |
+
throw new Error('Session not initialized');
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
const formData = new FormData();
|
| 142 |
+
files.forEach((file, index) => {
|
| 143 |
+
formData.append(`file${index}`, file);
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
const response = await fetch('/api/batch-upload', {
|
| 147 |
+
method: 'POST',
|
| 148 |
+
headers: {
|
| 149 |
+
'X-Session-ID': checker.sessionId
|
| 150 |
+
},
|
| 151 |
+
body: formData
|
| 152 |
+
});
|
| 153 |
+
|
| 154 |
+
if (!response.ok) {
|
| 155 |
+
throw new Error(`Upload failed: ${response.statusText}`);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
return await response.json();
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// Usage example:
|
| 162 |
+
document.getElementById('fileInput').addEventListener('change', async (e) => {
|
| 163 |
+
const files = Array.from(e.target.files);
|
| 164 |
+
try {
|
| 165 |
+
const result = await uploadMultipleFiles(files);
|
| 166 |
+
console.log(`Processed ${result.summary.totalFiles} files`);
|
| 167 |
+
console.log(`Batch ID: ${result.batchId}`);
|
| 168 |
+
|
| 169 |
+
// Show results to user
|
| 170 |
+
displayBatchResults(result);
|
| 171 |
+
} catch (error) {
|
| 172 |
+
console.error('Upload error:', error);
|
| 173 |
+
}
|
| 174 |
+
});
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
### Step 3: **Download Remediated Files** (Required)
|
| 178 |
+
```javascript
|
| 179 |
+
function downloadBatch(batchId) {
|
| 180 |
+
if (!checker.sessionId) {
|
| 181 |
+
alert('Session expired. Please refresh the page.');
|
| 182 |
+
return;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
const downloadUrl = `/api/batch-download?batchId=${batchId}&sessionId=${checker.sessionId}`;
|
| 186 |
+
|
| 187 |
+
// Create temporary download link
|
| 188 |
+
const link = document.createElement('a');
|
| 189 |
+
link.href = downloadUrl;
|
| 190 |
+
link.download = `batch-${batchId}-remediated.zip`;
|
| 191 |
+
document.body.appendChild(link);
|
| 192 |
+
link.click();
|
| 193 |
+
document.body.removeChild(link);
|
| 194 |
+
}
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
---
|
| 198 |
+
|
| 199 |
+
## 🔄 MIGRATION FROM EXISTING ENDPOINTS
|
| 200 |
+
|
| 201 |
+
### If you're currently using single-file endpoints:
|
| 202 |
+
|
| 203 |
+
**Old way:**
|
| 204 |
+
```javascript
|
| 205 |
+
// Single file upload
|
| 206 |
+
POST /api/upload-document
|
| 207 |
+
POST /api/download-document
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
**New way (backward compatible):**
|
| 211 |
+
```javascript
|
| 212 |
+
// Keep using single file endpoints for 1 file
|
| 213 |
+
// OR use batch endpoints for 1+ files
|
| 214 |
+
|
| 215 |
+
// For multiple files:
|
| 216 |
+
POST /api/batch-upload (new)
|
| 217 |
+
GET /api/batch-download (new)
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
### **Integration Options:**
|
| 221 |
+
|
| 222 |
+
1. **Quick Integration** (minimal changes):
|
| 223 |
+
- Add session initialization on app start
|
| 224 |
+
- Keep existing single-file flow
|
| 225 |
+
- Add optional multi-file upload as new feature
|
| 226 |
+
|
| 227 |
+
2. **Full Integration** (recommended):
|
| 228 |
+
- Replace single-file with batch endpoints
|
| 229 |
+
- Add drag-and-drop for multiple files
|
| 230 |
+
- Show batch progress and results
|
| 231 |
+
|
| 232 |
+
---
|
| 233 |
+
|
| 234 |
+
## 🎯 UI/UX RECOMMENDATIONS
|
| 235 |
+
|
| 236 |
+
### **File Upload Area:**
|
| 237 |
+
```html
|
| 238 |
+
<!-- Support both single and multiple files -->
|
| 239 |
+
<input type="file" multiple accept=".docx" id="fileInput">
|
| 240 |
+
|
| 241 |
+
<!-- Or drag-and-drop area -->
|
| 242 |
+
<div id="dropArea">
|
| 243 |
+
<p>Drop up to 10 DOCX files here, or click to select</p>
|
| 244 |
+
<button>Select Files</button>
|
| 245 |
+
</div>
|
| 246 |
+
```
|
| 247 |
+
|
| 248 |
+
### **Progress Display:**
|
| 249 |
+
```javascript
|
| 250 |
+
// Show batch processing progress
|
| 251 |
+
function displayBatchResults(result) {
|
| 252 |
+
const container = document.getElementById('results');
|
| 253 |
+
|
| 254 |
+
container.innerHTML = `
|
| 255 |
+
<h3>Batch Processing Complete</h3>
|
| 256 |
+
<p>Processed: ${result.summary.totalFiles} files</p>
|
| 257 |
+
<p>Successful: ${result.summary.successful}</p>
|
| 258 |
+
<p>Failed: ${result.summary.failed}</p>
|
| 259 |
+
|
| 260 |
+
<button onclick="downloadBatch('${result.batchId}')">
|
| 261 |
+
Download All Remediated Files
|
| 262 |
+
</button>
|
| 263 |
+
|
| 264 |
+
<div class="file-list">
|
| 265 |
+
${result.results.map(file => `
|
| 266 |
+
<div class="file-result ${file.success ? 'success' : 'error'}">
|
| 267 |
+
<strong>${file.filename}</strong>
|
| 268 |
+
${file.success ?
|
| 269 |
+
`<span>✓ ${file.summary.fixed} issues fixed</span>` :
|
| 270 |
+
`<span>✗ ${file.error}</span>`
|
| 271 |
+
}
|
| 272 |
+
</div>
|
| 273 |
+
`).join('')}
|
| 274 |
+
</div>
|
| 275 |
+
`;
|
| 276 |
+
}
|
| 277 |
+
```
|
| 278 |
+
|
| 279 |
+
---
|
| 280 |
+
|
| 281 |
+
## 🚨 IMPORTANT NOTES
|
| 282 |
+
|
| 283 |
+
1. **Session Required**: All new endpoints require a valid session ID
|
| 284 |
+
2. **Auto-Cleanup**: Files expire after 1 hour of inactivity
|
| 285 |
+
3. **No Permanent Storage**: Files are NOT saved permanently on the server
|
| 286 |
+
4. **Batch Limit**: Maximum 10 files per batch upload
|
| 287 |
+
5. **File Size**: Standard DOCX file size limits apply per file
|
| 288 |
+
|
| 289 |
+
---
|
| 290 |
+
|
| 291 |
+
## 📞 IMPLEMENTATION SUPPORT
|
| 292 |
+
|
| 293 |
+
**Ready-to-use example**: See `docs/batch-processing.html` for complete working implementation
|
| 294 |
+
|
| 295 |
+
**Test endpoints**: Use the existing test files in `tests/fixtures/` for testing
|
| 296 |
+
|
| 297 |
+
**Questions?** The backend is ready - just implement the session management and you're good to go! 🚀
|
README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#this gets the repo
|
| 2 |
+
git clone repo
|
| 3 |
+
|
| 4 |
+
#this gets up to date code
|
| 5 |
+
git pull
|
| 6 |
+
|
| 7 |
+
#this creates a branch which you can work on
|
| 8 |
+
git checkout -b "djo/your-branch-description"
|
| 9 |
+
|
| 10 |
+
#this installs everything you need
|
| 11 |
+
npm i
|
| 12 |
+
|
| 13 |
+
#this gives you secrets
|
| 14 |
+
get .env file from DJ or put secrets in manually into .env file which you create
|
| 15 |
+
|
| 16 |
+
##VERY IMPORTANT
|
| 17 |
+
make sure you create a git ignore file (ask chatgpt if you have never done this before) which ignores your .env file
|
| 18 |
+
|
| 19 |
+
#this runs the program
|
| 20 |
+
node autotag-pdf.js
|
| 21 |
+
|
| 22 |
+
#creates a branch with your changes
|
| 23 |
+
git push
|
| 24 |
+
|
| 25 |
+
#we can review pull requests as a team to identify if things are good for merge.
|
SHADOW_DEBUG.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
**SHADOW DEBUGGING GUIDE**
|
| 2 |
+
|
| 3 |
+
The shadow removal is working correctly in our tests. Here's how to debug why you might still see shadows:
|
| 4 |
+
|
| 5 |
+
## Step 1: Verify File Processing
|
| 6 |
+
1. Copy your problematic DOCX file to this directory
|
| 7 |
+
2. Rename it to 'user_test.docx'
|
| 8 |
+
3. Edit check-shadows.js and add 'user_test.docx' to the filesToCheck array
|
| 9 |
+
4. Run: node check-shadows.js
|
| 10 |
+
|
| 11 |
+
## Step 2: Test the Full Workflow
|
| 12 |
+
1. Upload your file through the frontend
|
| 13 |
+
2. Download the remediated version
|
| 14 |
+
3. Check if the downloaded file has shadows using the tool above
|
| 15 |
+
|
| 16 |
+
## Step 3: Visual vs XML Shadows
|
| 17 |
+
The shadows we remove are XML-level text shadows (<w:shadow/>). If you're still seeing visual shadows, they might be:
|
| 18 |
+
- CSS shadows from the document viewer
|
| 19 |
+
- Theme-based formatting
|
| 20 |
+
- Different shadow types (drawing objects, shapes, etc.)
|
| 21 |
+
|
| 22 |
+
## Step 4: Common Issues
|
| 23 |
+
- **Browser caching**: Clear cache and re-download
|
| 24 |
+
- **Wrong file**: Make sure you're opening the remediated file, not the original
|
| 25 |
+
- **File corruption**: Check if the file opens correctly in Word
|
| 26 |
+
- **Different shadow types**: Some shadows might be in drawing objects, not text runs
|
| 27 |
+
|
| 28 |
+
## Test Files Available:
|
| 29 |
+
- test_problematic.docx: Has shadows (for testing detection)
|
| 30 |
+
- test_remediated.docx: Shadows removed (for testing removal)
|
| 31 |
+
|
| 32 |
+
## Contact Info:
|
| 33 |
+
If shadows persist after these checks, please:
|
| 34 |
+
1. Share the specific file you're testing
|
| 35 |
+
2. Describe where you see the shadows (which text, which page)
|
| 36 |
+
3. Confirm you're opening the downloaded/remediated file
|
SHADOW_REMOVAL_COMPLETED.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Advanced Shadow Removal Implementation - COMPLETED ✅
|
| 2 |
+
|
| 3 |
+
## Problem Solved
|
| 4 |
+
You reported: **"The outer shadow, inner, and perspective is still there"**
|
| 5 |
+
|
| 6 |
+
## Root Cause Identified
|
| 7 |
+
The original shadow removal only handled basic `<w:shadow/>` elements, but **advanced shadow effects** use different XML namespaces and elements:
|
| 8 |
+
|
| 9 |
+
- **Outer shadows**: `<a:outerShdw>` (DrawingML)
|
| 10 |
+
- **Inner shadows**: `<a:innerShdw>` (DrawingML)
|
| 11 |
+
- **Perspective effects**: Office 2010+ text effects
|
| 12 |
+
- **Theme-based shadows**: Located in `word/theme/theme1.xml`
|
| 13 |
+
|
| 14 |
+
## Solution Implemented
|
| 15 |
+
|
| 16 |
+
### 1. Enhanced Shadow Detection & Removal
|
| 17 |
+
Both Node.js and Python implementations now handle:
|
| 18 |
+
|
| 19 |
+
**Basic Word Shadows:**
|
| 20 |
+
- `<w:shadow/>` and `<w:shadow>...</w:shadow>`
|
| 21 |
+
- Shadow attributes
|
| 22 |
+
|
| 23 |
+
**Advanced DrawingML Shadows:**
|
| 24 |
+
- `<a:outerShdw>` (outer shadow effects)
|
| 25 |
+
- `<a:innerShdw>` (inner shadow effects)
|
| 26 |
+
- `<a:prstShdw>` (preset shadow effects)
|
| 27 |
+
|
| 28 |
+
**Office 2010+ Effects:**
|
| 29 |
+
- `<w14:shadow>`, `<w15:shadow>` (version-specific shadows)
|
| 30 |
+
- `<w14:glow>` (glow effects)
|
| 31 |
+
- `<w14:reflection>` (reflection effects)
|
| 32 |
+
- `<w14:props3d>` (3D properties/perspective)
|
| 33 |
+
|
| 34 |
+
**Shadow Properties:**
|
| 35 |
+
- `outerShdw`, `innerShdw` property references
|
| 36 |
+
- All `*shdw*` attributes
|
| 37 |
+
|
| 38 |
+
### 2. Theme File Processing
|
| 39 |
+
Now processes **theme files** (`word/theme/theme1.xml`) where advanced shadow definitions are stored.
|
| 40 |
+
|
| 41 |
+
### 3. Files Updated
|
| 42 |
+
|
| 43 |
+
**Node.js API:**
|
| 44 |
+
- `api/download-document.js`: Enhanced `removeShadowsAndNormalizeFonts()` + theme processing
|
| 45 |
+
- `api/upload-document.js`: Enhanced shadow detection in `analyzeShadowsAndFonts()`
|
| 46 |
+
|
| 47 |
+
**Python Server:**
|
| 48 |
+
- `python-server/server.py`: Enhanced `remove_text_shadow_bytes()` + theme processing
|
| 49 |
+
|
| 50 |
+
## Test Results ✅
|
| 51 |
+
|
| 52 |
+
**Comprehensive Test Results:**
|
| 53 |
+
- ✅ **Basic shadows**: 2 removed (document.xml + styles.xml)
|
| 54 |
+
- ✅ **Advanced shadows**: 2 removed (theme1.xml DrawingML effects)
|
| 55 |
+
- ✅ **Total success**: 4/4 shadows completely removed
|
| 56 |
+
- ✅ **Enhanced test file**: `tests/fixtures/test_advanced_remediated.docx`
|
| 57 |
+
|
| 58 |
+
## Verification Files Created
|
| 59 |
+
|
| 60 |
+
1. **`check-shadows.js`**: Utility to verify any DOCX file for remaining shadows
|
| 61 |
+
2. **`test-advanced-shadows.js`**: Comprehensive shadow removal testing
|
| 62 |
+
3. **`test_advanced_remediated.docx`**: Clean test file with ALL shadows removed
|
| 63 |
+
|
| 64 |
+
## What to Test Now
|
| 65 |
+
|
| 66 |
+
**Use the enhanced remediated file**: `tests/fixtures/test_advanced_remediated.docx`
|
| 67 |
+
|
| 68 |
+
This file has been processed with the new comprehensive shadow removal and should have:
|
| 69 |
+
- ❌ **NO outer shadows**
|
| 70 |
+
- ❌ **NO inner shadows**
|
| 71 |
+
- ❌ **NO perspective effects**
|
| 72 |
+
- ❌ **NO text shadows of any type**
|
| 73 |
+
|
| 74 |
+
**Or test your own file:**
|
| 75 |
+
1. Upload through your frontend
|
| 76 |
+
2. Download the remediated version
|
| 77 |
+
3. Verify using: `node check-shadows.js` (modify to include your file)
|
| 78 |
+
|
| 79 |
+
## Technical Details
|
| 80 |
+
|
| 81 |
+
The enhanced removal now processes:
|
| 82 |
+
- `word/document.xml` ✅
|
| 83 |
+
- `word/styles.xml` ✅
|
| 84 |
+
- `word/theme/theme1.xml` ✅ **NEW**
|
| 85 |
+
- All shadow variants and properties ✅ **ENHANCED**
|
| 86 |
+
|
| 87 |
+
## Commit Hash
|
| 88 |
+
`f990dc9` - feat(shadow-removal): handle advanced shadow effects
|
| 89 |
+
|
| 90 |
+
---
|
| 91 |
+
|
| 92 |
+
**The outer shadow, inner shadow, and perspective effects should now be completely removed!** 🎉
|
TESTING_GUIDE.md
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
| 1 |
+
# 🧪 Complete Testing Guide - Step by Step
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
Your system has two parts:
|
| 6 |
+
1. **Python Backend** (FastAPI) - Analyzes PowerPoints and generates alt text
|
| 7 |
+
2. **Angular Frontend** (Web UI) - Upload interface for users
|
| 8 |
+
|
| 9 |
+
## ✅ Prerequisites Check
|
| 10 |
+
|
| 11 |
+
Before starting, verify everything is installed:
|
| 12 |
+
|
| 13 |
+
```bash
|
| 14 |
+
# Backend packages installed?
|
| 15 |
+
cd "Cycle 2 Testing/Accessibility-Checker-BE/python-server"
|
| 16 |
+
python -c "import fastapi; import transformers; print('✅ Backend ready')"
|
| 17 |
+
|
| 18 |
+
# Frontend dependencies installed?
|
| 19 |
+
cd "Cycle 2 Testing/Accessibility-Checker"
|
| 20 |
+
npm list angular 2>/dev/null | head -3
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## 🚀 Step 1: Start the Python Backend
|
| 26 |
+
|
| 27 |
+
### Open Terminal 1 (Backend)
|
| 28 |
+
|
| 29 |
+
```bash
|
| 30 |
+
cd "e:\Local Senior Project\Cycle 2 Testing\Accessibility-Checker-BE\python-server"
|
| 31 |
+
python server2.py
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
### Expected Output
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
✅ Local AI vision model loaded (BLIP - 100% FREE, No Costs)
|
| 38 |
+
📚 Loading ISO schema validation...
|
| 39 |
+
🚀 Uvicorn running on http://127.0.0.1:5000
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
**First run will download BLIP model (~1-2GB, takes 5-15 minutes)**
|
| 43 |
+
|
| 44 |
+
**Wait for this line before proceeding:**
|
| 45 |
+
```
|
| 46 |
+
Application startup complete
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
## 🚀 Step 2: Start the Angular Frontend
|
| 52 |
+
|
| 53 |
+
### Open Terminal 2 (Frontend)
|
| 54 |
+
|
| 55 |
+
```bash
|
| 56 |
+
cd "e:\Local Senior Project\Cycle 2 Testing\Accessibility-Checker"
|
| 57 |
+
npm start
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### Expected Output
|
| 61 |
+
|
| 62 |
+
```
|
| 63 |
+
✔ Compiled successfully
|
| 64 |
+
ℹ Application bundle generation complete
|
| 65 |
+
Initial Chunk Files | Names | Raw Size
|
| 66 |
+
vendor.js | | 2.5 MB |
|
| 67 |
+
main.js | | 250 KB |
|
| 68 |
+
...
|
| 69 |
+
✔ Build at: YYYY-MM-DD HH:MM:SS
|
| 70 |
+
✔ Serving from: .\
|
| 71 |
+
Application bundle generation complete
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### Open in Browser
|
| 75 |
+
|
| 76 |
+
Once you see "Compiled successfully", open:
|
| 77 |
+
```
|
| 78 |
+
http://localhost:4200
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
You should see the **Accessibility Checker** web interface.
|
| 82 |
+
|
| 83 |
+
---
|
| 84 |
+
|
| 85 |
+
## 📄 Step 3: Create or Get a Test PowerPoint
|
| 86 |
+
|
| 87 |
+
### Option A: Use Existing PowerPoint
|
| 88 |
+
- Look in: `Cycle 2 Testing\Accessibility-Checker-BE\test-docs\`
|
| 89 |
+
- Should contain sample PowerPoint files
|
| 90 |
+
|
| 91 |
+
### Option B: Create Simple Test PowerPoint
|
| 92 |
+
|
| 93 |
+
**For Windows (using PowerPoint):**
|
| 94 |
+
1. Open PowerPoint
|
| 95 |
+
2. Create a new presentation
|
| 96 |
+
3. Add a slide with:
|
| 97 |
+
- A title (e.g., "Test Slide")
|
| 98 |
+
- An image (any image)
|
| 99 |
+
- Leave the image WITHOUT alt text (that's what we're testing)
|
| 100 |
+
4. Save as: `test-presentation.pptx`
|
| 101 |
+
5. Save to a convenient location (e.g., Desktop)
|
| 102 |
+
|
| 103 |
+
**For Windows (using LibreOffice):**
|
| 104 |
+
```bash
|
| 105 |
+
# Install LibreOffice if needed
|
| 106 |
+
# Create presentation with libreoffice
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
**No PowerPoint installed?** Download a sample file from Microsoft Office templates or use the test files that might already exist.
|
| 110 |
+
|
| 111 |
+
---
|
| 112 |
+
|
| 113 |
+
## 📤 Step 4: Upload PowerPoint to System
|
| 114 |
+
|
| 115 |
+
### In the Web Browser (localhost:4200)
|
| 116 |
+
|
| 117 |
+
1. **Look for "Upload" button**
|
| 118 |
+
- Should be prominent on the page
|
| 119 |
+
- Usually labeled: "Upload PowerPoint" or "Choose File"
|
| 120 |
+
|
| 121 |
+
2. **Click and select your PowerPoint file**
|
| 122 |
+
- Navigate to your `test-presentation.pptx`
|
| 123 |
+
- Select it and upload
|
| 124 |
+
|
| 125 |
+
3. **Watch the Backend Console**
|
| 126 |
+
- You should see activity:
|
| 127 |
+
```
|
| 128 |
+
🔧 Starting alt text remediation for: test-presentation.pptx
|
| 129 |
+
AI Mode: LOCAL (100% FREE - No Costs)
|
| 130 |
+
🤖 Using FREE local AI (BLIP) for slide 1
|
| 131 |
+
✅ AI generated alt text for Picture 1: 'A colorful chart showing...'
|
| 132 |
+
✅ Remediation complete: 1 images processed
|
| 133 |
+
🤖 1 alt texts generated by FREE local AI (no cost)
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
+
## 📊 Step 5: View Results
|
| 139 |
+
|
| 140 |
+
### In Web Browser
|
| 141 |
+
|
| 142 |
+
After upload completes, you should see:
|
| 143 |
+
|
| 144 |
+
1. **Accessibility Report**
|
| 145 |
+
- Summary of issues found
|
| 146 |
+
- Number of images without alt text
|
| 147 |
+
- List of missing/bad alt text descriptions
|
| 148 |
+
|
| 149 |
+
2. **Sample Report Output**
|
| 150 |
+
```
|
| 151 |
+
FILE ANALYSIS RESULTS
|
| 152 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━
|
| 153 |
+
|
| 154 |
+
✅ Issues Fixed: 1
|
| 155 |
+
⚠️ Issues Flagged: 0
|
| 156 |
+
|
| 157 |
+
Image Alt Text Status:
|
| 158 |
+
• Slide 1 - Picture 1: "Bar chart with increasing values"
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
3. **Response JSON** (in browser console)
|
| 162 |
+
```json
|
| 163 |
+
{
|
| 164 |
+
"fileName": "test-presentation.pptx",
|
| 165 |
+
"suggestedFileName": "remediated-test-presentation.pptx",
|
| 166 |
+
"report": {
|
| 167 |
+
"summary": { "fixed": 1, "flagged": 0 },
|
| 168 |
+
"details": {
|
| 169 |
+
"imagesMissingOrBadAlt": []
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
+
|
| 177 |
+
## 💾 Step 6: Download Remediated File
|
| 178 |
+
|
| 179 |
+
### In Web Browser
|
| 180 |
+
|
| 181 |
+
1. **Look for "Download" button**
|
| 182 |
+
- Usually appears after upload
|
| 183 |
+
- Text might be: "Download Remediated PowerPoint" or "Download Fixed File"
|
| 184 |
+
|
| 185 |
+
2. **Click to download**
|
| 186 |
+
- File will save locally as: `remediated-test-presentation.pptx`
|
| 187 |
+
|
| 188 |
+
3. **Open downloaded file in PowerPoint**
|
| 189 |
+
```
|
| 190 |
+
Right-click image → Properties → Alt Text
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
4. **Verify alt text was added**
|
| 194 |
+
- Should see the AI-generated description
|
| 195 |
+
- Example: "Bar chart with increasing values"
|
| 196 |
+
|
| 197 |
+
---
|
| 198 |
+
|
| 199 |
+
## ✅ Verification Checklist
|
| 200 |
+
|
| 201 |
+
After completing all steps, check:
|
| 202 |
+
|
| 203 |
+
### Backend Console Should Show
|
| 204 |
+
- ✅ `✅ Local AI vision model loaded`
|
| 205 |
+
- ✅ `🤖 Using FREE local AI (BLIP) for slide X`
|
| 206 |
+
- ✅ `✅ AI generated alt text for Picture X`
|
| 207 |
+
- ✅ `✅ Remediation complete: X images processed`
|
| 208 |
+
- ✅ `🤖 X alt texts generated by FREE local AI`
|
| 209 |
+
|
| 210 |
+
### Downloaded File Should Have
|
| 211 |
+
- ✅ Original PowerPoint content preserved
|
| 212 |
+
- ✅ New alt text on all previously missing images
|
| 213 |
+
- ✅ Alt text is descriptive (not just "image" or "picture")
|
| 214 |
+
- ✅ File can be opened normally in PowerPoint
|
| 215 |
+
|
| 216 |
+
### Cost Should Be
|
| 217 |
+
- ✅ **$0.00** - No API charges
|
| 218 |
+
- ✅ No internet calls after first model download
|
| 219 |
+
- ✅ Everything local and private
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
## 🐛 Troubleshooting
|
| 224 |
+
|
| 225 |
+
### "Server not responding" / "Cannot connect to localhost:5000"
|
| 226 |
+
**Solution:**
|
| 227 |
+
1. Check Terminal 1 - is backend still running?
|
| 228 |
+
2. Look for errors in backend output
|
| 229 |
+
3. Restart backend: `Ctrl+C` then `python server2.py`
|
| 230 |
+
4. Wait for "Application startup complete"
|
| 231 |
+
|
| 232 |
+
### "Frontend not loading" / "Cannot access localhost:4200"
|
| 233 |
+
**Solution:**
|
| 234 |
+
1. Check Terminal 2 - is frontend still running?
|
| 235 |
+
2. Open http://localhost:4200 in browser
|
| 236 |
+
3. Check browser console for errors (F12)
|
| 237 |
+
4. Restart frontend: `Ctrl+C` then `npm start`
|
| 238 |
+
|
| 239 |
+
### "Model downloading..." for more than 20 minutes
|
| 240 |
+
**This is normal for first run!** Downloading 1-2GB takes time.
|
| 241 |
+
```
|
| 242 |
+
✔ First run: 5-15 minutes (downloading BLIP model)
|
| 243 |
+
✔ Subsequent runs: Instant (model cached)
|
| 244 |
+
```
|
| 245 |
+
|
| 246 |
+
### "AI not generating alt text" / Empty descriptions
|
| 247 |
+
**Check:**
|
| 248 |
+
1. Are images in PowerPoint actually visible?
|
| 249 |
+
2. Are images in supported formats (PNG, JPG)?
|
| 250 |
+
3. Try `python test_ai_setup.py` to verify AI works
|
| 251 |
+
4. Check backend console for error messages
|
| 252 |
+
|
| 253 |
+
### "Upload button doesn't appear"
|
| 254 |
+
**Solution:**
|
| 255 |
+
1. Check if frontend has compiled (look for "Compiled successfully")
|
| 256 |
+
2. Hard refresh browser: `Ctrl+Shift+R`
|
| 257 |
+
3. Open browser DevTools: `F12` → Console
|
| 258 |
+
4. Look for JavaScript errors
|
| 259 |
+
|
| 260 |
+
### "Downloaded file won't open"
|
| 261 |
+
**Solution:**
|
| 262 |
+
1. Check file size - should be similar to original
|
| 263 |
+
2. Try opening with different PowerPoint version
|
| 264 |
+
3. Check if file is corrupted - reupload
|
| 265 |
+
4. Look at backend logs for errors
|
| 266 |
+
|
| 267 |
+
---
|
| 268 |
+
|
| 269 |
+
## 📊 What to Expect: Real Example
|
| 270 |
+
|
| 271 |
+
### Input PowerPoint
|
| 272 |
+
- 3 slides
|
| 273 |
+
- 5 images total
|
| 274 |
+
- 0 images have alt text
|
| 275 |
+
|
| 276 |
+
### System Processing
|
| 277 |
+
```
|
| 278 |
+
🔧 Starting alt text remediation for: sample.pptx
|
| 279 |
+
AI Mode: LOCAL (100% FREE - No Costs)
|
| 280 |
+
🤖 Using FREE local AI (BLIP) for slide 1
|
| 281 |
+
✅ AI generated alt text for Picture 1: 'Professional man in business suit'
|
| 282 |
+
✅ AI generated alt text for Picture 2: 'Bar graph with red and blue columns'
|
| 283 |
+
🤖 Using FREE local AI (BLIP) for slide 2
|
| 284 |
+
✅ AI generated alt text for Picture 3: 'Team meeting in conference room'
|
| 285 |
+
✅ AI generated alt text for Picture 4: 'Laptop displaying code editor'
|
| 286 |
+
🤖 Using FREE local AI (BLIP) for slide 3
|
| 287 |
+
✅ AI generated alt text for Picture 5: 'Company logo on blue background'
|
| 288 |
+
✅ Remediation complete: 5 images processed
|
| 289 |
+
🤖 5 alt texts generated by FREE local AI (no cost)
|
| 290 |
+
```
|
| 291 |
+
|
| 292 |
+
### Output PowerPoint
|
| 293 |
+
- Same 3 slides, all images
|
| 294 |
+
- All 5 images now have descriptive alt text
|
| 295 |
+
- File works exactly like original
|
| 296 |
+
- **Cost: $0.00** 🎉
|
| 297 |
+
|
| 298 |
+
---
|
| 299 |
+
|
| 300 |
+
## 🎯 Testing Scenarios
|
| 301 |
+
|
| 302 |
+
### Test 1: Basic Image (Easy)
|
| 303 |
+
1. PowerPoint with 1 simple image
|
| 304 |
+
2. Expected: Describe what's in image
|
| 305 |
+
3. Example: "Logo design with blue colors"
|
| 306 |
+
|
| 307 |
+
### Test 2: Multiple Images (Medium)
|
| 308 |
+
1. PowerPoint with 3-5 images on different slides
|
| 309 |
+
2. Expected: Each gets unique description
|
| 310 |
+
3. Verify: All descriptions are different
|
| 311 |
+
|
| 312 |
+
### Test 3: Complex Presentation (Advanced)
|
| 313 |
+
1. Real presentation with charts, photos, logos
|
| 314 |
+
2. Expected: All get meaningful descriptions
|
| 315 |
+
3. Verify: Chart descriptions mention data/trends
|
| 316 |
+
|
| 317 |
+
---
|
| 318 |
+
|
| 319 |
+
## 📱 What The System Actually Does
|
| 320 |
+
|
| 321 |
+
### Internally
|
| 322 |
+
1. **Receives PowerPoint** → Unzips to XML
|
| 323 |
+
2. **Finds images** → Extracts from ZIP
|
| 324 |
+
3. **Analyzes images** → Uses local BLIP AI model
|
| 325 |
+
4. **Generates descriptions** → Creates alt text
|
| 326 |
+
5. **Updates XML** → Adds alt text to image properties
|
| 327 |
+
6. **Repackages** → Zips back into PowerPoint
|
| 328 |
+
7. **Delivers file** → User downloads fixed PowerPoint
|
| 329 |
+
|
| 330 |
+
### Data Flow
|
| 331 |
+
```
|
| 332 |
+
User PowerPoint
|
| 333 |
+
↓
|
| 334 |
+
Backend receives file
|
| 335 |
+
↓
|
| 336 |
+
Extract images from PowerPoint ZIP
|
| 337 |
+
↓
|
| 338 |
+
Send to LOCAL BLIP AI (runs on your computer)
|
| 339 |
+
↓
|
| 340 |
+
AI analyzes images
|
| 341 |
+
↓
|
| 342 |
+
AI generates descriptions
|
| 343 |
+
↓
|
| 344 |
+
Insert descriptions into PowerPoint XML
|
| 345 |
+
↓
|
| 346 |
+
Package back into PowerPoint file
|
| 347 |
+
↓
|
| 348 |
+
User downloads remediated file
|
| 349 |
+
```
|
| 350 |
+
|
| 351 |
+
**Key Point**: Everything runs locally - images never sent to internet!
|
| 352 |
+
|
| 353 |
+
---
|
| 354 |
+
|
| 355 |
+
## 💡 Tips for Best Results
|
| 356 |
+
|
| 357 |
+
1. **Use clear, simple images** - More likely to get good descriptions
|
| 358 |
+
2. **Include variety** - Test with photos, charts, logos
|
| 359 |
+
3. **Check backend console** - Understand what AI is doing
|
| 360 |
+
4. **Read descriptions carefully** - Verify they're accurate
|
| 361 |
+
5. **Edit if needed** - AI descriptions are starting point, not final
|
| 362 |
+
|
| 363 |
+
---
|
| 364 |
+
|
| 365 |
+
## 🚀 Next Steps After Testing
|
| 366 |
+
|
| 367 |
+
Once you verify everything works:
|
| 368 |
+
|
| 369 |
+
1. **Test with real presentations** from your team
|
| 370 |
+
2. **Collect feedback** - Is AI quality good enough?
|
| 371 |
+
3. **Adjust if needed** - Can tweak model in `.env`
|
| 372 |
+
4. **Deploy** - Set up on server for team to use
|
| 373 |
+
5. **Monitor costs** - Should always be $0 (local AI)
|
| 374 |
+
|
| 375 |
+
---
|
| 376 |
+
|
| 377 |
+
## 📞 Still Having Issues?
|
| 378 |
+
|
| 379 |
+
Check these in order:
|
| 380 |
+
|
| 381 |
+
1. **Backend running?** Terminal 1 shows "Application startup complete"
|
| 382 |
+
2. **Frontend running?** Terminal 2 shows "Compiled successfully"
|
| 383 |
+
3. **Both on correct ports?** Backend: 5000, Frontend: 4200
|
| 384 |
+
4. **Firewall blocking?** Windows Firewall might block local connections
|
| 385 |
+
5. **AI downloaded?** First run takes 5-15 min for BLIP model
|
| 386 |
+
|
| 387 |
+
If still stuck, check the **console output** - that's where errors appear!
|
| 388 |
+
|
| 389 |
+
---
|
| 390 |
+
|
| 391 |
+
## 🎉 Success Criteria
|
| 392 |
+
|
| 393 |
+
✅ Backend starts without errors
|
| 394 |
+
✅ Frontend loads in browser
|
| 395 |
+
✅ Can upload PowerPoint file
|
| 396 |
+
✅ System processes file (backend shows activity)
|
| 397 |
+
✅ Can download remediated file
|
| 398 |
+
✅ Downloaded file has alt text
|
| 399 |
+
✅ Alt text is descriptive (not generic)
|
| 400 |
+
✅ Cost is $0.00 (local AI only)
|
| 401 |
+
|
| 402 |
+
If all boxes checked → **Your system works!** 🚀
|
api/batch-download.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs').promises;
|
| 2 |
+
const path = require('path');
|
| 3 |
+
const JSZip = require('jszip');
|
| 4 |
+
const sessionManager = require('../lib/session-manager');
|
| 5 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 6 |
+
|
| 7 |
+
module.exports = async (req, res) => {
|
| 8 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'GET, OPTIONS' })) {
|
| 9 |
+
return;
|
| 10 |
+
}
|
| 11 |
+
applyCorsHeaders(req, res, { allowedMethods: 'GET, OPTIONS' });
|
| 12 |
+
|
| 13 |
+
if (req.method !== 'GET') {
|
| 14 |
+
res.status(405).json({ error: 'Method not allowed' });
|
| 15 |
+
return;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
const { batchId, sessionId } = req.query;
|
| 20 |
+
|
| 21 |
+
if (!batchId) {
|
| 22 |
+
res.status(400).json({ error: 'batchId parameter required' });
|
| 23 |
+
return;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
if (!sessionId) {
|
| 27 |
+
res.status(400).json({ error: 'sessionId parameter required' });
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Get session and verify it exists
|
| 32 |
+
const session = sessionManager.getOrCreateSession(sessionId);
|
| 33 |
+
if (session.sessionId !== sessionId) {
|
| 34 |
+
res.status(404).json({ error: 'Session expired or not found' });
|
| 35 |
+
return;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Load batch summary from session directory
|
| 39 |
+
const batchSummaryPath = `${session.directory}/batch-${batchId}-summary.json`;
|
| 40 |
+
let batchSummary;
|
| 41 |
+
|
| 42 |
+
try {
|
| 43 |
+
const summaryData = await fs.readFile(batchSummaryPath, 'utf8');
|
| 44 |
+
batchSummary = JSON.parse(summaryData);
|
| 45 |
+
} catch (error) {
|
| 46 |
+
res.status(404).json({ error: `Batch ${batchId} not found in session` });
|
| 47 |
+
return;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Create a ZIP file containing all remediated documents
|
| 51 |
+
const outputZip = new JSZip();
|
| 52 |
+
const batchFolder = outputZip.folder(`batch-${batchId}-remediated`);
|
| 53 |
+
|
| 54 |
+
let successCount = 0;
|
| 55 |
+
let errorCount = 0;
|
| 56 |
+
|
| 57 |
+
for (const result of batchSummary.results) {
|
| 58 |
+
if (!result.success) {
|
| 59 |
+
errorCount++;
|
| 60 |
+
// Add error file
|
| 61 |
+
batchFolder.file(`ERROR-${result.filename}.txt`,
|
| 62 |
+
`Error processing ${result.filename}:\n${result.error}`);
|
| 63 |
+
continue;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
try {
|
| 67 |
+
// Load the original file from session directory
|
| 68 |
+
const originalPath = `${session.directory}/original-${result.reportId}.docx`;
|
| 69 |
+
|
| 70 |
+
try {
|
| 71 |
+
const originalBuffer = await fs.readFile(originalPath);
|
| 72 |
+
|
| 73 |
+
// TODO: Apply remediation to the file here
|
| 74 |
+
// For now, just copy the original as "remediated"
|
| 75 |
+
batchFolder.file(`REMEDIATED-${result.filename}`, originalBuffer);
|
| 76 |
+
|
| 77 |
+
successCount++;
|
| 78 |
+
} catch (fileError) {
|
| 79 |
+
throw new Error(`Original file not found: ${fileError.message}`);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
} catch (error) {
|
| 83 |
+
errorCount++;
|
| 84 |
+
batchFolder.file(`ERROR-${result.filename}.txt`,
|
| 85 |
+
`Error remediating ${result.filename}:\n${error.message}`);
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// Add batch summary to the ZIP
|
| 90 |
+
batchFolder.file('batch-summary.json', JSON.stringify(batchSummary, null, 2));
|
| 91 |
+
batchFolder.file('README.txt',
|
| 92 |
+
`Batch Remediation Results\n` +
|
| 93 |
+
`========================\n` +
|
| 94 |
+
`Batch ID: ${batchId}\n` +
|
| 95 |
+
`Total Files: ${batchSummary.totalFiles}\n` +
|
| 96 |
+
`Successfully Processed: ${successCount}\n` +
|
| 97 |
+
`Errors: ${errorCount}\n` +
|
| 98 |
+
`Timestamp: ${batchSummary.timestamp}\n\n` +
|
| 99 |
+
`Files with "REMEDIATED-" prefix have been processed for accessibility.\n` +
|
| 100 |
+
`Files with "ERROR-" prefix encountered processing issues.\n`
|
| 101 |
+
);
|
| 102 |
+
|
| 103 |
+
// Generate the ZIP buffer
|
| 104 |
+
const zipBuffer = await outputZip.generateAsync({
|
| 105 |
+
type: 'nodebuffer',
|
| 106 |
+
compression: 'DEFLATE',
|
| 107 |
+
compressionOptions: { level: 6 }
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
// Send as download
|
| 111 |
+
res.setHeader('Content-Type', 'application/zip');
|
| 112 |
+
res.setHeader('Content-Disposition', `attachment; filename="batch-${batchId}-remediated.zip"`);
|
| 113 |
+
res.setHeader('Content-Length', zipBuffer.length);
|
| 114 |
+
|
| 115 |
+
res.end(zipBuffer);
|
| 116 |
+
|
| 117 |
+
} catch (error) {
|
| 118 |
+
console.error('Batch download error:', error);
|
| 119 |
+
res.status(500).json({ error: 'Internal server error during batch download' });
|
| 120 |
+
}
|
| 121 |
+
};
|
api/batch-upload.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const Busboy = require('busboy');
|
| 2 |
+
const JSZip = require('jszip');
|
| 3 |
+
const fs = require('fs').promises;
|
| 4 |
+
const path = require('path');
|
| 5 |
+
const sessionManager = require('../lib/session-manager');
|
| 6 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 7 |
+
|
| 8 |
+
// Helper function to send JSON with proper headers
|
| 9 |
+
function sendJson(res, status, data) {
|
| 10 |
+
res.setHeader('Content-Type', 'application/json');
|
| 11 |
+
res.status(status).end(JSON.stringify(data));
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
module.exports = async (req, res) => {
|
| 15 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'POST, OPTIONS' })) {
|
| 16 |
+
return;
|
| 17 |
+
}
|
| 18 |
+
applyCorsHeaders(req, res, { allowedMethods: 'POST, OPTIONS' });
|
| 19 |
+
|
| 20 |
+
if (req.method !== 'POST') {
|
| 21 |
+
sendJson(res, 405, { error: 'Method not allowed' });
|
| 22 |
+
return;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
try {
|
| 26 |
+
const busboy = Busboy({ headers: req.headers });
|
| 27 |
+
const uploadedFiles = []; // Store multiple files
|
| 28 |
+
const MAX_FILES = 10; // Allow up to 10 files per batch
|
| 29 |
+
let fileCount = 0;
|
| 30 |
+
|
| 31 |
+
busboy.on('file', (fieldname, file, info) => {
|
| 32 |
+
fileCount++;
|
| 33 |
+
|
| 34 |
+
if (fileCount > MAX_FILES) {
|
| 35 |
+
file.resume(); // Drain the file stream
|
| 36 |
+
return;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
const filename = info.filename;
|
| 40 |
+
const chunks = [];
|
| 41 |
+
|
| 42 |
+
file.on('data', (chunk) => {
|
| 43 |
+
chunks.push(chunk);
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
file.on('end', () => {
|
| 47 |
+
const fileData = Buffer.concat(chunks);
|
| 48 |
+
uploadedFiles.push({
|
| 49 |
+
filename: filename,
|
| 50 |
+
data: fileData,
|
| 51 |
+
size: fileData.length
|
| 52 |
+
});
|
| 53 |
+
});
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
busboy.on('finish', async () => {
|
| 57 |
+
if (uploadedFiles.length === 0) {
|
| 58 |
+
res.status(400).json({ error: 'No valid files uploaded' });
|
| 59 |
+
return;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
if (fileCount > MAX_FILES) {
|
| 63 |
+
res.status(400).json({
|
| 64 |
+
error: `Too many files. Maximum ${MAX_FILES} files allowed per batch.`,
|
| 65 |
+
received: fileCount
|
| 66 |
+
});
|
| 67 |
+
return;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Get or create session
|
| 71 |
+
const sessionId = req.headers['x-session-id'] || req.query.sessionId;
|
| 72 |
+
const session = sessionManager.getOrCreateSession(sessionId);
|
| 73 |
+
|
| 74 |
+
// Process each file and generate individual reports
|
| 75 |
+
const batchResults = {
|
| 76 |
+
batchId: Date.now(),
|
| 77 |
+
sessionId: session.sessionId,
|
| 78 |
+
timestamp: new Date().toISOString(),
|
| 79 |
+
totalFiles: uploadedFiles.length,
|
| 80 |
+
results: []
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
for (let i = 0; i < uploadedFiles.length; i++) {
|
| 84 |
+
const fileInfo = uploadedFiles[i];
|
| 85 |
+
|
| 86 |
+
try {
|
| 87 |
+
console.log(`Processing file ${i + 1}/${uploadedFiles.length}: ${fileInfo.filename}`);
|
| 88 |
+
|
| 89 |
+
// Process individual file (reuse existing logic)
|
| 90 |
+
const fileResult = await processSingleFile(fileInfo, session.directory);
|
| 91 |
+
|
| 92 |
+
// Add file to session
|
| 93 |
+
sessionManager.addFileToSession(session.sessionId, {
|
| 94 |
+
filename: fileInfo.filename,
|
| 95 |
+
reportId: fileResult.reportId,
|
| 96 |
+
originalPath: fileResult.originalFilePath,
|
| 97 |
+
reportPath: fileResult.reportPath,
|
| 98 |
+
processedAt: new Date().toISOString()
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
batchResults.results.push({
|
| 102 |
+
fileIndex: i + 1,
|
| 103 |
+
filename: fileInfo.filename,
|
| 104 |
+
fileSize: fileInfo.size,
|
| 105 |
+
success: true,
|
| 106 |
+
reportId: fileResult.reportId,
|
| 107 |
+
...fileResult.report
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
} catch (error) {
|
| 111 |
+
console.error(`Error processing ${fileInfo.filename}:`, error);
|
| 112 |
+
|
| 113 |
+
batchResults.results.push({
|
| 114 |
+
fileIndex: i + 1,
|
| 115 |
+
filename: fileInfo.filename,
|
| 116 |
+
fileSize: fileInfo.size,
|
| 117 |
+
success: false,
|
| 118 |
+
error: error.message
|
| 119 |
+
});
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// Save batch summary to session directory
|
| 124 |
+
const batchReportPath = `${session.directory}/batch-${batchResults.batchId}-summary.json`;
|
| 125 |
+
await fs.writeFile(batchReportPath, JSON.stringify(batchResults, null, 2));
|
| 126 |
+
|
| 127 |
+
// Add batch to session
|
| 128 |
+
sessionManager.addBatchToSession(session.sessionId, {
|
| 129 |
+
batchId: batchResults.batchId,
|
| 130 |
+
timestamp: batchResults.timestamp,
|
| 131 |
+
totalFiles: batchResults.totalFiles,
|
| 132 |
+
successful: batchResults.results.filter(r => r.success).length,
|
| 133 |
+
failed: batchResults.results.filter(r => !r.success).length,
|
| 134 |
+
reportPath: batchReportPath
|
| 135 |
+
});
|
| 136 |
+
|
| 137 |
+
// Return batch summary with session info
|
| 138 |
+
res.json({
|
| 139 |
+
message: `Successfully processed batch of ${uploadedFiles.length} files`,
|
| 140 |
+
sessionId: session.sessionId,
|
| 141 |
+
batchId: batchResults.batchId,
|
| 142 |
+
summary: {
|
| 143 |
+
totalFiles: batchResults.totalFiles,
|
| 144 |
+
successful: batchResults.results.filter(r => r.success).length,
|
| 145 |
+
failed: batchResults.results.filter(r => !r.success).length
|
| 146 |
+
},
|
| 147 |
+
results: batchResults.results,
|
| 148 |
+
expiresIn: '1 hour'
|
| 149 |
+
});
|
| 150 |
+
});
|
| 151 |
+
|
| 152 |
+
req.pipe(busboy);
|
| 153 |
+
|
| 154 |
+
} catch (error) {
|
| 155 |
+
console.error('Batch upload error:', error);
|
| 156 |
+
res.status(500).json({ error: 'Internal server error during batch processing' });
|
| 157 |
+
}
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
// Extract single file processing logic (from existing upload-document.js)
|
| 161 |
+
async function processSingleFile(fileInfo, sessionDirectory) {
|
| 162 |
+
const { filename, data } = fileInfo;
|
| 163 |
+
|
| 164 |
+
// Validate DOCX file
|
| 165 |
+
if (!filename.toLowerCase().endsWith('.docx')) {
|
| 166 |
+
throw new Error(`Invalid file type: ${filename}. Only .docx files are supported.`);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
let zip;
|
| 170 |
+
try {
|
| 171 |
+
zip = await JSZip.loadAsync(data);
|
| 172 |
+
} catch (error) {
|
| 173 |
+
throw new Error(`Invalid DOCX file: ${filename}. Unable to read as ZIP archive.`);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// Generate unique report ID for this file
|
| 177 |
+
const reportId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
| 178 |
+
|
| 179 |
+
// Initialize report structure
|
| 180 |
+
const report = {
|
| 181 |
+
filename: filename,
|
| 182 |
+
reportId: reportId,
|
| 183 |
+
timestamp: new Date().toISOString(),
|
| 184 |
+
summary: {
|
| 185 |
+
flagged: 0,
|
| 186 |
+
fixed: 0
|
| 187 |
+
},
|
| 188 |
+
details: {
|
| 189 |
+
hasProtection: false,
|
| 190 |
+
removedProtection: false,
|
| 191 |
+
languageDefaultFixed: null,
|
| 192 |
+
titleNeedsFixing: false,
|
| 193 |
+
textShadowsRemoved: false,
|
| 194 |
+
fontsNormalized: false,
|
| 195 |
+
fontSizesNormalized: false
|
| 196 |
+
}
|
| 197 |
+
};
|
| 198 |
+
|
| 199 |
+
// Run all analysis functions (copied from existing logic)
|
| 200 |
+
await analyzeDocumentStructure(zip, report);
|
| 201 |
+
await analyzeProtection(zip, report);
|
| 202 |
+
const shadowFontResults = await analyzeShadowsAndFonts(zip);
|
| 203 |
+
|
| 204 |
+
// Update report with shadow/font analysis
|
| 205 |
+
if (shadowFontResults.hasShadows) {
|
| 206 |
+
report.details.textShadowsRemoved = false; // Will be true after remediation
|
| 207 |
+
report.summary.flagged++;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
if (shadowFontResults.hasSerifFonts) {
|
| 211 |
+
report.details.fontsNormalized = false; // Will be true after remediation
|
| 212 |
+
report.summary.flagged++;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
if (shadowFontResults.hasSmallFonts) {
|
| 216 |
+
report.details.fontSizesNormalized = false; // Will be true after remediation
|
| 217 |
+
report.summary.flagged++;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
// Save original file and report to session directory (not permanent storage)
|
| 221 |
+
const originalFilePath = `${sessionDirectory}/original-${reportId}.docx`;
|
| 222 |
+
const reportPath = `${sessionDirectory}/${reportId}-accessibility-report.json`;
|
| 223 |
+
|
| 224 |
+
await fs.writeFile(originalFilePath, data);
|
| 225 |
+
await fs.writeFile(reportPath, JSON.stringify(report, null, 2));
|
| 226 |
+
|
| 227 |
+
return {
|
| 228 |
+
reportId: reportId,
|
| 229 |
+
report: report,
|
| 230 |
+
reportPath: reportPath,
|
| 231 |
+
originalFilePath: originalFilePath
|
| 232 |
+
};
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
// Copy existing analysis functions (you'll need to import these)
|
| 236 |
+
async function analyzeDocumentStructure(zip, report) {
|
| 237 |
+
// Implementation from existing upload-document.js
|
| 238 |
+
// ... existing logic ...
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
async function analyzeProtection(zip, report) {
|
| 242 |
+
// Implementation from existing upload-document.js
|
| 243 |
+
// ... existing logic ...
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
async function analyzeShadowsAndFonts(zip) {
|
| 247 |
+
// Implementation from existing upload-document.js
|
| 248 |
+
// ... existing logic ...
|
| 249 |
+
}
|
api/cors-test.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 2 |
+
|
| 3 |
+
module.exports = async (req, res) => {
|
| 4 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'GET, POST, PUT, DELETE, OPTIONS' })) {
|
| 5 |
+
return;
|
| 6 |
+
}
|
| 7 |
+
applyCorsHeaders(req, res, { allowedMethods: 'GET, POST, PUT, DELETE, OPTIONS' });
|
| 8 |
+
|
| 9 |
+
res.setHeader('Content-Type', 'application/json');
|
| 10 |
+
|
| 11 |
+
if (req.method === 'OPTIONS') {
|
| 12 |
+
return res.status(200).end();
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
return res.status(200).end(JSON.stringify({ ok: true }));
|
| 16 |
+
};
|
api/download-document.js
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const Busboy = require('busboy');
|
| 2 |
+
const JSZip = require('jszip');
|
| 3 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 4 |
+
|
| 5 |
+
// Helper function to send JSON with proper headers
|
| 6 |
+
function sendJson(res, status, data) {
|
| 7 |
+
res.setHeader('Content-Type', 'application/json');
|
| 8 |
+
res.status(status).end(JSON.stringify(data));
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
module.exports = async (req, res) => {
|
| 12 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'POST, OPTIONS' })) {
|
| 13 |
+
return;
|
| 14 |
+
}
|
| 15 |
+
applyCorsHeaders(req, res, { allowedMethods: 'POST, OPTIONS' });
|
| 16 |
+
|
| 17 |
+
if (req.method !== 'POST') {
|
| 18 |
+
sendJson(res, 405, { error: 'Method not allowed' });
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
try {
|
| 23 |
+
const busboy = Busboy({ headers: req.headers });
|
| 24 |
+
let fileData = null;
|
| 25 |
+
let filename = null;
|
| 26 |
+
|
| 27 |
+
busboy.on('file', (fieldname, file, info) => {
|
| 28 |
+
filename = info.filename;
|
| 29 |
+
const chunks = [];
|
| 30 |
+
|
| 31 |
+
file.on('data', (chunk) => {
|
| 32 |
+
chunks.push(chunk);
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
file.on('end', () => {
|
| 36 |
+
fileData = Buffer.concat(chunks);
|
| 37 |
+
});
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
busboy.on('finish', async () => {
|
| 41 |
+
if (!fileData || !filename) {
|
| 42 |
+
res.status(400).json({ error: 'No file uploaded' });
|
| 43 |
+
return;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
if (!filename.toLowerCase().endsWith('.docx')) {
|
| 47 |
+
res.status(400).json({ error: 'Please upload a .docx file' });
|
| 48 |
+
return;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
try {
|
| 52 |
+
const remediatedFile = await remediateDocx(fileData, filename);
|
| 53 |
+
|
| 54 |
+
// Always fix filename: replace underscores with hyphens and add -remediated suffix
|
| 55 |
+
let suggestedName = filename
|
| 56 |
+
.replace(/_/g, '-') // Replace all underscores with hyphens
|
| 57 |
+
.replace(/\.docx$/i, '-remediated.docx'); // Add -remediated before extension
|
| 58 |
+
|
| 59 |
+
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
| 60 |
+
res.setHeader('Content-Disposition', `attachment; filename="${suggestedName}"`);
|
| 61 |
+
res.status(200).send(remediatedFile);
|
| 62 |
+
|
| 63 |
+
} catch (error) {
|
| 64 |
+
res.status(500).json({ error: error.message });
|
| 65 |
+
}
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
req.pipe(busboy);
|
| 69 |
+
|
| 70 |
+
} catch (error) {
|
| 71 |
+
res.status(500).json({ error: error.message });
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
async function remediateDocx(fileData, filename) {
|
| 76 |
+
try {
|
| 77 |
+
const zip = await JSZip.loadAsync(fileData);
|
| 78 |
+
|
| 79 |
+
// Helper function to write only if content changed
|
| 80 |
+
const writeIfChanged = (filename, original, modified) => {
|
| 81 |
+
if (original !== modified && modified !== null) {
|
| 82 |
+
zip.file(filename, modified);
|
| 83 |
+
return true;
|
| 84 |
+
}
|
| 85 |
+
return false;
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
// Process document.xml
|
| 89 |
+
const docFile = zip.file('word/document.xml');
|
| 90 |
+
if (docFile) {
|
| 91 |
+
const origDocXml = await docFile.async('string');
|
| 92 |
+
const afterShadows = removeShadowsOnly(origDocXml);
|
| 93 |
+
const afterInlineContent = applyInlineContentFixes(afterShadows || origDocXml);
|
| 94 |
+
writeIfChanged('word/document.xml', origDocXml, afterInlineContent);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// Process styles.xml
|
| 98 |
+
const stylesFile = zip.file('word/styles.xml');
|
| 99 |
+
if (stylesFile) {
|
| 100 |
+
const origStylesXml = await stylesFile.async('string');
|
| 101 |
+
const afterStylesShadows = removeShadowsOnly(origStylesXml);
|
| 102 |
+
writeIfChanged('word/styles.xml', origStylesXml, afterStylesShadows);
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
// Process theme files
|
| 106 |
+
const themeFile = zip.file('word/theme/theme1.xml');
|
| 107 |
+
if (themeFile) {
|
| 108 |
+
const origThemeXml = await themeFile.async('string');
|
| 109 |
+
const afterTheme = removeShadowsOnly(origThemeXml);
|
| 110 |
+
writeIfChanged('word/theme/theme1.xml', origThemeXml, afterTheme);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Protection removal
|
| 114 |
+
try {
|
| 115 |
+
const settingsFile = zip.file('word/settings.xml');
|
| 116 |
+
if (settingsFile) {
|
| 117 |
+
const origSettings = await settingsFile.async('string');
|
| 118 |
+
const hasAnyProt = /<w:(?:documentProtection|writeProtection|readOnlyRecommended|editRestrictions|formProtection|protection|docProtection|enforcement|locked|trackRevisions|crypt)\b/.test(origSettings);
|
| 119 |
+
if (hasAnyProt) {
|
| 120 |
+
let cleaned = origSettings;
|
| 121 |
+
|
| 122 |
+
cleaned = cleaned.replace(/<w:(?:documentProtection|writeProtection|readOnlyRecommended|editRestrictions|formProtection|protection|docProtection)[^>]*\/>/g, '');
|
| 123 |
+
cleaned = cleaned.replace(/<w:(?:documentProtection|writeProtection|readOnlyRecommended|editRestrictions|formProtection|protection|docProtection)[^>]*>[\s\S]*?<\/w:(?:documentProtection|writeProtection|readOnlyRecommended|editRestrictions|formProtection|protection|docProtection)>/g, '');
|
| 124 |
+
cleaned = cleaned.replace(/<w:(?:enforcement|locked|trackRevisions)[^>]*\/>/g, '');
|
| 125 |
+
cleaned = cleaned.replace(/<w:(?:enforcement|locked|trackRevisions)[^>]*>[\s\S]*?<\/w:(?:enforcement|locked|trackRevisions)>/g, '');
|
| 126 |
+
cleaned = cleaned.replace(/<w:crypt[^>]*\/>/g, '');
|
| 127 |
+
cleaned = cleaned.replace(/<w:crypt[^>]*>[\s\S]*?<\/w:crypt[^>]*>/g, '');
|
| 128 |
+
cleaned = cleaned.replace(/\s?w:(?:locked|trackRevisions|enforcement)="[^"]*"/g, '');
|
| 129 |
+
|
| 130 |
+
writeIfChanged('word/settings.xml', origSettings, cleaned);
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
} catch (e) {
|
| 134 |
+
console.warn('[remediateDocx] Protection removal failed:', e.message);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
// Generate with proper compression
|
| 138 |
+
const remediatedBuffer = await zip.generateAsync({
|
| 139 |
+
type: 'nodebuffer',
|
| 140 |
+
compression: 'DEFLATE',
|
| 141 |
+
compressionOptions: { level: 6 }
|
| 142 |
+
});
|
| 143 |
+
|
| 144 |
+
return remediatedBuffer;
|
| 145 |
+
|
| 146 |
+
} catch (error) {
|
| 147 |
+
throw new Error(`Failed to remediate document: ${error.message}`);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
function applyInlineContentFixes(xmlContent) {
|
| 154 |
+
if (!xmlContent) return null;
|
| 155 |
+
|
| 156 |
+
const original = xmlContent;
|
| 157 |
+
let fixedXml = xmlContent;
|
| 158 |
+
|
| 159 |
+
// Apply the same patterns as in the analysis function
|
| 160 |
+
const floatingPatterns = [
|
| 161 |
+
// DrawingML anchor patterns (modern Word drawings)
|
| 162 |
+
{
|
| 163 |
+
pattern: /<wp:anchor[^>]*>([\s\S]*?)<\/wp:anchor>/g,
|
| 164 |
+
replacement: function(match, content) {
|
| 165 |
+
// Convert anchor (floating) to inline
|
| 166 |
+
return `<wp:inline>${content}</wp:inline>`;
|
| 167 |
+
}
|
| 168 |
+
},
|
| 169 |
+
// Text wrapping patterns
|
| 170 |
+
{
|
| 171 |
+
pattern: /<wp:wrapSquare[^>]*\/>/g,
|
| 172 |
+
replacement: ''
|
| 173 |
+
},
|
| 174 |
+
{
|
| 175 |
+
pattern: /<wp:wrapTight[^>]*>[\s\S]*?<\/wp:wrapTight>/g,
|
| 176 |
+
replacement: ''
|
| 177 |
+
},
|
| 178 |
+
{
|
| 179 |
+
pattern: /<wp:wrapThrough[^>]*>[\s\S]*?<\/wp:wrapThrough>/g,
|
| 180 |
+
replacement: ''
|
| 181 |
+
},
|
| 182 |
+
{
|
| 183 |
+
pattern: /<wp:wrapTopAndBottom[^>]*\/>/g,
|
| 184 |
+
replacement: ''
|
| 185 |
+
},
|
| 186 |
+
{
|
| 187 |
+
pattern: /<wp:wrapNone[^>]*\/>/g,
|
| 188 |
+
replacement: ''
|
| 189 |
+
},
|
| 190 |
+
// Position and alignment patterns
|
| 191 |
+
{
|
| 192 |
+
pattern: /<wp:positionH[^>]*>[\s\S]*?<\/wp:positionH>/g,
|
| 193 |
+
replacement: ''
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
pattern: /<wp:positionV[^>]*>[\s\S]*?<\/wp:positionV>/g,
|
| 197 |
+
replacement: ''
|
| 198 |
+
},
|
| 199 |
+
// VML patterns for legacy compatibility
|
| 200 |
+
{
|
| 201 |
+
pattern: /mso-position-horizontal:[^;]*;?/g,
|
| 202 |
+
replacement: ''
|
| 203 |
+
},
|
| 204 |
+
{
|
| 205 |
+
pattern: /mso-position-vertical:[^;]*;?/g,
|
| 206 |
+
replacement: ''
|
| 207 |
+
},
|
| 208 |
+
{
|
| 209 |
+
pattern: /mso-wrap-style:[^;]*;?/g,
|
| 210 |
+
replacement: ''
|
| 211 |
+
},
|
| 212 |
+
{
|
| 213 |
+
pattern: /left:\s*[^;]*;?/g,
|
| 214 |
+
replacement: ''
|
| 215 |
+
},
|
| 216 |
+
{
|
| 217 |
+
pattern: /top:\s*[^;]*;?/g,
|
| 218 |
+
replacement: ''
|
| 219 |
+
}
|
| 220 |
+
];
|
| 221 |
+
|
| 222 |
+
// Apply fixes for floating elements
|
| 223 |
+
floatingPatterns.forEach(patternObj => {
|
| 224 |
+
const { pattern, replacement } = patternObj;
|
| 225 |
+
|
| 226 |
+
if (typeof replacement === 'function') {
|
| 227 |
+
fixedXml = fixedXml.replace(pattern, replacement);
|
| 228 |
+
} else {
|
| 229 |
+
fixedXml = fixedXml.replace(pattern, replacement);
|
| 230 |
+
}
|
| 231 |
+
});
|
| 232 |
+
|
| 233 |
+
// Special handling for drawing elements - ensure they are inline
|
| 234 |
+
const drawingPattern = /<w:drawing[^>]*>[\s\S]*?<\/w:drawing>/g;
|
| 235 |
+
const drawingMatches = fixedXml.match(drawingPattern);
|
| 236 |
+
|
| 237 |
+
if (drawingMatches) {
|
| 238 |
+
drawingMatches.forEach(drawing => {
|
| 239 |
+
// Check if this drawing contains floating elements
|
| 240 |
+
if (drawing.includes('wp:anchor') && !drawing.includes('wp:inline')) {
|
| 241 |
+
// Convert anchor to inline within the drawing
|
| 242 |
+
let fixedDrawing = drawing.replace(/<wp:anchor[^>]*>/g, '<wp:inline>');
|
| 243 |
+
fixedDrawing = fixedDrawing.replace(/<\/wp:anchor>/g, '</wp:inline>');
|
| 244 |
+
|
| 245 |
+
if (fixedDrawing !== drawing) {
|
| 246 |
+
fixedXml = fixedXml.replace(drawing, fixedDrawing);
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
});
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
// If nothing changed, return null
|
| 253 |
+
if (fixedXml === original) return null;
|
| 254 |
+
return fixedXml;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
function removeShadowsOnly(xmlContent) {
|
| 258 |
+
const original = xmlContent;
|
| 259 |
+
let fixedXml = xmlContent;
|
| 260 |
+
|
| 261 |
+
// 1. Remove basic Word text shadows
|
| 262 |
+
fixedXml = fixedXml.replace(/<w:shadow\s*\/>/g, '');
|
| 263 |
+
fixedXml = fixedXml.replace(/<w:shadow[^>]*>.*?<\/w:shadow>/g, '');
|
| 264 |
+
fixedXml = fixedXml.replace(/\s+\w*shadow\w*\s*=\s*"[^"]*"/g, '');
|
| 265 |
+
|
| 266 |
+
// 2. Remove advanced DrawingML shadow effects
|
| 267 |
+
fixedXml = fixedXml.replace(/<a:outerShdw[^>]*\/>/g, '');
|
| 268 |
+
fixedXml = fixedXml.replace(/<a:outerShdw[^>]*>.*?<\/a:outerShdw>/g, '');
|
| 269 |
+
fixedXml = fixedXml.replace(/<a:innerShdw[^>]*\/>/g, '');
|
| 270 |
+
fixedXml = fixedXml.replace(/<a:innerShdw[^>]*>.*?<\/a:innerShdw>/g, '');
|
| 271 |
+
fixedXml = fixedXml.replace(/<a:prstShdw[^>]*\/>/g, '');
|
| 272 |
+
fixedXml = fixedXml.replace(/<a:prstShdw[^>]*>.*?<\/a:prstShdw>/g, '');
|
| 273 |
+
|
| 274 |
+
// 3. Remove Office 2010+ shadow effects
|
| 275 |
+
fixedXml = fixedXml.replace(/<w14:shadow[^>]*\/>/g, '');
|
| 276 |
+
fixedXml = fixedXml.replace(/<w14:shadow[^>]*>.*?<\/w14:shadow>/g, '');
|
| 277 |
+
fixedXml = fixedXml.replace(/<w15:shadow[^>]*\/>/g, '');
|
| 278 |
+
fixedXml = fixedXml.replace(/<w15:shadow[^>]*>.*?<\/w15:shadow>/g, '');
|
| 279 |
+
|
| 280 |
+
// 4. Remove shadow-related text effects and 3D properties
|
| 281 |
+
fixedXml = fixedXml.replace(/<w14:glow[^>]*\/>/g, '');
|
| 282 |
+
fixedXml = fixedXml.replace(/<w14:glow[^>]*>.*?<\/w14:glow>/g, '');
|
| 283 |
+
fixedXml = fixedXml.replace(/<w14:reflection[^>]*\/>/g, '');
|
| 284 |
+
fixedXml = fixedXml.replace(/<w14:reflection[^>]*>.*?<\/w14:reflection>/g, '');
|
| 285 |
+
fixedXml = fixedXml.replace(/<w14:props3d[^>]*\/>/g, '');
|
| 286 |
+
fixedXml = fixedXml.replace(/<w14:props3d[^>]*>.*?<\/w14:props3d>/g, '');
|
| 287 |
+
|
| 288 |
+
// 5. Remove shadow properties and attributes (safely)
|
| 289 |
+
// Remove only within attribute values, not entire element names
|
| 290 |
+
fixedXml = fixedXml.replace(/\s+\w*shdw\w*\s*=\s*"[^"]*"/g, '');
|
| 291 |
+
|
| 292 |
+
// NOTE: Font normalization, font size fixes, and line spacing fixes have been
|
| 293 |
+
// removed - these are now flagged for user attention instead of auto-fixed
|
| 294 |
+
|
| 295 |
+
// If nothing changed, return null so callers can avoid rewriting the part
|
| 296 |
+
if (fixedXml === original) return null;
|
| 297 |
+
return fixedXml;
|
| 298 |
+
}
|
api/reports.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs').promises;
|
| 2 |
+
const path = require('path');
|
| 3 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 4 |
+
|
| 5 |
+
module.exports = async (req, res) => {
|
| 6 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'GET, DELETE, OPTIONS' })) {
|
| 7 |
+
return;
|
| 8 |
+
}
|
| 9 |
+
applyCorsHeaders(req, res, { allowedMethods: 'GET, DELETE, OPTIONS' });
|
| 10 |
+
|
| 11 |
+
const { action, reportId, batchId, limit = 50 } = req.query;
|
| 12 |
+
|
| 13 |
+
try {
|
| 14 |
+
switch (req.method) {
|
| 15 |
+
case 'GET':
|
| 16 |
+
if (action === 'list') {
|
| 17 |
+
await listReports(req, res, { limit: parseInt(limit) });
|
| 18 |
+
} else if (action === 'batches') {
|
| 19 |
+
await listBatches(req, res);
|
| 20 |
+
} else if (reportId) {
|
| 21 |
+
await getReport(req, res, reportId);
|
| 22 |
+
} else if (batchId) {
|
| 23 |
+
await getBatch(req, res, batchId);
|
| 24 |
+
} else {
|
| 25 |
+
res.status(400).json({ error: 'Missing action or ID parameter' });
|
| 26 |
+
}
|
| 27 |
+
break;
|
| 28 |
+
|
| 29 |
+
case 'DELETE':
|
| 30 |
+
if (reportId) {
|
| 31 |
+
await deleteReport(req, res, reportId);
|
| 32 |
+
} else if (batchId) {
|
| 33 |
+
await deleteBatch(req, res, batchId);
|
| 34 |
+
} else {
|
| 35 |
+
res.status(400).json({ error: 'Missing reportId or batchId parameter' });
|
| 36 |
+
}
|
| 37 |
+
break;
|
| 38 |
+
|
| 39 |
+
default:
|
| 40 |
+
res.status(405).json({ error: 'Method not allowed' });
|
| 41 |
+
}
|
| 42 |
+
} catch (error) {
|
| 43 |
+
console.error('Reports API error:', error);
|
| 44 |
+
res.status(500).json({ error: 'Internal server error' });
|
| 45 |
+
}
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
async function listReports(req, res, options = {}) {
|
| 49 |
+
const reportsDir = 'reports';
|
| 50 |
+
const files = await fs.readdir(reportsDir);
|
| 51 |
+
|
| 52 |
+
// Filter for individual reports (not batch summaries)
|
| 53 |
+
const reportFiles = files
|
| 54 |
+
.filter(f => f.endsWith('-accessibility-report.json'))
|
| 55 |
+
.sort((a, b) => {
|
| 56 |
+
// Sort by timestamp (newest first)
|
| 57 |
+
const aTime = parseInt(a.split('-')[0]);
|
| 58 |
+
const bTime = parseInt(b.split('-')[0]);
|
| 59 |
+
return bTime - aTime;
|
| 60 |
+
})
|
| 61 |
+
.slice(0, options.limit);
|
| 62 |
+
|
| 63 |
+
const reports = [];
|
| 64 |
+
|
| 65 |
+
for (const file of reportFiles) {
|
| 66 |
+
try {
|
| 67 |
+
const filePath = path.join(reportsDir, file);
|
| 68 |
+
const content = await fs.readFile(filePath, 'utf8');
|
| 69 |
+
const report = JSON.parse(content);
|
| 70 |
+
|
| 71 |
+
reports.push({
|
| 72 |
+
reportId: report.reportId,
|
| 73 |
+
filename: report.filename,
|
| 74 |
+
timestamp: report.timestamp,
|
| 75 |
+
summary: report.summary,
|
| 76 |
+
filePath: file
|
| 77 |
+
});
|
| 78 |
+
} catch (error) {
|
| 79 |
+
console.warn(`Failed to read report ${file}:`, error.message);
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
res.json({
|
| 84 |
+
totalReports: reports.length,
|
| 85 |
+
reports: reports
|
| 86 |
+
});
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
async function listBatches(req, res) {
|
| 90 |
+
const reportsDir = 'reports';
|
| 91 |
+
const files = await fs.readdir(reportsDir);
|
| 92 |
+
|
| 93 |
+
// Filter for batch summaries
|
| 94 |
+
const batchFiles = files
|
| 95 |
+
.filter(f => f.startsWith('batch-') && f.endsWith('-summary.json'))
|
| 96 |
+
.sort((a, b) => {
|
| 97 |
+
// Sort by timestamp (newest first)
|
| 98 |
+
const aTime = parseInt(a.split('-')[1]);
|
| 99 |
+
const bTime = parseInt(b.split('-')[1]);
|
| 100 |
+
return bTime - aTime;
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
const batches = [];
|
| 104 |
+
|
| 105 |
+
for (const file of batchFiles) {
|
| 106 |
+
try {
|
| 107 |
+
const filePath = path.join(reportsDir, file);
|
| 108 |
+
const content = await fs.readFile(filePath, 'utf8');
|
| 109 |
+
const batch = JSON.parse(content);
|
| 110 |
+
|
| 111 |
+
batches.push({
|
| 112 |
+
batchId: batch.batchId,
|
| 113 |
+
timestamp: batch.timestamp,
|
| 114 |
+
totalFiles: batch.totalFiles,
|
| 115 |
+
successful: batch.results.filter(r => r.success).length,
|
| 116 |
+
failed: batch.results.filter(r => !r.success).length,
|
| 117 |
+
filePath: file
|
| 118 |
+
});
|
| 119 |
+
} catch (error) {
|
| 120 |
+
console.warn(`Failed to read batch ${file}:`, error.message);
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
res.json({
|
| 125 |
+
totalBatches: batches.length,
|
| 126 |
+
batches: batches
|
| 127 |
+
});
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
async function getReport(req, res, reportId) {
|
| 131 |
+
const reportPath = `reports/${reportId}-accessibility-report.json`;
|
| 132 |
+
|
| 133 |
+
try {
|
| 134 |
+
const content = await fs.readFile(reportPath, 'utf8');
|
| 135 |
+
const report = JSON.parse(content);
|
| 136 |
+
res.json(report);
|
| 137 |
+
} catch (error) {
|
| 138 |
+
res.status(404).json({ error: `Report ${reportId} not found` });
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
async function getBatch(req, res, batchId) {
|
| 143 |
+
const batchPath = `reports/batch-${batchId}-summary.json`;
|
| 144 |
+
|
| 145 |
+
try {
|
| 146 |
+
const content = await fs.readFile(batchPath, 'utf8');
|
| 147 |
+
const batch = JSON.parse(content);
|
| 148 |
+
res.json(batch);
|
| 149 |
+
} catch (error) {
|
| 150 |
+
res.status(404).json({ error: `Batch ${batchId} not found` });
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
async function deleteReport(req, res, reportId) {
|
| 155 |
+
const reportPath = `reports/${reportId}-accessibility-report.json`;
|
| 156 |
+
|
| 157 |
+
try {
|
| 158 |
+
await fs.unlink(reportPath);
|
| 159 |
+
res.json({ message: `Report ${reportId} deleted successfully` });
|
| 160 |
+
} catch (error) {
|
| 161 |
+
res.status(404).json({ error: `Report ${reportId} not found` });
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
async function deleteBatch(req, res, batchId) {
|
| 166 |
+
const batchPath = `reports/batch-${batchId}-summary.json`;
|
| 167 |
+
|
| 168 |
+
try {
|
| 169 |
+
await fs.unlink(batchPath);
|
| 170 |
+
|
| 171 |
+
// Also delete individual reports from this batch if they exist
|
| 172 |
+
// This is optional - you might want to keep individual reports
|
| 173 |
+
|
| 174 |
+
res.json({ message: `Batch ${batchId} deleted successfully` });
|
| 175 |
+
} catch (error) {
|
| 176 |
+
res.status(404).json({ error: `Batch ${batchId} not found` });
|
| 177 |
+
}
|
| 178 |
+
}
|
api/session.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const sessionManager = require('../lib/session-manager');
|
| 2 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 3 |
+
|
| 4 |
+
module.exports = async (req, res) => {
|
| 5 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'POST, GET, OPTIONS', allowedHeaders: 'Content-Type, Authorization, X-Session-ID' })) {
|
| 6 |
+
return;
|
| 7 |
+
}
|
| 8 |
+
applyCorsHeaders(req, res, { allowedMethods: 'POST, GET, OPTIONS', allowedHeaders: 'Content-Type, Authorization, X-Session-ID' });
|
| 9 |
+
|
| 10 |
+
try {
|
| 11 |
+
const sessionId = req.headers['x-session-id'] || req.query.sessionId || req.body?.sessionId;
|
| 12 |
+
|
| 13 |
+
switch (req.method) {
|
| 14 |
+
case 'POST':
|
| 15 |
+
// Heartbeat - keep session alive
|
| 16 |
+
if (sessionId && sessionManager.heartbeat(sessionId)) {
|
| 17 |
+
res.json({
|
| 18 |
+
success: true,
|
| 19 |
+
sessionId: sessionId,
|
| 20 |
+
message: 'Session refreshed'
|
| 21 |
+
});
|
| 22 |
+
} else {
|
| 23 |
+
// Create new session if doesn't exist
|
| 24 |
+
const newSession = sessionManager.getOrCreateSession(null);
|
| 25 |
+
res.json({
|
| 26 |
+
success: true,
|
| 27 |
+
sessionId: newSession.sessionId,
|
| 28 |
+
message: 'New session created'
|
| 29 |
+
});
|
| 30 |
+
}
|
| 31 |
+
break;
|
| 32 |
+
|
| 33 |
+
case 'GET':
|
| 34 |
+
if (req.query.action === 'stats') {
|
| 35 |
+
// Get session statistics (for debugging)
|
| 36 |
+
const stats = sessionManager.getSessionStats();
|
| 37 |
+
res.json(stats);
|
| 38 |
+
} else if (sessionId) {
|
| 39 |
+
// Get session info
|
| 40 |
+
const session = sessionManager.getOrCreateSession(sessionId);
|
| 41 |
+
res.json({
|
| 42 |
+
sessionId: session.sessionId,
|
| 43 |
+
createdAt: session.createdAt,
|
| 44 |
+
lastActivity: session.lastActivity,
|
| 45 |
+
files: sessionManager.getSessionFiles(sessionId),
|
| 46 |
+
batches: sessionManager.getSessionBatches(sessionId),
|
| 47 |
+
expiresIn: '1 hour from last activity'
|
| 48 |
+
});
|
| 49 |
+
} else {
|
| 50 |
+
res.status(400).json({ error: 'sessionId required' });
|
| 51 |
+
}
|
| 52 |
+
break;
|
| 53 |
+
|
| 54 |
+
default:
|
| 55 |
+
res.status(405).json({ error: 'Method not allowed' });
|
| 56 |
+
}
|
| 57 |
+
} catch (error) {
|
| 58 |
+
console.error('Session API error:', error);
|
| 59 |
+
res.status(500).json({ error: 'Internal server error' });
|
| 60 |
+
}
|
| 61 |
+
};
|
api/upload-document.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const Busboy = require('busboy');
|
| 2 |
+
const JSZip = require('jszip');
|
| 3 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 4 |
+
|
| 5 |
+
let analyzePowerPoint;
|
| 6 |
+
try {
|
| 7 |
+
const pptxAnalyzer = require('../lib/pptx-analyzer');
|
| 8 |
+
analyzePowerPoint = pptxAnalyzer.analyzePowerPoint;
|
| 9 |
+
} catch (err) {
|
| 10 |
+
console.error('Failed to load pptx-analyzer:', err);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
// Helper function to send JSON with proper headers
|
| 14 |
+
function sendJson(res, status, data) {
|
| 15 |
+
res.setHeader('Content-Type', 'application/json');
|
| 16 |
+
res.status(status).end(JSON.stringify(data));
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// Helper function to extract text from paragraph XML - moved to top for availability
|
| 20 |
+
function extractTextFromParagraph(paragraphXml) {
|
| 21 |
+
const textMatches = paragraphXml.match(/<w:t[^>]*>(.*?)<\/w:t>/g);
|
| 22 |
+
if (!textMatches) return '';
|
| 23 |
+
|
| 24 |
+
return textMatches
|
| 25 |
+
.map(t => t.replace(/<w:t[^>]*>|<\/w:t>/g, ''))
|
| 26 |
+
.join('')
|
| 27 |
+
.trim();
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
module.exports = async (req, res) => {
|
| 31 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'POST, OPTIONS' })) {
|
| 32 |
+
return;
|
| 33 |
+
}
|
| 34 |
+
applyCorsHeaders(req, res, { allowedMethods: 'POST, OPTIONS' });
|
| 35 |
+
|
| 36 |
+
if (req.method !== 'POST') {
|
| 37 |
+
sendJson(res, 405, { error: 'Method not allowed' });
|
| 38 |
+
return;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
try {
|
| 42 |
+
const busboy = Busboy({ headers: req.headers });
|
| 43 |
+
let fileData = null;
|
| 44 |
+
let filename = null;
|
| 45 |
+
|
| 46 |
+
busboy.on('file', (fieldname, file, info) => {
|
| 47 |
+
filename = info.filename;
|
| 48 |
+
const chunks = [];
|
| 49 |
+
|
| 50 |
+
file.on('data', (chunk) => {
|
| 51 |
+
chunks.push(chunk);
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
file.on('end', () => {
|
| 55 |
+
fileData = Buffer.concat(chunks);
|
| 56 |
+
});
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
busboy.on('finish', async () => {
|
| 60 |
+
if (!fileData || !filename) {
|
| 61 |
+
sendJson(res, 400, { error: 'No file uploaded' });
|
| 62 |
+
return;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
const filenameLower = filename.toLowerCase();
|
| 66 |
+
|
| 67 |
+
// Support both PowerPoint and Word documents
|
| 68 |
+
const isPowerPoint = ['.pptx', '.ppt', '.pps', '.pot', '.potx', '.ppsx'].some(ext => filenameLower.endsWith(ext));
|
| 69 |
+
const isWord = filenameLower.endsWith('.docx');
|
| 70 |
+
|
| 71 |
+
if (!isPowerPoint && !isWord) {
|
| 72 |
+
sendJson(res, 400, { error: 'Please upload a PowerPoint or Word document (.docx, .pptx)' });
|
| 73 |
+
return;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
try {
|
| 77 |
+
let report;
|
| 78 |
+
if (isPowerPoint) {
|
| 79 |
+
// Route PowerPoint files to the PowerPoint analyzer
|
| 80 |
+
if (!analyzePowerPoint) {
|
| 81 |
+
throw new Error('PowerPoint analyzer not available');
|
| 82 |
+
}
|
| 83 |
+
report = await analyzePowerPoint(fileData, filename);
|
| 84 |
+
} else {
|
| 85 |
+
// Route Word documents to the Word analyzer
|
| 86 |
+
report = await analyzeDocx(fileData, filename);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
sendJson(res, 200, {
|
| 90 |
+
fileName: filename,
|
| 91 |
+
suggestedFileName: filename,
|
| 92 |
+
report: report
|
| 93 |
+
});
|
| 94 |
+
} catch (error) {
|
| 95 |
+
console.error('Analysis error:', error);
|
| 96 |
+
sendJson(res, 500, { error: error.message });
|
| 97 |
+
}
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
req.pipe(busboy);
|
| 101 |
+
|
| 102 |
+
} catch (error) {
|
| 103 |
+
console.error('Upload error:', error);
|
| 104 |
+
sendJson(res, 500, { error: error.message });
|
| 105 |
+
}
|
| 106 |
+
};
|
| 107 |
+
module.exports.analyzeDocx = analyzeDocx;
|
| 108 |
+
async function analyzeDocx(fileData, filename) {
|
| 109 |
+
const report = {
|
| 110 |
+
fileName: filename,
|
| 111 |
+
suggestedFileName: filename,
|
| 112 |
+
summary: { fixed: 0, flagged: 0 },
|
| 113 |
+
details: {
|
| 114 |
+
// Requirement 1: Lists are formatted correctly
|
| 115 |
+
hyphenatedParagraphsNeedingLists: [],
|
| 116 |
+
formattedListsCount: 0,
|
| 117 |
+
|
| 118 |
+
// Requirement 2: Images have alt text (max 250 chars)
|
| 119 |
+
imagesMissingAltText: [],
|
| 120 |
+
imagesWithAltTextOver250Chars: [],
|
| 121 |
+
imagesWithValidAltText: 0,
|
| 122 |
+
}
|
| 123 |
+
};
|
| 124 |
+
|
| 125 |
+
try {
|
| 126 |
+
const zip = await JSZip.loadAsync(fileData);
|
| 127 |
+
|
| 128 |
+
// Read core documents needed for the two requirements
|
| 129 |
+
const documentXml = await zip.file('word/document.xml')?.async('string');
|
| 130 |
+
const relsXml = await zip.file('word/_rels/document.xml.rels')?.async('string');
|
| 131 |
+
|
| 132 |
+
// ===== REQUIREMENT 1: Check for lists formatted correctly =====
|
| 133 |
+
if (documentXml) {
|
| 134 |
+
const listIssues = analyzeListFormatting(documentXml);
|
| 135 |
+
if (listIssues.hyphenatedParagraphs.length > 0) {
|
| 136 |
+
report.details.hyphenatedParagraphsNeedingLists = listIssues.hyphenatedParagraphs;
|
| 137 |
+
report.summary.flagged += listIssues.hyphenatedParagraphs.length;
|
| 138 |
+
}
|
| 139 |
+
report.details.formattedListsCount = listIssues.properlyFormattedLists;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// ===== REQUIREMENT 2: Check for images with alt text =====
|
| 143 |
+
if (relsXml && documentXml) {
|
| 144 |
+
const imageAnalysis = analyzeImageAltText(documentXml, relsXml);
|
| 145 |
+
|
| 146 |
+
if (imageAnalysis.missingAltText.length > 0) {
|
| 147 |
+
report.details.imagesMissingAltText = imageAnalysis.missingAltText;
|
| 148 |
+
report.summary.flagged += imageAnalysis.missingAltText.length;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
if (imageAnalysis.altTextOver250Chars.length > 0) {
|
| 152 |
+
report.details.imagesWithAltTextOver250Chars = imageAnalysis.altTextOver250Chars;
|
| 153 |
+
report.summary.flagged += imageAnalysis.altTextOver250Chars.length;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
report.details.imagesWithValidAltText = imageAnalysis.validAltTextCount;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
return report;
|
| 160 |
+
|
| 161 |
+
} catch (error) {
|
| 162 |
+
console.error('[analyzeDocx] Error analyzing document:', error);
|
| 163 |
+
return {
|
| 164 |
+
fileName: filename,
|
| 165 |
+
error: error.message,
|
| 166 |
+
summary: { fixed: 0, flagged: 0 },
|
| 167 |
+
details: {}
|
| 168 |
+
};
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
// ===== HELPER FUNCTIONS =====
|
| 173 |
+
|
| 174 |
+
/**
|
| 175 |
+
* Analyze list formatting in the document
|
| 176 |
+
* Detects hyphenated paragraphs that should be formatted as lists
|
| 177 |
+
*/
|
| 178 |
+
function analyzeListFormatting(documentXml) {
|
| 179 |
+
const results = {
|
| 180 |
+
hyphenatedParagraphs: [],
|
| 181 |
+
properlyFormattedLists: 0
|
| 182 |
+
};
|
| 183 |
+
|
| 184 |
+
if (!documentXml) return results;
|
| 185 |
+
|
| 186 |
+
// Extract all paragraphs
|
| 187 |
+
const paragraphMatches = documentXml.match(/<w:p[^>]*>([\s\S]*?)<\/w:p>/g) || [];
|
| 188 |
+
|
| 189 |
+
paragraphMatches.forEach((paragraph, index) => {
|
| 190 |
+
// Extract text content from paragraph
|
| 191 |
+
const textMatches = paragraph.match(/<w:t[^>]*>(.*?)<\/w:t>/g) || [];
|
| 192 |
+
const text = textMatches
|
| 193 |
+
.map(t => t.replace(/<w:t[^>]*>|<\/w:t>/g, ''))
|
| 194 |
+
.join('')
|
| 195 |
+
.trim();
|
| 196 |
+
|
| 197 |
+
// Check if paragraph starts with hyphen/dash (indicates list formatting issue)
|
| 198 |
+
if (text && /^[-–—]\s+/.test(text)) {
|
| 199 |
+
results.hyphenatedParagraphs.push({
|
| 200 |
+
index: index + 1,
|
| 201 |
+
text: text.substring(0, 100), // First 100 chars
|
| 202 |
+
message: 'This paragraph appears to be a list item but is formatted as a regular paragraph'
|
| 203 |
+
});
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
// Count properly formatted lists (pPr contains pStyle with list references)
|
| 207 |
+
if (paragraph.includes('pStyle w:val="ListParagraph"') || paragraph.includes('numPr')) {
|
| 208 |
+
results.properlyFormattedLists++;
|
| 209 |
+
}
|
| 210 |
+
});
|
| 211 |
+
|
| 212 |
+
return results;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
/**
|
| 216 |
+
* Analyze image alt text requirements
|
| 217 |
+
* Checks for missing alt text and validates length
|
| 218 |
+
*/
|
| 219 |
+
function analyzeImageAltText(documentXml, relsXml) {
|
| 220 |
+
const results = {
|
| 221 |
+
missingAltText: [],
|
| 222 |
+
altTextOver250Chars: [],
|
| 223 |
+
validAltTextCount: 0
|
| 224 |
+
};
|
| 225 |
+
|
| 226 |
+
if (!documentXml || !relsXml) return results;
|
| 227 |
+
|
| 228 |
+
// Find all images/drawings
|
| 229 |
+
const drawingMatches = documentXml.match(/<wp:inline[^>]*>[\s\S]*?<\/wp:inline>|<wp:anchor[^>]*>[\s\S]*?<\/wp:anchor>/g) || [];
|
| 230 |
+
|
| 231 |
+
drawingMatches.forEach((drawing, index) => {
|
| 232 |
+
// Extract relationship ID to find the image file
|
| 233 |
+
const rIdMatch = drawing.match(/r:embed="(rId\d+)"/);
|
| 234 |
+
if (!rIdMatch) return;
|
| 235 |
+
|
| 236 |
+
const rId = rIdMatch[1];
|
| 237 |
+
|
| 238 |
+
// Extract alternate text (docProperties)
|
| 239 |
+
const altTextMatch = drawing.match(/<wp:docPr[^>]*descr="([^"]*)"/) || drawing.match(/<wp:cNvPicPr[^>]*>[\s\S]*?<a:picLocks[^>]*descr="([^"]*)"/);
|
| 240 |
+
const altText = altTextMatch ? altTextMatch[1] : null;
|
| 241 |
+
|
| 242 |
+
// Also check for extent/alt description in other formats
|
| 243 |
+
const titleMatch = drawing.match(/<wp:docPr[^>]*name="([^"]*)"[^>]*title="([^"]*)"/) || drawing.match(/<wp:docPr[^>]*title="([^"]*)"[^>]*name="([^"]*)"/);
|
| 244 |
+
|
| 245 |
+
// Check if this image has proper alt text
|
| 246 |
+
if (!altText || altText.trim() === '') {
|
| 247 |
+
results.missingAltText.push({
|
| 248 |
+
index: index + 1,
|
| 249 |
+
rId: rId,
|
| 250 |
+
message: 'Image is missing alt text description'
|
| 251 |
+
});
|
| 252 |
+
} else if (altText.length > 250) {
|
| 253 |
+
results.altTextOver250Chars.push({
|
| 254 |
+
index: index + 1,
|
| 255 |
+
rId: rId,
|
| 256 |
+
altText: altText.substring(0, 100) + '...',
|
| 257 |
+
length: altText.length,
|
| 258 |
+
message: `Alt text is ${altText.length} characters (max 250)`
|
| 259 |
+
});
|
| 260 |
+
} else {
|
| 261 |
+
// Valid alt text
|
| 262 |
+
results.validAltTextCount++;
|
| 263 |
+
}
|
| 264 |
+
});
|
| 265 |
+
|
| 266 |
+
return results;
|
| 267 |
+
}
|
| 268 |
+
|
api/upload-powerpoint.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const Busboy = require('busboy');
|
| 2 |
+
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
|
| 3 |
+
|
| 4 |
+
let analyzePowerPoint;
|
| 5 |
+
try {
|
| 6 |
+
const pptxAnalyzer = require('../lib/pptx-analyzer');
|
| 7 |
+
analyzePowerPoint = pptxAnalyzer.analyzePowerPoint;
|
| 8 |
+
} catch (err) {
|
| 9 |
+
console.error('Failed to load pptx-analyzer:', err);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
// Helper function to send JSON with proper headers
|
| 13 |
+
function sendJson(res, status, data) {
|
| 14 |
+
res.setHeader('Content-Type', 'application/json');
|
| 15 |
+
res.status(status).end(JSON.stringify(data));
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
module.exports = async (req, res) => {
|
| 19 |
+
if (handleCorsPreflight(req, res, { allowedMethods: 'POST, OPTIONS' })) {
|
| 20 |
+
return;
|
| 21 |
+
}
|
| 22 |
+
applyCorsHeaders(req, res, { allowedMethods: 'POST, OPTIONS' });
|
| 23 |
+
|
| 24 |
+
if (req.method !== 'POST') {
|
| 25 |
+
sendJson(res, 405, { error: 'Method not allowed' });
|
| 26 |
+
return;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
try {
|
| 30 |
+
const busboy = Busboy({ headers: req.headers });
|
| 31 |
+
let fileData = null;
|
| 32 |
+
let filename = null;
|
| 33 |
+
|
| 34 |
+
busboy.on('file', (fieldname, file, info) => {
|
| 35 |
+
filename = info.filename;
|
| 36 |
+
const chunks = [];
|
| 37 |
+
|
| 38 |
+
file.on('data', (chunk) => {
|
| 39 |
+
chunks.push(chunk);
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
file.on('end', () => {
|
| 43 |
+
fileData = Buffer.concat(chunks);
|
| 44 |
+
});
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
busboy.on('finish', async () => {
|
| 48 |
+
if (!fileData || !filename) {
|
| 49 |
+
sendJson(res, 400, { error: 'No file uploaded' });
|
| 50 |
+
return;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// Validate PowerPoint file types
|
| 54 |
+
const validExtensions = ['.pptx', '.ppt', '.pps', '.potx'];
|
| 55 |
+
const isValid = validExtensions.some(ext => filename.toLowerCase().endsWith(ext));
|
| 56 |
+
|
| 57 |
+
if (!isValid) {
|
| 58 |
+
sendJson(res, 400, { error: 'Please upload a PowerPoint file (.pptx, .ppt, .pps, or .potx)' });
|
| 59 |
+
return;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
try {
|
| 63 |
+
if (!analyzePowerPoint) {
|
| 64 |
+
throw new Error('PowerPoint analyzer not available');
|
| 65 |
+
}
|
| 66 |
+
const report = await analyzePowerPoint(fileData, filename);
|
| 67 |
+
sendJson(res, 200, {
|
| 68 |
+
fileName: filename,
|
| 69 |
+
suggestedFileName: filename,
|
| 70 |
+
report: report
|
| 71 |
+
});
|
| 72 |
+
} catch (error) {
|
| 73 |
+
console.error('PowerPoint analysis error:', error);
|
| 74 |
+
sendJson(res, 500, { error: error.message });
|
| 75 |
+
}
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
req.pipe(busboy);
|
| 79 |
+
|
| 80 |
+
} catch (error) {
|
| 81 |
+
console.error('Upload error:', error);
|
| 82 |
+
sendJson(res, 500, { error: error.message });
|
| 83 |
+
}
|
| 84 |
+
};
|
check-shadows.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs');
|
| 2 |
+
const JSZip = require('jszip');
|
| 3 |
+
|
| 4 |
+
async function checkDocumentForShadows(filePath) {
|
| 5 |
+
console.log(`\n=== Checking ${filePath} for Shadows ===`);
|
| 6 |
+
|
| 7 |
+
if (!fs.existsSync(filePath)) {
|
| 8 |
+
console.log('❌ File not found:', filePath);
|
| 9 |
+
return false;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
try {
|
| 13 |
+
const buffer = fs.readFileSync(filePath);
|
| 14 |
+
const zip = new JSZip();
|
| 15 |
+
await zip.loadAsync(buffer);
|
| 16 |
+
|
| 17 |
+
let totalShadows = 0;
|
| 18 |
+
const shadowDetails = [];
|
| 19 |
+
|
| 20 |
+
// Check main XML files
|
| 21 |
+
const xmlFiles = [
|
| 22 |
+
'word/document.xml',
|
| 23 |
+
'word/styles.xml',
|
| 24 |
+
'word/numbering.xml',
|
| 25 |
+
'word/settings.xml'
|
| 26 |
+
];
|
| 27 |
+
|
| 28 |
+
for (const fileName of xmlFiles) {
|
| 29 |
+
const file = zip.file(fileName);
|
| 30 |
+
if (file) {
|
| 31 |
+
const xmlContent = await file.async('string');
|
| 32 |
+
|
| 33 |
+
// Find all shadow-related elements
|
| 34 |
+
const shadowPatterns = [
|
| 35 |
+
/<w:shadow[^>]*>/gi,
|
| 36 |
+
/<w14:shadow[^>]*>/gi,
|
| 37 |
+
/<a:shadow[^>]*>/gi,
|
| 38 |
+
/shadow\w*\s*=\s*"[^"]*"/gi,
|
| 39 |
+
];
|
| 40 |
+
|
| 41 |
+
let fileShadows = 0;
|
| 42 |
+
const fileDetails = [];
|
| 43 |
+
|
| 44 |
+
shadowPatterns.forEach(pattern => {
|
| 45 |
+
const matches = xmlContent.match(pattern) || [];
|
| 46 |
+
if (matches.length > 0) {
|
| 47 |
+
fileShadows += matches.length;
|
| 48 |
+
fileDetails.push({
|
| 49 |
+
pattern: pattern.toString(),
|
| 50 |
+
count: matches.length,
|
| 51 |
+
samples: matches.slice(0, 3)
|
| 52 |
+
});
|
| 53 |
+
}
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
if (fileShadows > 0) {
|
| 57 |
+
totalShadows += fileShadows;
|
| 58 |
+
shadowDetails.push({
|
| 59 |
+
file: fileName,
|
| 60 |
+
count: fileShadows,
|
| 61 |
+
details: fileDetails
|
| 62 |
+
});
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Report results
|
| 68 |
+
if (totalShadows === 0) {
|
| 69 |
+
console.log('✅ NO SHADOWS FOUND - Document is clean!');
|
| 70 |
+
return true;
|
| 71 |
+
} else {
|
| 72 |
+
console.log(`❌ ${totalShadows} SHADOW ELEMENTS FOUND:`);
|
| 73 |
+
shadowDetails.forEach(fileInfo => {
|
| 74 |
+
console.log(`\n 📄 ${fileInfo.file}: ${fileInfo.count} shadows`);
|
| 75 |
+
fileInfo.details.forEach(detail => {
|
| 76 |
+
console.log(` Pattern: ${detail.pattern}`);
|
| 77 |
+
console.log(` Count: ${detail.count}`);
|
| 78 |
+
detail.samples.forEach(sample => {
|
| 79 |
+
console.log(` Sample: "${sample}"`);
|
| 80 |
+
});
|
| 81 |
+
});
|
| 82 |
+
});
|
| 83 |
+
return false;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
} catch (error) {
|
| 87 |
+
console.log('❌ Error reading file:', error.message);
|
| 88 |
+
return false;
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
async function main() {
|
| 93 |
+
console.log('Shadow Detection Utility');
|
| 94 |
+
console.log('========================');
|
| 95 |
+
|
| 96 |
+
// Check our test files
|
| 97 |
+
const filesToCheck = [
|
| 98 |
+
'tests/fixtures/test_problematic.docx',
|
| 99 |
+
'tests/fixtures/test_remediated.docx',
|
| 100 |
+
'tests/fixtures/test_fully_remediated.docx'
|
| 101 |
+
];
|
| 102 |
+
|
| 103 |
+
for (const file of filesToCheck) {
|
| 104 |
+
await checkDocumentForShadows(file);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
console.log('\n📋 SUMMARY:');
|
| 108 |
+
console.log('- test_problematic.docx: Original file with intentional shadows');
|
| 109 |
+
console.log('- test_remediated.docx: Processed with Node.js remediation function');
|
| 110 |
+
console.log('- test_fully_remediated.docx: Processed with enhanced removal');
|
| 111 |
+
console.log('\n💡 TO TEST YOUR OWN FILE:');
|
| 112 |
+
console.log('Copy your DOCX file to this directory and modify the filesToCheck array above.');
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
main();
|
debug-detection.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs');
|
| 2 |
+
const JSZip = require('jszip');
|
| 3 |
+
|
| 4 |
+
async function debugDetection() {
|
| 5 |
+
console.log('=== Debugging Detection Issues ===\n');
|
| 6 |
+
|
| 7 |
+
// Test with an actual document
|
| 8 |
+
const testFile = 'reports/Protected_remediated_by_agent.docx';
|
| 9 |
+
|
| 10 |
+
if (!fs.existsSync(testFile)) {
|
| 11 |
+
console.log('Test file not found, trying other files...');
|
| 12 |
+
const reports = fs.readdirSync('reports');
|
| 13 |
+
const docxFiles = reports.filter(f => f.endsWith('.docx'));
|
| 14 |
+
if (docxFiles.length === 0) {
|
| 15 |
+
console.log('No .docx files found in reports folder');
|
| 16 |
+
return;
|
| 17 |
+
}
|
| 18 |
+
console.log(`Using ${docxFiles[0]} instead`);
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
try {
|
| 22 |
+
const fileData = fs.readFileSync(testFile);
|
| 23 |
+
const zip = await JSZip.loadAsync(fileData);
|
| 24 |
+
|
| 25 |
+
console.log('1. CHECKING DOCUMENT.XML');
|
| 26 |
+
const documentXml = await zip.file('word/document.xml')?.async('string');
|
| 27 |
+
if (documentXml) {
|
| 28 |
+
console.log(`Document XML length: ${documentXml.length}`);
|
| 29 |
+
|
| 30 |
+
// Check for shadows
|
| 31 |
+
const shadowTests = [
|
| 32 |
+
/<w:shadow\s*\/>/,
|
| 33 |
+
/<w:shadow[^>]*>/,
|
| 34 |
+
/<a:outerShdw[^>]*>/,
|
| 35 |
+
/<w14:shadow[^>]*>/
|
| 36 |
+
];
|
| 37 |
+
|
| 38 |
+
console.log('\nShadow detection:');
|
| 39 |
+
shadowTests.forEach((regex, i) => {
|
| 40 |
+
const matches = documentXml.match(regex);
|
| 41 |
+
console.log(` Test ${i+1}: ${matches ? matches.length + ' matches' : 'no matches'}`);
|
| 42 |
+
if (matches) console.log(` First match: ${matches[0].slice(0, 100)}`);
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
// Check for serif fonts
|
| 46 |
+
console.log('\nFont detection:');
|
| 47 |
+
const serifMatches = documentXml.match(/(Times|Georgia|Garamond|serif)/gi);
|
| 48 |
+
console.log(` Serif fonts: ${serifMatches ? serifMatches.length + ' matches' : 'none found'}`);
|
| 49 |
+
if (serifMatches) console.log(` Found: ${[...new Set(serifMatches)].join(', ')}`);
|
| 50 |
+
|
| 51 |
+
// Check font declarations
|
| 52 |
+
const fontMatches = documentXml.match(/w:ascii="[^"]*"/g);
|
| 53 |
+
if (fontMatches) {
|
| 54 |
+
console.log(` Font declarations: ${fontMatches.length}`);
|
| 55 |
+
const uniqueFonts = [...new Set(fontMatches.map(m => m.match(/w:ascii="([^"]*)"/)[1]))];
|
| 56 |
+
console.log(` Fonts found: ${uniqueFonts.join(', ')}`);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Check for small font sizes
|
| 60 |
+
console.log('\nFont size detection:');
|
| 61 |
+
const sizeMatches = documentXml.match(/<w:sz w:val="(\d+)"/g);
|
| 62 |
+
if (sizeMatches) {
|
| 63 |
+
console.log(` Size declarations: ${sizeMatches.length}`);
|
| 64 |
+
const sizes = sizeMatches.map(m => parseInt(m.match(/w:val="(\d+)"/)[1]));
|
| 65 |
+
const smallSizes = sizes.filter(s => s < 22);
|
| 66 |
+
console.log(` Sizes found: ${[...new Set(sizes)].sort((a,b) => a-b).join(', ')}`);
|
| 67 |
+
console.log(` Small sizes (< 22): ${smallSizes.length > 0 ? smallSizes.join(', ') : 'none'}`);
|
| 68 |
+
} else {
|
| 69 |
+
console.log(' No size declarations found');
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// Check line spacing
|
| 73 |
+
console.log('\nLine spacing detection:');
|
| 74 |
+
const spacingMatches = documentXml.match(/<w:spacing[^>]*w:line="(\d+)"[^>]*\/>/g);
|
| 75 |
+
if (spacingMatches) {
|
| 76 |
+
console.log(` Spacing declarations: ${spacingMatches.length}`);
|
| 77 |
+
spacingMatches.forEach(match => {
|
| 78 |
+
const lineValue = parseInt(match.match(/w:line="(\d+)"/)[1]);
|
| 79 |
+
console.log(` ${match} -> ${lineValue} ${lineValue < 360 ? '(NEEDS FIX)' : '(OK)'}`);
|
| 80 |
+
});
|
| 81 |
+
} else {
|
| 82 |
+
console.log(' No explicit spacing declarations found');
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// Check for exact spacing
|
| 86 |
+
if (documentXml.includes('w:lineRule="exact"')) {
|
| 87 |
+
console.log(' Found exact line spacing rule (NEEDS FIX)');
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// Check for paragraphs without spacing
|
| 91 |
+
const totalParas = (documentXml.match(/<w:p[^>]*>/g) || []).length;
|
| 92 |
+
const parasWithSpacing = (documentXml.match(/<w:p[^>]*>.*?<w:pPr[^>]*>.*?<w:spacing/gs) || []).length;
|
| 93 |
+
console.log(` Total paragraphs: ${totalParas}`);
|
| 94 |
+
console.log(` Paragraphs with spacing: ${parasWithSpacing}`);
|
| 95 |
+
console.log(` Paragraphs without spacing: ${totalParas - parasWithSpacing} ${totalParas - parasWithSpacing > 0 ? '(NEEDS FIX)' : '(OK)'}`);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
console.log('\n2. CHECKING STYLES.XML');
|
| 99 |
+
const stylesXml = await zip.file('word/styles.xml')?.async('string');
|
| 100 |
+
if (stylesXml) {
|
| 101 |
+
console.log(`Styles XML length: ${stylesXml.length}`);
|
| 102 |
+
|
| 103 |
+
// Quick checks for styles
|
| 104 |
+
const styleSerifMatches = stylesXml.match(/(Times|Georgia|Garamond|serif)/gi);
|
| 105 |
+
console.log(`Serif fonts in styles: ${styleSerifMatches ? styleSerifMatches.length : 0}`);
|
| 106 |
+
|
| 107 |
+
const styleSizeMatches = stylesXml.match(/<w:sz w:val="(\d+)"/g);
|
| 108 |
+
if (styleSizeMatches) {
|
| 109 |
+
const sizes = styleSizeMatches.map(m => parseInt(m.match(/w:val="(\d+)"/)[1]));
|
| 110 |
+
const smallSizes = sizes.filter(s => s < 22);
|
| 111 |
+
console.log(`Small font sizes in styles: ${smallSizes.length > 0 ? smallSizes.join(', ') : 'none'}`);
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
} catch (error) {
|
| 116 |
+
console.error('Debug failed:', error.message);
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
debugDetection();
|
docs/batch-processing.html
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Batch Document Processing</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
|
| 9 |
+
.upload-area { border: 2px dashed #ccc; padding: 40px; text-align: center; margin: 20px 0; }
|
| 10 |
+
.upload-area.dragover { border-color: #007cba; background-color: #f0f8ff; }
|
| 11 |
+
.file-list { margin: 20px 0; }
|
| 12 |
+
.file-item { padding: 10px; border: 1px solid #ddd; margin: 5px 0; display: flex; justify-content: space-between; align-items: center; }
|
| 13 |
+
.file-item.processing { background-color: #fff3cd; }
|
| 14 |
+
.file-item.success { background-color: #d4edda; }
|
| 15 |
+
.file-item.error { background-color: #f8d7da; }
|
| 16 |
+
.progress-bar { width: 100%; height: 20px; background-color: #f0f0f0; border-radius: 10px; overflow: hidden; margin: 10px 0; }
|
| 17 |
+
.progress-fill { height: 100%; background-color: #007cba; transition: width 0.3s ease; }
|
| 18 |
+
.results { margin: 20px 0; }
|
| 19 |
+
.batch-history { margin: 30px 0; }
|
| 20 |
+
.batch-item { padding: 15px; border: 1px solid #ddd; margin: 10px 0; border-radius: 5px; }
|
| 21 |
+
button { padding: 10px 20px; margin: 5px; cursor: pointer; }
|
| 22 |
+
.btn-primary { background-color: #007cba; color: white; border: none; }
|
| 23 |
+
.btn-secondary { background-color: #6c757d; color: white; border: none; }
|
| 24 |
+
.btn-danger { background-color: #dc3545; color: white; border: none; }
|
| 25 |
+
</style>
|
| 26 |
+
</head>
|
| 27 |
+
<body>
|
| 28 |
+
<h1>Accessibility Checker - Batch Processing</h1>
|
| 29 |
+
|
| 30 |
+
<div class="upload-section">
|
| 31 |
+
<h2>Upload Multiple Documents</h2>
|
| 32 |
+
<div id="uploadArea" class="upload-area">
|
| 33 |
+
<p>Drop up to 10 DOCX files here, or click to select</p>
|
| 34 |
+
<input type="file" id="fileInput" multiple accept=".docx" style="display: none;">
|
| 35 |
+
<button onclick="document.getElementById('fileInput').click()" class="btn-primary">Select Files</button>
|
| 36 |
+
</div>
|
| 37 |
+
|
| 38 |
+
<div id="fileList" class="file-list"></div>
|
| 39 |
+
|
| 40 |
+
<div id="progressSection" style="display: none;">
|
| 41 |
+
<h3>Processing Files...</h3>
|
| 42 |
+
<div class="progress-bar">
|
| 43 |
+
<div id="progressFill" class="progress-fill" style="width: 0%;"></div>
|
| 44 |
+
</div>
|
| 45 |
+
<div id="progressText">Preparing upload...</div>
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
+
<button id="uploadBtn" onclick="uploadFiles()" class="btn-primary" style="display: none;">
|
| 49 |
+
Upload and Process Files
|
| 50 |
+
</button>
|
| 51 |
+
</div>
|
| 52 |
+
|
| 53 |
+
<div id="results" class="results" style="display: none;">
|
| 54 |
+
<h2>Processing Results</h2>
|
| 55 |
+
<div id="resultsContent"></div>
|
| 56 |
+
</div>
|
| 57 |
+
|
| 58 |
+
<div class="batch-history">
|
| 59 |
+
<h2>Previous Batches</h2>
|
| 60 |
+
<button onclick="loadBatchHistory()" class="btn-secondary">Load Batch History</button>
|
| 61 |
+
<div id="batchHistory"></div>
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
<script>
|
| 65 |
+
let selectedFiles = [];
|
| 66 |
+
let sessionId = null;
|
| 67 |
+
const API_BASE = window.location.origin; // Adjust as needed
|
| 68 |
+
|
| 69 |
+
// Session management
|
| 70 |
+
async function initializeSession() {
|
| 71 |
+
try {
|
| 72 |
+
const response = await fetch(`${API_BASE}/api/session`, {
|
| 73 |
+
method: 'POST',
|
| 74 |
+
headers: { 'Content-Type': 'application/json' }
|
| 75 |
+
});
|
| 76 |
+
const data = await response.json();
|
| 77 |
+
sessionId = data.sessionId;
|
| 78 |
+
console.log('Session initialized:', sessionId);
|
| 79 |
+
|
| 80 |
+
// Start heartbeat to keep session alive
|
| 81 |
+
startHeartbeat();
|
| 82 |
+
|
| 83 |
+
// Load existing session data
|
| 84 |
+
loadSessionData();
|
| 85 |
+
|
| 86 |
+
} catch (error) {
|
| 87 |
+
console.error('Failed to initialize session:', error);
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
function startHeartbeat() {
|
| 92 |
+
// Send heartbeat every 5 minutes
|
| 93 |
+
setInterval(async () => {
|
| 94 |
+
if (sessionId) {
|
| 95 |
+
try {
|
| 96 |
+
await fetch(`${API_BASE}/api/session`, {
|
| 97 |
+
method: 'POST',
|
| 98 |
+
headers: {
|
| 99 |
+
'Content-Type': 'application/json',
|
| 100 |
+
'X-Session-ID': sessionId
|
| 101 |
+
}
|
| 102 |
+
});
|
| 103 |
+
} catch (error) {
|
| 104 |
+
console.warn('Heartbeat failed:', error);
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
}, 5 * 60 * 1000); // 5 minutes
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
async function loadSessionData() {
|
| 111 |
+
if (!sessionId) return;
|
| 112 |
+
|
| 113 |
+
try {
|
| 114 |
+
const response = await fetch(`${API_BASE}/api/session?sessionId=${sessionId}`);
|
| 115 |
+
const sessionData = await response.json();
|
| 116 |
+
|
| 117 |
+
// Display existing batches from this session
|
| 118 |
+
displaySessionHistory(sessionData);
|
| 119 |
+
|
| 120 |
+
} catch (error) {
|
| 121 |
+
console.warn('Failed to load session data:', error);
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
function displaySessionHistory(sessionData) {
|
| 126 |
+
const historyDiv = document.getElementById('batchHistory');
|
| 127 |
+
|
| 128 |
+
if (sessionData.batches.length === 0) {
|
| 129 |
+
historyDiv.innerHTML = '<p>No batches in this session yet.</p>';
|
| 130 |
+
return;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
historyDiv.innerHTML = '<h3>This Session:</h3>' +
|
| 134 |
+
sessionData.batches.map(batch => `
|
| 135 |
+
<div class="batch-item">
|
| 136 |
+
<h4>Batch ${batch.batchId}</h4>
|
| 137 |
+
<p><strong>Files:</strong> ${batch.totalFiles} (${batch.successful} successful, ${batch.failed} failed)</p>
|
| 138 |
+
<p><strong>Processed:</strong> ${new Date(batch.timestamp).toLocaleString()}</p>
|
| 139 |
+
<button onclick="downloadBatch('${batch.batchId}')" class="btn-primary">Download</button>
|
| 140 |
+
</div>
|
| 141 |
+
`).join('');
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// Cleanup on page unload
|
| 145 |
+
window.addEventListener('beforeunload', () => {
|
| 146 |
+
// Note: Session will auto-expire after 1 hour of inactivity
|
| 147 |
+
// No need to manually cleanup as the server handles it
|
| 148 |
+
});
|
| 149 |
+
|
| 150 |
+
// File selection and drag/drop
|
| 151 |
+
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
|
| 152 |
+
|
| 153 |
+
const uploadArea = document.getElementById('uploadArea');
|
| 154 |
+
uploadArea.addEventListener('dragover', (e) => {
|
| 155 |
+
e.preventDefault();
|
| 156 |
+
uploadArea.classList.add('dragover');
|
| 157 |
+
});
|
| 158 |
+
|
| 159 |
+
uploadArea.addEventListener('dragleave', () => {
|
| 160 |
+
uploadArea.classList.remove('dragover');
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
uploadArea.addEventListener('drop', (e) => {
|
| 164 |
+
e.preventDefault();
|
| 165 |
+
uploadArea.classList.remove('dragover');
|
| 166 |
+
const files = Array.from(e.dataTransfer.files).filter(f => f.name.endsWith('.docx'));
|
| 167 |
+
handleFiles(files);
|
| 168 |
+
});
|
| 169 |
+
|
| 170 |
+
function handleFileSelect(e) {
|
| 171 |
+
const files = Array.from(e.target.files);
|
| 172 |
+
handleFiles(files);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
function handleFiles(files) {
|
| 176 |
+
selectedFiles = files.slice(0, 10); // Limit to 10 files
|
| 177 |
+
displayFileList();
|
| 178 |
+
document.getElementById('uploadBtn').style.display = selectedFiles.length > 0 ? 'block' : 'none';
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
function displayFileList() {
|
| 182 |
+
const fileList = document.getElementById('fileList');
|
| 183 |
+
if (selectedFiles.length === 0) {
|
| 184 |
+
fileList.innerHTML = '';
|
| 185 |
+
return;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
fileList.innerHTML = `<h3>Selected Files (${selectedFiles.length}):</h3>`;
|
| 189 |
+
selectedFiles.forEach((file, index) => {
|
| 190 |
+
const fileItem = document.createElement('div');
|
| 191 |
+
fileItem.className = 'file-item';
|
| 192 |
+
fileItem.innerHTML = `
|
| 193 |
+
<div>
|
| 194 |
+
<strong>${file.name}</strong>
|
| 195 |
+
<br><small>${(file.size / 1024).toFixed(1)} KB</small>
|
| 196 |
+
</div>
|
| 197 |
+
<button onclick="removeFile(${index})" class="btn-danger">Remove</button>
|
| 198 |
+
`;
|
| 199 |
+
fileList.appendChild(fileItem);
|
| 200 |
+
});
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
function removeFile(index) {
|
| 204 |
+
selectedFiles.splice(index, 1);
|
| 205 |
+
displayFileList();
|
| 206 |
+
document.getElementById('uploadBtn').style.display = selectedFiles.length > 0 ? 'block' : 'none';
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
async function uploadFiles() {
|
| 210 |
+
if (selectedFiles.length === 0 || !sessionId) return;
|
| 211 |
+
|
| 212 |
+
document.getElementById('progressSection').style.display = 'block';
|
| 213 |
+
document.getElementById('uploadBtn').disabled = true;
|
| 214 |
+
|
| 215 |
+
const formData = new FormData();
|
| 216 |
+
selectedFiles.forEach((file, index) => {
|
| 217 |
+
formData.append(`file${index}`, file);
|
| 218 |
+
});
|
| 219 |
+
|
| 220 |
+
try {
|
| 221 |
+
updateProgress(10, 'Uploading files...');
|
| 222 |
+
|
| 223 |
+
const response = await fetch(`${API_BASE}/api/batch-upload`, {
|
| 224 |
+
method: 'POST',
|
| 225 |
+
headers: {
|
| 226 |
+
'X-Session-ID': sessionId
|
| 227 |
+
},
|
| 228 |
+
body: formData
|
| 229 |
+
});
|
| 230 |
+
|
| 231 |
+
updateProgress(90, 'Processing files...');
|
| 232 |
+
|
| 233 |
+
if (!response.ok) {
|
| 234 |
+
throw new Error(`Upload failed: ${response.statusText}`);
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
const result = await response.json();
|
| 238 |
+
updateProgress(100, 'Complete!');
|
| 239 |
+
|
| 240 |
+
displayResults(result);
|
| 241 |
+
|
| 242 |
+
// Refresh session data to show new batch
|
| 243 |
+
loadSessionData();
|
| 244 |
+
|
| 245 |
+
// Clear selection
|
| 246 |
+
selectedFiles = [];
|
| 247 |
+
displayFileList();
|
| 248 |
+
document.getElementById('uploadBtn').style.display = 'none';
|
| 249 |
+
|
| 250 |
+
} catch (error) {
|
| 251 |
+
console.error('Upload error:', error);
|
| 252 |
+
updateProgress(0, `Error: ${error.message}`);
|
| 253 |
+
} finally {
|
| 254 |
+
document.getElementById('uploadBtn').disabled = false;
|
| 255 |
+
setTimeout(() => {
|
| 256 |
+
document.getElementById('progressSection').style.display = 'none';
|
| 257 |
+
}, 2000);
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
function updateProgress(percent, text) {
|
| 262 |
+
document.getElementById('progressFill').style.width = percent + '%';
|
| 263 |
+
document.getElementById('progressText').textContent = text;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
function displayResults(result) {
|
| 267 |
+
const resultsDiv = document.getElementById('results');
|
| 268 |
+
const resultsContent = document.getElementById('resultsContent');
|
| 269 |
+
|
| 270 |
+
resultsContent.innerHTML = `
|
| 271 |
+
<div class="batch-item">
|
| 272 |
+
<h3>Batch ${result.batchId}</h3>
|
| 273 |
+
<p><strong>Total Files:</strong> ${result.summary.totalFiles}</p>
|
| 274 |
+
<p><strong>Successful:</strong> ${result.summary.successful}</p>
|
| 275 |
+
<p><strong>Failed:</strong> ${result.summary.failed}</p>
|
| 276 |
+
|
| 277 |
+
<button onclick="downloadBatch('${result.batchId}')" class="btn-primary">
|
| 278 |
+
Download Remediated Files
|
| 279 |
+
</button>
|
| 280 |
+
|
| 281 |
+
<h4>File Details:</h4>
|
| 282 |
+
<div class="file-list">
|
| 283 |
+
${result.results.map(r => `
|
| 284 |
+
<div class="file-item ${r.success ? 'success' : 'error'}">
|
| 285 |
+
<div>
|
| 286 |
+
<strong>${r.filename}</strong>
|
| 287 |
+
${r.success ?
|
| 288 |
+
`<br><small>✓ Processed successfully</small>` :
|
| 289 |
+
`<br><small>✗ Error: ${r.error}</small>`
|
| 290 |
+
}
|
| 291 |
+
</div>
|
| 292 |
+
</div>
|
| 293 |
+
`).join('')}
|
| 294 |
+
</div>
|
| 295 |
+
</div>
|
| 296 |
+
`;
|
| 297 |
+
|
| 298 |
+
resultsDiv.style.display = 'block';
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
async function downloadBatch(batchId) {
|
| 302 |
+
if (sessionId) {
|
| 303 |
+
window.open(`${API_BASE}/api/batch-download?batchId=${batchId}&sessionId=${sessionId}`, '_blank');
|
| 304 |
+
}
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
async function deleteBatch(batchId) {
|
| 308 |
+
if (!confirm(`Delete batch ${batchId}?`)) return;
|
| 309 |
+
|
| 310 |
+
try {
|
| 311 |
+
const response = await fetch(`${API_BASE}/api/reports?batchId=${batchId}`, {
|
| 312 |
+
method: 'DELETE'
|
| 313 |
+
});
|
| 314 |
+
|
| 315 |
+
if (response.ok) {
|
| 316 |
+
loadBatchHistory(); // Refresh the list
|
| 317 |
+
} else {
|
| 318 |
+
alert('Failed to delete batch');
|
| 319 |
+
}
|
| 320 |
+
} catch (error) {
|
| 321 |
+
console.error('Error deleting batch:', error);
|
| 322 |
+
}
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
// Initialize session on page load
|
| 326 |
+
initializeSession();
|
| 327 |
+
</script>
|
| 328 |
+
</body>
|
| 329 |
+
</html>
|
docs/remediate-example.html
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
| 6 |
+
<title>Remediate & Download Example</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Arial; padding: 24px; }
|
| 9 |
+
.banner { padding: 12px; border: 1px solid #d0d7de; background: #f6f8fa; margin-bottom: 16px; }
|
| 10 |
+
.btn { display: inline-block; padding: 8px 12px; background: #0366d6; color: white; border-radius: 6px; text-decoration: none; }
|
| 11 |
+
.muted { color: #666; font-size: 0.95rem }
|
| 12 |
+
pre { background:#f3f4f6;padding:12px;border-radius:6px; }
|
| 13 |
+
</style>
|
| 14 |
+
</head>
|
| 15 |
+
<body>
|
| 16 |
+
<h1>Remediate & Download (example)</h1>
|
| 17 |
+
|
| 18 |
+
<div class="banner" id="remediateBanner">
|
| 19 |
+
<strong>Tip:</strong> If the downloaded file opens in Protected View, Windows may have marked it as downloaded from the Internet.
|
| 20 |
+
See the "Unblock" instructions below.
|
| 21 |
+
</div>
|
| 22 |
+
|
| 23 |
+
<p class="muted">This example triggers a native download by posting a file to the backend `/download-document` endpoint. Use the form file input to pick a .docx and click "Remediate & Download".</p>
|
| 24 |
+
|
| 25 |
+
<form id="remediateForm" action="/api/download-document" method="post" enctype="multipart/form-data" style="margin-top:12px;">
|
| 26 |
+
<input id="fileInput" name="file" type="file" accept=".docx" />
|
| 27 |
+
<button id="go" class="btn" type="submit">Remediate & Download</button>
|
| 28 |
+
</form>
|
| 29 |
+
|
| 30 |
+
<h2>If your file opens in Protected View</h2>
|
| 31 |
+
<p>Windows may add the Mark-of-the-Web (Zone.Identifier) to downloaded files. To remove it locally:</p>
|
| 32 |
+
<pre>PowerShell: Unblock-File -Path 'C:\path\to\your\downloaded.docx'</pre>
|
| 33 |
+
<p>To check for alternate data streams (Zone.Identifier):</p>
|
| 34 |
+
<pre>PowerShell: Get-Item -Path 'C:\path\to\your\downloaded.docx' -Stream *</pre>
|
| 35 |
+
|
| 36 |
+
<h3>Optional: programmatic download example (fetch + blob)</h3>
|
| 37 |
+
<p class="muted">If you prefer fetching the file with JS and saving a blob (note: native downloads via form submit often behave better for Content-Disposition handling and browser integration):</p>
|
| 38 |
+
<pre>
|
| 39 |
+
// Example (browser):
|
| 40 |
+
// const data = new FormData();
|
| 41 |
+
// data.append('file', fileInput.files[0]);
|
| 42 |
+
// fetch('/api/download-document', { method: 'POST', body: data })
|
| 43 |
+
// .then(r => {
|
| 44 |
+
// const filename = r.headers.get('content-disposition')?.split('filename=')?.[1]?.replace(/\"/g, '') || 'remediated.docx';
|
| 45 |
+
// return r.blob().then(b => ({ b, filename }));
|
| 46 |
+
// })
|
| 47 |
+
// .then(({ b, filename }) => {
|
| 48 |
+
// const url = URL.createObjectURL(b);
|
| 49 |
+
// const a = document.createElement('a');
|
| 50 |
+
// a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove();
|
| 51 |
+
// })
|
| 52 |
+
</pre>
|
| 53 |
+
|
| 54 |
+
<script>
|
| 55 |
+
// Small UX: show a notice if user tries to remediate without selecting a file
|
| 56 |
+
document.getElementById('remediateForm').addEventListener('submit', function (e) {
|
| 57 |
+
const f = document.getElementById('fileInput');
|
| 58 |
+
if (!f.files || !f.files.length) {
|
| 59 |
+
e.preventDefault();
|
| 60 |
+
alert('Please pick a .docx file first');
|
| 61 |
+
return false;
|
| 62 |
+
}
|
| 63 |
+
// Let the form submit normally so the browser triggers a download.
|
| 64 |
+
});
|
| 65 |
+
</script>
|
| 66 |
+
</body>
|
| 67 |
+
</html>
|
lib/cors-middleware.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const ALLOWED_ORIGINS = [
|
| 2 |
+
'https://ai-chat-bot-education-2026.vercel.app',
|
| 3 |
+
'https://accessibilitychecker25-arch.github.io',
|
| 4 |
+
'https://kmoreland126.github.io',
|
| 5 |
+
'http://localhost:3000',
|
| 6 |
+
'http://localhost:4200'
|
| 7 |
+
];
|
| 8 |
+
|
| 9 |
+
function getAllowedOrigin(origin) {
|
| 10 |
+
if (origin && ALLOWED_ORIGINS.includes(origin)) {
|
| 11 |
+
return origin;
|
| 12 |
+
}
|
| 13 |
+
return null;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
function applyCorsHeaders(req, res, options = {}) {
|
| 17 |
+
const allowedMethods = options.allowedMethods || 'GET, POST, OPTIONS';
|
| 18 |
+
const allowedHeaders = options.allowedHeaders || 'Content-Type, Authorization, X-Session-ID';
|
| 19 |
+
const exposeHeaders = options.exposeHeaders || 'Content-Disposition, Content-Type';
|
| 20 |
+
|
| 21 |
+
// Allow any origin to access this API. This resolves CORS missing allow origin issues
|
| 22 |
+
// for deployed frontends that may be on different domains or preview URLs.
|
| 23 |
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 24 |
+
|
| 25 |
+
res.setHeader('Access-Control-Allow-Methods', allowedMethods);
|
| 26 |
+
res.setHeader('Access-Control-Allow-Headers', allowedHeaders);
|
| 27 |
+
res.setHeader('Access-Control-Expose-Headers', exposeHeaders);
|
| 28 |
+
res.setHeader('Access-Control-Max-Age', '86400');
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function handleCorsPreflight(req, res, options = {}) {
|
| 32 |
+
applyCorsHeaders(req, res, options);
|
| 33 |
+
if (req.method === 'OPTIONS') {
|
| 34 |
+
res.status(200).end();
|
| 35 |
+
return true;
|
| 36 |
+
}
|
| 37 |
+
return false;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
module.exports = {
|
| 41 |
+
applyCorsHeaders,
|
| 42 |
+
handleCorsPreflight,
|
| 43 |
+
};
|
lib/pptx-analyzer.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const JSZip = require('jszip');
|
| 2 |
+
|
| 3 |
+
// Main PowerPoint analysis function
|
| 4 |
+
async function analyzePowerPoint(fileData, filename) {
|
| 5 |
+
const report = {
|
| 6 |
+
fileName: filename,
|
| 7 |
+
suggestedFileName: filename,
|
| 8 |
+
summary: { fixed: 0, flagged: 0 },
|
| 9 |
+
details: {
|
| 10 |
+
listFormattingIssues: [],
|
| 11 |
+
imagesMissingOrBadAlt: [],
|
| 12 |
+
}
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
try {
|
| 16 |
+
const zip = await JSZip.loadAsync(fileData);
|
| 17 |
+
|
| 18 |
+
// Get list of slides
|
| 19 |
+
const slides = [];
|
| 20 |
+
zip.forEach((relativePath, file) => {
|
| 21 |
+
if (relativePath.match(/^ppt\/slides\/slide\d+\.xml$/)) {
|
| 22 |
+
slides.push(relativePath);
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
// Sort slides by number
|
| 27 |
+
slides.sort((a, b) => {
|
| 28 |
+
const numA = parseInt(a.match(/slide(\d+)\.xml$/)?.[1] || '0');
|
| 29 |
+
const numB = parseInt(b.match(/slide(\d+)\.xml$/)?.[1] || '0');
|
| 30 |
+
return numA - numB;
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
console.log(`[analyzePowerPoint] Found ${slides.length} slides`);
|
| 34 |
+
|
| 35 |
+
// Analyze each slide
|
| 36 |
+
for (let i = 0; i < slides.length; i++) {
|
| 37 |
+
const slidePath = slides[i];
|
| 38 |
+
const slideNumber = i + 1;
|
| 39 |
+
const slideXml = await zip.file(slidePath)?.async('string');
|
| 40 |
+
const slideRelsPath = slidePath.replace('ppt/slides/', 'ppt/slides/_rels/').replace('.xml', '.xml.rels');
|
| 41 |
+
const slideRels = await zip.file(slideRelsPath)?.async('string');
|
| 42 |
+
|
| 43 |
+
if (slideXml) {
|
| 44 |
+
// Check for list formatting issues (hyphenated paragraphs)
|
| 45 |
+
const listIssues = checkListFormatting(slideXml, slideNumber);
|
| 46 |
+
if (listIssues.length > 0) {
|
| 47 |
+
report.details.listFormattingIssues.push(...listIssues);
|
| 48 |
+
report.summary.flagged += listIssues.length;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Check images for alt text
|
| 52 |
+
const imageIssues = await analyzeSlideImages(slideXml, slideRels, slideNumber);
|
| 53 |
+
if (imageIssues.length > 0) {
|
| 54 |
+
report.details.imagesMissingOrBadAlt.push(...imageIssues);
|
| 55 |
+
report.summary.flagged += imageIssues.length;
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
console.log(`[analyzePowerPoint] Analysis complete. Fixed: ${report.summary.fixed}, Flagged: ${report.summary.flagged}`);
|
| 61 |
+
return report;
|
| 62 |
+
|
| 63 |
+
} catch (error) {
|
| 64 |
+
console.error('[analyzePowerPoint] Error:', error);
|
| 65 |
+
throw new Error(`Failed to analyze PowerPoint: ${error.message}`);
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// Check for list formatting issues (hyphenated paragraphs that should be lists)
|
| 70 |
+
function checkListFormatting(slideXml, slideNumber) {
|
| 71 |
+
const issues = [];
|
| 72 |
+
|
| 73 |
+
// Find all text elements in the slide
|
| 74 |
+
const textMatches = slideXml.matchAll(/<a:t[^>]*>(.*?)<\/a:t>/g);
|
| 75 |
+
|
| 76 |
+
for (const match of textMatches) {
|
| 77 |
+
const text = match[1];
|
| 78 |
+
|
| 79 |
+
// Check for hyphenated paragraphs that look like lists
|
| 80 |
+
// Pattern: line starting with "-", "•", "–", "—" followed by text
|
| 81 |
+
if (/^[\s]*[-–—•]\s+.+/.test(text)) {
|
| 82 |
+
issues.push({
|
| 83 |
+
slideNumber: slideNumber,
|
| 84 |
+
location: `Slide ${slideNumber}`,
|
| 85 |
+
issue: `Possible improperly formatted list: "${text.substring(0, 50)}..."`,
|
| 86 |
+
type: 'listFormatting'
|
| 87 |
+
});
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
return issues;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
// Analyze images in a slide
|
| 95 |
+
async function analyzeSlideImages(slideXml, slideRels, slideNumber) {
|
| 96 |
+
const issues = [];
|
| 97 |
+
|
| 98 |
+
// Find all picture elements
|
| 99 |
+
const picMatches = slideXml.matchAll(/<p:pic[\s\S]*?<\/p:pic>/g);
|
| 100 |
+
|
| 101 |
+
for (const picMatch of picMatches) {
|
| 102 |
+
const picXml = picMatch[0];
|
| 103 |
+
|
| 104 |
+
// Check for alt text (descr attribute in <p:cNvPr>)
|
| 105 |
+
const nvPicPr = picXml.match(/<p:nvPicPr>([\s\S]*?)<\/p:nvPicPr>/);
|
| 106 |
+
if (nvPicPr) {
|
| 107 |
+
const cNvPr = nvPicPr[1].match(/<p:cNvPr[^>]*>/);
|
| 108 |
+
if (cNvPr) {
|
| 109 |
+
const descrMatch = cNvPr[0].match(/descr="([^"]*)"/);
|
| 110 |
+
const altText = descrMatch ? descrMatch[1] : '';
|
| 111 |
+
|
| 112 |
+
if (!altText || altText.trim().length === 0) {
|
| 113 |
+
issues.push({
|
| 114 |
+
slideNumber: slideNumber,
|
| 115 |
+
location: `Slide ${slideNumber}`,
|
| 116 |
+
issue: 'Image missing alt text',
|
| 117 |
+
type: 'image'
|
| 118 |
+
});
|
| 119 |
+
} else if (altText.length > 250) {
|
| 120 |
+
issues.push({
|
| 121 |
+
slideNumber: slideNumber,
|
| 122 |
+
location: `Slide ${slideNumber}`,
|
| 123 |
+
issue: `Image alt text is too long (${altText.length} characters, max 250)`,
|
| 124 |
+
type: 'image'
|
| 125 |
+
});
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
return issues;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
module.exports = { analyzePowerPoint };
|
lib/session-manager.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Session-based file storage with automatic cleanup
|
| 2 |
+
const fs = require('fs').promises;
|
| 3 |
+
const path = require('path');
|
| 4 |
+
|
| 5 |
+
class SessionManager {
|
| 6 |
+
constructor() {
|
| 7 |
+
this.sessions = new Map();
|
| 8 |
+
this.cleanupInterval = 30 * 60 * 1000; // 30 minutes
|
| 9 |
+
this.sessionTimeout = 60 * 60 * 1000; // 1 hour
|
| 10 |
+
|
| 11 |
+
// Start cleanup timer
|
| 12 |
+
setInterval(() => this.cleanupExpiredSessions(), this.cleanupInterval);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// Create a new session
|
| 16 |
+
createSession() {
|
| 17 |
+
const sessionId = Date.now() + '-' + Math.random().toString(36).substr(2, 9);
|
| 18 |
+
const sessionDir = `temp-sessions/${sessionId}`;
|
| 19 |
+
|
| 20 |
+
const session = {
|
| 21 |
+
sessionId,
|
| 22 |
+
createdAt: Date.now(),
|
| 23 |
+
lastActivity: Date.now(),
|
| 24 |
+
directory: sessionDir,
|
| 25 |
+
files: [],
|
| 26 |
+
batches: [],
|
| 27 |
+
reports: []
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
this.sessions.set(sessionId, session);
|
| 31 |
+
|
| 32 |
+
// Create session directory
|
| 33 |
+
this.ensureSessionDirectory(sessionDir);
|
| 34 |
+
|
| 35 |
+
return session;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Get existing session or create new one
|
| 39 |
+
getOrCreateSession(sessionId) {
|
| 40 |
+
if (sessionId && this.sessions.has(sessionId)) {
|
| 41 |
+
const session = this.sessions.get(sessionId);
|
| 42 |
+
session.lastActivity = Date.now();
|
| 43 |
+
return session;
|
| 44 |
+
}
|
| 45 |
+
return this.createSession();
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Update session activity (keeps it alive)
|
| 49 |
+
heartbeat(sessionId) {
|
| 50 |
+
if (this.sessions.has(sessionId)) {
|
| 51 |
+
const session = this.sessions.get(sessionId);
|
| 52 |
+
session.lastActivity = Date.now();
|
| 53 |
+
return true;
|
| 54 |
+
}
|
| 55 |
+
return false;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// Add file to session
|
| 59 |
+
addFileToSession(sessionId, fileInfo) {
|
| 60 |
+
const session = this.sessions.get(sessionId);
|
| 61 |
+
if (session) {
|
| 62 |
+
session.files.push(fileInfo);
|
| 63 |
+
session.lastActivity = Date.now();
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Add batch to session
|
| 68 |
+
addBatchToSession(sessionId, batchInfo) {
|
| 69 |
+
const session = this.sessions.get(sessionId);
|
| 70 |
+
if (session) {
|
| 71 |
+
session.batches.push(batchInfo);
|
| 72 |
+
session.lastActivity = Date.now();
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
// Get session files
|
| 77 |
+
getSessionFiles(sessionId) {
|
| 78 |
+
const session = this.sessions.get(sessionId);
|
| 79 |
+
return session ? session.files : [];
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// Get session batches
|
| 83 |
+
getSessionBatches(sessionId) {
|
| 84 |
+
const session = this.sessions.get(sessionId);
|
| 85 |
+
return session ? session.batches : [];
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// Clean up expired sessions
|
| 89 |
+
async cleanupExpiredSessions() {
|
| 90 |
+
const now = Date.now();
|
| 91 |
+
const expiredSessions = [];
|
| 92 |
+
|
| 93 |
+
for (const [sessionId, session] of this.sessions) {
|
| 94 |
+
if (now - session.lastActivity > this.sessionTimeout) {
|
| 95 |
+
expiredSessions.push(sessionId);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
for (const sessionId of expiredSessions) {
|
| 100 |
+
await this.destroySession(sessionId);
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
if (expiredSessions.length > 0) {
|
| 104 |
+
console.log(`🧹 Cleaned up ${expiredSessions.length} expired sessions`);
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// Manually destroy a session
|
| 109 |
+
async destroySession(sessionId) {
|
| 110 |
+
const session = this.sessions.get(sessionId);
|
| 111 |
+
if (!session) return;
|
| 112 |
+
|
| 113 |
+
try {
|
| 114 |
+
// Delete all session files
|
| 115 |
+
await this.deleteDirectory(session.directory);
|
| 116 |
+
console.log(`🗑️ Deleted session directory: ${session.directory}`);
|
| 117 |
+
} catch (error) {
|
| 118 |
+
console.warn(`Failed to delete session directory ${session.directory}:`, error.message);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// Remove from memory
|
| 122 |
+
this.sessions.delete(sessionId);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Ensure session directory exists
|
| 126 |
+
async ensureSessionDirectory(sessionDir) {
|
| 127 |
+
try {
|
| 128 |
+
await fs.mkdir(sessionDir, { recursive: true });
|
| 129 |
+
} catch (error) {
|
| 130 |
+
if (error.code !== 'EEXIST') {
|
| 131 |
+
throw error;
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// Recursively delete directory
|
| 137 |
+
async deleteDirectory(dirPath) {
|
| 138 |
+
try {
|
| 139 |
+
const stats = await fs.stat(dirPath);
|
| 140 |
+
if (stats.isDirectory()) {
|
| 141 |
+
const files = await fs.readdir(dirPath);
|
| 142 |
+
await Promise.all(
|
| 143 |
+
files.map(file => this.deleteDirectory(path.join(dirPath, file)))
|
| 144 |
+
);
|
| 145 |
+
await fs.rmdir(dirPath);
|
| 146 |
+
} else {
|
| 147 |
+
await fs.unlink(dirPath);
|
| 148 |
+
}
|
| 149 |
+
} catch (error) {
|
| 150 |
+
if (error.code !== 'ENOENT') {
|
| 151 |
+
throw error;
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
// Get session stats
|
| 157 |
+
getSessionStats() {
|
| 158 |
+
return {
|
| 159 |
+
activeSessions: this.sessions.size,
|
| 160 |
+
sessions: Array.from(this.sessions.values()).map(s => ({
|
| 161 |
+
sessionId: s.sessionId,
|
| 162 |
+
createdAt: s.createdAt,
|
| 163 |
+
lastActivity: s.lastActivity,
|
| 164 |
+
filesCount: s.files.length,
|
| 165 |
+
batchesCount: s.batches.length
|
| 166 |
+
}))
|
| 167 |
+
};
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
// Global session manager instance
|
| 172 |
+
const sessionManager = new SessionManager();
|
| 173 |
+
|
| 174 |
+
module.exports = sessionManager;
|
local-test-color-contrast.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// local-test-color-contrast.js
|
| 2 |
+
// Locally invoke the backend's `analyzeDocx` function to test logic such as color contrast and line spacing.
|
| 3 |
+
//local testing feature for the backend.Command: node local-test-color-contrast.js
|
| 4 |
+
const fs = require('fs');
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
// Reference the modified upload-document handler function
|
| 8 |
+
const uploadHandler = require('./api/upload-document');
|
| 9 |
+
const analyzeDocx = uploadHandler.analyzeDocx;
|
| 10 |
+
|
| 11 |
+
async function run() {
|
| 12 |
+
try {
|
| 13 |
+
// Test docx files are located in the test-docs directory.
|
| 14 |
+
const testPath = path.join(
|
| 15 |
+
__dirname,
|
| 16 |
+
'test-docs',
|
| 17 |
+
'Set one row to a very light gray.docx'
|
| 18 |
+
);
|
| 19 |
+
|
| 20 |
+
const fileData = fs.readFileSync(testPath);
|
| 21 |
+
const report = await analyzeDocx(fileData, path.basename(testPath));
|
| 22 |
+
|
| 23 |
+
console.log('=== Local analyzeDocx report ===');
|
| 24 |
+
console.log(JSON.stringify(report, null, 2));
|
| 25 |
+
} catch (err) {
|
| 26 |
+
console.error('Local test failed:', err);
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
run();
|
package-lock.json
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "accessibility-checker-be",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"lockfileVersion": 3,
|
| 5 |
+
"requires": true,
|
| 6 |
+
"packages": {
|
| 7 |
+
"": {
|
| 8 |
+
"name": "accessibility-checker-be",
|
| 9 |
+
"version": "1.0.0",
|
| 10 |
+
"dependencies": {
|
| 11 |
+
"busboy": "^1.6.0",
|
| 12 |
+
"docx": "^8.5.0",
|
| 13 |
+
"jszip": "^3.10.1"
|
| 14 |
+
},
|
| 15 |
+
"engines": {
|
| 16 |
+
"node": ">=18"
|
| 17 |
+
}
|
| 18 |
+
},
|
| 19 |
+
"node_modules/busboy": {
|
| 20 |
+
"version": "1.6.0",
|
| 21 |
+
"resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",
|
| 22 |
+
"integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==",
|
| 23 |
+
"dependencies": {
|
| 24 |
+
"streamsearch": "^1.1.0"
|
| 25 |
+
},
|
| 26 |
+
"engines": {
|
| 27 |
+
"node": ">=10.16.0"
|
| 28 |
+
}
|
| 29 |
+
},
|
| 30 |
+
"node_modules/core-util-is": {
|
| 31 |
+
"version": "1.0.3",
|
| 32 |
+
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
|
| 33 |
+
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==",
|
| 34 |
+
"license": "MIT"
|
| 35 |
+
},
|
| 36 |
+
"node_modules/docx": {
|
| 37 |
+
"version": "8.5.0",
|
| 38 |
+
"resolved": "https://registry.npmjs.org/docx/-/docx-8.5.0.tgz",
|
| 39 |
+
"integrity": "sha512-4SbcbedPXTciySXiSnNNLuJXpvxFe5nqivbiEHXyL8P/w0wx2uW7YXNjnYgjW0e2e6vy+L/tMISU/oAiXCl57Q==",
|
| 40 |
+
"license": "MIT",
|
| 41 |
+
"dependencies": {
|
| 42 |
+
"@types/node": "^20.3.1",
|
| 43 |
+
"jszip": "^3.10.1",
|
| 44 |
+
"nanoid": "^5.0.4",
|
| 45 |
+
"xml": "^1.0.1",
|
| 46 |
+
"xml-js": "^1.6.8"
|
| 47 |
+
},
|
| 48 |
+
"engines": {
|
| 49 |
+
"node": ">=10"
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
"node_modules/docx/node_modules/@types/node": {
|
| 53 |
+
"version": "20.19.24",
|
| 54 |
+
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.24.tgz",
|
| 55 |
+
"integrity": "sha512-FE5u0ezmi6y9OZEzlJfg37mqqf6ZDSF2V/NLjUyGrR9uTZ7Sb9F7bLNZ03S4XVUNRWGA7Ck4c1kK+YnuWjl+DA==",
|
| 56 |
+
"license": "MIT",
|
| 57 |
+
"dependencies": {
|
| 58 |
+
"undici-types": "~6.21.0"
|
| 59 |
+
}
|
| 60 |
+
},
|
| 61 |
+
"node_modules/docx/node_modules/undici-types": {
|
| 62 |
+
"version": "6.21.0",
|
| 63 |
+
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
|
| 64 |
+
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
|
| 65 |
+
"license": "MIT"
|
| 66 |
+
},
|
| 67 |
+
"node_modules/immediate": {
|
| 68 |
+
"version": "3.0.6",
|
| 69 |
+
"resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",
|
| 70 |
+
"integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ=="
|
| 71 |
+
},
|
| 72 |
+
"node_modules/inherits": {
|
| 73 |
+
"version": "2.0.4",
|
| 74 |
+
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
|
| 75 |
+
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
|
| 76 |
+
"license": "ISC"
|
| 77 |
+
},
|
| 78 |
+
"node_modules/isarray": {
|
| 79 |
+
"version": "1.0.0",
|
| 80 |
+
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
| 81 |
+
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
|
| 82 |
+
"license": "MIT"
|
| 83 |
+
},
|
| 84 |
+
"node_modules/jszip": {
|
| 85 |
+
"version": "3.10.1",
|
| 86 |
+
"resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz",
|
| 87 |
+
"integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==",
|
| 88 |
+
"license": "(MIT OR GPL-3.0-or-later)",
|
| 89 |
+
"dependencies": {
|
| 90 |
+
"lie": "~3.3.0",
|
| 91 |
+
"pako": "~1.0.2",
|
| 92 |
+
"readable-stream": "~2.3.6",
|
| 93 |
+
"setimmediate": "^1.0.5"
|
| 94 |
+
}
|
| 95 |
+
},
|
| 96 |
+
"node_modules/jszip/node_modules/readable-stream": {
|
| 97 |
+
"version": "2.3.8",
|
| 98 |
+
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz",
|
| 99 |
+
"integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==",
|
| 100 |
+
"dependencies": {
|
| 101 |
+
"core-util-is": "~1.0.0",
|
| 102 |
+
"inherits": "~2.0.3",
|
| 103 |
+
"isarray": "~1.0.0",
|
| 104 |
+
"process-nextick-args": "~2.0.0",
|
| 105 |
+
"safe-buffer": "~5.1.1",
|
| 106 |
+
"string_decoder": "~1.1.1",
|
| 107 |
+
"util-deprecate": "~1.0.1"
|
| 108 |
+
}
|
| 109 |
+
},
|
| 110 |
+
"node_modules/jszip/node_modules/safe-buffer": {
|
| 111 |
+
"version": "5.1.2",
|
| 112 |
+
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
| 113 |
+
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
| 114 |
+
},
|
| 115 |
+
"node_modules/jszip/node_modules/string_decoder": {
|
| 116 |
+
"version": "1.1.1",
|
| 117 |
+
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
|
| 118 |
+
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
|
| 119 |
+
"dependencies": {
|
| 120 |
+
"safe-buffer": "~5.1.0"
|
| 121 |
+
}
|
| 122 |
+
},
|
| 123 |
+
"node_modules/lie": {
|
| 124 |
+
"version": "3.3.0",
|
| 125 |
+
"resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz",
|
| 126 |
+
"integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==",
|
| 127 |
+
"dependencies": {
|
| 128 |
+
"immediate": "~3.0.5"
|
| 129 |
+
}
|
| 130 |
+
},
|
| 131 |
+
"node_modules/nanoid": {
|
| 132 |
+
"version": "5.1.6",
|
| 133 |
+
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.1.6.tgz",
|
| 134 |
+
"integrity": "sha512-c7+7RQ+dMB5dPwwCp4ee1/iV/q2P6aK1mTZcfr1BTuVlyW9hJYiMPybJCcnBlQtuSmTIWNeazm/zqNoZSSElBg==",
|
| 135 |
+
"funding": [
|
| 136 |
+
{
|
| 137 |
+
"type": "github",
|
| 138 |
+
"url": "https://github.com/sponsors/ai"
|
| 139 |
+
}
|
| 140 |
+
],
|
| 141 |
+
"license": "MIT",
|
| 142 |
+
"bin": {
|
| 143 |
+
"nanoid": "bin/nanoid.js"
|
| 144 |
+
},
|
| 145 |
+
"engines": {
|
| 146 |
+
"node": "^18 || >=20"
|
| 147 |
+
}
|
| 148 |
+
},
|
| 149 |
+
"node_modules/pako": {
|
| 150 |
+
"version": "1.0.11",
|
| 151 |
+
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
|
| 152 |
+
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
|
| 153 |
+
},
|
| 154 |
+
"node_modules/process-nextick-args": {
|
| 155 |
+
"version": "2.0.1",
|
| 156 |
+
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
| 157 |
+
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==",
|
| 158 |
+
"license": "MIT"
|
| 159 |
+
},
|
| 160 |
+
"node_modules/sax": {
|
| 161 |
+
"version": "1.4.1",
|
| 162 |
+
"resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz",
|
| 163 |
+
"integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==",
|
| 164 |
+
"license": "ISC"
|
| 165 |
+
},
|
| 166 |
+
"node_modules/setimmediate": {
|
| 167 |
+
"version": "1.0.5",
|
| 168 |
+
"resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz",
|
| 169 |
+
"integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA=="
|
| 170 |
+
},
|
| 171 |
+
"node_modules/streamsearch": {
|
| 172 |
+
"version": "1.1.0",
|
| 173 |
+
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz",
|
| 174 |
+
"integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==",
|
| 175 |
+
"engines": {
|
| 176 |
+
"node": ">=10.0.0"
|
| 177 |
+
}
|
| 178 |
+
},
|
| 179 |
+
"node_modules/util-deprecate": {
|
| 180 |
+
"version": "1.0.2",
|
| 181 |
+
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
| 182 |
+
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
|
| 183 |
+
"license": "MIT"
|
| 184 |
+
},
|
| 185 |
+
"node_modules/xml": {
|
| 186 |
+
"version": "1.0.1",
|
| 187 |
+
"resolved": "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz",
|
| 188 |
+
"integrity": "sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==",
|
| 189 |
+
"license": "MIT"
|
| 190 |
+
},
|
| 191 |
+
"node_modules/xml-js": {
|
| 192 |
+
"version": "1.6.11",
|
| 193 |
+
"resolved": "https://registry.npmjs.org/xml-js/-/xml-js-1.6.11.tgz",
|
| 194 |
+
"integrity": "sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==",
|
| 195 |
+
"license": "MIT",
|
| 196 |
+
"dependencies": {
|
| 197 |
+
"sax": "^1.2.4"
|
| 198 |
+
},
|
| 199 |
+
"bin": {
|
| 200 |
+
"xml-js": "bin/cli.js"
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
}
|
package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "accessibility-checker-be",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "DOCX Accessibility Checker Backend",
|
| 5 |
+
"dependencies": {
|
| 6 |
+
"busboy": "^1.6.0",
|
| 7 |
+
"docx": "^8.5.0",
|
| 8 |
+
"jszip": "^3.10.1"
|
| 9 |
+
},
|
| 10 |
+
"engines": {
|
| 11 |
+
"node": ">=18"
|
| 12 |
+
}
|
| 13 |
+
}
|
python-server/.env.example
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ========================================
|
| 2 |
+
# FREE Local AI Configuration
|
| 3 |
+
# (NO API KEYS, NO COSTS, 100% FREE!)
|
| 4 |
+
# ========================================
|
| 5 |
+
|
| 6 |
+
# Local AI Model - 100% FREE runs on your computer
|
| 7 |
+
# Options:
|
| 8 |
+
# blip-base (default - fast, good quality)
|
| 9 |
+
# blip-large (slower, better quality)
|
| 10 |
+
# git-base (alternative model)
|
| 11 |
+
LOCAL_VISION_MODEL=blip-base
|
| 12 |
+
|
| 13 |
+
# Enable/Disable AI Alt Text Generation (default: true)
|
| 14 |
+
# Set to false to use placeholder text instead
|
| 15 |
+
ENABLE_AI_ALT_TEXT=true
|
| 16 |
+
|
| 17 |
+
# ========================================
|
| 18 |
+
# Optional Server Configuration
|
| 19 |
+
# ========================================
|
| 20 |
+
|
| 21 |
+
# Host and port for the FastAPI server (defaults used if not set)
|
| 22 |
+
# SERVER_HOST=127.0.0.1
|
| 23 |
+
# SERVER_PORT=5000
|
python-server/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🚀 Quick Start: FREE AI Alt Text Generation
|
| 2 |
+
|
| 3 |
+
## 2-Minute Setup (100% FREE!)
|
| 4 |
+
|
| 5 |
+
### Step 1: Install Dependencies
|
| 6 |
+
```bash
|
| 7 |
+
cd "Cycle 2 Testing/Accessibility-Checker-BE/python-server"
|
| 8 |
+
pip install -r requirements.txt
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
**That's it!** No configuration needed. The system works with smart defaults.
|
| 12 |
+
|
| 13 |
+
**First run note**: The AI model downloads ~1-2GB (one time only, then cached)
|
| 14 |
+
|
| 15 |
+
### Step 2: Start the Server
|
| 16 |
+
```bash
|
| 17 |
+
python server2.py
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
Look for: `✅ Local AI vision model loaded (BLIP - 100% FREE, No Costs)`
|
| 21 |
+
|
| 22 |
+
### Step 3: Test It!
|
| 23 |
+
Upload a PowerPoint through the frontend. The system will:
|
| 24 |
+
- ✅ Analyze accessibility issues
|
| 25 |
+
- ✅ Generate AI alt text for images **using FREE local AI**
|
| 26 |
+
- ✅ Create a remediated file for download
|
| 27 |
+
- ✅ **Zero API costs, zero API keys needed!**
|
| 28 |
+
|
| 29 |
+
### Optional: Customize Settings
|
| 30 |
+
If you want to change settings (like using a different AI model):
|
| 31 |
+
```bash
|
| 32 |
+
cp .env.example .env
|
| 33 |
+
# Edit .env with any text editor to customize
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
**But don't worry** - the system works perfectly without .env! It's completely optional.
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## What's New?
|
| 41 |
+
|
| 42 |
+
### Before (Placeholder Alt Text)
|
| 43 |
+
```
|
| 44 |
+
"Image on slide 3"
|
| 45 |
+
"decorative"
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
### After (FREE AI-Generated Alt Text)
|
| 49 |
+
```
|
| 50 |
+
"Bar chart with four colored bars showing increasing values"
|
| 51 |
+
"Person standing at whiteboard presenting to seated audience"
|
| 52 |
+
"Company logo with red and blue colors"
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## How It Works
|
| 58 |
+
|
| 59 |
+
### 🆓 The Only Option: Local BLIP Model (100% FREE!)
|
| 60 |
+
|
| 61 |
+
**Local BLIP AI Model**
|
| 62 |
+
- ✅ **100% Free, unlimited usage**
|
| 63 |
+
- ✅ Runs on your computer (offline after first download)
|
| 64 |
+
- ✅ No internet required for processing
|
| 65 |
+
- ✅ No API keys needed
|
| 66 |
+
- ✅ No account creation
|
| 67 |
+
- ✅ No surprise billing - ever!
|
| 68 |
+
- ✅ Fast and good quality (7/10)
|
| 69 |
+
- ⬇️ ~1GB download on first run
|
| 70 |
+
- ⚡ Instant on subsequent runs
|
| 71 |
+
|
| 72 |
+
## Why This Setup?
|
| 73 |
+
|
| 74 |
+
All OpenAI references have been **completely removed** from the project to eliminate any possibility of surprise billing. The free local AI model is:
|
| 75 |
+
|
| 76 |
+
- **Good enough** - Works great for academic projects
|
| 77 |
+
- **Cost effective** - $0 per image vs $0.17 with paid APIs
|
| 78 |
+
- **Simple** - No configuration needed
|
| 79 |
+
- **Safe** - Runs on your own computer, no data sent anywhere
|
| 80 |
+
---
|
| 81 |
+
|
| 82 |
+
## Configuration (100% Optional!)
|
| 83 |
+
|
| 84 |
+
### Why no .env file is needed
|
| 85 |
+
|
| 86 |
+
The system works perfectly with smart defaults:
|
| 87 |
+
- ✅ Uses local BLIP model automatically
|
| 88 |
+
- ✅ Enables AI alt text generation
|
| 89 |
+
- ✅ No API keys to configure
|
| 90 |
+
|
| 91 |
+
**Just install and run - that's it!**
|
| 92 |
+
|
| 93 |
+
### Optional: Customize (Create .env)
|
| 94 |
+
|
| 95 |
+
If you want to change settings, copy the template:
|
| 96 |
+
|
| 97 |
+
```bash
|
| 98 |
+
# Copy template
|
| 99 |
+
cp .env.example .env
|
| 100 |
+
|
| 101 |
+
# Edit with your preferred editor
|
| 102 |
+
# Optional settings you might change:
|
| 103 |
+
LOCAL_VISION_MODEL=blip-base # Use blip-large for better quality
|
| 104 |
+
ENABLE_AI_ALT_TEXT=true # Set to false to disable AI (for debugging)
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
**See `ENV_FILE_GUIDE.md` for complete .env documentation.**
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## Server Console Output
|
| 112 |
+
|
| 113 |
+
When everything is working:
|
| 114 |
+
|
| 115 |
+
```
|
| 116 |
+
✅ Local AI vision model loaded (BLIP - 100% FREE, No Costs)
|
| 117 |
+
🚀 Starting alt text remediation for: document.pptx
|
| 118 |
+
AI Mode: LOCAL (100% FREE - No Costs)
|
| 119 |
+
🤖 Using FREE local AI (BLIP) for slide 1
|
| 120 |
+
✅ AI generated alt text for Picture 1: 'Professional man in business suit...'
|
| 121 |
+
✅ Remediation complete: 3 images processed
|
| 122 |
+
🤖 3 alt texts generated by FREE local AI (no cost)
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
---
|
| 126 |
+
|
| 127 |
+
## Troubleshooting
|
| 128 |
+
|
| 129 |
+
### Problem: Slow download on first run
|
| 130 |
+
**Explanation**: System is downloading BLIP AI model (~1-2GB)
|
| 131 |
+
**Solution**: This only happens once. Subsequent runs are instant. Be patient!
|
| 132 |
+
**Time estimate**: 5-15 minutes depending on internet
|
| 133 |
+
|
| 134 |
+
### Problem: "transformers not installed"
|
| 135 |
+
**Solution**:
|
| 136 |
+
```bash
|
| 137 |
+
pip install -r requirements.txt
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### Problem: "ModuleNotFoundError: No module named 'local_vision'"
|
| 141 |
+
**Solution**: Make sure you're running from the `python-server/` directory
|
| 142 |
+
```bash
|
| 143 |
+
cd python-server
|
| 144 |
+
python server2.py
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
### Problem: Out of memory errors
|
| 148 |
+
**Solution**: Close other programs or use smaller model
|
| 149 |
+
```bash
|
| 150 |
+
# In .env:
|
| 151 |
+
LOCAL_VISION_MODEL=blip-base
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
### Problem: Alt text not being generated
|
| 155 |
+
**Check the console output**:
|
| 156 |
+
1. Does it show "✅ Local AI vision model loaded"?
|
| 157 |
+
2. Are images in supported formats (PNG, JPG, GIF)?
|
| 158 |
+
3. Is `ENABLE_AI_ALT_TEXT` set to true?
|
| 159 |
+
|
| 160 |
+
**Run diagnostics**:
|
| 161 |
+
```bash
|
| 162 |
+
python test_ai_setup.py
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
### Problem: "This model requires transformers version X.X"
|
| 166 |
+
**Solution**:
|
| 167 |
+
```bash
|
| 168 |
+
pip install --upgrade transformers torch
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## Cost: FREE Forever!
|
| 174 |
+
|
| 175 |
+
| Item | Cost |
|
| 176 |
+
|------|------|
|
| 177 |
+
| Local BLIP AI Model | $0 |
|
| 178 |
+
| First download (one-time) | $0 |
|
| 179 |
+
| Unlimited alt text generation | $0 |
|
| 180 |
+
| Monthly hosting | $0 (free tier) |
|
| 181 |
+
| **Total for entire team** | **$0 forever** |
|
| 182 |
+
|
| 183 |
+
**Compared to alternatives**:
|
| 184 |
+
- OpenAI: ~$0.17/image = $5-10 per presentation
|
| 185 |
+
- Google Vision: $1.50/100 images
|
| 186 |
+
- Azure: $1/$5/10 per 1000 requests
|
| 187 |
+
- **Our solution**: $0 per anything! 🎉
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
|
| 191 |
+
## Documentation
|
| 192 |
+
|
| 193 |
+
For more detailed information, see:
|
| 194 |
+
|
| 195 |
+
- **ENV_FILE_GUIDE.md** - Complete .env explanation (optional)
|
| 196 |
+
- **OPENAI_REMOVAL_COMPLETE.md** - Why OpenAI was removed for safety
|
| 197 |
+
- **AI_ALT_TEXT_SETUP.md** - Deep technical documentation
|
| 198 |
+
- **STUDENT_SETUP.md** - Student-friendly setup guide
|
| 199 |
+
- **FREE_AI_OPTIONS.md** - Comparison of all free alternatives
|
| 200 |
+
|
| 201 |
+
---
|
| 202 |
+
|
| 203 |
+
## Summary
|
| 204 |
+
|
| 205 |
+
✅ **Fastest Setup**:
|
| 206 |
+
```bash
|
| 207 |
+
pip install -r requirements.txt
|
| 208 |
+
python server2.py
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
✅ **No Configuration Needed**: Works with defaults
|
| 212 |
+
|
| 213 |
+
✅ **100% FREE**: No API keys, no monthly bills, no surprises
|
| 214 |
+
|
| 215 |
+
✅ **High Quality**: BLIP model produces excellent alt text descriptions
|
| 216 |
+
|
| 217 |
+
✅ **Easy to Use**: Upload PowerPoint, download fixed version
|
| 218 |
+
|
| 219 |
+
✅ **For Students**: Zero cost, zero complexity
|
| 220 |
+
|
| 221 |
+
**Ready to generate alt text for your presentations!** 🚀
|
python-server/TESTING_READY.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 6 |
+
- fastapi (FastAPI web framework)
|
| 7 |
+
- uvicorn (ASGI server)
|
| 8 |
+
- lxml (XML processing)
|
| 9 |
+
- transformers (AI/ML models)
|
| 10 |
+
- torch (PyTorch ML framework)
|
| 11 |
+
- pillow/PIL (image processing)
|
| 12 |
+
- python-docx (Word document handling)
|
| 13 |
+
- pywin32 (Windows COM automation)
|
| 14 |
+
- python-dotenv (environment configuration)
|
| 15 |
+
|
| 16 |
+
## 📋 What's Installed
|
| 17 |
+
|
| 18 |
+
**Core AI System:**
|
| 19 |
+
- `local_vision.py` - FREE local AI model integration (BLIP/GIT)
|
| 20 |
+
|
| 21 |
+
**Server:**
|
| 22 |
+
- `server2.py` - Main FastAPI backend with alt text remediation
|
| 23 |
+
|
| 24 |
+
**Config:**
|
| 25 |
+
- `requirements.txt` - Updated with compatible versions
|
| 26 |
+
- `.env.example` - Configuration template (optional)
|
| 27 |
+
- `.gitignore` - Protects .env files
|
| 28 |
+
|
| 29 |
+
**Testing:**
|
| 30 |
+
- `test_ai_setup.py` - Diagnostic test script
|
| 31 |
+
|
| 32 |
+
**Docs:**
|
| 33 |
+
- `QUICKSTART.md` - Quick start guide
|
| 34 |
+
- `README.md` - Project overview
|
| 35 |
+
|
| 36 |
+
## 🚀 To Start the Server
|
| 37 |
+
|
| 38 |
+
```bash
|
| 39 |
+
cd python-server
|
| 40 |
+
python server2.py
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
You should see:
|
| 44 |
+
```
|
| 45 |
+
✅ Local AI vision model loaded (BLIP - 100% FREE, No Costs)
|
| 46 |
+
🚀 Server running on http://localhost:5000
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
**First run will download BLIP model (~1-2GB) - takes 5-15 minutes**
|
| 50 |
+
|
| 51 |
+
## 🧪 To Test AI Setup
|
| 52 |
+
|
| 53 |
+
```bash
|
| 54 |
+
cd python-server
|
| 55 |
+
python test_ai_setup.py
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
This will verify:
|
| 59 |
+
- ✅ Transformers library
|
| 60 |
+
- ✅ Local BLIP model
|
| 61 |
+
- ✅ Image processing
|
| 62 |
+
- ✅ AI alt text generation
|
| 63 |
+
|
| 64 |
+
## 📁 File Structure
|
| 65 |
+
|
| 66 |
+
```
|
| 67 |
+
Accessibility-Checker-BE/
|
| 68 |
+
├── python-server/
|
| 69 |
+
│ ├── server2.py ← Main backend
|
| 70 |
+
│ ├── local_vision.py ← FREE AI engine
|
| 71 |
+
│ ├── test_ai_setup.py ← Test script
|
| 72 |
+
│ ├── requirements.txt ← Dependencies (all installed)
|
| 73 |
+
│ ├── .env.example ← Config template
|
| 74 |
+
│ ├── .gitignore ← Git ignore rules
|
| 75 |
+
│ ├── QUICKSTART.md ← Quick start
|
| 76 |
+
│ ├── TESTING_READY.md ← This file
|
| 77 |
+
│ └── README.md ← Documentation
|
| 78 |
+
├── api/ ← API code
|
| 79 |
+
├── lib/ ← Libraries
|
| 80 |
+
├── docs/ ← Documentation
|
| 81 |
+
└── tests/ ← Test files
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
## 💰 Cost Verification
|
| 85 |
+
|
| 86 |
+
| Component | Cost |
|
| 87 |
+
|-----------|------|
|
| 88 |
+
| Local BLIP AI | $0 |
|
| 89 |
+
| Unlimited alt text generation | $0/month |
|
| 90 |
+
| API keys required | 0 |
|
| 91 |
+
| Surprise billing | IMPOSSIBLE |
|
| 92 |
+
|
| 93 |
+
## ⚠️ Important Notes
|
| 94 |
+
|
| 95 |
+
1. **No .env file needed** - System works with defaults
|
| 96 |
+
2. **First run is slow** - BLIP model downloads (~1-2GB, 5-15 min)
|
| 97 |
+
3. **Subsequent runs are fast** - Model is cached locally
|
| 98 |
+
4. **100% private** - Images never leave your computer
|
| 99 |
+
5. **100% free** - No API calls, no costs
|
| 100 |
+
|
| 101 |
+
## ✨ What's Removed
|
| 102 |
+
|
| 103 |
+
- ❌ OpenAI integration (not recommended for students)
|
| 104 |
+
- ❌ API key configuration (no longer needed)
|
| 105 |
+
- ❌ Paid billing risk (completely eliminated)
|
| 106 |
+
- ❌ Unnecessary documentation files (cleaned up)
|
| 107 |
+
|
| 108 |
+
## 🎯 Next Steps
|
| 109 |
+
|
| 110 |
+
1. **Start the server:**
|
| 111 |
+
```bash
|
| 112 |
+
python server2.py
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
2. **Upload a PowerPoint file** through the Angular frontend
|
| 116 |
+
|
| 117 |
+
3. **Watch the console** for AI progress:
|
| 118 |
+
```
|
| 119 |
+
🤖 Using FREE local AI (BLIP) for slide 1
|
| 120 |
+
✅ AI generated alt text for Picture 1: '...'
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
4. **Download the remediated PowerPoint**
|
| 124 |
+
|
| 125 |
+
## 🐛 Troubleshooting
|
| 126 |
+
|
| 127 |
+
### "Module not found" errors
|
| 128 |
+
```bash
|
| 129 |
+
pip install -r requirements.txt
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
### First run taking forever
|
| 133 |
+
Normal! BLIP model is ~1-2GB. Wait 5-15 minutes. After download completes, subsequent runs are instant.
|
| 134 |
+
|
| 135 |
+
### Out of memory
|
| 136 |
+
Close other programs or use:
|
| 137 |
+
```bash
|
| 138 |
+
# In .env:
|
| 139 |
+
LOCAL_VISION_MODEL=blip-base
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
### Can't connect to server
|
| 143 |
+
Check that:
|
| 144 |
+
1. Server is running: `python server2.py`
|
| 145 |
+
2. Port 5000 is available
|
| 146 |
+
3. Firewall allows localhost:5000
|
| 147 |
+
|
| 148 |
+
## 📊 Package Versions Installed
|
| 149 |
+
|
| 150 |
+
- fastapi ≥ 0.100.0
|
| 151 |
+
- uvicorn ≥ 0.28.0
|
| 152 |
+
- lxml ≥ 5.0.0 (installed: 6.0.2)
|
| 153 |
+
- transformers ≥ 4.35.0 (installed: 5.3.0)
|
| 154 |
+
- torch ≥ 2.0.0 (installed: 2.10.0)
|
| 155 |
+
- python-docx ≥ 1.0.0
|
| 156 |
+
- pillow (Pillow) ≥ 10.0.0
|
| 157 |
+
- pywin32 ≥ 306
|
| 158 |
+
|
| 159 |
+
## 🎉 Ready to Go!
|
| 160 |
+
|
| 161 |
+
Everything is installed and ready. Your codebase is:
|
| 162 |
+
- ✅ Clean (unnecessary docs removed)
|
| 163 |
+
- ✅ Tested (packages verified importable)
|
| 164 |
+
- ✅ Free (100% local AI, $0 cost)
|
| 165 |
+
- ✅ Ready (just run `python server2.py`)
|
| 166 |
+
|
| 167 |
+
Start testing! 🚀
|
python-server/app.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Entry point for Hugging Face Spaces deployment.
|
| 4 |
+
This file launches the FastAPI application from server2.py
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from server2 import app
|
| 8 |
+
|
| 9 |
+
# The app variable is automatically detected by HF Spaces
|
| 10 |
+
# HF Spaces will run: uvicorn app:app --host 0.0.0.0 --port 7860
|
| 11 |
+
|
| 12 |
+
if __name__ == "__main__":
|
| 13 |
+
import uvicorn
|
| 14 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
python-server/color_contrast.py
ADDED
|
@@ -0,0 +1,752 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import colorsys
|
| 5 |
+
import posixpath
|
| 6 |
+
from collections import OrderedDict
|
| 7 |
+
from typing import Dict, List, Optional, Tuple
|
| 8 |
+
|
| 9 |
+
from lxml import etree
|
| 10 |
+
|
| 11 |
+
P_NS = "http://schemas.openxmlformats.org/presentationml/2006/main"
|
| 12 |
+
A_NS = "http://schemas.openxmlformats.org/drawingml/2006/main"
|
| 13 |
+
R_NS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
| 14 |
+
REL_NS = "http://schemas.openxmlformats.org/package/2006/relationships"
|
| 15 |
+
|
| 16 |
+
NS = {"p": P_NS, "a": A_NS, "r": R_NS}
|
| 17 |
+
RELATIONSHIP_NS = {"rel": REL_NS}
|
| 18 |
+
|
| 19 |
+
DEFAULT_COLOR_MAP = {
|
| 20 |
+
"bg1": "lt1",
|
| 21 |
+
"tx1": "dk1",
|
| 22 |
+
"bg2": "lt2",
|
| 23 |
+
"tx2": "dk2",
|
| 24 |
+
"accent1": "accent1",
|
| 25 |
+
"accent2": "accent2",
|
| 26 |
+
"accent3": "accent3",
|
| 27 |
+
"accent4": "accent4",
|
| 28 |
+
"accent5": "accent5",
|
| 29 |
+
"accent6": "accent6",
|
| 30 |
+
"hlink": "hlink",
|
| 31 |
+
"folHlink": "folHlink",
|
| 32 |
+
}
|
| 33 |
+
DEFAULT_THEME_COLORS = {
|
| 34 |
+
"dk1": "000000",
|
| 35 |
+
"lt1": "FFFFFF",
|
| 36 |
+
"dk2": "1F1F1F",
|
| 37 |
+
"lt2": "EEECE1",
|
| 38 |
+
"accent1": "4F81BD",
|
| 39 |
+
"accent2": "C0504D",
|
| 40 |
+
"accent3": "9BBB59",
|
| 41 |
+
"accent4": "8064A2",
|
| 42 |
+
"accent5": "4BACC6",
|
| 43 |
+
"accent6": "F79646",
|
| 44 |
+
"hlink": "0000FF",
|
| 45 |
+
"folHlink": "800080",
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _parser() -> etree.XMLParser:
|
| 50 |
+
return etree.XMLParser(remove_blank_text=False, recover=True)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def parse_xml_bytes(xml_bytes: bytes):
|
| 54 |
+
return etree.fromstring(xml_bytes, parser=_parser())
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _local_name(element) -> str:
|
| 58 |
+
return etree.QName(element).localname
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def hex_to_rgb(hex_value: str) -> Tuple[int, int, int]:
|
| 62 |
+
value = (hex_value or "").strip().replace("#", "")
|
| 63 |
+
if len(value) == 3:
|
| 64 |
+
value = "".join(ch * 2 for ch in value)
|
| 65 |
+
if len(value) != 6:
|
| 66 |
+
raise ValueError(f"Invalid hex color: {hex_value}")
|
| 67 |
+
return tuple(int(value[i:i + 2], 16) for i in (0, 2, 4))
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def rgb_to_hex(rgb: Tuple[int, int, int]) -> str:
|
| 71 |
+
return "{:02X}{:02X}{:02X}".format(*rgb)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def clamp_channel(value: float) -> int:
|
| 75 |
+
return max(0, min(255, int(round(value))))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def srgb_to_linear(channel: int) -> float:
|
| 79 |
+
c = channel / 255.0
|
| 80 |
+
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def relative_luminance(rgb: Tuple[int, int, int]) -> float:
|
| 84 |
+
r, g, b = (srgb_to_linear(c) for c in rgb)
|
| 85 |
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def contrast_ratio(fg: Tuple[int, int, int], bg: Tuple[int, int, int]) -> float:
|
| 89 |
+
l1 = relative_luminance(fg)
|
| 90 |
+
l2 = relative_luminance(bg)
|
| 91 |
+
lighter = max(l1, l2)
|
| 92 |
+
darker = min(l1, l2)
|
| 93 |
+
return (lighter + 0.05) / (darker + 0.05)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def is_large_text(font_size_pt: Optional[float], is_bold: bool) -> bool:
|
| 97 |
+
if font_size_pt is None:
|
| 98 |
+
return False
|
| 99 |
+
if is_bold and font_size_pt >= 14:
|
| 100 |
+
return True
|
| 101 |
+
return font_size_pt >= 18
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def required_contrast(font_size_pt: Optional[float], is_bold: bool) -> float:
|
| 105 |
+
return 3.0 if is_large_text(font_size_pt, is_bold) else 4.5
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def _join_zip_path(base_path: str, target: str) -> str:
|
| 109 |
+
if target.startswith("/"):
|
| 110 |
+
return target.lstrip("/")
|
| 111 |
+
base_dir = posixpath.dirname(base_path)
|
| 112 |
+
return posixpath.normpath(posixpath.join(base_dir, target))
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def _resolve_relationship_target(zip_ref, source_part: str, rels_path: str, rel_type_suffix: str) -> Optional[str]:
|
| 116 |
+
if rels_path not in zip_ref.namelist():
|
| 117 |
+
return None
|
| 118 |
+
root = parse_xml_bytes(zip_ref.read(rels_path))
|
| 119 |
+
for rel in root.findall("rel:Relationship", namespaces=RELATIONSHIP_NS):
|
| 120 |
+
rel_type = rel.get("Type", "")
|
| 121 |
+
if rel_type.endswith(rel_type_suffix):
|
| 122 |
+
target = rel.get("Target")
|
| 123 |
+
if target:
|
| 124 |
+
return _join_zip_path(source_part, target)
|
| 125 |
+
return None
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def _has_non_opaque_alpha(color_element) -> bool:
|
| 129 |
+
for child in color_element:
|
| 130 |
+
if _local_name(child) == "alpha":
|
| 131 |
+
try:
|
| 132 |
+
return int(child.get("val", "100000")) < 100000
|
| 133 |
+
except Exception:
|
| 134 |
+
return True
|
| 135 |
+
return False
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def _resolve_scheme_color_name(name: str, context: Dict) -> str:
|
| 139 |
+
mapped = context["color_map"].get(name, name)
|
| 140 |
+
return context["theme_colors"].get(mapped, context["theme_colors"].get(name, context["default_text"]))
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def resolve_color_from_color_element(color_element, context: Dict) -> Tuple[Optional[str], Optional[str]]:
|
| 144 |
+
if color_element is None:
|
| 145 |
+
return None, None
|
| 146 |
+
|
| 147 |
+
if _has_non_opaque_alpha(color_element):
|
| 148 |
+
return None, "transparentColor"
|
| 149 |
+
|
| 150 |
+
local = _local_name(color_element)
|
| 151 |
+
if local == "srgbClr":
|
| 152 |
+
return (color_element.get("val") or "").upper() or None, None
|
| 153 |
+
if local == "sysClr":
|
| 154 |
+
return (color_element.get("lastClr") or "").upper() or None, None
|
| 155 |
+
if local == "schemeClr":
|
| 156 |
+
val = color_element.get("val") or ""
|
| 157 |
+
return _resolve_scheme_color_name(val, context), None
|
| 158 |
+
if local == "prstClr":
|
| 159 |
+
preset = color_element.get("val", "").lower()
|
| 160 |
+
preset_map = {
|
| 161 |
+
"white": "FFFFFF",
|
| 162 |
+
"black": "000000",
|
| 163 |
+
"gray": "808080",
|
| 164 |
+
"grey": "808080",
|
| 165 |
+
"red": "FF0000",
|
| 166 |
+
"green": "008000",
|
| 167 |
+
"blue": "0000FF",
|
| 168 |
+
"yellow": "FFFF00",
|
| 169 |
+
}
|
| 170 |
+
return preset_map.get(preset), None
|
| 171 |
+
return None, "unresolvedColorElement"
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def resolve_color_from_fill_parent(parent, context: Dict) -> Tuple[Optional[str], Optional[str]]:
|
| 175 |
+
if parent is None:
|
| 176 |
+
return None, None
|
| 177 |
+
|
| 178 |
+
solid_fill = parent.find("a:solidFill", namespaces=NS)
|
| 179 |
+
if solid_fill is not None:
|
| 180 |
+
for child in solid_fill:
|
| 181 |
+
color, reason = resolve_color_from_color_element(child, context)
|
| 182 |
+
if color or reason:
|
| 183 |
+
return color, reason
|
| 184 |
+
return None, "unresolvedSolidFill"
|
| 185 |
+
|
| 186 |
+
if parent.find("a:blipFill", namespaces=NS) is not None:
|
| 187 |
+
return None, "imageFill"
|
| 188 |
+
if parent.find("a:gradFill", namespaces=NS) is not None:
|
| 189 |
+
return None, "gradientFill"
|
| 190 |
+
if parent.find("a:pattFill", namespaces=NS) is not None:
|
| 191 |
+
return None, "patternFill"
|
| 192 |
+
if parent.find("a:noFill", namespaces=NS) is not None:
|
| 193 |
+
return None, "transparentFill"
|
| 194 |
+
|
| 195 |
+
return None, None
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def _extract_background_from_root(root, context: Dict) -> Tuple[Optional[str], Optional[str]]:
|
| 199 |
+
bg_pr = root.find(".//p:cSld/p:bg/p:bgPr", namespaces=NS)
|
| 200 |
+
if bg_pr is not None:
|
| 201 |
+
color, reason = resolve_color_from_fill_parent(bg_pr, context)
|
| 202 |
+
if color or reason:
|
| 203 |
+
return color, reason
|
| 204 |
+
|
| 205 |
+
bg_ref = root.find(".//p:cSld/p:bg/p:bgRef", namespaces=NS)
|
| 206 |
+
if bg_ref is not None:
|
| 207 |
+
for child in bg_ref:
|
| 208 |
+
color, reason = resolve_color_from_color_element(child, context)
|
| 209 |
+
if color or reason:
|
| 210 |
+
return color, reason
|
| 211 |
+
return None, "backgroundReference"
|
| 212 |
+
|
| 213 |
+
return None, None
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def _build_slide_background_map(zip_ref, context: Dict) -> Dict[str, Dict[str, Optional[str]]]:
|
| 217 |
+
background_map: Dict[str, Dict[str, Optional[str]]] = {}
|
| 218 |
+
slide_paths = sorted(
|
| 219 |
+
[n for n in zip_ref.namelist() if n.startswith("ppt/slides/slide") and n.endswith(".xml")]
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
for slide_path in slide_paths:
|
| 223 |
+
slide_root = parse_xml_bytes(zip_ref.read(slide_path))
|
| 224 |
+
slide_color, slide_reason = _extract_background_from_root(slide_root, context)
|
| 225 |
+
if slide_color or slide_reason:
|
| 226 |
+
background_map[slide_path] = {"color": slide_color, "reason": slide_reason}
|
| 227 |
+
continue
|
| 228 |
+
|
| 229 |
+
rels_path = slide_path.replace("ppt/slides/", "ppt/slides/_rels/") + ".rels"
|
| 230 |
+
layout_path = _resolve_relationship_target(zip_ref, slide_path, rels_path, "/slideLayout")
|
| 231 |
+
layout_color = layout_reason = None
|
| 232 |
+
master_path = None
|
| 233 |
+
|
| 234 |
+
if layout_path and layout_path in zip_ref.namelist():
|
| 235 |
+
layout_root = parse_xml_bytes(zip_ref.read(layout_path))
|
| 236 |
+
layout_color, layout_reason = _extract_background_from_root(layout_root, context)
|
| 237 |
+
layout_rels_path = layout_path.replace("ppt/slideLayouts/", "ppt/slideLayouts/_rels/") + ".rels"
|
| 238 |
+
master_path = _resolve_relationship_target(zip_ref, layout_path, layout_rels_path, "/slideMaster")
|
| 239 |
+
|
| 240 |
+
master_color = master_reason = None
|
| 241 |
+
if master_path and master_path in zip_ref.namelist():
|
| 242 |
+
master_root = parse_xml_bytes(zip_ref.read(master_path))
|
| 243 |
+
master_color, master_reason = _extract_background_from_root(master_root, context)
|
| 244 |
+
|
| 245 |
+
final_color = slide_color or layout_color or master_color or "FFFFFF"
|
| 246 |
+
final_reason = slide_reason or layout_reason or master_reason
|
| 247 |
+
background_map[slide_path] = {"color": final_color, "reason": final_reason}
|
| 248 |
+
|
| 249 |
+
return background_map
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def build_pptx_color_context(zip_ref) -> Dict:
|
| 253 |
+
theme_colors = dict(DEFAULT_THEME_COLORS)
|
| 254 |
+
color_map = dict(DEFAULT_COLOR_MAP)
|
| 255 |
+
|
| 256 |
+
try:
|
| 257 |
+
if "ppt/theme/theme1.xml" in zip_ref.namelist():
|
| 258 |
+
root = parse_xml_bytes(zip_ref.read("ppt/theme/theme1.xml"))
|
| 259 |
+
clr_scheme = root.find(".//a:themeElements/a:clrScheme", namespaces=NS)
|
| 260 |
+
if clr_scheme is not None:
|
| 261 |
+
for child in clr_scheme:
|
| 262 |
+
local = etree.QName(child).localname
|
| 263 |
+
srgb = child.find("a:srgbClr", namespaces=NS)
|
| 264 |
+
sysclr = child.find("a:sysClr", namespaces=NS)
|
| 265 |
+
if srgb is not None and srgb.get("val"):
|
| 266 |
+
theme_colors[local] = srgb.get("val").upper()
|
| 267 |
+
elif sysclr is not None:
|
| 268 |
+
theme_colors[local] = (sysclr.get("lastClr") or "000000").upper()
|
| 269 |
+
except Exception:
|
| 270 |
+
pass
|
| 271 |
+
|
| 272 |
+
try:
|
| 273 |
+
masters = sorted(
|
| 274 |
+
[n for n in zip_ref.namelist() if n.startswith("ppt/slideMasters/slideMaster") and n.endswith(".xml")]
|
| 275 |
+
)
|
| 276 |
+
for master_name in masters[:1]:
|
| 277 |
+
root = parse_xml_bytes(zip_ref.read(master_name))
|
| 278 |
+
clr_map = root.find(".//p:clrMap", namespaces=NS)
|
| 279 |
+
if clr_map is not None:
|
| 280 |
+
for key in list(DEFAULT_COLOR_MAP.keys()):
|
| 281 |
+
if clr_map.get(key):
|
| 282 |
+
color_map[key] = clr_map.get(key)
|
| 283 |
+
except Exception:
|
| 284 |
+
pass
|
| 285 |
+
|
| 286 |
+
default_text_key = color_map.get("tx1", "dk1")
|
| 287 |
+
default_text = theme_colors.get(default_text_key, theme_colors.get("dk1", "000000"))
|
| 288 |
+
context = {
|
| 289 |
+
"theme_colors": theme_colors,
|
| 290 |
+
"color_map": color_map,
|
| 291 |
+
"default_text": default_text,
|
| 292 |
+
}
|
| 293 |
+
context["slide_backgrounds"] = _build_slide_background_map(zip_ref, context)
|
| 294 |
+
context["slide_path_map"] = {
|
| 295 |
+
int(path.split("slide")[-1].split(".xml")[0]): path
|
| 296 |
+
for path in context["slide_backgrounds"].keys()
|
| 297 |
+
if "slide" in path
|
| 298 |
+
}
|
| 299 |
+
return context
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def get_slide_background(slide_number: int, context: Dict) -> Tuple[Optional[str], Optional[str]]:
|
| 303 |
+
slide_path = context.get("slide_path_map", {}).get(slide_number)
|
| 304 |
+
info = context.get("slide_backgrounds", {}).get(slide_path or "", {})
|
| 305 |
+
return info.get("color", "FFFFFF"), info.get("reason")
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
def describe_shape(shape) -> Tuple[str, str]:
|
| 309 |
+
cnvpr = shape.find(".//p:cNvPr", namespaces=NS)
|
| 310 |
+
shape_id = cnvpr.get("id") if cnvpr is not None and cnvpr.get("id") else ""
|
| 311 |
+
shape_name = cnvpr.get("name") if cnvpr is not None and cnvpr.get("name") else ""
|
| 312 |
+
return shape_id, shape_name
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
def get_text_style(text_node, context: Dict) -> Tuple[Optional[str], Optional[float], bool, Optional[str], object]:
|
| 316 |
+
rpr = text_node.find("a:rPr", namespaces=NS)
|
| 317 |
+
if rpr is None:
|
| 318 |
+
rpr = text_node.find("a:fldPr", namespaces=NS)
|
| 319 |
+
|
| 320 |
+
font_size_pt: Optional[float] = None
|
| 321 |
+
is_bold = False
|
| 322 |
+
color_hex: Optional[str] = None
|
| 323 |
+
unresolved_reason: Optional[str] = None
|
| 324 |
+
|
| 325 |
+
if rpr is not None:
|
| 326 |
+
if rpr.get("sz"):
|
| 327 |
+
try:
|
| 328 |
+
font_size_pt = int(rpr.get("sz")) / 100.0
|
| 329 |
+
except Exception:
|
| 330 |
+
font_size_pt = None
|
| 331 |
+
is_bold = rpr.get("b") in {"1", "true", "True"}
|
| 332 |
+
color_hex, unresolved_reason = resolve_color_from_fill_parent(rpr, context)
|
| 333 |
+
|
| 334 |
+
if color_hex is None and unresolved_reason is None:
|
| 335 |
+
color_hex = context.get("default_text")
|
| 336 |
+
|
| 337 |
+
return color_hex, font_size_pt, is_bold, unresolved_reason, rpr
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def _iter_shape_ancestors(node):
|
| 341 |
+
current = node.getparent()
|
| 342 |
+
while current is not None:
|
| 343 |
+
yield current
|
| 344 |
+
current = current.getparent()
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
def get_shape_background(shape, slide_background_hex: Optional[str], slide_background_reason: Optional[str], context: Dict) -> Tuple[Optional[str], Optional[str]]:
|
| 348 |
+
sppr = shape.find("p:spPr", namespaces=NS)
|
| 349 |
+
if sppr is not None:
|
| 350 |
+
color, reason = resolve_color_from_fill_parent(sppr, context)
|
| 351 |
+
if color:
|
| 352 |
+
return color, None
|
| 353 |
+
if reason and reason not in {"transparentFill", None}:
|
| 354 |
+
return None, reason
|
| 355 |
+
if reason == "transparentFill":
|
| 356 |
+
# try ancestor groups first, then slide background
|
| 357 |
+
pass
|
| 358 |
+
|
| 359 |
+
for ancestor in _iter_shape_ancestors(shape):
|
| 360 |
+
if _local_name(ancestor) != "grpSp":
|
| 361 |
+
continue
|
| 362 |
+
grp_sppr = ancestor.find("p:grpSpPr", namespaces=NS)
|
| 363 |
+
if grp_sppr is not None:
|
| 364 |
+
color, reason = resolve_color_from_fill_parent(grp_sppr, context)
|
| 365 |
+
if color:
|
| 366 |
+
return color, None
|
| 367 |
+
if reason and reason not in {"transparentFill", None}:
|
| 368 |
+
return None, f"group{reason[:1].upper()}{reason[1:]}"
|
| 369 |
+
|
| 370 |
+
return slide_background_hex, slide_background_reason
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
def _collect_run_text(paragraph, node) -> str:
|
| 374 |
+
text_node = node.find("a:t", namespaces=NS)
|
| 375 |
+
text = text_node.text if text_node is not None else ""
|
| 376 |
+
return text if text and text.strip() else ""
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
def get_text_runs_for_shape(shape) -> List[Tuple[object, str, object]]:
|
| 380 |
+
results: List[Tuple[object, str, object]] = []
|
| 381 |
+
for paragraph in shape.findall(".//p:txBody/a:p", namespaces=NS):
|
| 382 |
+
for node in paragraph:
|
| 383 |
+
local = _local_name(node)
|
| 384 |
+
if local in {"r", "fld"}:
|
| 385 |
+
text = _collect_run_text(paragraph, node)
|
| 386 |
+
if text:
|
| 387 |
+
results.append((node, text, paragraph))
|
| 388 |
+
return results
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def get_text_runs_for_table_cell(cell) -> List[Tuple[object, str, object]]:
|
| 392 |
+
results: List[Tuple[object, str, object]] = []
|
| 393 |
+
for paragraph in cell.findall(".//a:txBody/a:p", namespaces=NS):
|
| 394 |
+
for node in paragraph:
|
| 395 |
+
local = _local_name(node)
|
| 396 |
+
if local in {"r", "fld"}:
|
| 397 |
+
text = _collect_run_text(paragraph, node)
|
| 398 |
+
if text:
|
| 399 |
+
results.append((node, text, paragraph))
|
| 400 |
+
return results
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def _manual_issue(
|
| 404 |
+
slide_number: int,
|
| 405 |
+
shape_id: str,
|
| 406 |
+
shape_name: str,
|
| 407 |
+
text: str,
|
| 408 |
+
reason: str,
|
| 409 |
+
) -> Dict:
|
| 410 |
+
return {
|
| 411 |
+
"slideNumber": slide_number,
|
| 412 |
+
"shapeId": shape_id,
|
| 413 |
+
"shapeName": shape_name,
|
| 414 |
+
"text": text[:160],
|
| 415 |
+
"issue": "Manual review required for color contrast",
|
| 416 |
+
"type": "colorContrastManualReview",
|
| 417 |
+
"reason": reason,
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
def _merge_issue_entries(items: List[Dict]) -> List[Dict]:
|
| 422 |
+
merged: "OrderedDict[Tuple, Dict]" = OrderedDict()
|
| 423 |
+
for item in items:
|
| 424 |
+
if item.get("type") == "colorContrast":
|
| 425 |
+
key = (
|
| 426 |
+
item.get("slideNumber"),
|
| 427 |
+
item.get("shapeId"),
|
| 428 |
+
item.get("type"),
|
| 429 |
+
item.get("foregroundColor"),
|
| 430 |
+
item.get("backgroundColor"),
|
| 431 |
+
item.get("requiredRatio"),
|
| 432 |
+
item.get("fontSizePt"),
|
| 433 |
+
item.get("isBold"),
|
| 434 |
+
)
|
| 435 |
+
elif item.get("type") == "colorContrastManualReview":
|
| 436 |
+
key = (
|
| 437 |
+
item.get("slideNumber"),
|
| 438 |
+
item.get("shapeId"),
|
| 439 |
+
item.get("type"),
|
| 440 |
+
item.get("reason"),
|
| 441 |
+
)
|
| 442 |
+
else:
|
| 443 |
+
key = tuple(sorted(item.items()))
|
| 444 |
+
|
| 445 |
+
if key not in merged:
|
| 446 |
+
merged[key] = dict(item)
|
| 447 |
+
continue
|
| 448 |
+
|
| 449 |
+
existing_text = merged[key].get("text", "")
|
| 450 |
+
new_text = item.get("text", "")
|
| 451 |
+
if new_text and new_text not in existing_text:
|
| 452 |
+
merged[key]["text"] = (existing_text + " " + new_text).strip()[:160]
|
| 453 |
+
return list(merged.values())
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
def _merge_fix_entries(items: List[Dict]) -> List[Dict]:
|
| 457 |
+
merged: "OrderedDict[Tuple, Dict]" = OrderedDict()
|
| 458 |
+
for item in items:
|
| 459 |
+
key = (
|
| 460 |
+
item.get("slideNumber"),
|
| 461 |
+
item.get("shapeId"),
|
| 462 |
+
item.get("fix"),
|
| 463 |
+
item.get("beforeColor"),
|
| 464 |
+
item.get("afterColor"),
|
| 465 |
+
item.get("backgroundColor"),
|
| 466 |
+
item.get("requiredRatio"),
|
| 467 |
+
item.get("fontSizePt"),
|
| 468 |
+
item.get("isBold"),
|
| 469 |
+
)
|
| 470 |
+
if key not in merged:
|
| 471 |
+
merged[key] = dict(item)
|
| 472 |
+
continue
|
| 473 |
+
existing_text = merged[key].get("text", "")
|
| 474 |
+
new_text = item.get("text", "")
|
| 475 |
+
if new_text and new_text not in existing_text:
|
| 476 |
+
merged[key]["text"] = (existing_text + " " + new_text).strip()[:160]
|
| 477 |
+
return list(merged.values())
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
def _adjust_lightness(rgb: Tuple[int, int, int], new_l: float) -> Tuple[int, int, int]:
|
| 481 |
+
r, g, b = (c / 255.0 for c in rgb)
|
| 482 |
+
h, l, s = colorsys.rgb_to_hls(r, g, b)
|
| 483 |
+
nr, ng, nb = colorsys.hls_to_rgb(h, max(0.0, min(1.0, new_l)), s)
|
| 484 |
+
return (clamp_channel(nr * 255), clamp_channel(ng * 255), clamp_channel(nb * 255))
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
def choose_accessible_text_color(
|
| 488 |
+
foreground_rgb: Tuple[int, int, int],
|
| 489 |
+
background_rgb: Tuple[int, int, int],
|
| 490 |
+
required_ratio_value: float,
|
| 491 |
+
) -> Optional[Tuple[int, int, int]]:
|
| 492 |
+
current_ratio = contrast_ratio(foreground_rgb, background_rgb)
|
| 493 |
+
if current_ratio >= required_ratio_value:
|
| 494 |
+
return foreground_rgb
|
| 495 |
+
|
| 496 |
+
r, g, b = (c / 255.0 for c in foreground_rgb)
|
| 497 |
+
_, lightness, _ = colorsys.rgb_to_hls(r, g, b)
|
| 498 |
+
|
| 499 |
+
def search(direction: str) -> Optional[Tuple[float, Tuple[int, int, int]]]:
|
| 500 |
+
low, high = (0.0, lightness) if direction == "darken" else (lightness, 1.0)
|
| 501 |
+
candidate = None
|
| 502 |
+
for _ in range(24):
|
| 503 |
+
mid = (low + high) / 2.0
|
| 504 |
+
test_rgb = _adjust_lightness(foreground_rgb, mid)
|
| 505 |
+
ratio_value = contrast_ratio(test_rgb, background_rgb)
|
| 506 |
+
if ratio_value >= required_ratio_value:
|
| 507 |
+
candidate = (mid, test_rgb)
|
| 508 |
+
if direction == "darken":
|
| 509 |
+
low = mid
|
| 510 |
+
else:
|
| 511 |
+
high = mid
|
| 512 |
+
else:
|
| 513 |
+
if direction == "darken":
|
| 514 |
+
high = mid
|
| 515 |
+
else:
|
| 516 |
+
low = mid
|
| 517 |
+
return candidate
|
| 518 |
+
|
| 519 |
+
candidates = []
|
| 520 |
+
for direction in ("darken", "lighten"):
|
| 521 |
+
result = search(direction)
|
| 522 |
+
if result is not None:
|
| 523 |
+
new_l, new_rgb = result
|
| 524 |
+
candidates.append((abs(new_l - lightness), new_rgb))
|
| 525 |
+
|
| 526 |
+
if not candidates:
|
| 527 |
+
black_ratio = contrast_ratio((0, 0, 0), background_rgb)
|
| 528 |
+
white_ratio = contrast_ratio((255, 255, 255), background_rgb)
|
| 529 |
+
if black_ratio >= required_ratio_value or white_ratio >= required_ratio_value:
|
| 530 |
+
return (0, 0, 0) if black_ratio >= white_ratio else (255, 255, 255)
|
| 531 |
+
return None
|
| 532 |
+
|
| 533 |
+
candidates.sort(key=lambda item: item[0])
|
| 534 |
+
return candidates[0][1]
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
def _set_text_color(text_node, new_hex: str):
|
| 538 |
+
rpr = text_node.find("a:rPr", namespaces=NS)
|
| 539 |
+
if rpr is None:
|
| 540 |
+
rpr = etree.Element(f"{{{A_NS}}}rPr")
|
| 541 |
+
text_node.insert(0, rpr)
|
| 542 |
+
|
| 543 |
+
for child in list(rpr):
|
| 544 |
+
if _local_name(child) in {"solidFill", "gradFill", "blipFill", "pattFill", "noFill"}:
|
| 545 |
+
rpr.remove(child)
|
| 546 |
+
|
| 547 |
+
solid_fill = etree.Element(f"{{{A_NS}}}solidFill")
|
| 548 |
+
srgb = etree.Element(f"{{{A_NS}}}srgbClr")
|
| 549 |
+
srgb.set("val", new_hex.upper())
|
| 550 |
+
solid_fill.append(srgb)
|
| 551 |
+
rpr.insert(0, solid_fill)
|
| 552 |
+
|
| 553 |
+
|
| 554 |
+
def _analyze_runs(
|
| 555 |
+
run_records: List[Tuple[object, str, object]],
|
| 556 |
+
slide_number: int,
|
| 557 |
+
shape_id: str,
|
| 558 |
+
shape_name: str,
|
| 559 |
+
background_hex: Optional[str],
|
| 560 |
+
background_reason: Optional[str],
|
| 561 |
+
context: Dict,
|
| 562 |
+
) -> List[Dict]:
|
| 563 |
+
issues: List[Dict] = []
|
| 564 |
+
if background_hex is None:
|
| 565 |
+
preview = " ".join(text for _, text, _ in run_records)[:160]
|
| 566 |
+
if preview:
|
| 567 |
+
issues.append(_manual_issue(slide_number, shape_id, shape_name, preview, background_reason or "unresolvedBackground"))
|
| 568 |
+
return issues
|
| 569 |
+
|
| 570 |
+
background_rgb = hex_to_rgb(background_hex)
|
| 571 |
+
for text_node, text, _ in run_records:
|
| 572 |
+
foreground_hex, font_size_pt, is_bold, color_reason, _ = get_text_style(text_node, context)
|
| 573 |
+
if foreground_hex is None:
|
| 574 |
+
issues.append(_manual_issue(slide_number, shape_id, shape_name, text, color_reason or "unresolvedTextColor"))
|
| 575 |
+
continue
|
| 576 |
+
|
| 577 |
+
foreground_rgb = hex_to_rgb(foreground_hex)
|
| 578 |
+
needed = required_contrast(font_size_pt, is_bold)
|
| 579 |
+
ratio_value = contrast_ratio(foreground_rgb, background_rgb)
|
| 580 |
+
if ratio_value < needed:
|
| 581 |
+
issues.append({
|
| 582 |
+
"slideNumber": slide_number,
|
| 583 |
+
"shapeId": shape_id,
|
| 584 |
+
"shapeName": shape_name,
|
| 585 |
+
"text": text[:160],
|
| 586 |
+
"issue": "Insufficient color contrast",
|
| 587 |
+
"type": "colorContrast",
|
| 588 |
+
"foregroundColor": f"#{foreground_hex.upper()}",
|
| 589 |
+
"backgroundColor": f"#{background_hex.upper()}",
|
| 590 |
+
"contrastRatio": round(ratio_value, 2),
|
| 591 |
+
"requiredRatio": needed,
|
| 592 |
+
"fontSizePt": round(font_size_pt, 2) if font_size_pt is not None else None,
|
| 593 |
+
"isBold": is_bold,
|
| 594 |
+
})
|
| 595 |
+
return issues
|
| 596 |
+
|
| 597 |
+
|
| 598 |
+
def _remediate_runs(
|
| 599 |
+
run_records: List[Tuple[object, str, object]],
|
| 600 |
+
slide_number: int,
|
| 601 |
+
shape_id: str,
|
| 602 |
+
shape_name: str,
|
| 603 |
+
background_hex: Optional[str],
|
| 604 |
+
background_reason: Optional[str],
|
| 605 |
+
context: Dict,
|
| 606 |
+
) -> Tuple[int, List[Dict]]:
|
| 607 |
+
fixed = 0
|
| 608 |
+
fix_details: List[Dict] = []
|
| 609 |
+
if background_hex is None:
|
| 610 |
+
return fixed, fix_details
|
| 611 |
+
|
| 612 |
+
background_rgb = hex_to_rgb(background_hex)
|
| 613 |
+
for text_node, text, _ in run_records:
|
| 614 |
+
foreground_hex, font_size_pt, is_bold, color_reason, _ = get_text_style(text_node, context)
|
| 615 |
+
if foreground_hex is None:
|
| 616 |
+
continue
|
| 617 |
+
|
| 618 |
+
foreground_rgb = hex_to_rgb(foreground_hex)
|
| 619 |
+
needed = required_contrast(font_size_pt, is_bold)
|
| 620 |
+
before_ratio = contrast_ratio(foreground_rgb, background_rgb)
|
| 621 |
+
if before_ratio >= needed:
|
| 622 |
+
continue
|
| 623 |
+
|
| 624 |
+
new_rgb = choose_accessible_text_color(foreground_rgb, background_rgb, needed)
|
| 625 |
+
if new_rgb is None:
|
| 626 |
+
continue
|
| 627 |
+
|
| 628 |
+
new_hex = rgb_to_hex(new_rgb)
|
| 629 |
+
if new_hex.upper() == foreground_hex.upper():
|
| 630 |
+
continue
|
| 631 |
+
|
| 632 |
+
after_ratio = contrast_ratio(new_rgb, background_rgb)
|
| 633 |
+
_set_text_color(text_node, new_hex)
|
| 634 |
+
fixed += 1
|
| 635 |
+
fix_details.append({
|
| 636 |
+
"slideNumber": slide_number,
|
| 637 |
+
"shapeId": shape_id,
|
| 638 |
+
"shapeName": shape_name,
|
| 639 |
+
"text": text[:160],
|
| 640 |
+
"fix": "adjustedTextColorForContrast",
|
| 641 |
+
"beforeColor": f"#{foreground_hex.upper()}",
|
| 642 |
+
"afterColor": f"#{new_hex.upper()}",
|
| 643 |
+
"backgroundColor": f"#{background_hex.upper()}",
|
| 644 |
+
"beforeContrastRatio": round(before_ratio, 2),
|
| 645 |
+
"afterContrastRatio": round(after_ratio, 2),
|
| 646 |
+
"requiredRatio": needed,
|
| 647 |
+
"fontSizePt": round(font_size_pt, 2) if font_size_pt is not None else None,
|
| 648 |
+
"isBold": is_bold,
|
| 649 |
+
})
|
| 650 |
+
return fixed, fix_details
|
| 651 |
+
|
| 652 |
+
|
| 653 |
+
def check_slide_color_contrast(slide_xml_bytes: bytes, slide_number: int, context: Dict) -> List[Dict]:
|
| 654 |
+
root = parse_xml_bytes(slide_xml_bytes)
|
| 655 |
+
slide_background_hex, slide_background_reason = get_slide_background(slide_number, context)
|
| 656 |
+
issues: List[Dict] = []
|
| 657 |
+
|
| 658 |
+
for shape in root.xpath(".//p:sp[p:txBody]", namespaces=NS):
|
| 659 |
+
shape_id, shape_name = describe_shape(shape)
|
| 660 |
+
shape_background_hex, shape_background_reason = get_shape_background(
|
| 661 |
+
shape,
|
| 662 |
+
slide_background_hex,
|
| 663 |
+
slide_background_reason,
|
| 664 |
+
context,
|
| 665 |
+
)
|
| 666 |
+
issues.extend(
|
| 667 |
+
_analyze_runs(
|
| 668 |
+
get_text_runs_for_shape(shape),
|
| 669 |
+
slide_number,
|
| 670 |
+
shape_id,
|
| 671 |
+
shape_name,
|
| 672 |
+
shape_background_hex,
|
| 673 |
+
shape_background_reason,
|
| 674 |
+
context,
|
| 675 |
+
)
|
| 676 |
+
)
|
| 677 |
+
|
| 678 |
+
for frame in root.xpath(".//p:graphicFrame[a:graphic/a:graphicData/a:tbl]", namespaces=NS):
|
| 679 |
+
shape_id, shape_name = describe_shape(frame)
|
| 680 |
+
tbl = frame.find(".//a:tbl", namespaces=NS)
|
| 681 |
+
if tbl is None:
|
| 682 |
+
continue
|
| 683 |
+
for idx, cell in enumerate(tbl.findall(".//a:tr/a:tc", namespaces=NS), start=1):
|
| 684 |
+
tc_pr = cell.find("a:tcPr", namespaces=NS)
|
| 685 |
+
cell_color, cell_reason = resolve_color_from_fill_parent(tc_pr, context) if tc_pr is not None else (None, None)
|
| 686 |
+
if cell_reason == "transparentFill" or (cell_color is None and cell_reason is None):
|
| 687 |
+
cell_color, cell_reason = slide_background_hex, slide_background_reason
|
| 688 |
+
issues.extend(
|
| 689 |
+
_analyze_runs(
|
| 690 |
+
get_text_runs_for_table_cell(cell),
|
| 691 |
+
slide_number,
|
| 692 |
+
shape_id,
|
| 693 |
+
f"{shape_name} cell {idx}",
|
| 694 |
+
cell_color,
|
| 695 |
+
cell_reason,
|
| 696 |
+
context,
|
| 697 |
+
)
|
| 698 |
+
)
|
| 699 |
+
|
| 700 |
+
return _merge_issue_entries(issues)
|
| 701 |
+
|
| 702 |
+
|
| 703 |
+
def remediate_slide_color_contrast(slide_xml_bytes: bytes, slide_number: int, context: Dict):
|
| 704 |
+
root = parse_xml_bytes(slide_xml_bytes)
|
| 705 |
+
slide_background_hex, slide_background_reason = get_slide_background(slide_number, context)
|
| 706 |
+
fixed_total = 0
|
| 707 |
+
fix_details: List[Dict] = []
|
| 708 |
+
|
| 709 |
+
for shape in root.xpath(".//p:sp[p:txBody]", namespaces=NS):
|
| 710 |
+
shape_id, shape_name = describe_shape(shape)
|
| 711 |
+
shape_background_hex, shape_background_reason = get_shape_background(
|
| 712 |
+
shape,
|
| 713 |
+
slide_background_hex,
|
| 714 |
+
slide_background_reason,
|
| 715 |
+
context,
|
| 716 |
+
)
|
| 717 |
+
fixed, details = _remediate_runs(
|
| 718 |
+
get_text_runs_for_shape(shape),
|
| 719 |
+
slide_number,
|
| 720 |
+
shape_id,
|
| 721 |
+
shape_name,
|
| 722 |
+
shape_background_hex,
|
| 723 |
+
shape_background_reason,
|
| 724 |
+
context,
|
| 725 |
+
)
|
| 726 |
+
fixed_total += fixed
|
| 727 |
+
fix_details.extend(details)
|
| 728 |
+
|
| 729 |
+
for frame in root.xpath(".//p:graphicFrame[a:graphic/a:graphicData/a:tbl]", namespaces=NS):
|
| 730 |
+
shape_id, shape_name = describe_shape(frame)
|
| 731 |
+
tbl = frame.find(".//a:tbl", namespaces=NS)
|
| 732 |
+
if tbl is None:
|
| 733 |
+
continue
|
| 734 |
+
for idx, cell in enumerate(tbl.findall(".//a:tr/a:tc", namespaces=NS), start=1):
|
| 735 |
+
tc_pr = cell.find("a:tcPr", namespaces=NS)
|
| 736 |
+
cell_color, cell_reason = resolve_color_from_fill_parent(tc_pr, context) if tc_pr is not None else (None, None)
|
| 737 |
+
if cell_reason == "transparentFill" or (cell_color is None and cell_reason is None):
|
| 738 |
+
cell_color, cell_reason = slide_background_hex, slide_background_reason
|
| 739 |
+
fixed, details = _remediate_runs(
|
| 740 |
+
get_text_runs_for_table_cell(cell),
|
| 741 |
+
slide_number,
|
| 742 |
+
shape_id,
|
| 743 |
+
f"{shape_name} cell {idx}",
|
| 744 |
+
cell_color,
|
| 745 |
+
cell_reason,
|
| 746 |
+
context,
|
| 747 |
+
)
|
| 748 |
+
fixed_total += fixed
|
| 749 |
+
fix_details.extend(details)
|
| 750 |
+
|
| 751 |
+
new_bytes = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone=None)
|
| 752 |
+
return new_bytes, fixed_total, _merge_fix_entries(fix_details)
|
python-server/last_report.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fileName": "6-presentation-bottomrow.pptx",
|
| 3 |
+
"suggestedFileName": "6-presentation-bottomrow.pptx",
|
| 4 |
+
"report": {
|
| 5 |
+
"fileName": "6-presentation-bottomrow.pptx",
|
| 6 |
+
"suggestedFileName": "6-presentation-bottomrow.pptx",
|
| 7 |
+
"summary": {
|
| 8 |
+
"fixed": 0,
|
| 9 |
+
"flagged": 6
|
| 10 |
+
},
|
| 11 |
+
"details": {
|
| 12 |
+
"titleNeedsFixing": false,
|
| 13 |
+
"slidesMissingTitles": [
|
| 14 |
+
{
|
| 15 |
+
"missing": true,
|
| 16 |
+
"slideNumber": 1,
|
| 17 |
+
"message": "Slide 1 is missing a title"
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"missing": true,
|
| 21 |
+
"slideNumber": 2,
|
| 22 |
+
"message": "Slide 2 is missing a title"
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"missing": true,
|
| 26 |
+
"slideNumber": 3,
|
| 27 |
+
"message": "Slide 3 is missing a title"
|
| 28 |
+
}
|
| 29 |
+
],
|
| 30 |
+
"imagesMissingOrBadAlt": [
|
| 31 |
+
{
|
| 32 |
+
"slideNumber": 1,
|
| 33 |
+
"location": "Slide 1",
|
| 34 |
+
"issue": "Image missing alt text",
|
| 35 |
+
"type": "image"
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"slideNumber": 2,
|
| 39 |
+
"location": "Slide 2",
|
| 40 |
+
"issue": "Image missing alt text",
|
| 41 |
+
"type": "image"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"slideNumber": 3,
|
| 45 |
+
"location": "Slide 3",
|
| 46 |
+
"issue": "Image missing alt text",
|
| 47 |
+
"type": "image"
|
| 48 |
+
}
|
| 49 |
+
],
|
| 50 |
+
"gifsDetected": [],
|
| 51 |
+
"fileNameNeedsFixing": false,
|
| 52 |
+
"hiddenSlidesDetected": [],
|
| 53 |
+
"listFormattingIssues": []
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
}
|
python-server/local_vision.py
ADDED
|
@@ -0,0 +1,377 @@
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Local AI Vision Models for Alt Text Generation (100% FREE)
|
| 3 |
+
Uses Hugging Face transformers to run models locally - no API costs!
|
| 4 |
+
|
| 5 |
+
Supported models:
|
| 6 |
+
- BLIP: Good balance of speed and quality
|
| 7 |
+
- GIT: More detailed descriptions
|
| 8 |
+
- LLAVA: Most advanced (requires more resources)
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
from typing import Optional
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
import io
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
from PIL import Image
|
| 18 |
+
PIL_AVAILABLE = True
|
| 19 |
+
except ImportError:
|
| 20 |
+
PIL_AVAILABLE = False
|
| 21 |
+
print("⚠️ Pillow not installed. Run: pip install pillow")
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 25 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 26 |
+
import torch
|
| 27 |
+
TRANSFORMERS_AVAILABLE = True
|
| 28 |
+
except ImportError:
|
| 29 |
+
TRANSFORMERS_AVAILABLE = False
|
| 30 |
+
print("⚠️ Transformers not installed. Run: pip install transformers torch")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class LocalVisionModel:
|
| 34 |
+
"""
|
| 35 |
+
Local AI model for generating image descriptions
|
| 36 |
+
Runs on your computer - 100% FREE with no API limits!
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
def __init__(self, model_name: str = "blip-base"):
|
| 40 |
+
"""
|
| 41 |
+
Initialize local vision model
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
model_name: Model to use
|
| 45 |
+
- "blip-base" (default): Fast, good quality, ~1GB
|
| 46 |
+
- "blip-large": Better quality, slower, ~2GB
|
| 47 |
+
- "git-base": Alternative model, ~1.5GB
|
| 48 |
+
"""
|
| 49 |
+
self.model_name = model_name
|
| 50 |
+
self.enabled = False
|
| 51 |
+
self.model = None
|
| 52 |
+
self.processor = None
|
| 53 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 54 |
+
|
| 55 |
+
if not TRANSFORMERS_AVAILABLE:
|
| 56 |
+
print("❌ Transformers library not available")
|
| 57 |
+
print(" Install with: pip install transformers torch")
|
| 58 |
+
return
|
| 59 |
+
|
| 60 |
+
if not PIL_AVAILABLE:
|
| 61 |
+
print("❌ Pillow not available")
|
| 62 |
+
print(" Install with: pip install pillow")
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
# Load model
|
| 66 |
+
try:
|
| 67 |
+
print(f"📥 Loading {model_name} model... (this may take a minute on first run)")
|
| 68 |
+
|
| 69 |
+
if "blip" in model_name.lower():
|
| 70 |
+
self._load_blip_model(model_name)
|
| 71 |
+
elif "git" in model_name.lower():
|
| 72 |
+
self._load_git_model()
|
| 73 |
+
else:
|
| 74 |
+
print(f"⚠️ Unknown model: {model_name}, defaulting to BLIP")
|
| 75 |
+
self._load_blip_model("blip-base")
|
| 76 |
+
|
| 77 |
+
self.enabled = True
|
| 78 |
+
print(f"✅ {model_name} model loaded successfully on {self.device}")
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"❌ Failed to load model: {e}")
|
| 82 |
+
self.enabled = False
|
| 83 |
+
|
| 84 |
+
def _load_blip_model(self, model_name: str):
|
| 85 |
+
"""Load BLIP model (recommended for most use cases)"""
|
| 86 |
+
if "large" in model_name:
|
| 87 |
+
model_id = "Salesforce/blip-image-captioning-large"
|
| 88 |
+
else:
|
| 89 |
+
model_id = "Salesforce/blip-image-captioning-base"
|
| 90 |
+
|
| 91 |
+
self.processor = BlipProcessor.from_pretrained(model_id)
|
| 92 |
+
self.model = BlipForConditionalGeneration.from_pretrained(model_id)
|
| 93 |
+
self.model.to(self.device)
|
| 94 |
+
self.model_type = "blip"
|
| 95 |
+
|
| 96 |
+
def _load_git_model(self):
|
| 97 |
+
"""Load GIT model (alternative to BLIP)"""
|
| 98 |
+
model_id = "microsoft/git-base"
|
| 99 |
+
self.processor = AutoProcessor.from_pretrained(model_id)
|
| 100 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 101 |
+
self.model.to(self.device)
|
| 102 |
+
self.model_type = "git"
|
| 103 |
+
|
| 104 |
+
def is_enabled(self) -> bool:
|
| 105 |
+
"""Check if model is loaded and ready"""
|
| 106 |
+
return self.enabled and self.model is not None
|
| 107 |
+
|
| 108 |
+
def generate_alt_text(
|
| 109 |
+
self,
|
| 110 |
+
image_data: bytes,
|
| 111 |
+
shape_name: str = "",
|
| 112 |
+
slide_number: int = 0,
|
| 113 |
+
max_length: int = 250
|
| 114 |
+
) -> Optional[str]:
|
| 115 |
+
"""
|
| 116 |
+
Generate alt text for an image using local AI
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
image_data: Raw image bytes
|
| 120 |
+
shape_name: Shape name (for context)
|
| 121 |
+
slide_number: Slide number (for context)
|
| 122 |
+
max_length: Maximum alt text length
|
| 123 |
+
|
| 124 |
+
Returns:
|
| 125 |
+
Generated alt text or None if failed
|
| 126 |
+
"""
|
| 127 |
+
if not self.is_enabled():
|
| 128 |
+
return None
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
# Convert bytes to PIL Image
|
| 132 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 133 |
+
|
| 134 |
+
# Check if image looks decorative (very small, likely a logo/icon)
|
| 135 |
+
if image.size[0] < 100 and image.size[1] < 100:
|
| 136 |
+
# Small image - likely decorative
|
| 137 |
+
if any(hint in shape_name.lower() for hint in ["logo", "icon", "background", "border"]):
|
| 138 |
+
return "decorative"
|
| 139 |
+
|
| 140 |
+
# Generate description
|
| 141 |
+
if self.model_type == "blip":
|
| 142 |
+
alt_text = self._generate_blip(image)
|
| 143 |
+
elif self.model_type == "git":
|
| 144 |
+
alt_text = self._generate_git(image)
|
| 145 |
+
else:
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
# Clean up the text
|
| 149 |
+
alt_text = self._clean_alt_text(alt_text, max_length)
|
| 150 |
+
|
| 151 |
+
return alt_text
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f"Error generating alt text: {e}")
|
| 155 |
+
return None
|
| 156 |
+
|
| 157 |
+
def _generate_blip(self, image: Image.Image) -> str:
|
| 158 |
+
"""Generate caption using BLIP model"""
|
| 159 |
+
# Process image
|
| 160 |
+
inputs = self.processor(image, return_tensors="pt").to(self.device)
|
| 161 |
+
|
| 162 |
+
# Generate caption
|
| 163 |
+
with torch.no_grad():
|
| 164 |
+
out = self.model.generate(
|
| 165 |
+
**inputs,
|
| 166 |
+
max_length=50,
|
| 167 |
+
num_beams=5, # Better quality with beam search
|
| 168 |
+
early_stopping=True
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
caption = self.processor.decode(out[0], skip_special_tokens=True)
|
| 172 |
+
return caption
|
| 173 |
+
|
| 174 |
+
def _generate_git(self, image: Image.Image) -> str:
|
| 175 |
+
"""Generate caption using GIT model"""
|
| 176 |
+
# Process image
|
| 177 |
+
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 178 |
+
|
| 179 |
+
# Generate caption
|
| 180 |
+
with torch.no_grad():
|
| 181 |
+
generated_ids = self.model.generate(
|
| 182 |
+
pixel_values=inputs.pixel_values,
|
| 183 |
+
max_length=50
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
caption = self.processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 187 |
+
return caption
|
| 188 |
+
|
| 189 |
+
def _clean_alt_text(self, alt_text: str, max_length: int) -> str:
|
| 190 |
+
"""Clean and format generated alt text"""
|
| 191 |
+
# Remove common prefixes that BLIP adds
|
| 192 |
+
prefixes_to_remove = [
|
| 193 |
+
"a picture of ",
|
| 194 |
+
"an image of ",
|
| 195 |
+
"a photo of ",
|
| 196 |
+
"there is ",
|
| 197 |
+
"arafed ", # Common BLIP artifact
|
| 198 |
+
]
|
| 199 |
+
|
| 200 |
+
alt_text_lower = alt_text.lower()
|
| 201 |
+
for prefix in prefixes_to_remove:
|
| 202 |
+
if alt_text_lower.startswith(prefix):
|
| 203 |
+
alt_text = alt_text[len(prefix):]
|
| 204 |
+
break
|
| 205 |
+
|
| 206 |
+
# Capitalize first letter
|
| 207 |
+
if alt_text:
|
| 208 |
+
alt_text = alt_text[0].upper() + alt_text[1:]
|
| 209 |
+
|
| 210 |
+
# Truncate if needed
|
| 211 |
+
if len(alt_text) > max_length:
|
| 212 |
+
alt_text = alt_text[:max_length-3] + "..."
|
| 213 |
+
|
| 214 |
+
return alt_text.strip()
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
class HuggingFaceInferenceAPI:
|
| 218 |
+
"""
|
| 219 |
+
Hugging Face Inference API (FREE tier available)
|
| 220 |
+
Falls back to this if local models don't work
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
def __init__(self, api_token: Optional[str] = None):
|
| 224 |
+
"""
|
| 225 |
+
Initialize Hugging Face Inference API
|
| 226 |
+
|
| 227 |
+
Args:
|
| 228 |
+
api_token: HF token (if None, reads from HF_TOKEN env var)
|
| 229 |
+
Get free token at: https://huggingface.co/settings/tokens
|
| 230 |
+
"""
|
| 231 |
+
self.api_token = api_token or os.getenv("HF_TOKEN")
|
| 232 |
+
self.enabled = False
|
| 233 |
+
|
| 234 |
+
if not self.api_token:
|
| 235 |
+
print("⚠️ No Hugging Face token found. Set HF_TOKEN environment variable.")
|
| 236 |
+
print(" Get free token at: https://huggingface.co/settings/tokens")
|
| 237 |
+
return
|
| 238 |
+
|
| 239 |
+
try:
|
| 240 |
+
import requests
|
| 241 |
+
self.requests = requests
|
| 242 |
+
self.enabled = True
|
| 243 |
+
self.api_url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base"
|
| 244 |
+
print("✅ Hugging Face Inference API initialized")
|
| 245 |
+
except ImportError:
|
| 246 |
+
print("❌ 'requests' library not available. Run: pip install requests")
|
| 247 |
+
|
| 248 |
+
def is_enabled(self) -> bool:
|
| 249 |
+
"""Check if API is ready"""
|
| 250 |
+
return self.enabled and self.api_token is not None
|
| 251 |
+
|
| 252 |
+
def generate_alt_text(
|
| 253 |
+
self,
|
| 254 |
+
image_data: bytes,
|
| 255 |
+
shape_name: str = "",
|
| 256 |
+
slide_number: int = 0,
|
| 257 |
+
max_length: int = 250
|
| 258 |
+
) -> Optional[str]:
|
| 259 |
+
"""
|
| 260 |
+
Generate alt text using Hugging Face Inference API
|
| 261 |
+
|
| 262 |
+
Args:
|
| 263 |
+
image_data: Raw image bytes
|
| 264 |
+
shape_name: Shape name
|
| 265 |
+
slide_number: Slide number
|
| 266 |
+
max_length: Maximum length
|
| 267 |
+
|
| 268 |
+
Returns:
|
| 269 |
+
Generated alt text or None
|
| 270 |
+
"""
|
| 271 |
+
if not self.is_enabled():
|
| 272 |
+
return None
|
| 273 |
+
|
| 274 |
+
try:
|
| 275 |
+
headers = {"Authorization": f"Bearer {self.api_token}"}
|
| 276 |
+
response = self.requests.post(
|
| 277 |
+
self.api_url,
|
| 278 |
+
headers=headers,
|
| 279 |
+
data=image_data,
|
| 280 |
+
timeout=30
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
if response.status_code == 200:
|
| 284 |
+
result = response.json()
|
| 285 |
+
if isinstance(result, list) and len(result) > 0:
|
| 286 |
+
caption = result[0].get("generated_text", "")
|
| 287 |
+
return self._clean_alt_text(caption, max_length)
|
| 288 |
+
else:
|
| 289 |
+
print(f"HF API error: {response.status_code}")
|
| 290 |
+
return None
|
| 291 |
+
|
| 292 |
+
except Exception as e:
|
| 293 |
+
print(f"HF API request failed: {e}")
|
| 294 |
+
return None
|
| 295 |
+
|
| 296 |
+
def _clean_alt_text(self, alt_text: str, max_length: int) -> str:
|
| 297 |
+
"""Clean generated text"""
|
| 298 |
+
# Remove common prefixes
|
| 299 |
+
prefixes = ["a picture of ", "an image of ", "a photo of "]
|
| 300 |
+
alt_text_lower = alt_text.lower()
|
| 301 |
+
for prefix in prefixes:
|
| 302 |
+
if alt_text_lower.startswith(prefix):
|
| 303 |
+
alt_text = alt_text[len(prefix):]
|
| 304 |
+
break
|
| 305 |
+
|
| 306 |
+
# Capitalize first letter
|
| 307 |
+
if alt_text:
|
| 308 |
+
alt_text = alt_text[0].upper() + alt_text[1:]
|
| 309 |
+
|
| 310 |
+
# Truncate if needed
|
| 311 |
+
if len(alt_text) > max_length:
|
| 312 |
+
alt_text = alt_text[:max_length-3] + "..."
|
| 313 |
+
|
| 314 |
+
return alt_text.strip()
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
# Singleton instances
|
| 318 |
+
_local_model: Optional[LocalVisionModel] = None
|
| 319 |
+
_hf_api: Optional[HuggingFaceInferenceAPI] = None
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def get_vision_model() -> Optional[LocalVisionModel]:
|
| 323 |
+
"""Get or create local vision model singleton"""
|
| 324 |
+
global _local_model
|
| 325 |
+
if _local_model is None:
|
| 326 |
+
model_name = os.getenv("LOCAL_VISION_MODEL", "blip-base")
|
| 327 |
+
_local_model = LocalVisionModel(model_name)
|
| 328 |
+
return _local_model
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def get_hf_api() -> Optional[HuggingFaceInferenceAPI]:
|
| 332 |
+
"""Get or create Hugging Face API singleton"""
|
| 333 |
+
global _hf_api
|
| 334 |
+
if _hf_api is None:
|
| 335 |
+
_hf_api = HuggingFaceInferenceAPI()
|
| 336 |
+
return _hf_api
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
def generate_alt_text_free(
|
| 340 |
+
image_data: bytes,
|
| 341 |
+
shape_name: str = "",
|
| 342 |
+
slide_number: int = 0,
|
| 343 |
+
max_length: int = 250
|
| 344 |
+
) -> Optional[str]:
|
| 345 |
+
"""
|
| 346 |
+
Generate alt text using FREE methods (tries local first, then HF API)
|
| 347 |
+
|
| 348 |
+
Priority:
|
| 349 |
+
1. Local AI model (completely free, unlimited)
|
| 350 |
+
2. Hugging Face Inference API (free tier)
|
| 351 |
+
3. None (fallback to placeholder in main code)
|
| 352 |
+
|
| 353 |
+
Args:
|
| 354 |
+
image_data: Raw image bytes
|
| 355 |
+
shape_name: Shape name
|
| 356 |
+
slide_number: Slide number
|
| 357 |
+
max_length: Maximum length
|
| 358 |
+
|
| 359 |
+
Returns:
|
| 360 |
+
Generated alt text or None
|
| 361 |
+
"""
|
| 362 |
+
# Try local model first (best option - free and unlimited)
|
| 363 |
+
local_model = get_vision_model()
|
| 364 |
+
if local_model and local_model.is_enabled():
|
| 365 |
+
result = local_model.generate_alt_text(image_data, shape_name, slide_number, max_length)
|
| 366 |
+
if result:
|
| 367 |
+
return result
|
| 368 |
+
|
| 369 |
+
# Fallback to Hugging Face API (free tier)
|
| 370 |
+
hf_api = get_hf_api()
|
| 371 |
+
if hf_api and hf_api.is_enabled():
|
| 372 |
+
result = hf_api.generate_alt_text(image_data, shape_name, slide_number, max_length)
|
| 373 |
+
if result:
|
| 374 |
+
return result
|
| 375 |
+
|
| 376 |
+
# If both fail, return None (main code will use placeholder)
|
| 377 |
+
return None
|
python-server/output/remediated-test1.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9236f0b7f979a7fb6bd92447bb13cbb976bf5ba6ec4c81ac58879a39e808b664
|
| 3 |
+
size 122004
|
python-server/output/remediated-test2.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6aac4013b5453a2c533701b4ce9269579493963fa684e8c8c8a169cc80571238
|
| 3 |
+
size 4072624
|
python-server/requirements.txt
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# FastAPI web framework
|
| 2 |
+
fastapi>=0.100.0
|
| 3 |
+
uvicorn[standard]>=0.28.0
|
| 4 |
+
|
| 5 |
+
# Document processing
|
| 6 |
+
python-docx>=1.0.0
|
| 7 |
+
lxml>=5.0.0
|
| 8 |
+
python-multipart>=0.0.9
|
| 9 |
+
|
| 10 |
+
# FREE Local AI Vision Models for Alt Text Generation
|
| 11 |
+
# BLIP and GIT models run locally on CPU/GPU - 100% FREE, No API Costs!
|
| 12 |
+
transformers>=4.35.0
|
| 13 |
+
torch>=2.0.0
|
| 14 |
+
pillow>=10.0.0
|
| 15 |
+
|
| 16 |
+
# Optional: For faster inference with NVIDIA GPU
|
| 17 |
+
# accelerate>=0.25.0
|
| 18 |
+
|
| 19 |
+
# Windows COM automation for legacy PowerPoint conversion (Windows only)
|
| 20 |
+
pywin32>=306; sys_platform == 'win32'
|
| 21 |
+
|
| 22 |
+
# Environment variable management
|
| 23 |
+
python-dotenv>=1.0.0
|
python-server/server2.py
ADDED
|
@@ -0,0 +1,1421 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import shutil
|
| 4 |
+
from typing import List, Optional
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import zipfile
|
| 7 |
+
import xml.etree.ElementTree as ET
|
| 8 |
+
import re
|
| 9 |
+
import json
|
| 10 |
+
from lxml import etree
|
| 11 |
+
|
| 12 |
+
import platform
|
| 13 |
+
import subprocess
|
| 14 |
+
import uuid
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
import win32com.client
|
| 18 |
+
except ImportError:
|
| 19 |
+
win32com = None
|
| 20 |
+
|
| 21 |
+
# Load environment variables (optional)
|
| 22 |
+
try:
|
| 23 |
+
from dotenv import load_dotenv
|
| 24 |
+
load_dotenv()
|
| 25 |
+
except ImportError:
|
| 26 |
+
pass # .env is optional
|
| 27 |
+
|
| 28 |
+
# Import FREE Local AI Vision - Only Option!
|
| 29 |
+
AI_AVAILABLE = False
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
from local_vision import generate_alt_text_free, get_vision_model
|
| 33 |
+
local_model = get_vision_model()
|
| 34 |
+
|
| 35 |
+
if local_model and local_model.is_enabled():
|
| 36 |
+
AI_AVAILABLE = True
|
| 37 |
+
print("✅ Local AI vision model loaded (BLIP - 100% FREE, No Costs)")
|
| 38 |
+
else:
|
| 39 |
+
print("⚠️ Local AI model not ready yet (will download on first use)")
|
| 40 |
+
except ImportError as e:
|
| 41 |
+
print(f"⚠️ AI vision module not available: {e}")
|
| 42 |
+
print("ℹ️ Will use placeholder alt text")
|
| 43 |
+
|
| 44 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Body, Request, Response
|
| 45 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 46 |
+
from fastapi.responses import FileResponse, JSONResponse, PlainTextResponse
|
| 47 |
+
from fastapi.exceptions import RequestValidationError
|
| 48 |
+
from starlette.exceptions import HTTPException as StarletteHTTPException
|
| 49 |
+
import traceback
|
| 50 |
+
|
| 51 |
+
from color_contrast import (
|
| 52 |
+
build_pptx_color_context,
|
| 53 |
+
check_slide_color_contrast,
|
| 54 |
+
remediate_slide_color_contrast,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# ---------- CONFIG ----------
|
| 58 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 59 |
+
UPLOAD_DIR = BASE_DIR / "uploads"
|
| 60 |
+
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 61 |
+
|
| 62 |
+
OUTPUT_DIR = BASE_DIR / "output"
|
| 63 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 64 |
+
|
| 65 |
+
# ---------- APP SETUP ----------
|
| 66 |
+
app = FastAPI()
|
| 67 |
+
|
| 68 |
+
# Configure CORS (Angular frontend -> Python backend)
|
| 69 |
+
origins = [
|
| 70 |
+
"http://localhost:4200",
|
| 71 |
+
"http://localhost:3000",
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
app.add_middleware(
|
| 75 |
+
CORSMiddleware,
|
| 76 |
+
allow_origins=origins,
|
| 77 |
+
allow_credentials=True,
|
| 78 |
+
allow_methods=["*"],
|
| 79 |
+
allow_headers=["*"],
|
| 80 |
+
expose_headers=["Content-Disposition"],
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
@app.exception_handler(Exception)
|
| 84 |
+
async def debug_exception_handler(request: Request, exc: Exception):
|
| 85 |
+
traceback.print_exc()
|
| 86 |
+
return PlainTextResponse(str(exc), status_code=500)
|
| 87 |
+
|
| 88 |
+
@app.middleware("http")
|
| 89 |
+
async def access_log(request: Request, call_next):
|
| 90 |
+
t0 = time.time()
|
| 91 |
+
response = await call_next(request)
|
| 92 |
+
ms = (time.time() - t0) * 1000
|
| 93 |
+
print(f"[{request.method}] {request.url.path} -> {response.status_code} ({ms:.2f} ms)")
|
| 94 |
+
return response
|
| 95 |
+
|
| 96 |
+
@app.get("/")
|
| 97 |
+
def health_check():
|
| 98 |
+
return {"status": "running", "service": "PowerPoint Accessibility Backend"}
|
| 99 |
+
|
| 100 |
+
SOFFICE_PATH = r"C:\Program Files\LibreOffice\program\soffice.exe"
|
| 101 |
+
|
| 102 |
+
def is_windows() -> bool:
|
| 103 |
+
return platform.system().lower().startswith("win")
|
| 104 |
+
|
| 105 |
+
def convert_legacy_ppt_to_pptx_powerpoint(src_path: Path, out_dir: Path) -> Path:
|
| 106 |
+
|
| 107 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 108 |
+
dst_path = out_dir / f"{src_path.stem}.pptx"
|
| 109 |
+
|
| 110 |
+
if win32com is None:
|
| 111 |
+
raise RuntimeError("win32com is required for legacy PowerPoint conversion on Windows.")
|
| 112 |
+
|
| 113 |
+
pp = win32com.client.Dispatch("PowerPoint.Application")
|
| 114 |
+
pp.Visible = 1
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
pres = pp.Presentations.Open(str(src_path), 1, 0, 0) # ReadOnly=1, WithWindow=0
|
| 118 |
+
try:
|
| 119 |
+
pres.SaveAs(str(dst_path), 24) # 24 = ppSaveAsOpenXMLPresentation (.pptx)
|
| 120 |
+
finally:
|
| 121 |
+
pres.Close()
|
| 122 |
+
finally:
|
| 123 |
+
pp.Quit()
|
| 124 |
+
|
| 125 |
+
if not dst_path.exists():
|
| 126 |
+
raise RuntimeError("PowerPoint conversion did not produce a .pptx file.")
|
| 127 |
+
return dst_path
|
| 128 |
+
|
| 129 |
+
def convert_legacy_to_pptx(src_path: Path, out_dir: Path) -> Path:
|
| 130 |
+
|
| 131 |
+
if is_windows():
|
| 132 |
+
try:
|
| 133 |
+
return convert_legacy_ppt_to_pptx_powerpoint(src_path, out_dir)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
# fallback to LibreOffice if PowerPoint fails
|
| 136 |
+
return convert_legacy_ppt_to_pptx_powerpoint(src_path, out_dir)
|
| 137 |
+
else:
|
| 138 |
+
return convert_legacy_ppt_to_pptx_powerpoint(src_path, out_dir)
|
| 139 |
+
|
| 140 |
+
@app.post("/upload")
|
| 141 |
+
async def upload_files(
|
| 142 |
+
files: Optional[List[UploadFile]] = File(default=None),
|
| 143 |
+
file: Optional[UploadFile] = File(default=None),
|
| 144 |
+
pptxFile: Optional[UploadFile] = File(default=None),
|
| 145 |
+
docxFile: Optional[UploadFile] = File(default=None),
|
| 146 |
+
):
|
| 147 |
+
incoming: List[UploadFile] = []
|
| 148 |
+
if files:
|
| 149 |
+
incoming.extend(files)
|
| 150 |
+
if file:
|
| 151 |
+
incoming.append(file)
|
| 152 |
+
if pptxFile:
|
| 153 |
+
incoming.append(pptxFile)
|
| 154 |
+
if docxFile:
|
| 155 |
+
incoming.append(docxFile)
|
| 156 |
+
|
| 157 |
+
if not incoming:
|
| 158 |
+
raise HTTPException(
|
| 159 |
+
status_code=400,
|
| 160 |
+
detail="No file uploaded. Send multipart/form-data with one of: files, file, pptxFile, docxFile"
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
if len(incoming) > 10:
|
| 164 |
+
raise HTTPException(
|
| 165 |
+
status_code=400,
|
| 166 |
+
detail=f"Too many files. You uploaded {len(incoming)}, but the limit is 10."
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
results = []
|
| 170 |
+
|
| 171 |
+
for up in incoming:
|
| 172 |
+
try:
|
| 173 |
+
filename = up.filename or "unnamed.pptx"
|
| 174 |
+
filename_lower = filename.lower()
|
| 175 |
+
allowed_ext = (".pptx", ".ppt", ".pps", ".pot", ".potx", ".ppsx")
|
| 176 |
+
|
| 177 |
+
if not filename_lower.endswith(allowed_ext):
|
| 178 |
+
results.append({
|
| 179 |
+
"fileName": filename,
|
| 180 |
+
"error": "Invalid file type. Please upload a PowerPoint file."
|
| 181 |
+
})
|
| 182 |
+
continue
|
| 183 |
+
|
| 184 |
+
# save with unique name to avoid collisions
|
| 185 |
+
unique_prefix = uuid.uuid4().hex[:8]
|
| 186 |
+
saved_name = f"{unique_prefix}_{filename}"
|
| 187 |
+
file_location = UPLOAD_DIR / saved_name
|
| 188 |
+
|
| 189 |
+
with file_location.open("wb") as buffer:
|
| 190 |
+
shutil.copyfileobj(up.file, buffer)
|
| 191 |
+
|
| 192 |
+
ext = Path(filename_lower).suffix
|
| 193 |
+
converted_dir = UPLOAD_DIR / "converted" / unique_prefix
|
| 194 |
+
converted_dir.mkdir(parents=True, exist_ok=True)
|
| 195 |
+
|
| 196 |
+
if ext in [".ppt", ".pps", ".pot"]:
|
| 197 |
+
pptx_input = convert_legacy_to_pptx(file_location, converted_dir)
|
| 198 |
+
else:
|
| 199 |
+
pptx_input = file_location
|
| 200 |
+
|
| 201 |
+
base = Path(filename).stem
|
| 202 |
+
out_name = f"remediated-{base}.pptx"
|
| 203 |
+
out_path = OUTPUT_DIR / f"{unique_prefix}_{out_name}"
|
| 204 |
+
|
| 205 |
+
original_report = analyze_powerpoint(pptx_input, filename)
|
| 206 |
+
|
| 207 |
+
alt_fixed_count, alt_fix_details, contrast_fixed_count, contrast_fix_details, dup_fixed_count, dup_fix_details = remediate_accessibility_pptx(pptx_input, out_path)
|
| 208 |
+
|
| 209 |
+
post_remediation_report = analyze_powerpoint(out_path, out_name)
|
| 210 |
+
|
| 211 |
+
report = original_report
|
| 212 |
+
report["fileName"] = out_name
|
| 213 |
+
report["summary"]["fixed"] += alt_fixed_count + contrast_fixed_count + dup_fixed_count
|
| 214 |
+
report["details"]["autoFixedAltText"] = alt_fix_details
|
| 215 |
+
report["details"]["autoFixedColorContrast"] = contrast_fix_details
|
| 216 |
+
report["details"]["duplicateTitleFixes"] = dup_fix_details
|
| 217 |
+
report["details"]["remainingColorContrastIssues"] = post_remediation_report["details"].get("colorContrastIssues", [])
|
| 218 |
+
report["details"]["remainingImagesMissingOrBadAlt"] = post_remediation_report["details"].get("imagesMissingOrBadAlt", [])
|
| 219 |
+
|
| 220 |
+
results.append({
|
| 221 |
+
"fileName": filename,
|
| 222 |
+
# "suggestedFileName": f"{unique_prefix}_{out_name}",
|
| 223 |
+
"suggestedFileName": out_name,
|
| 224 |
+
"report": report
|
| 225 |
+
})
|
| 226 |
+
|
| 227 |
+
except Exception as e:
|
| 228 |
+
results.append({
|
| 229 |
+
"fileName": getattr(up, "filename", "unknown"),
|
| 230 |
+
"error": str(e)
|
| 231 |
+
})
|
| 232 |
+
|
| 233 |
+
return JSONResponse(content={"files": results})
|
| 234 |
+
|
| 235 |
+
@app.post("/api/session")
|
| 236 |
+
def create_session():
|
| 237 |
+
return {"sessionId": uuid.uuid4().hex}
|
| 238 |
+
|
| 239 |
+
def get_slide_num(path: str) -> int:
|
| 240 |
+
"""
|
| 241 |
+
Extract numeric slide number from path for sorting.
|
| 242 |
+
"""
|
| 243 |
+
m = re.search(r"ppt/slides/slide(\d+)\.xml$", path)
|
| 244 |
+
return int(m.group(1)) if m else 10**9
|
| 245 |
+
|
| 246 |
+
def analyze_powerpoint(file_path, filename):
|
| 247 |
+
"""Analyze PowerPoint file for accessibility issues."""
|
| 248 |
+
report = {
|
| 249 |
+
"fileName": filename,
|
| 250 |
+
"summary": {
|
| 251 |
+
"fixed": 0,
|
| 252 |
+
"flagged": 0
|
| 253 |
+
},
|
| 254 |
+
"details": {
|
| 255 |
+
"slidesMissingTitles": [],
|
| 256 |
+
"imagesMissingOrBadAlt": [],
|
| 257 |
+
"gifsDetected": [],
|
| 258 |
+
"listFormattingIssues": [],
|
| 259 |
+
"colorContrastIssues": [],
|
| 260 |
+
"titleNeedsFixing": False,
|
| 261 |
+
"fileNameNeedsFixing": False,
|
| 262 |
+
"autoFixedAltText": [],
|
| 263 |
+
"autoFixedColorContrast": [],
|
| 264 |
+
"remainingColorContrastIssues": [],
|
| 265 |
+
"remainingImagesMissingOrBadAlt": [],
|
| 266 |
+
"duplicateSlides": [],
|
| 267 |
+
"rawUrlFindings": [],
|
| 268 |
+
"nonEnglishFindings": [],
|
| 269 |
+
"likelyDecorativeImages": [],
|
| 270 |
+
"headerFooterFindings": [],
|
| 271 |
+
"duplicateTitleFixes": []
|
| 272 |
+
}
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
try:
|
| 276 |
+
with zipfile.ZipFile(file_path, 'r') as zip_file:
|
| 277 |
+
contrast_context = build_pptx_color_context(zip_file)
|
| 278 |
+
|
| 279 |
+
# ---- Title metadata check ----
|
| 280 |
+
if 'docProps/core.xml' in zip_file.namelist():
|
| 281 |
+
core_xml = zip_file.read('docProps/core.xml').decode('utf-8', errors='ignore')
|
| 282 |
+
if '<dc:title/>' in core_xml or '<dc:title></dc:title>' in core_xml:
|
| 283 |
+
report["details"]["titleNeedsFixing"] = True
|
| 284 |
+
report["summary"]["flagged"] += 1
|
| 285 |
+
|
| 286 |
+
# ---- File name check ----
|
| 287 |
+
if "_" in filename or filename.lower().startswith("presentation") or filename.lower().startswith("untitled"):
|
| 288 |
+
report["details"]["fileNameNeedsFixing"] = True
|
| 289 |
+
report["summary"]["flagged"] += 1
|
| 290 |
+
|
| 291 |
+
# ---- Collect slides in TRUE numeric order ----
|
| 292 |
+
slides = [
|
| 293 |
+
name for name in zip_file.namelist()
|
| 294 |
+
if name.startswith("ppt/slides/slide") and name.endswith(".xml")
|
| 295 |
+
]
|
| 296 |
+
slides = sorted(slides, key=get_slide_num)
|
| 297 |
+
|
| 298 |
+
# ---- Analyze each slide in presentation order ----
|
| 299 |
+
previous_slide_signature = None
|
| 300 |
+
for slide_path in slides:
|
| 301 |
+
slide_number = get_slide_num(slide_path)
|
| 302 |
+
slide_xml = zip_file.read(slide_path).decode('utf-8', errors='ignore')
|
| 303 |
+
|
| 304 |
+
# Check slide title
|
| 305 |
+
title_check = check_slide_title(slide_xml, slide_number)
|
| 306 |
+
if title_check["missing"]:
|
| 307 |
+
report["details"]["slidesMissingTitles"].append(title_check)
|
| 308 |
+
report["summary"]["flagged"] += 1
|
| 309 |
+
|
| 310 |
+
# Check images
|
| 311 |
+
image_issues = check_slide_images(slide_xml, slide_number)
|
| 312 |
+
if image_issues:
|
| 313 |
+
report["details"]["imagesMissingOrBadAlt"].extend(image_issues)
|
| 314 |
+
report["summary"]["flagged"] += len(image_issues)
|
| 315 |
+
|
| 316 |
+
# Check list formatting
|
| 317 |
+
list_issues = check_list_formatting(slide_xml, slide_number)
|
| 318 |
+
if list_issues:
|
| 319 |
+
report["details"]["listFormattingIssues"].extend(list_issues)
|
| 320 |
+
report["summary"]["flagged"] += len(list_issues)
|
| 321 |
+
|
| 322 |
+
# Check color contrast
|
| 323 |
+
contrast_issues = check_slide_color_contrast(zip_file.read(slide_path), slide_number, contrast_context)
|
| 324 |
+
if contrast_issues:
|
| 325 |
+
report["details"]["colorContrastIssues"].extend(contrast_issues)
|
| 326 |
+
report["summary"]["flagged"] += len(contrast_issues)
|
| 327 |
+
|
| 328 |
+
# ===== NEW FEATURE CHECKS (Phase 1) =====
|
| 329 |
+
|
| 330 |
+
# Check for duplicate slides
|
| 331 |
+
current_signature = get_slide_signature(slide_xml)
|
| 332 |
+
if previous_slide_signature is not None and current_signature == previous_slide_signature:
|
| 333 |
+
report["details"]["duplicateSlides"].append({
|
| 334 |
+
"slideNumber": slide_number,
|
| 335 |
+
"duplicateOf": slide_number - 1,
|
| 336 |
+
"message": f"Slide {slide_number} appears to be an exact duplicate of Slide {slide_number - 1}"
|
| 337 |
+
})
|
| 338 |
+
report["summary"]["flagged"] += 1
|
| 339 |
+
previous_slide_signature = current_signature
|
| 340 |
+
|
| 341 |
+
# Check for raw URLs in text
|
| 342 |
+
url_issues = detect_raw_urls(slide_xml, slide_number)
|
| 343 |
+
if url_issues:
|
| 344 |
+
report["details"]["rawUrlFindings"].extend(url_issues)
|
| 345 |
+
report["summary"]["flagged"] += len(url_issues)
|
| 346 |
+
|
| 347 |
+
# Check for non-English text
|
| 348 |
+
non_english_issues = detect_non_english_text(slide_xml, slide_number)
|
| 349 |
+
if non_english_issues:
|
| 350 |
+
report["details"]["nonEnglishFindings"].extend(non_english_issues)
|
| 351 |
+
report["summary"]["flagged"] += len(non_english_issues)
|
| 352 |
+
|
| 353 |
+
# Check for likely decorative images
|
| 354 |
+
decorative_candidates = detect_likely_decorative_images(slide_xml, slide_number)
|
| 355 |
+
if decorative_candidates:
|
| 356 |
+
report["details"]["likelyDecorativeImages"].extend(decorative_candidates)
|
| 357 |
+
report["summary"]["flagged"] += len(decorative_candidates)
|
| 358 |
+
|
| 359 |
+
# Check for header/footer content
|
| 360 |
+
footer_issues = detect_header_footer_content(slide_xml, slide_number)
|
| 361 |
+
if footer_issues:
|
| 362 |
+
report["details"]["headerFooterFindings"].extend(footer_issues)
|
| 363 |
+
report["summary"]["flagged"] += len(footer_issues)
|
| 364 |
+
|
| 365 |
+
# ---- GIF check ----
|
| 366 |
+
gif_files = [
|
| 367 |
+
name for name in zip_file.namelist()
|
| 368 |
+
if name.startswith("ppt/media/") and name.lower().endswith(".gif")
|
| 369 |
+
]
|
| 370 |
+
if gif_files:
|
| 371 |
+
report["details"]["gifsDetected"] = gif_files
|
| 372 |
+
report["summary"]["flagged"] += len(gif_files)
|
| 373 |
+
|
| 374 |
+
except Exception as e:
|
| 375 |
+
print(f"Error analyzing PowerPoint: {e}")
|
| 376 |
+
raise
|
| 377 |
+
|
| 378 |
+
return report
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
def check_slide_title(slide_xml: str, slide_number: int):
|
| 382 |
+
"""Check if slide has a title."""
|
| 383 |
+
# Look for title placeholder
|
| 384 |
+
title_pattern = r'<p:ph[^>]*type="(title|ctrTitle)"[^>]*>'
|
| 385 |
+
has_title_placeholder = re.search(title_pattern, slide_xml)
|
| 386 |
+
|
| 387 |
+
if not has_title_placeholder:
|
| 388 |
+
return {
|
| 389 |
+
"missing": True,
|
| 390 |
+
"slideNumber": slide_number,
|
| 391 |
+
"message": f"Slide {slide_number} is missing a title"
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
# Check if title has text
|
| 395 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 396 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 397 |
+
|
| 398 |
+
if not any(text.strip() for text in text_matches):
|
| 399 |
+
return {
|
| 400 |
+
"missing": True,
|
| 401 |
+
"slideNumber": slide_number,
|
| 402 |
+
"message": f"Slide {slide_number} has an empty title"
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
return {"missing": False}
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
def check_list_formatting(slide_xml: str, slide_number: int):
|
| 409 |
+
"""Check for list-like content that is not semantically marked as a list."""
|
| 410 |
+
issues = []
|
| 411 |
+
|
| 412 |
+
# Find all text elements
|
| 413 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 414 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 415 |
+
|
| 416 |
+
for text in text_matches:
|
| 417 |
+
# Check for hyphenated list patterns
|
| 418 |
+
if re.match(r'^[\s]*[-–—•]\s+.+', text):
|
| 419 |
+
issues.append({
|
| 420 |
+
"slideNumber": slide_number,
|
| 421 |
+
"location": f"Slide {slide_number}",
|
| 422 |
+
"issue": f'Possible improperly formatted list: "{text[:50]}..."',
|
| 423 |
+
"type": "listFormatting"
|
| 424 |
+
})
|
| 425 |
+
|
| 426 |
+
# Check for paragraph indentation patterns that often indicate manual bullets.
|
| 427 |
+
paragraphs = re.findall(r'<a:p\b[\s\S]*?</a:p>', slide_xml)
|
| 428 |
+
previous_level = 0
|
| 429 |
+
previous_text = ""
|
| 430 |
+
|
| 431 |
+
for para_xml in paragraphs:
|
| 432 |
+
para_texts = re.findall(r'<a:t[^>]*>(.*?)</a:t>', para_xml)
|
| 433 |
+
para_text = " ".join(t.strip() for t in para_texts if t and t.strip())
|
| 434 |
+
if not para_text:
|
| 435 |
+
continue
|
| 436 |
+
|
| 437 |
+
first_raw_text = para_texts[0] if para_texts else ""
|
| 438 |
+
|
| 439 |
+
ppr_match = re.search(r'<a:pPr([^>]*)>', para_xml)
|
| 440 |
+
ppr_attrs = ppr_match.group(1) if ppr_match else ""
|
| 441 |
+
|
| 442 |
+
lvl_match = re.search(r'\blvl="(\d+)"', ppr_attrs)
|
| 443 |
+
level = int(lvl_match.group(1)) if lvl_match else 0
|
| 444 |
+
|
| 445 |
+
mar_match = re.search(r'\bmarL="(\d+)"', ppr_attrs)
|
| 446 |
+
mar_left = int(mar_match.group(1)) if mar_match else 0
|
| 447 |
+
|
| 448 |
+
has_explicit_bullet = bool(re.search(r'<a:bu(Char|AutoNum|Blip)\b', para_xml))
|
| 449 |
+
has_bu_none = bool(re.search(r'<a:buNone\b', para_xml))
|
| 450 |
+
has_text_bullet = bool(re.match(r'^\s*[-–—•*]\s+.+', para_text))
|
| 451 |
+
has_manual_leading_indent = bool(re.match(r'^[ \t]+\S', first_raw_text))
|
| 452 |
+
visually_indented = (level > 0 or mar_left > 0)
|
| 453 |
+
|
| 454 |
+
# If a line becomes more indented than the previous line but lacks bullet semantics,
|
| 455 |
+
# treat it as an improperly formatted list candidate.
|
| 456 |
+
if visually_indented and not has_explicit_bullet and not has_text_bullet and previous_text and level > previous_level:
|
| 457 |
+
issues.append({
|
| 458 |
+
"slideNumber": slide_number,
|
| 459 |
+
"location": f"Slide {slide_number}",
|
| 460 |
+
"issue": f'Indented line appears list-like but is not marked as a list: "{para_text[:50]}..."',
|
| 461 |
+
"type": "listFormatting"
|
| 462 |
+
})
|
| 463 |
+
|
| 464 |
+
# Also catch manual indentation done by adding leading spaces while bullets are disabled.
|
| 465 |
+
if has_bu_none and has_manual_leading_indent and not has_text_bullet and previous_text:
|
| 466 |
+
issues.append({
|
| 467 |
+
"slideNumber": slide_number,
|
| 468 |
+
"location": f"Slide {slide_number}",
|
| 469 |
+
"issue": f'Manually indented paragraph with bullets disabled looks like a list item: "{para_text[:50]}..."',
|
| 470 |
+
"type": "listFormatting"
|
| 471 |
+
})
|
| 472 |
+
|
| 473 |
+
previous_level = level
|
| 474 |
+
previous_text = para_text
|
| 475 |
+
|
| 476 |
+
return issues
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
# ========== NEW FEATURE HELPERS (Phase 1) ==========
|
| 480 |
+
|
| 481 |
+
def extract_all_text_from_slide(slide_xml: str) -> str:
|
| 482 |
+
"""Extract all visible text content from a slide for analysis."""
|
| 483 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 484 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 485 |
+
return ' '.join(text_matches)
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
def get_slide_signature(slide_xml: str) -> str:
|
| 489 |
+
"""Generate a normalized signature for a slide to detect exact duplicates."""
|
| 490 |
+
# Get all text and normalize whitespace
|
| 491 |
+
all_text = extract_all_text_from_slide(slide_xml)
|
| 492 |
+
normalized = re.sub(r'\s+', ' ', all_text.strip()).lower()
|
| 493 |
+
|
| 494 |
+
# Count visible shapes/images as a structural hint
|
| 495 |
+
pic_count = len(re.findall(r'<p:pic[\s\S]*?</p:pic>', slide_xml))
|
| 496 |
+
shape_count = len(re.findall(r'<p:sp[\s\S]*?</p:sp>', slide_xml))
|
| 497 |
+
|
| 498 |
+
# Return a deterministic hash-like signature
|
| 499 |
+
signature = f"{normalized}|pics:{pic_count}|shapes:{shape_count}"
|
| 500 |
+
return signature
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def detect_raw_urls(slide_xml: str, slide_number: int) -> List[dict]:
|
| 504 |
+
"""Detect plain URLs in visible text (http/https/www patterns)."""
|
| 505 |
+
issues = []
|
| 506 |
+
|
| 507 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 508 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 509 |
+
|
| 510 |
+
# Regex to find plain URLs
|
| 511 |
+
url_pattern = r'(?:https?://|www\.)[^\s<>"]+'
|
| 512 |
+
|
| 513 |
+
for text in text_matches:
|
| 514 |
+
url_matches = re.finditer(url_pattern, text)
|
| 515 |
+
for url_match in url_matches:
|
| 516 |
+
issues.append({
|
| 517 |
+
"slideNumber": slide_number,
|
| 518 |
+
"location": f"Slide {slide_number}",
|
| 519 |
+
"matchedText": url_match.group(0),
|
| 520 |
+
"context": text[:80],
|
| 521 |
+
"type": "rawUrl",
|
| 522 |
+
"recommendation": "Replace raw URLs with descriptive link text"
|
| 523 |
+
})
|
| 524 |
+
|
| 525 |
+
return issues
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
def detect_non_english_text(slide_xml: str, slide_number: int) -> List[dict]:
|
| 529 |
+
"""Detect clearly non-English text runs using conservative language markers."""
|
| 530 |
+
issues = []
|
| 531 |
+
|
| 532 |
+
def _is_substantial_text(text: str) -> bool:
|
| 533 |
+
cleaned = text.strip()
|
| 534 |
+
if not cleaned:
|
| 535 |
+
return False
|
| 536 |
+
alpha_chars = sum(1 for c in cleaned if c.isalpha())
|
| 537 |
+
word_count = len(re.findall(r"[A-Za-zÀ-ÖØ-öø-ÿ']+", cleaned))
|
| 538 |
+
return alpha_chars >= 8 and word_count >= 2
|
| 539 |
+
def _tokenize(text: str) -> List[str]:
|
| 540 |
+
return re.findall(r"[A-Za-zÀ-ÖØ-öø-ÿ']+", text.lower())
|
| 541 |
+
|
| 542 |
+
def _has_non_latin_script(text: str) -> bool:
|
| 543 |
+
return bool(re.search(r"[\u0400-\u04FF\u0600-\u06FF\u0900-\u0DFF\u3040-\u30FF\u4E00-\u9FFF]", text))
|
| 544 |
+
|
| 545 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 546 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 547 |
+
|
| 548 |
+
english_stopwords = {
|
| 549 |
+
"the", "and", "for", "with", "this", "that", "from", "are", "is", "of", "to", "in", "on", "by",
|
| 550 |
+
"a", "an", "it", "as", "at", "be", "or", "we", "you", "they", "was", "were", "have", "has"
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
language_hints = {
|
| 554 |
+
"es": {"el", "la", "los", "las", "de", "del", "que", "para", "con", "una", "uno", "como", "por", "este", "esta", "es", "en", "y"},
|
| 555 |
+
"fr": {"le", "la", "les", "des", "une", "un", "avec", "pour", "que", "est", "dans", "sur", "et", "de"},
|
| 556 |
+
"de": {"der", "die", "das", "und", "mit", "für", "ist", "nicht", "ein", "eine", "den", "zu", "auf"},
|
| 557 |
+
"pt": {"o", "a", "os", "as", "de", "do", "da", "que", "com", "para", "uma", "um", "e", "não", "em"},
|
| 558 |
+
"it": {"il", "lo", "la", "gli", "le", "di", "che", "con", "per", "una", "un", "è", "e", "in"}
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
for text in text_matches:
|
| 562 |
+
cleaned_text = text.strip()
|
| 563 |
+
if len(cleaned_text) < 3 or not _is_substantial_text(cleaned_text):
|
| 564 |
+
continue
|
| 565 |
+
|
| 566 |
+
if _has_non_latin_script(cleaned_text):
|
| 567 |
+
issues.append({
|
| 568 |
+
"slideNumber": slide_number,
|
| 569 |
+
"location": f"Slide {slide_number}",
|
| 570 |
+
"detectedLanguage": "non-Latin script",
|
| 571 |
+
"sampleText": cleaned_text[:60],
|
| 572 |
+
"type": "nonEnglishText",
|
| 573 |
+
"recommendation": "Verify non-English content is intentional or provide translation"
|
| 574 |
+
})
|
| 575 |
+
continue
|
| 576 |
+
|
| 577 |
+
tokens = _tokenize(cleaned_text)
|
| 578 |
+
if len(tokens) < 3:
|
| 579 |
+
continue
|
| 580 |
+
|
| 581 |
+
en_hits = sum(1 for t in tokens if t in english_stopwords)
|
| 582 |
+
best_lang = None
|
| 583 |
+
best_hits = 0
|
| 584 |
+
|
| 585 |
+
for lang_code, hints in language_hints.items():
|
| 586 |
+
hits = sum(1 for t in tokens if t in hints)
|
| 587 |
+
if hits > best_hits:
|
| 588 |
+
best_hits = hits
|
| 589 |
+
best_lang = lang_code
|
| 590 |
+
|
| 591 |
+
# Only flag when the non-English signal is very strong.
|
| 592 |
+
# This intentionally avoids guessing on short or ambiguous phrases.
|
| 593 |
+
if best_lang and best_hits >= 3 and best_hits >= en_hits + 2:
|
| 594 |
+
issues.append({
|
| 595 |
+
"slideNumber": slide_number,
|
| 596 |
+
"location": f"Slide {slide_number}",
|
| 597 |
+
"detectedLanguage": f"{best_lang} (heuristic)",
|
| 598 |
+
"sampleText": cleaned_text[:60],
|
| 599 |
+
"type": "nonEnglishText",
|
| 600 |
+
"recommendation": "Verify non-English content is intentional or provide translation"
|
| 601 |
+
})
|
| 602 |
+
|
| 603 |
+
return issues
|
| 604 |
+
|
| 605 |
+
|
| 606 |
+
def detect_likely_decorative_images(slide_xml: str, slide_number: int) -> List[dict]:
|
| 607 |
+
"""Detect images that are likely decorative (logo, icon, watermark)."""
|
| 608 |
+
candidates = []
|
| 609 |
+
|
| 610 |
+
pic_pattern = r'<p:pic[\s\S]*?</p:pic>'
|
| 611 |
+
pic_matches = re.findall(pic_pattern, slide_xml)
|
| 612 |
+
|
| 613 |
+
decorative_hints = ["background", "bg", "decor", "decoration", "border", "divider", "logo", "icon", "watermark", "pattern", "frame"]
|
| 614 |
+
|
| 615 |
+
for pic_xml in pic_matches:
|
| 616 |
+
cnvpr_pattern = r'<p:cNvPr([^>]*)/?>'
|
| 617 |
+
m = re.search(cnvpr_pattern, pic_xml)
|
| 618 |
+
attrs = m.group(1) if m else ""
|
| 619 |
+
|
| 620 |
+
def get_attr(attr_name: str) -> str:
|
| 621 |
+
am = re.search(rf'{attr_name}="([^"]*)"', attrs)
|
| 622 |
+
return am.group(1) if am else ""
|
| 623 |
+
|
| 624 |
+
shape_id = get_attr("id")
|
| 625 |
+
shape_name = get_attr("name")
|
| 626 |
+
alt_text = get_attr("descr")
|
| 627 |
+
|
| 628 |
+
# Check if image name or alt text suggests it's decorative
|
| 629 |
+
name_lower = (shape_name or "").lower()
|
| 630 |
+
alt_lower = (alt_text or "").lower()
|
| 631 |
+
|
| 632 |
+
is_likely_decorative = any(hint in name_lower for hint in decorative_hints) or \
|
| 633 |
+
(alt_lower == "decorative")
|
| 634 |
+
|
| 635 |
+
if is_likely_decorative:
|
| 636 |
+
candidates.append({
|
| 637 |
+
"slideNumber": slide_number,
|
| 638 |
+
"shapeId": shape_id,
|
| 639 |
+
"shapeName": shape_name,
|
| 640 |
+
"altText": alt_text or "(none)",
|
| 641 |
+
"type": "likelyDecorativeImage",
|
| 642 |
+
"recommendation": "Confirm this image is decorative; if so, set alt text to 'decorative' to skip auto-generation"
|
| 643 |
+
})
|
| 644 |
+
|
| 645 |
+
return candidates
|
| 646 |
+
|
| 647 |
+
|
| 648 |
+
def detect_header_footer_content(slide_xml: str, slide_number: int) -> List[dict]:
|
| 649 |
+
"""Detect header/footer placeholder content and repeated footer-like text."""
|
| 650 |
+
issues = []
|
| 651 |
+
|
| 652 |
+
def _is_page_number_only(text: str) -> bool:
|
| 653 |
+
cleaned = re.sub(r'\s+', ' ', (text or '')).strip()
|
| 654 |
+
if not cleaned:
|
| 655 |
+
return False
|
| 656 |
+
return bool(re.fullmatch(r'(?:page\s*)?\d+(?:\s*/\s*\d+)?', cleaned, flags=re.IGNORECASE))
|
| 657 |
+
|
| 658 |
+
# Check for explicit footer/date/slide number placeholders.
|
| 659 |
+
# If the placeholder type is only slide-number (sldNum), ignore it.
|
| 660 |
+
placeholder_types = re.findall(r'<p:ph[^>]*type="(ftr|dt|sldNum)"', slide_xml)
|
| 661 |
+
if placeholder_types:
|
| 662 |
+
only_slide_number_placeholder = all(t == "sldNum" for t in placeholder_types)
|
| 663 |
+
if only_slide_number_placeholder:
|
| 664 |
+
placeholder_types = []
|
| 665 |
+
|
| 666 |
+
if placeholder_types:
|
| 667 |
+
text_matches = [t.strip() for t in re.findall(r'<a:t[^>]*>(.*?)</a:t>', slide_xml) if t and t.strip()]
|
| 668 |
+
if text_matches and all(_is_page_number_only(t) for t in text_matches):
|
| 669 |
+
return issues
|
| 670 |
+
issues.append({
|
| 671 |
+
"slideNumber": slide_number,
|
| 672 |
+
"location": f"Slide {slide_number}",
|
| 673 |
+
"type": "headerFooterPlaceholder",
|
| 674 |
+
"recommendation": "Header/footer content detected; consider moving critical info to slide body for better accessibility"
|
| 675 |
+
})
|
| 676 |
+
|
| 677 |
+
# Check for repeated identical text at slide end (footer-like pattern).
|
| 678 |
+
# This is intentionally strict to avoid false positives on list content.
|
| 679 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 680 |
+
text_matches = [t.strip() for t in re.findall(text_pattern, slide_xml) if t and t.strip()]
|
| 681 |
+
|
| 682 |
+
if len(text_matches) >= 3:
|
| 683 |
+
last_texts = text_matches[-3:]
|
| 684 |
+
normalized_last = [re.sub(r'\s+', ' ', t).strip().lower() for t in last_texts]
|
| 685 |
+
looks_like_bullet = any(re.match(r'^[-–—•*]\s+', t) for t in last_texts)
|
| 686 |
+
|
| 687 |
+
if (
|
| 688 |
+
len(set(normalized_last)) == 1
|
| 689 |
+
and 1 < len(last_texts[0]) < 80
|
| 690 |
+
and not looks_like_bullet
|
| 691 |
+
and not _is_page_number_only(last_texts[0])
|
| 692 |
+
):
|
| 693 |
+
issues.append({
|
| 694 |
+
"slideNumber": slide_number,
|
| 695 |
+
"location": f"Slide {slide_number}",
|
| 696 |
+
"repeatedText": last_texts[0][:40] if last_texts else "",
|
| 697 |
+
"type": "footerLikePattern",
|
| 698 |
+
"recommendation": "Repeated footer-like text detected; ensure all important content is duplicated in slide body"
|
| 699 |
+
})
|
| 700 |
+
|
| 701 |
+
return issues
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
def remediate_duplicate_slide_title(slide_xml_bytes: bytes, slide_number: int, is_duplicate: bool, duplicate_index: int) -> tuple:
|
| 705 |
+
"""
|
| 706 |
+
Fix duplicate slide titles by appending Part N to the title text.
|
| 707 |
+
Returns: (new_xml_bytes, fixed_count, fix_details)
|
| 708 |
+
"""
|
| 709 |
+
if not is_duplicate:
|
| 710 |
+
return slide_xml_bytes, 0, []
|
| 711 |
+
|
| 712 |
+
try:
|
| 713 |
+
ns = {
|
| 714 |
+
"p": "http://schemas.openxmlformats.org/presentationml/2006/main",
|
| 715 |
+
"a": "http://schemas.openxmlformats.org/drawingml/2006/main"
|
| 716 |
+
}
|
| 717 |
+
|
| 718 |
+
root = etree.fromstring(slide_xml_bytes, parser=etree.XMLParser(remove_blank_text=False, recover=True))
|
| 719 |
+
|
| 720 |
+
# Find title shape - look for sp containing a title placeholder
|
| 721 |
+
title_sp = None
|
| 722 |
+
for sp in root.findall(".//p:sp", namespaces=ns):
|
| 723 |
+
ph = sp.find(".//p:ph", namespaces=ns)
|
| 724 |
+
if ph is not None:
|
| 725 |
+
ph_type = ph.get("type", "")
|
| 726 |
+
if ph_type in ["title", "ctrTitle"]:
|
| 727 |
+
title_sp = sp
|
| 728 |
+
break
|
| 729 |
+
|
| 730 |
+
if title_sp is None:
|
| 731 |
+
return slide_xml_bytes, 0, []
|
| 732 |
+
|
| 733 |
+
# Find the text element within the title shape
|
| 734 |
+
text_elem = title_sp.find(".//a:t", namespaces=ns)
|
| 735 |
+
if text_elem is None:
|
| 736 |
+
return slide_xml_bytes, 0, []
|
| 737 |
+
|
| 738 |
+
old_title = text_elem.text or ""
|
| 739 |
+
new_title = f"{old_title} - Part {duplicate_index}"
|
| 740 |
+
text_elem.text = new_title
|
| 741 |
+
|
| 742 |
+
new_bytes = etree.tostring(
|
| 743 |
+
root,
|
| 744 |
+
xml_declaration=True,
|
| 745 |
+
encoding="UTF-8",
|
| 746 |
+
standalone=None
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
return new_bytes, 1, [{
|
| 750 |
+
"slideNumber": slide_number,
|
| 751 |
+
"fix": "appendedPartNumber",
|
| 752 |
+
"oldTitle": old_title,
|
| 753 |
+
"newTitle": new_title
|
| 754 |
+
}]
|
| 755 |
+
|
| 756 |
+
except Exception as e:
|
| 757 |
+
print(f" ⚠️ Error fixing duplicate title on slide {slide_number}: {e}")
|
| 758 |
+
return slide_xml_bytes, 0, []
|
| 759 |
+
|
| 760 |
+
|
| 761 |
+
ALT_TEXT_MAX = 250
|
| 762 |
+
|
| 763 |
+
def check_slide_images(slide_xml: str, slide_number: int):
|
| 764 |
+
issues = []
|
| 765 |
+
|
| 766 |
+
pic_pattern = r'<p:pic[\s\S]*?</p:pic>'
|
| 767 |
+
pic_matches = re.findall(pic_pattern, slide_xml)
|
| 768 |
+
|
| 769 |
+
for pic_xml in pic_matches:
|
| 770 |
+
cnvpr_pattern = r'<p:cNvPr([^>]*)/?>'
|
| 771 |
+
m = re.search(cnvpr_pattern, pic_xml)
|
| 772 |
+
attrs = m.group(1) if m else ""
|
| 773 |
+
|
| 774 |
+
def get_attr(attr_name: str) -> str:
|
| 775 |
+
am = re.search(rf'{attr_name}="([^"]*)"', attrs)
|
| 776 |
+
return am.group(1) if am else ""
|
| 777 |
+
|
| 778 |
+
shape_id = get_attr("id")
|
| 779 |
+
shape_name = get_attr("name")
|
| 780 |
+
alt_text = get_attr("descr")
|
| 781 |
+
|
| 782 |
+
alt_text_clean = (alt_text or "").strip().lower()
|
| 783 |
+
is_decorative = (alt_text_clean == "decorative")
|
| 784 |
+
|
| 785 |
+
# --- RULES ---
|
| 786 |
+
|
| 787 |
+
# 1. Missing alt text
|
| 788 |
+
if not alt_text or alt_text.strip() == "":
|
| 789 |
+
issues.append({
|
| 790 |
+
"slideNumber": slide_number,
|
| 791 |
+
"shapeId": shape_id,
|
| 792 |
+
"shapeName": shape_name,
|
| 793 |
+
"issue": "Image missing alt text",
|
| 794 |
+
"type": "imageAltMissing"
|
| 795 |
+
})
|
| 796 |
+
|
| 797 |
+
# 2. Decorative images
|
| 798 |
+
elif is_decorative:
|
| 799 |
+
continue
|
| 800 |
+
|
| 801 |
+
# 3. Too long alt text
|
| 802 |
+
elif len(alt_text) > ALT_TEXT_MAX:
|
| 803 |
+
issues.append({
|
| 804 |
+
"slideNumber": slide_number,
|
| 805 |
+
"shapeId": shape_id,
|
| 806 |
+
"shapeName": shape_name,
|
| 807 |
+
"issue": f"Alt text exceeds {ALT_TEXT_MAX} characters",
|
| 808 |
+
"type": "imageAltTooLong",
|
| 809 |
+
"length": len(alt_text),
|
| 810 |
+
"max": ALT_TEXT_MAX
|
| 811 |
+
})
|
| 812 |
+
|
| 813 |
+
elif alt_text_clean in ["image", "picture", "photo"]:
|
| 814 |
+
issues.append({
|
| 815 |
+
"slideNumber": slide_number,
|
| 816 |
+
"shapeId": shape_id,
|
| 817 |
+
"shapeName": shape_name,
|
| 818 |
+
"issue": "Alt text is too generic",
|
| 819 |
+
"type": "imageAltTooGeneric"
|
| 820 |
+
})
|
| 821 |
+
|
| 822 |
+
return issues
|
| 823 |
+
|
| 824 |
+
def escape_xml_attr(s: str) -> str:
|
| 825 |
+
return (s.replace("&", "&")
|
| 826 |
+
.replace('"', """)
|
| 827 |
+
.replace("<", "<")
|
| 828 |
+
.replace(">", ">"))
|
| 829 |
+
|
| 830 |
+
def choose_default_alt(shape_name: str, slide_number: int) -> str:
|
| 831 |
+
"""
|
| 832 |
+
Heuristic:
|
| 833 |
+
- If it looks decorative (name hints), set "decorative"
|
| 834 |
+
- Otherwise set a non-generic placeholder
|
| 835 |
+
"""
|
| 836 |
+
n = (shape_name or "").lower()
|
| 837 |
+
decorative_hints = ["background", "bg", "decor", "decoration", "border", "divider", "logo", "icon", "watermark"]
|
| 838 |
+
if any(h in n for h in decorative_hints):
|
| 839 |
+
return "decorative"
|
| 840 |
+
return f"Image on slide {slide_number}"
|
| 841 |
+
|
| 842 |
+
def remediate_slide_alt_text(slide_xml: str, slide_number: int):
|
| 843 |
+
"""
|
| 844 |
+
Returns: (new_xml, fixed_count, fix_details)
|
| 845 |
+
Fix rules:
|
| 846 |
+
- Missing descr -> add descr (decorative or placeholder)
|
| 847 |
+
- descr > 250 -> truncate
|
| 848 |
+
- descr is generic image/picture/photo -> replace with placeholder
|
| 849 |
+
"""
|
| 850 |
+
fixed = 0
|
| 851 |
+
fix_details = []
|
| 852 |
+
|
| 853 |
+
pic_pattern = r'<p:pic[\s\S]*?</p:pic>'
|
| 854 |
+
pics = re.findall(pic_pattern, slide_xml)
|
| 855 |
+
|
| 856 |
+
# If no pics, return unchanged
|
| 857 |
+
if not pics:
|
| 858 |
+
return slide_xml, 0, []
|
| 859 |
+
|
| 860 |
+
new_xml = slide_xml
|
| 861 |
+
|
| 862 |
+
for pic_xml in pics:
|
| 863 |
+
# Extract cNvPr attrs
|
| 864 |
+
cnvpr_pattern = r'<p:cNvPr([^>]*)/?>'
|
| 865 |
+
m = re.search(cnvpr_pattern, pic_xml)
|
| 866 |
+
attrs = m.group(1) if m else ""
|
| 867 |
+
|
| 868 |
+
def get_attr(attr_name: str) -> str:
|
| 869 |
+
am = re.search(rf'{attr_name}="([^"]*)"', attrs)
|
| 870 |
+
return am.group(1) if am else ""
|
| 871 |
+
|
| 872 |
+
shape_id = get_attr("id")
|
| 873 |
+
shape_name = get_attr("name")
|
| 874 |
+
alt_text = get_attr("descr")
|
| 875 |
+
alt_clean = (alt_text or "").strip().lower()
|
| 876 |
+
|
| 877 |
+
# Decide what to write (if needed)
|
| 878 |
+
if not alt_text or alt_text.strip() == "":
|
| 879 |
+
new_alt = choose_default_alt(shape_name, slide_number)
|
| 880 |
+
fixed += 1
|
| 881 |
+
fix_details.append({
|
| 882 |
+
"slideNumber": slide_number,
|
| 883 |
+
"shapeId": shape_id,
|
| 884 |
+
"shapeName": shape_name,
|
| 885 |
+
"fix": "addedAltText",
|
| 886 |
+
"altText": new_alt
|
| 887 |
+
})
|
| 888 |
+
# update in the FULL slide XML by matching the cNvPr with this id
|
| 889 |
+
new_xml = set_cnvpr_descr(new_xml, shape_id, new_alt)
|
| 890 |
+
|
| 891 |
+
elif len(alt_text) > ALT_TEXT_MAX:
|
| 892 |
+
new_alt = alt_text[:ALT_TEXT_MAX]
|
| 893 |
+
fixed += 1
|
| 894 |
+
fix_details.append({
|
| 895 |
+
"slideNumber": slide_number,
|
| 896 |
+
"shapeId": shape_id,
|
| 897 |
+
"shapeName": shape_name,
|
| 898 |
+
"fix": "truncatedAltText",
|
| 899 |
+
"altText": new_alt
|
| 900 |
+
})
|
| 901 |
+
new_xml = set_cnvpr_descr(new_xml, shape_id, new_alt)
|
| 902 |
+
|
| 903 |
+
elif alt_clean in ["image", "picture", "photo"]:
|
| 904 |
+
new_alt = f"Image on slide {slide_number}"
|
| 905 |
+
fixed += 1
|
| 906 |
+
fix_details.append({
|
| 907 |
+
"slideNumber": slide_number,
|
| 908 |
+
"shapeId": shape_id,
|
| 909 |
+
"shapeName": shape_name,
|
| 910 |
+
"fix": "replacedGenericAltText",
|
| 911 |
+
"altText": new_alt
|
| 912 |
+
})
|
| 913 |
+
new_xml = set_cnvpr_descr(new_xml, shape_id, new_alt)
|
| 914 |
+
|
| 915 |
+
return new_xml, fixed, fix_details
|
| 916 |
+
|
| 917 |
+
def set_cnvpr_descr(full_slide_xml: str, shape_id: str, new_alt: str) -> str:
|
| 918 |
+
"""
|
| 919 |
+
Sets/updates descr="..." on the <p:cNvPr ... id="{shape_id}" ...> element.
|
| 920 |
+
Works for both self-closing (<p:cNvPr ... />) and normal (<p:cNvPr ...>).
|
| 921 |
+
"""
|
| 922 |
+
if not shape_id:
|
| 923 |
+
return full_slide_xml
|
| 924 |
+
|
| 925 |
+
escaped = escape_xml_attr(new_alt)
|
| 926 |
+
|
| 927 |
+
# 1) Replace existing descr if present
|
| 928 |
+
pattern_has_descr = rf'(<p:cNvPr\b[^>]*\bid="{re.escape(shape_id)}"[^>]*\bdescr=")([^"]*)(")'
|
| 929 |
+
if re.search(pattern_has_descr, full_slide_xml):
|
| 930 |
+
return re.sub(pattern_has_descr, rf'\1{escaped}\3', full_slide_xml)
|
| 931 |
+
|
| 932 |
+
# 2) Inject descr before the tag closes (handles .../> and ...>)
|
| 933 |
+
pattern_inject = rf'(<p:cNvPr\b[^>]*\bid="{re.escape(shape_id)}"[^>]*?)(\s*/?>)'
|
| 934 |
+
return re.sub(pattern_inject, rf'\1 descr="{escaped}"\2', full_slide_xml, count=1)
|
| 935 |
+
|
| 936 |
+
P_NS = "http://schemas.openxmlformats.org/presentationml/2006/main"
|
| 937 |
+
A_NS = "http://schemas.openxmlformats.org/drawingml/2006/main"
|
| 938 |
+
R_NS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
| 939 |
+
|
| 940 |
+
def extract_image_from_pptx_slide(
|
| 941 |
+
pptx_path: Path,
|
| 942 |
+
slide_number: int,
|
| 943 |
+
rel_id: str
|
| 944 |
+
) -> Optional[bytes]:
|
| 945 |
+
"""
|
| 946 |
+
Extract image data from PowerPoint using relationship ID
|
| 947 |
+
|
| 948 |
+
Args:
|
| 949 |
+
pptx_path: Path to the PowerPoint file
|
| 950 |
+
slide_number: Slide number (1-indexed)
|
| 951 |
+
rel_id: Relationship ID (e.g., 'rId2')
|
| 952 |
+
|
| 953 |
+
Returns:
|
| 954 |
+
Image bytes or None if not found
|
| 955 |
+
"""
|
| 956 |
+
try:
|
| 957 |
+
with zipfile.ZipFile(pptx_path, 'r') as zip_ref:
|
| 958 |
+
# Get relationship file for this slide
|
| 959 |
+
rels_path = f'ppt/slides/_rels/slide{slide_number}.xml.rels'
|
| 960 |
+
|
| 961 |
+
if rels_path not in zip_ref.namelist():
|
| 962 |
+
return None
|
| 963 |
+
|
| 964 |
+
rels_xml = zip_ref.read(rels_path).decode('utf-8')
|
| 965 |
+
|
| 966 |
+
# Find the target for this relationship ID
|
| 967 |
+
# <Relationship Id="rId2" Target="../media/image1.png" />
|
| 968 |
+
pattern = rf'<Relationship[^>]*Id="{re.escape(rel_id)}"[^>]*Target="([^"]*)"[^>]*/>'
|
| 969 |
+
match = re.search(pattern, rels_xml)
|
| 970 |
+
|
| 971 |
+
if not match:
|
| 972 |
+
return None
|
| 973 |
+
|
| 974 |
+
target = match.group(1)
|
| 975 |
+
# Convert relative path to absolute in ZIP
|
| 976 |
+
if target.startswith('../'):
|
| 977 |
+
media_path = 'ppt/' + target[3:]
|
| 978 |
+
else:
|
| 979 |
+
media_path = target
|
| 980 |
+
|
| 981 |
+
if media_path in zip_ref.namelist():
|
| 982 |
+
return zip_ref.read(media_path)
|
| 983 |
+
|
| 984 |
+
except Exception as e:
|
| 985 |
+
print(f"Error extracting image {rel_id} from slide {slide_number}: {e}")
|
| 986 |
+
|
| 987 |
+
return None
|
| 988 |
+
|
| 989 |
+
def get_image_rel_id_for_pic(pic_element, namespaces: dict) -> Optional[str]:
|
| 990 |
+
"""
|
| 991 |
+
Extract the relationship ID for an image from a p:pic element
|
| 992 |
+
|
| 993 |
+
Args:
|
| 994 |
+
pic_element: The p:pic XML element
|
| 995 |
+
namespaces: XML namespaces dict
|
| 996 |
+
|
| 997 |
+
Returns:
|
| 998 |
+
Relationship ID (e.g., 'rId2') or None
|
| 999 |
+
"""
|
| 1000 |
+
try:
|
| 1001 |
+
# Navigate: p:pic -> p:blipFill -> a:blip[@r:embed]
|
| 1002 |
+
blip = pic_element.find('.//a:blip[@r:embed]', namespaces)
|
| 1003 |
+
if blip is not None:
|
| 1004 |
+
return blip.get(f'{{{R_NS}}}embed')
|
| 1005 |
+
except Exception as e:
|
| 1006 |
+
print(f"Error getting rel ID from pic element: {e}")
|
| 1007 |
+
|
| 1008 |
+
return None
|
| 1009 |
+
|
| 1010 |
+
def set_alt_text_in_slide_xml(
|
| 1011 |
+
slide_xml_bytes: bytes,
|
| 1012 |
+
slide_number: int,
|
| 1013 |
+
pptx_path: Optional[Path] = None
|
| 1014 |
+
):
|
| 1015 |
+
"""
|
| 1016 |
+
Finds all picture cNvPr nodes and fixes their 'descr' safely.
|
| 1017 |
+
Uses FREE local AI for intelligent alt text generation.
|
| 1018 |
+
|
| 1019 |
+
Args:
|
| 1020 |
+
slide_xml_bytes: The slide XML as bytes
|
| 1021 |
+
slide_number: Slide number (1-indexed)
|
| 1022 |
+
pptx_path: Path to the PowerPoint file (needed for AI image extraction)
|
| 1023 |
+
|
| 1024 |
+
Returns: (new_xml_bytes, fixed_count, fix_details)
|
| 1025 |
+
"""
|
| 1026 |
+
parser = etree.XMLParser(remove_blank_text=False, recover=False)
|
| 1027 |
+
root = etree.fromstring(slide_xml_bytes, parser=parser)
|
| 1028 |
+
|
| 1029 |
+
ns = {
|
| 1030 |
+
"p": P_NS,
|
| 1031 |
+
"a": A_NS,
|
| 1032 |
+
"r": R_NS
|
| 1033 |
+
}
|
| 1034 |
+
|
| 1035 |
+
fixed = 0
|
| 1036 |
+
fix_details = []
|
| 1037 |
+
|
| 1038 |
+
# Check if AI is available and enabled
|
| 1039 |
+
use_ai = AI_AVAILABLE and os.getenv("ENABLE_AI_ALT_TEXT", "true").lower() == "true"
|
| 1040 |
+
|
| 1041 |
+
if use_ai:
|
| 1042 |
+
print(f"🤖 Using FREE local AI (BLIP) for slide {slide_number}")
|
| 1043 |
+
else:
|
| 1044 |
+
print(f"ℹ️ Using placeholder alt text for slide {slide_number}")
|
| 1045 |
+
|
| 1046 |
+
# Pictures: p:pic -> p:nvPicPr -> p:cNvPr
|
| 1047 |
+
pic_elements = root.xpath(".//p:pic", namespaces=ns)
|
| 1048 |
+
|
| 1049 |
+
for pic in pic_elements:
|
| 1050 |
+
cnvpr = pic.find(".//p:nvPicPr/p:cNvPr", namespaces=ns)
|
| 1051 |
+
if cnvpr is None:
|
| 1052 |
+
continue
|
| 1053 |
+
|
| 1054 |
+
shape_id = cnvpr.get("id") or ""
|
| 1055 |
+
shape_name = cnvpr.get("name") or ""
|
| 1056 |
+
descr = cnvpr.get("descr") # can be None
|
| 1057 |
+
|
| 1058 |
+
# Get relationship ID for AI image extraction
|
| 1059 |
+
rel_id = get_image_rel_id_for_pic(pic, ns) if use_ai and pptx_path else None
|
| 1060 |
+
|
| 1061 |
+
# Decide if we need a fix
|
| 1062 |
+
if descr is None or descr.strip() == "":
|
| 1063 |
+
new_alt = None
|
| 1064 |
+
|
| 1065 |
+
# Try AI generation first
|
| 1066 |
+
if use_ai and pptx_path and rel_id:
|
| 1067 |
+
try:
|
| 1068 |
+
image_data = extract_image_from_pptx_slide(pptx_path, slide_number, rel_id)
|
| 1069 |
+
if image_data:
|
| 1070 |
+
new_alt = generate_alt_text_free(
|
| 1071 |
+
image_data,
|
| 1072 |
+
shape_name=shape_name,
|
| 1073 |
+
slide_number=slide_number,
|
| 1074 |
+
max_length=ALT_TEXT_MAX
|
| 1075 |
+
)
|
| 1076 |
+
if new_alt:
|
| 1077 |
+
print(f" ✅ AI generated alt text for {shape_name}: '{new_alt[:50]}...'")
|
| 1078 |
+
except Exception as e:
|
| 1079 |
+
print(f" ⚠️ AI alt text generation failed for {shape_name}: {e}")
|
| 1080 |
+
|
| 1081 |
+
# Fallback to placeholder if AI fails or is disabled
|
| 1082 |
+
if not new_alt:
|
| 1083 |
+
new_alt = choose_default_alt(shape_name, slide_number)
|
| 1084 |
+
|
| 1085 |
+
cnvpr.set("descr", new_alt)
|
| 1086 |
+
fixed += 1
|
| 1087 |
+
fix_details.append({
|
| 1088 |
+
"slideNumber": slide_number,
|
| 1089 |
+
"shapeId": shape_id,
|
| 1090 |
+
"shapeName": shape_name,
|
| 1091 |
+
"fix": "addedAltText" if use_ai else "addedPlaceholderAltText",
|
| 1092 |
+
"altText": new_alt,
|
| 1093 |
+
"aiGenerated": use_ai and rel_id is not None
|
| 1094 |
+
})
|
| 1095 |
+
|
| 1096 |
+
elif len(descr) > ALT_TEXT_MAX:
|
| 1097 |
+
new_alt = None
|
| 1098 |
+
|
| 1099 |
+
if use_ai and pptx_path and rel_id:
|
| 1100 |
+
try:
|
| 1101 |
+
image_data = extract_image_from_pptx_slide(pptx_path, slide_number, rel_id)
|
| 1102 |
+
if image_data:
|
| 1103 |
+
new_alt = generate_alt_text_free(
|
| 1104 |
+
image_data,
|
| 1105 |
+
shape_name=shape_name,
|
| 1106 |
+
slide_number=slide_number,
|
| 1107 |
+
max_length=ALT_TEXT_MAX
|
| 1108 |
+
)
|
| 1109 |
+
except Exception as e:
|
| 1110 |
+
print(f"AI alt text generation failed for long alt text on {shape_name}: {e}")
|
| 1111 |
+
|
| 1112 |
+
if not new_alt:
|
| 1113 |
+
new_alt = descr[:ALT_TEXT_MAX]
|
| 1114 |
+
|
| 1115 |
+
cnvpr.set("descr", new_alt)
|
| 1116 |
+
fixed += 1
|
| 1117 |
+
fix_details.append({
|
| 1118 |
+
"slideNumber": slide_number,
|
| 1119 |
+
"shapeId": shape_id,
|
| 1120 |
+
"shapeName": shape_name,
|
| 1121 |
+
"fix": "replacedLongAltText" if new_alt != descr[:ALT_TEXT_MAX] else "truncatedAltText",
|
| 1122 |
+
"altText": new_alt
|
| 1123 |
+
})
|
| 1124 |
+
|
| 1125 |
+
else:
|
| 1126 |
+
# Check for generic descriptions that could be improved
|
| 1127 |
+
descr_lower = descr.lower()
|
| 1128 |
+
if descr_lower in ["image", "picture", "photo"]:
|
| 1129 |
+
new_alt = None
|
| 1130 |
+
|
| 1131 |
+
# Try AI generation for generic descriptions
|
| 1132 |
+
if use_ai and pptx_path and rel_id:
|
| 1133 |
+
try:
|
| 1134 |
+
image_data = extract_image_from_pptx_slide(pptx_path, slide_number, rel_id)
|
| 1135 |
+
if image_data:
|
| 1136 |
+
new_alt = generate_alt_text_free(
|
| 1137 |
+
image_data,
|
| 1138 |
+
shape_name=shape_name,
|
| 1139 |
+
slide_number=slide_number,
|
| 1140 |
+
max_length=ALT_TEXT_MAX
|
| 1141 |
+
)
|
| 1142 |
+
if new_alt:
|
| 1143 |
+
print(f" ✅ AI replaced generic alt text for {shape_name}: '{new_alt[:50]}...'")
|
| 1144 |
+
except Exception as e:
|
| 1145 |
+
print(f" ⚠️ AI alt text generation failed for {shape_name}: {e}")
|
| 1146 |
+
|
| 1147 |
+
# Fallback to placeholder
|
| 1148 |
+
if not new_alt:
|
| 1149 |
+
new_alt = f"Image on slide {slide_number}"
|
| 1150 |
+
|
| 1151 |
+
cnvpr.set("descr", new_alt)
|
| 1152 |
+
fixed += 1
|
| 1153 |
+
fix_details.append({
|
| 1154 |
+
"slideNumber": slide_number,
|
| 1155 |
+
"shapeId": shape_id,
|
| 1156 |
+
"shapeName": shape_name,
|
| 1157 |
+
"fix": "replacedGenericAltText",
|
| 1158 |
+
"altText": new_alt,
|
| 1159 |
+
"aiGenerated": use_ai and rel_id is not None
|
| 1160 |
+
})
|
| 1161 |
+
new_bytes = etree.tostring(
|
| 1162 |
+
root,
|
| 1163 |
+
xml_declaration=True,
|
| 1164 |
+
encoding="UTF-8",
|
| 1165 |
+
standalone=None
|
| 1166 |
+
)
|
| 1167 |
+
return new_bytes, fixed, fix_details
|
| 1168 |
+
|
| 1169 |
+
def remediate_alt_text_pptx(src_pptx: Path, dst_pptx: Path):
|
| 1170 |
+
"""
|
| 1171 |
+
Remediate alt text in PowerPoint file using AI-powered descriptions,
|
| 1172 |
+
while processing slides in true numeric presentation order.
|
| 1173 |
+
"""
|
| 1174 |
+
fixed_total = 0
|
| 1175 |
+
all_fix_details = []
|
| 1176 |
+
|
| 1177 |
+
print(f"\n🔧 Starting alt text remediation for: {src_pptx.name}")
|
| 1178 |
+
print(f" AI Mode: {os.getenv('ENABLE_AI_ALT_TEXT', 'true')}")
|
| 1179 |
+
|
| 1180 |
+
with zipfile.ZipFile(src_pptx, "r") as zin, zipfile.ZipFile(dst_pptx, "w", compression=zipfile.ZIP_DEFLATED) as zout:
|
| 1181 |
+
# Build a lookup of all original zip entries
|
| 1182 |
+
info_by_name = {item.filename: item for item in zin.infolist()}
|
| 1183 |
+
|
| 1184 |
+
# Separate slide XMLs from everything else
|
| 1185 |
+
slide_names = [
|
| 1186 |
+
name for name in info_by_name.keys()
|
| 1187 |
+
if re.match(r"ppt/slides/slide\d+\.xml$", name)
|
| 1188 |
+
]
|
| 1189 |
+
slide_names = sorted(slide_names, key=get_slide_num)
|
| 1190 |
+
|
| 1191 |
+
non_slide_names = [
|
| 1192 |
+
name for name in info_by_name.keys()
|
| 1193 |
+
if name not in slide_names
|
| 1194 |
+
]
|
| 1195 |
+
|
| 1196 |
+
# Write non-slide files first exactly as they are
|
| 1197 |
+
for name in non_slide_names:
|
| 1198 |
+
item = info_by_name[name]
|
| 1199 |
+
data = zin.read(name)
|
| 1200 |
+
zout.writestr(item, data)
|
| 1201 |
+
|
| 1202 |
+
# Then write slides in true numeric order
|
| 1203 |
+
for name in slide_names:
|
| 1204 |
+
item = info_by_name[name]
|
| 1205 |
+
data = zin.read(name)
|
| 1206 |
+
|
| 1207 |
+
slide_num = get_slide_num(name)
|
| 1208 |
+
try:
|
| 1209 |
+
new_data, fixed, details = set_alt_text_in_slide_xml(
|
| 1210 |
+
data,
|
| 1211 |
+
slide_num,
|
| 1212 |
+
pptx_path=src_pptx
|
| 1213 |
+
)
|
| 1214 |
+
if fixed:
|
| 1215 |
+
data = new_data
|
| 1216 |
+
fixed_total += fixed
|
| 1217 |
+
all_fix_details.extend(details)
|
| 1218 |
+
except Exception as e:
|
| 1219 |
+
print(f" ⚠️ Error processing slide {slide_num}: {e}")
|
| 1220 |
+
|
| 1221 |
+
zout.writestr(item, data)
|
| 1222 |
+
|
| 1223 |
+
print(f"\n✅ Remediation complete: {fixed_total} images processed")
|
| 1224 |
+
ai_count = sum(1 for d in all_fix_details if d.get("aiGenerated", False))
|
| 1225 |
+
if ai_count > 0:
|
| 1226 |
+
print(f" 🤖 {ai_count} alt texts generated by FREE local AI (no cost)")
|
| 1227 |
+
|
| 1228 |
+
return fixed_total, all_fix_details
|
| 1229 |
+
|
| 1230 |
+
def remediate_accessibility_pptx(src_pptx: Path, dst_pptx: Path):
|
| 1231 |
+
"""
|
| 1232 |
+
Remediate alt text, color contrast, and duplicate slide titles in one pass.
|
| 1233 |
+
"""
|
| 1234 |
+
alt_fixed_total = 0
|
| 1235 |
+
all_alt_fix_details = []
|
| 1236 |
+
contrast_fixed_total = 0
|
| 1237 |
+
all_contrast_fix_details = []
|
| 1238 |
+
duplicate_title_fixed_total = 0
|
| 1239 |
+
all_duplicate_title_fixes = []
|
| 1240 |
+
|
| 1241 |
+
print(f"\n🔧 Starting accessibility remediation for: {src_pptx.name}")
|
| 1242 |
+
print(f" AI Alt Text Mode: {os.getenv('ENABLE_AI_ALT_TEXT', 'true')}")
|
| 1243 |
+
|
| 1244 |
+
with zipfile.ZipFile(src_pptx, "r") as zin, zipfile.ZipFile(dst_pptx, "w", compression=zipfile.ZIP_DEFLATED) as zout:
|
| 1245 |
+
info_by_name = {item.filename: item for item in zin.infolist()}
|
| 1246 |
+
contrast_context = build_pptx_color_context(zin)
|
| 1247 |
+
|
| 1248 |
+
slide_names = [
|
| 1249 |
+
name for name in info_by_name.keys()
|
| 1250 |
+
if re.match(r"ppt/slides/slide\d+\.xml$", name)
|
| 1251 |
+
]
|
| 1252 |
+
slide_names = sorted(slide_names, key=get_slide_num)
|
| 1253 |
+
|
| 1254 |
+
non_slide_names = [
|
| 1255 |
+
name for name in info_by_name.keys()
|
| 1256 |
+
if name not in slide_names
|
| 1257 |
+
]
|
| 1258 |
+
|
| 1259 |
+
for name in non_slide_names:
|
| 1260 |
+
item = info_by_name[name]
|
| 1261 |
+
data = zin.read(name)
|
| 1262 |
+
zout.writestr(item, data)
|
| 1263 |
+
|
| 1264 |
+
previous_slide_signature = None
|
| 1265 |
+
duplicate_run_count = 1
|
| 1266 |
+
|
| 1267 |
+
for name in slide_names:
|
| 1268 |
+
item = info_by_name[name]
|
| 1269 |
+
data = zin.read(name)
|
| 1270 |
+
slide_num = get_slide_num(name)
|
| 1271 |
+
|
| 1272 |
+
# Decode to check for duplicates
|
| 1273 |
+
slide_xml_str = data.decode('utf-8', errors='ignore')
|
| 1274 |
+
current_signature = get_slide_signature(slide_xml_str)
|
| 1275 |
+
|
| 1276 |
+
# Check if this is a duplicate of the previous slide
|
| 1277 |
+
is_duplicate = (previous_slide_signature is not None and
|
| 1278 |
+
current_signature == previous_slide_signature)
|
| 1279 |
+
|
| 1280 |
+
if is_duplicate:
|
| 1281 |
+
duplicate_run_count += 1
|
| 1282 |
+
part_number = duplicate_run_count
|
| 1283 |
+
else:
|
| 1284 |
+
duplicate_run_count = 1
|
| 1285 |
+
|
| 1286 |
+
previous_slide_signature = current_signature
|
| 1287 |
+
|
| 1288 |
+
try:
|
| 1289 |
+
new_data, fixed, details = set_alt_text_in_slide_xml(
|
| 1290 |
+
data,
|
| 1291 |
+
slide_num,
|
| 1292 |
+
pptx_path=src_pptx
|
| 1293 |
+
)
|
| 1294 |
+
if fixed:
|
| 1295 |
+
data = new_data
|
| 1296 |
+
alt_fixed_total += fixed
|
| 1297 |
+
all_alt_fix_details.extend(details)
|
| 1298 |
+
except Exception as e:
|
| 1299 |
+
print(f" ⚠️ Error processing alt text on slide {slide_num}: {e}")
|
| 1300 |
+
|
| 1301 |
+
try:
|
| 1302 |
+
new_data, fixed, details = remediate_slide_color_contrast(
|
| 1303 |
+
data,
|
| 1304 |
+
slide_num,
|
| 1305 |
+
contrast_context
|
| 1306 |
+
)
|
| 1307 |
+
if fixed:
|
| 1308 |
+
data = new_data
|
| 1309 |
+
contrast_fixed_total += fixed
|
| 1310 |
+
all_contrast_fix_details.extend(details)
|
| 1311 |
+
except Exception as e:
|
| 1312 |
+
print(f" ⚠️ Error processing color contrast on slide {slide_num}: {e}")
|
| 1313 |
+
|
| 1314 |
+
# Handle duplicate slide title remediation
|
| 1315 |
+
if is_duplicate:
|
| 1316 |
+
try:
|
| 1317 |
+
new_data, fixed, details = remediate_duplicate_slide_title(
|
| 1318 |
+
data,
|
| 1319 |
+
slide_num,
|
| 1320 |
+
is_duplicate=True,
|
| 1321 |
+
duplicate_index=part_number
|
| 1322 |
+
)
|
| 1323 |
+
if fixed:
|
| 1324 |
+
data = new_data
|
| 1325 |
+
duplicate_title_fixed_total += fixed
|
| 1326 |
+
all_duplicate_title_fixes.extend(details)
|
| 1327 |
+
print(f" ✅ Duplicate slide {slide_num} title fixed: appended Part {part_number}")
|
| 1328 |
+
except Exception as e:
|
| 1329 |
+
print(f" ⚠️ Error fixing duplicate title on slide {slide_num}: {e}")
|
| 1330 |
+
|
| 1331 |
+
zout.writestr(item, data)
|
| 1332 |
+
|
| 1333 |
+
print(f"\n✅ Accessibility remediation complete")
|
| 1334 |
+
print(f" Alt text fixes: {alt_fixed_total}")
|
| 1335 |
+
print(f" Color contrast fixes: {contrast_fixed_total}")
|
| 1336 |
+
print(f" Duplicate title fixes: {duplicate_title_fixed_total}")
|
| 1337 |
+
|
| 1338 |
+
return alt_fixed_total, all_alt_fix_details, contrast_fixed_total, all_contrast_fix_details, duplicate_title_fixed_total, all_duplicate_title_fixes
|
| 1339 |
+
|
| 1340 |
+
|
| 1341 |
+
@app.get("/download")
|
| 1342 |
+
def download_all_files():
|
| 1343 |
+
candidates = [p for p in OUTPUT_DIR.glob("*") if p.is_file()]
|
| 1344 |
+
if not candidates:
|
| 1345 |
+
raise HTTPException(status_code=404, detail="No files available to download yet.")
|
| 1346 |
+
|
| 1347 |
+
zip_name = f"remediated-files-{uuid.uuid4().hex[:8]}.zip"
|
| 1348 |
+
zip_path = OUTPUT_DIR / zip_name
|
| 1349 |
+
|
| 1350 |
+
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
| 1351 |
+
for p in candidates:
|
| 1352 |
+
clean_name = re.sub(r"^[0-9a-f]{8}_", "", p.name)
|
| 1353 |
+
zf.write(p, arcname=clean_name)
|
| 1354 |
+
|
| 1355 |
+
return FileResponse(
|
| 1356 |
+
path=str(zip_path),
|
| 1357 |
+
media_type="application/zip",
|
| 1358 |
+
filename="remediated-files.zip"
|
| 1359 |
+
)
|
| 1360 |
+
|
| 1361 |
+
@app.post("/download")
|
| 1362 |
+
async def download_selected_files(request: Request):
|
| 1363 |
+
body = await request.json()
|
| 1364 |
+
|
| 1365 |
+
file_name = body.get("fileName") or body.get("filename") or body.get("suggestedFileName")
|
| 1366 |
+
files = body.get("files", [])
|
| 1367 |
+
|
| 1368 |
+
# Case 1: single file download
|
| 1369 |
+
if file_name:
|
| 1370 |
+
file_path = OUTPUT_DIR / file_name
|
| 1371 |
+
|
| 1372 |
+
if not file_path.exists():
|
| 1373 |
+
matches = list(OUTPUT_DIR.glob(f"*_{file_name}"))
|
| 1374 |
+
if matches:
|
| 1375 |
+
file_path = matches[0]
|
| 1376 |
+
else:
|
| 1377 |
+
raise HTTPException(status_code=404, detail=f"File not found: {file_name}")
|
| 1378 |
+
|
| 1379 |
+
return FileResponse(
|
| 1380 |
+
path=str(file_path),
|
| 1381 |
+
media_type="application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
| 1382 |
+
filename=file_name
|
| 1383 |
+
)
|
| 1384 |
+
|
| 1385 |
+
# Case 2: multiple files -> zip
|
| 1386 |
+
if files:
|
| 1387 |
+
zip_name = f"remediated-files-{uuid.uuid4().hex[:8]}.zip"
|
| 1388 |
+
zip_path = OUTPUT_DIR / zip_name
|
| 1389 |
+
|
| 1390 |
+
added_any = False
|
| 1391 |
+
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
| 1392 |
+
for name in files:
|
| 1393 |
+
file_path = OUTPUT_DIR / name
|
| 1394 |
+
|
| 1395 |
+
# if clean name not found, try prefixed stored file
|
| 1396 |
+
if not file_path.exists():
|
| 1397 |
+
matches = list(OUTPUT_DIR.glob(f"*_{name}"))
|
| 1398 |
+
if matches:
|
| 1399 |
+
file_path = matches[0]
|
| 1400 |
+
else:
|
| 1401 |
+
continue
|
| 1402 |
+
|
| 1403 |
+
clean_name = re.sub(r"^[0-9a-f]{8}_", "", file_path.name)
|
| 1404 |
+
zf.write(file_path, arcname=clean_name)
|
| 1405 |
+
added_any = True
|
| 1406 |
+
|
| 1407 |
+
if not added_any:
|
| 1408 |
+
raise HTTPException(status_code=404, detail="None of the requested files were found.")
|
| 1409 |
+
|
| 1410 |
+
return FileResponse(
|
| 1411 |
+
path=str(zip_path),
|
| 1412 |
+
media_type="application/zip",
|
| 1413 |
+
filename="remediated-files.zip"
|
| 1414 |
+
)
|
| 1415 |
+
|
| 1416 |
+
raise HTTPException(status_code=400, detail="No file name(s) provided.")
|
| 1417 |
+
|
| 1418 |
+
# ---------- RUN ----------
|
| 1419 |
+
if __name__ == "__main__":
|
| 1420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| 9 |
+
|
| 10 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Body
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 13 |
+
from starlette.requests import Request
|
| 14 |
+
|
| 15 |
+
# ---------- CONFIG ----------
|
| 16 |
+
UPLOAD_DIR = Path("uploads")
|
| 17 |
+
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
OUTPUT_DIR = Path("output")
|
| 20 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
# ---------- APP SETUP ----------
|
| 23 |
+
app = FastAPI()
|
| 24 |
+
|
| 25 |
+
# Configure CORS (Angular frontend -> Python backend)
|
| 26 |
+
origins = [
|
| 27 |
+
"http://localhost:4200",
|
| 28 |
+
"http://localhost:3000",
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
app.add_middleware(
|
| 32 |
+
CORSMiddleware,
|
| 33 |
+
allow_origins=origins,
|
| 34 |
+
allow_credentials=True,
|
| 35 |
+
allow_methods=["*"],
|
| 36 |
+
allow_headers=["*"],
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Optional: request logging (safe - does NOT print file bytes)
|
| 40 |
+
@app.middleware("http")
|
| 41 |
+
async def access_log(request: Request, call_next):
|
| 42 |
+
t0 = time.time()
|
| 43 |
+
response = await call_next(request)
|
| 44 |
+
ms = (time.time() - t0) * 1000
|
| 45 |
+
print(f"[{request.method}] {request.url.path} -> {response.status_code} ({ms:.2f} ms)")
|
| 46 |
+
return response
|
| 47 |
+
|
| 48 |
+
@app.get("/")
|
| 49 |
+
def health_check():
|
| 50 |
+
return {"status": "running", "service": "PowerPoint Accessibility Backend"}
|
| 51 |
+
|
| 52 |
+
# ---------- UPLOAD ROUTE ----------
|
| 53 |
+
@app.post("/upload")
|
| 54 |
+
async def upload_files(files: List[UploadFile] = File(...)):
|
| 55 |
+
"""
|
| 56 |
+
Accepts PowerPoint files, analyzes them, and returns accessibility report.
|
| 57 |
+
"""
|
| 58 |
+
if len(files) == 0:
|
| 59 |
+
raise HTTPException(status_code=400, detail="No file uploaded")
|
| 60 |
+
|
| 61 |
+
if len(files) > 7:
|
| 62 |
+
raise HTTPException(
|
| 63 |
+
status_code=400,
|
| 64 |
+
detail=f"Too many files. You uploaded {len(files)}, but the limit is 7."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# For now, handle single file upload
|
| 68 |
+
file = files[0]
|
| 69 |
+
filename = file.filename or "unnamed.pptx"
|
| 70 |
+
filename_lower = filename.lower()
|
| 71 |
+
|
| 72 |
+
# Validate extension
|
| 73 |
+
allowed_ext = (".pptx", ".ppt", ".pps", ".potx")
|
| 74 |
+
if not filename_lower.endswith(allowed_ext):
|
| 75 |
+
raise HTTPException(
|
| 76 |
+
status_code=400,
|
| 77 |
+
detail=f"Invalid file type. Please upload a PowerPoint file (.pptx, .ppt, .pps, or .potx)"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Save file
|
| 81 |
+
try:
|
| 82 |
+
file_location = UPLOAD_DIR / filename
|
| 83 |
+
with file_location.open("wb") as buffer:
|
| 84 |
+
shutil.copyfileobj(file.file, buffer)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error saving {filename}: {e}")
|
| 87 |
+
raise HTTPException(status_code=500, detail=f"Failed to save file: {str(e)}")
|
| 88 |
+
|
| 89 |
+
# Analyze the PowerPoint file
|
| 90 |
+
try:
|
| 91 |
+
report = analyze_powerpoint(file_location, filename)
|
| 92 |
+
return JSONResponse(content={
|
| 93 |
+
"fileName": filename,
|
| 94 |
+
"suggestedFileName": filename,
|
| 95 |
+
"report": report
|
| 96 |
+
})
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Error analyzing {filename}: {e}")
|
| 99 |
+
raise HTTPException(status_code=500, detail=f"Failed to analyze file: {str(e)}")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def analyze_powerpoint(file_path: Path, filename: str):
|
| 103 |
+
"""
|
| 104 |
+
Analyze PowerPoint file for accessibility issues.
|
| 105 |
+
Checks:
|
| 106 |
+
1. Slide titles (missing or empty)
|
| 107 |
+
2. Image alt text
|
| 108 |
+
3. GIF detection
|
| 109 |
+
4. Presentation title
|
| 110 |
+
5. File naming
|
| 111 |
+
6. Hidden slides
|
| 112 |
+
7. List formatting issues
|
| 113 |
+
"""
|
| 114 |
+
report = {
|
| 115 |
+
"fileName": filename,
|
| 116 |
+
"suggestedFileName": filename,
|
| 117 |
+
"summary": {"fixed": 0, "flagged": 0},
|
| 118 |
+
"details": {
|
| 119 |
+
"titleNeedsFixing": False,
|
| 120 |
+
"slidesMissingTitles": [],
|
| 121 |
+
"imagesMissingOrBadAlt": [],
|
| 122 |
+
"gifsDetected": [],
|
| 123 |
+
"fileNameNeedsFixing": False,
|
| 124 |
+
"hiddenSlidesDetected": [],
|
| 125 |
+
"listFormattingIssues": [],
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
# Open PPTX as ZIP
|
| 131 |
+
with zipfile.ZipFile(file_path, 'r') as zip_file:
|
| 132 |
+
# Check presentation title
|
| 133 |
+
try:
|
| 134 |
+
core_xml = zip_file.read('docProps/core.xml').decode('utf-8')
|
| 135 |
+
if '<dc:title></dc:title>' in core_xml or '<dc:title/>' in core_xml:
|
| 136 |
+
report["details"]["titleNeedsFixing"] = True
|
| 137 |
+
report["summary"]["flagged"] += 1
|
| 138 |
+
except:
|
| 139 |
+
pass
|
| 140 |
+
|
| 141 |
+
# Check filename
|
| 142 |
+
if '_' in filename or filename.lower().startswith('presentation') or filename.lower().startswith('untitled'):
|
| 143 |
+
report["details"]["fileNameNeedsFixing"] = True
|
| 144 |
+
report["summary"]["flagged"] += 1
|
| 145 |
+
|
| 146 |
+
# Get list of slides
|
| 147 |
+
slides = [name for name in zip_file.namelist() if name.startswith('ppt/slides/slide') and name.endswith('.xml')]
|
| 148 |
+
slides.sort()
|
| 149 |
+
|
| 150 |
+
# Analyze each slide
|
| 151 |
+
for i, slide_path in enumerate(slides):
|
| 152 |
+
slide_number = i + 1
|
| 153 |
+
slide_xml = zip_file.read(slide_path).decode('utf-8')
|
| 154 |
+
|
| 155 |
+
# Check slide title
|
| 156 |
+
title_check = check_slide_title(slide_xml, slide_number)
|
| 157 |
+
if title_check["missing"]:
|
| 158 |
+
report["details"]["slidesMissingTitles"].append(title_check)
|
| 159 |
+
report["summary"]["flagged"] += 1
|
| 160 |
+
|
| 161 |
+
# Check images
|
| 162 |
+
image_issues = check_slide_images(slide_xml, slide_number)
|
| 163 |
+
if image_issues:
|
| 164 |
+
report["details"]["imagesMissingOrBadAlt"].extend(image_issues)
|
| 165 |
+
report["summary"]["flagged"] += len(image_issues)
|
| 166 |
+
|
| 167 |
+
# Check for list formatting issues
|
| 168 |
+
list_issues = check_list_formatting(slide_xml, slide_number)
|
| 169 |
+
if list_issues:
|
| 170 |
+
report["details"]["listFormattingIssues"].extend(list_issues)
|
| 171 |
+
report["summary"]["flagged"] += len(list_issues)
|
| 172 |
+
|
| 173 |
+
# Check for GIFs
|
| 174 |
+
gif_files = [name for name in zip_file.namelist() if name.startswith('ppt/media/') and name.lower().endswith('.gif')]
|
| 175 |
+
if gif_files:
|
| 176 |
+
report["details"]["gifsDetected"] = gif_files
|
| 177 |
+
report["summary"]["flagged"] += len(gif_files)
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
print(f"Error analyzing PowerPoint: {e}")
|
| 181 |
+
raise
|
| 182 |
+
|
| 183 |
+
return report
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def check_slide_title(slide_xml: str, slide_number: int):
|
| 187 |
+
"""Check if slide has a title."""
|
| 188 |
+
# Look for title placeholder
|
| 189 |
+
title_pattern = r'<p:ph[^>]*type="(title|ctrTitle)"[^>]*>'
|
| 190 |
+
has_title_placeholder = re.search(title_pattern, slide_xml)
|
| 191 |
+
|
| 192 |
+
if not has_title_placeholder:
|
| 193 |
+
return {
|
| 194 |
+
"missing": True,
|
| 195 |
+
"slideNumber": slide_number,
|
| 196 |
+
"message": f"Slide {slide_number} is missing a title"
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
# Check if title has text
|
| 200 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 201 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 202 |
+
|
| 203 |
+
if not any(text.strip() for text in text_matches):
|
| 204 |
+
return {
|
| 205 |
+
"missing": True,
|
| 206 |
+
"slideNumber": slide_number,
|
| 207 |
+
"message": f"Slide {slide_number} has an empty title"
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
return {"missing": False}
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def check_list_formatting(slide_xml: str, slide_number: int):
|
| 214 |
+
"""Check for hyphenated paragraphs that should be lists."""
|
| 215 |
+
issues = []
|
| 216 |
+
|
| 217 |
+
# Find all text elements
|
| 218 |
+
text_pattern = r'<a:t[^>]*>(.*?)</a:t>'
|
| 219 |
+
text_matches = re.findall(text_pattern, slide_xml)
|
| 220 |
+
|
| 221 |
+
for text in text_matches:
|
| 222 |
+
# Check for hyphenated list patterns
|
| 223 |
+
if re.match(r'^[\s]*[-–—•]\s+.+', text):
|
| 224 |
+
issues.append({
|
| 225 |
+
"slideNumber": slide_number,
|
| 226 |
+
"location": f"Slide {slide_number}",
|
| 227 |
+
"issue": f'Possible improperly formatted list: "{text[:50]}..."',
|
| 228 |
+
"type": "listFormatting"
|
| 229 |
+
})
|
| 230 |
+
|
| 231 |
+
return issues
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def check_slide_images(slide_xml: str, slide_number: int):
|
| 235 |
+
"""Check images for missing alt text."""
|
| 236 |
+
issues = []
|
| 237 |
+
|
| 238 |
+
# Find all picture elements
|
| 239 |
+
pic_pattern = r'<p:pic[\s\S]*?</p:pic>'
|
| 240 |
+
pic_matches = re.findall(pic_pattern, slide_xml)
|
| 241 |
+
|
| 242 |
+
for pic_xml in pic_matches:
|
| 243 |
+
# Check for alt text in descr attribute
|
| 244 |
+
descr_pattern = r'<p:cNvPr[^>]*descr="([^"]*)"'
|
| 245 |
+
descr_match = re.search(descr_pattern, pic_xml)
|
| 246 |
+
|
| 247 |
+
alt_text = descr_match.group(1) if descr_match else ""
|
| 248 |
+
|
| 249 |
+
if not alt_text or alt_text.strip() == "":
|
| 250 |
+
issues.append({
|
| 251 |
+
"slideNumber": slide_number,
|
| 252 |
+
"location": f"Slide {slide_number}",
|
| 253 |
+
"issue": "Image missing alt text",
|
| 254 |
+
"type": "image"
|
| 255 |
+
})
|
| 256 |
+
|
| 257 |
+
return issues
|
| 258 |
+
|
| 259 |
+
# ---------- DOWNLOAD ROUTES ----------
|
| 260 |
+
@app.get("/download/{filename}")
|
| 261 |
+
def download_file(filename: str):
|
| 262 |
+
"""
|
| 263 |
+
Direct download by filename from /output.
|
| 264 |
+
"""
|
| 265 |
+
file_path = OUTPUT_DIR / filename
|
| 266 |
+
if not file_path.exists():
|
| 267 |
+
raise HTTPException(status_code=404, detail=f"File not found: {filename}")
|
| 268 |
+
|
| 269 |
+
return FileResponse(
|
| 270 |
+
path=str(file_path),
|
| 271 |
+
media_type="application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
| 272 |
+
filename=filename
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
@app.post("/download")
|
| 276 |
+
async def download_latest(payload: dict = Body(default={})):
|
| 277 |
+
"""
|
| 278 |
+
Supports current frontend that POSTs to /download.
|
| 279 |
+
If payload contains {"filename": "..."} we use that.
|
| 280 |
+
Otherwise returns the newest file from /output.
|
| 281 |
+
"""
|
| 282 |
+
filename = payload.get("filename") if isinstance(payload, dict) else None
|
| 283 |
+
|
| 284 |
+
if filename:
|
| 285 |
+
file_path = OUTPUT_DIR / filename
|
| 286 |
+
if not file_path.exists():
|
| 287 |
+
raise HTTPException(status_code=404, detail=f"File not found: {filename}")
|
| 288 |
+
else:
|
| 289 |
+
candidates = [p for p in OUTPUT_DIR.glob("*") if p.is_file()]
|
| 290 |
+
if not candidates:
|
| 291 |
+
raise HTTPException(status_code=404, detail="No files available to download yet.")
|
| 292 |
+
file_path = max(candidates, key=lambda p: p.stat().st_mtime)
|
| 293 |
+
filename = file_path.name
|
| 294 |
+
|
| 295 |
+
return FileResponse(
|
| 296 |
+
path=str(file_path),
|
| 297 |
+
media_type="application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
| 298 |
+
filename=filename
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# ---------- RUN ----------
|
| 302 |
+
if __name__ == "__main__":
|
| 303 |
+
import uvicorn
|
| 304 |
+
uvicorn.run(app, host="127.0.0.1", port=5000)
|
python-server/server_output.log
ADDED
|
Binary file (2.26 kB). View file
|
|
|
python-server/uploads/17-Inquiry_Methods.ppt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1d1c952058ea39853fd5bb58a55ea7f7df40411470b2b37baf528ecbf7a6d06f
|
| 3 |
+
size 423424
|
python-server/uploads/17-Testing_Methods.ppt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fa129bcd00c0ecd852927fd94c3397c5e785aa78b9b321be867acf23bd3e4385
|
| 3 |
+
size 404992
|
python-server/uploads/6-presentation-bottomrow.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39136c34c74592172d9ef36ea62a0a28b7e970344975dd41a7454e2e8cf3a3f2
|
| 3 |
+
size 174741
|
python-server/uploads/Accessibility_Chatbot_Spike_Presentation.pptx
ADDED
|
Binary file (38.7 kB). View file
|
|
|
python-server/uploads/COMP - 5620 UID Chapter 12 presentation-1-1-1.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e9c4505473cb243cd0e12851ecdb5ee35a5eb05f8d66f67b06fb961fe659678
|
| 3 |
+
size 15002374
|
python-server/uploads/Group 9- Chapter 13 Presentation.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b74fd2dac7a6ab08b4acbab66109df57a13d30ba7c0da2a63fce256bc4f5aea
|
| 3 |
+
size 120723
|
python-server/uploads/Group1_Chap11_V1_AB.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c74427d970a6173db462537d373612bb2bbc30930be6bf05ec68d0df134e3dad
|
| 3 |
+
size 6106915
|