Spaces:
Sleeping
Sleeping
OCR Extracter web app - for pdf
#1
by VsinghiAssociates - opened
- .dockerignore +0 -12
- Dockerfile +0 -26
- Dockerfile.hf +0 -26
- README.md +6 -56
- app.py +0 -592
- requirements.txt +0 -7
.dockerignore
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
.git
|
| 2 |
-
.github
|
| 3 |
-
__pycache__/
|
| 4 |
-
*.py[cod]
|
| 5 |
-
.venv/
|
| 6 |
-
venv/
|
| 7 |
-
uploads/
|
| 8 |
-
processed/
|
| 9 |
-
*.pdf
|
| 10 |
-
*.xlsx
|
| 11 |
-
*.xls
|
| 12 |
-
*.csv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dockerfile
DELETED
|
@@ -1,26 +0,0 @@
|
|
| 1 |
-
FROM python:3.10-slim
|
| 2 |
-
|
| 3 |
-
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
-
PYTHONUNBUFFERED=1 \
|
| 5 |
-
PORT=7860
|
| 6 |
-
|
| 7 |
-
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
-
ocrmypdf \
|
| 9 |
-
tesseract-ocr \
|
| 10 |
-
poppler-utils \
|
| 11 |
-
ghostscript \
|
| 12 |
-
qpdf \
|
| 13 |
-
gcc \
|
| 14 |
-
python3-dev \
|
| 15 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
-
|
| 17 |
-
WORKDIR /app
|
| 18 |
-
|
| 19 |
-
COPY requirements.txt .
|
| 20 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
-
|
| 22 |
-
COPY . .
|
| 23 |
-
|
| 24 |
-
EXPOSE 7860
|
| 25 |
-
|
| 26 |
-
CMD ["python", "app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dockerfile.hf
DELETED
|
@@ -1,26 +0,0 @@
|
|
| 1 |
-
FROM python:3.10-slim
|
| 2 |
-
|
| 3 |
-
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
-
PYTHONUNBUFFERED=1 \
|
| 5 |
-
PORT=7860
|
| 6 |
-
|
| 7 |
-
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
-
ocrmypdf \
|
| 9 |
-
tesseract-ocr \
|
| 10 |
-
poppler-utils \
|
| 11 |
-
ghostscript \
|
| 12 |
-
qpdf \
|
| 13 |
-
gcc \
|
| 14 |
-
python3-dev \
|
| 15 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
-
|
| 17 |
-
WORKDIR /app
|
| 18 |
-
|
| 19 |
-
COPY requirements.txt .
|
| 20 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
-
|
| 22 |
-
COPY . .
|
| 23 |
-
|
| 24 |
-
EXPOSE 7860
|
| 25 |
-
|
| 26 |
-
CMD ["python", "app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,61 +1,11 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
-
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
Document Converter Pro is a Flask web app for:
|
| 13 |
-
|
| 14 |
-
- PDF OCR conversion with `ocrmypdf`
|
| 15 |
-
- Excel to CSV conversion with `pandas` and `openpyxl`
|
| 16 |
-
- Fast default OCR mode that skips pages that already contain text
|
| 17 |
-
- Streaming `.xlsx` conversion to reduce memory use on large workbooks
|
| 18 |
-
|
| 19 |
-
## Important: GitHub Pages will not run this app
|
| 20 |
-
|
| 21 |
-
This project uses Python and system OCR tools, so it cannot be hosted directly on GitHub Pages. GitHub Pages only serves static HTML/CSS/JavaScript.
|
| 22 |
-
|
| 23 |
-
Use GitHub as the source repository, then deploy the app to a Python/container host such as Hugging Face Spaces, Render, Fly.io, Railway, or any Docker-capable server.
|
| 24 |
-
|
| 25 |
-
## Deploy from GitHub to Hugging Face Spaces
|
| 26 |
-
|
| 27 |
-
1. Push this folder to a GitHub repository.
|
| 28 |
-
2. Create a Hugging Face Space.
|
| 29 |
-
3. Select **Docker** as the Space SDK.
|
| 30 |
-
4. In your GitHub repository settings, add:
|
| 31 |
-
- Repository secret `HF_TOKEN`: a Hugging Face access token with write access
|
| 32 |
-
- Repository variable `HF_SPACE`: your Space id, for example `yourname/document-converter-pro`
|
| 33 |
-
5. Push to the `main` branch, or run the **Deploy to Hugging Face Space** workflow manually from the GitHub Actions tab.
|
| 34 |
-
|
| 35 |
-
The included `Dockerfile` installs the required OCR system packages and starts `app.py` on port `7860`.
|
| 36 |
-
|
| 37 |
-
## Run locally
|
| 38 |
-
|
| 39 |
-
Install system dependencies first:
|
| 40 |
-
|
| 41 |
-
- `ocrmypdf`
|
| 42 |
-
- `tesseract-ocr`
|
| 43 |
-
- `poppler-utils`
|
| 44 |
-
- `ghostscript`
|
| 45 |
-
- `qpdf`
|
| 46 |
-
|
| 47 |
-
Then run:
|
| 48 |
-
|
| 49 |
-
```bash
|
| 50 |
-
pip install -r requirements.txt
|
| 51 |
-
python app.py
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
Open `http://localhost:7860`.
|
| 55 |
-
|
| 56 |
-
## Files
|
| 57 |
-
|
| 58 |
-
- `app.py`: deployable Flask app
|
| 59 |
-
- `Dockerfile`: container build for deployment
|
| 60 |
-
- `requirements.txt`: Python dependencies
|
| 61 |
-
- `.github/workflows/deploy-huggingface.yml`: optional GitHub Actions deployment workflow
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Ocr New
|
| 3 |
+
emoji: 🌖
|
| 4 |
+
colorFrom: pink
|
| 5 |
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: apache-2.0
|
| 9 |
---
|
| 10 |
|
| 11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
DELETED
|
@@ -1,592 +0,0 @@
|
|
| 1 |
-
import csv
|
| 2 |
-
import multiprocessing
|
| 3 |
-
import os
|
| 4 |
-
import tempfile
|
| 5 |
-
|
| 6 |
-
import ocrmypdf
|
| 7 |
-
import pandas as pd
|
| 8 |
-
from flask import Flask, after_this_request, render_template_string, request, send_file
|
| 9 |
-
from openpyxl import load_workbook
|
| 10 |
-
from werkzeug.utils import secure_filename
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
app = Flask(__name__)
|
| 14 |
-
app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
PAGE = """
|
| 18 |
-
<!doctype html>
|
| 19 |
-
<html lang="en">
|
| 20 |
-
<head>
|
| 21 |
-
<meta charset="utf-8">
|
| 22 |
-
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 23 |
-
<title>Document Converter Pro</title>
|
| 24 |
-
<style>
|
| 25 |
-
:root {
|
| 26 |
-
color-scheme: light;
|
| 27 |
-
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 28 |
-
color: #172033;
|
| 29 |
-
background: #e8edf5;
|
| 30 |
-
}
|
| 31 |
-
* {
|
| 32 |
-
box-sizing: border-box;
|
| 33 |
-
}
|
| 34 |
-
body {
|
| 35 |
-
margin: 0;
|
| 36 |
-
min-height: 100vh;
|
| 37 |
-
padding: 28px 18px;
|
| 38 |
-
background:
|
| 39 |
-
radial-gradient(circle at top left, rgba(37, 99, 235, 0.16), transparent 30rem),
|
| 40 |
-
linear-gradient(135deg, #f8fafc 0%, #e8edf5 52%, #e6f4ef 100%);
|
| 41 |
-
}
|
| 42 |
-
main {
|
| 43 |
-
width: min(1120px, 100%);
|
| 44 |
-
margin: 0 auto;
|
| 45 |
-
}
|
| 46 |
-
header {
|
| 47 |
-
display: grid;
|
| 48 |
-
gap: 18px;
|
| 49 |
-
padding: 34px 0 26px;
|
| 50 |
-
}
|
| 51 |
-
h1 {
|
| 52 |
-
margin: 0 0 8px;
|
| 53 |
-
font-size: clamp(2rem, 5vw, 4rem);
|
| 54 |
-
line-height: 1;
|
| 55 |
-
letter-spacing: 0;
|
| 56 |
-
color: #0f172a;
|
| 57 |
-
}
|
| 58 |
-
p {
|
| 59 |
-
margin: 0;
|
| 60 |
-
color: #526173;
|
| 61 |
-
line-height: 1.5;
|
| 62 |
-
}
|
| 63 |
-
.hero-copy {
|
| 64 |
-
max-width: 740px;
|
| 65 |
-
}
|
| 66 |
-
.hero-copy p {
|
| 67 |
-
font-size: 1.03rem;
|
| 68 |
-
}
|
| 69 |
-
.toolbar {
|
| 70 |
-
display: flex;
|
| 71 |
-
flex-wrap: wrap;
|
| 72 |
-
gap: 10px;
|
| 73 |
-
}
|
| 74 |
-
.pill {
|
| 75 |
-
display: inline-flex;
|
| 76 |
-
align-items: center;
|
| 77 |
-
min-height: 32px;
|
| 78 |
-
padding: 6px 10px;
|
| 79 |
-
border-radius: 999px;
|
| 80 |
-
border: 1px solid rgba(37, 99, 235, 0.18);
|
| 81 |
-
background: rgba(255, 255, 255, 0.72);
|
| 82 |
-
color: #1f3f75;
|
| 83 |
-
font-size: 0.88rem;
|
| 84 |
-
font-weight: 700;
|
| 85 |
-
}
|
| 86 |
-
.grid {
|
| 87 |
-
display: grid;
|
| 88 |
-
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 89 |
-
gap: 18px;
|
| 90 |
-
}
|
| 91 |
-
section {
|
| 92 |
-
display: grid;
|
| 93 |
-
align-content: start;
|
| 94 |
-
gap: 18px;
|
| 95 |
-
min-height: 440px;
|
| 96 |
-
padding: 26px;
|
| 97 |
-
background: rgba(255, 255, 255, 0.86);
|
| 98 |
-
border: 1px solid rgba(148, 163, 184, 0.32);
|
| 99 |
-
border-radius: 8px;
|
| 100 |
-
box-shadow: 0 20px 55px rgba(15, 23, 42, 0.12);
|
| 101 |
-
backdrop-filter: blur(12px);
|
| 102 |
-
}
|
| 103 |
-
.panel-top {
|
| 104 |
-
display: grid;
|
| 105 |
-
gap: 10px;
|
| 106 |
-
}
|
| 107 |
-
h2 {
|
| 108 |
-
margin: 0;
|
| 109 |
-
font-size: 1.35rem;
|
| 110 |
-
color: #111827;
|
| 111 |
-
}
|
| 112 |
-
.tag {
|
| 113 |
-
width: fit-content;
|
| 114 |
-
padding: 6px 9px;
|
| 115 |
-
border-radius: 6px;
|
| 116 |
-
background: #ecfdf5;
|
| 117 |
-
color: #047857;
|
| 118 |
-
font-size: 0.78rem;
|
| 119 |
-
font-weight: 800;
|
| 120 |
-
text-transform: uppercase;
|
| 121 |
-
letter-spacing: 0.05em;
|
| 122 |
-
}
|
| 123 |
-
form {
|
| 124 |
-
display: grid;
|
| 125 |
-
gap: 16px;
|
| 126 |
-
margin-top: 4px;
|
| 127 |
-
}
|
| 128 |
-
input[type="file"],
|
| 129 |
-
select {
|
| 130 |
-
width: 100%;
|
| 131 |
-
min-height: 48px;
|
| 132 |
-
padding: 12px 14px;
|
| 133 |
-
border: 1px solid #cbd5e1;
|
| 134 |
-
border-radius: 6px;
|
| 135 |
-
background: #ffffff;
|
| 136 |
-
color: #172033;
|
| 137 |
-
font: inherit;
|
| 138 |
-
}
|
| 139 |
-
input[type="file"]::file-selector-button {
|
| 140 |
-
margin-right: 12px;
|
| 141 |
-
border: 0;
|
| 142 |
-
border-radius: 5px;
|
| 143 |
-
background: #e0f2fe;
|
| 144 |
-
color: #075985;
|
| 145 |
-
padding: 9px 12px;
|
| 146 |
-
font-weight: 800;
|
| 147 |
-
}
|
| 148 |
-
button {
|
| 149 |
-
min-height: 44px;
|
| 150 |
-
border: 0;
|
| 151 |
-
border-radius: 6px;
|
| 152 |
-
background: linear-gradient(135deg, #2563eb, #059669);
|
| 153 |
-
color: white;
|
| 154 |
-
font-weight: 700;
|
| 155 |
-
cursor: pointer;
|
| 156 |
-
font: inherit;
|
| 157 |
-
box-shadow: 0 10px 22px rgba(37, 99, 235, 0.22);
|
| 158 |
-
}
|
| 159 |
-
button:hover {
|
| 160 |
-
filter: brightness(0.96);
|
| 161 |
-
}
|
| 162 |
-
button:disabled {
|
| 163 |
-
cursor: wait;
|
| 164 |
-
background: #94a3b8;
|
| 165 |
-
box-shadow: none;
|
| 166 |
-
}
|
| 167 |
-
.message {
|
| 168 |
-
padding: 14px 16px;
|
| 169 |
-
margin-bottom: 18px;
|
| 170 |
-
border: 1px solid #fecdd3;
|
| 171 |
-
border-radius: 8px;
|
| 172 |
-
color: #9f1239;
|
| 173 |
-
background: #fff1f2;
|
| 174 |
-
}
|
| 175 |
-
.progress-wrap {
|
| 176 |
-
display: none;
|
| 177 |
-
gap: 8px;
|
| 178 |
-
}
|
| 179 |
-
.progress-wrap.active {
|
| 180 |
-
display: grid;
|
| 181 |
-
}
|
| 182 |
-
.progress-label {
|
| 183 |
-
display: flex;
|
| 184 |
-
justify-content: space-between;
|
| 185 |
-
gap: 12px;
|
| 186 |
-
color: #334155;
|
| 187 |
-
font-size: 0.9rem;
|
| 188 |
-
font-weight: 750;
|
| 189 |
-
}
|
| 190 |
-
.progress-track {
|
| 191 |
-
height: 10px;
|
| 192 |
-
overflow: hidden;
|
| 193 |
-
border-radius: 999px;
|
| 194 |
-
background: #dbe5f1;
|
| 195 |
-
}
|
| 196 |
-
.progress-bar {
|
| 197 |
-
width: 0%;
|
| 198 |
-
height: 100%;
|
| 199 |
-
border-radius: inherit;
|
| 200 |
-
background: linear-gradient(90deg, #2563eb, #22c55e, #f59e0b);
|
| 201 |
-
transition: width 320ms ease;
|
| 202 |
-
}
|
| 203 |
-
.status {
|
| 204 |
-
min-height: 24px;
|
| 205 |
-
color: #2563eb;
|
| 206 |
-
font-weight: 650;
|
| 207 |
-
}
|
| 208 |
-
.status.success {
|
| 209 |
-
color: #047857;
|
| 210 |
-
}
|
| 211 |
-
.status.error {
|
| 212 |
-
color: #9f1239;
|
| 213 |
-
}
|
| 214 |
-
.download-link {
|
| 215 |
-
display: none;
|
| 216 |
-
align-items: center;
|
| 217 |
-
justify-content: center;
|
| 218 |
-
min-height: 44px;
|
| 219 |
-
border-radius: 6px;
|
| 220 |
-
background: #111827;
|
| 221 |
-
color: #ffffff;
|
| 222 |
-
text-decoration: none;
|
| 223 |
-
font-weight: 800;
|
| 224 |
-
}
|
| 225 |
-
.download-link.ready {
|
| 226 |
-
display: flex;
|
| 227 |
-
}
|
| 228 |
-
.details {
|
| 229 |
-
display: grid;
|
| 230 |
-
gap: 8px;
|
| 231 |
-
margin-top: auto;
|
| 232 |
-
padding-top: 8px;
|
| 233 |
-
color: #64748b;
|
| 234 |
-
font-size: 0.9rem;
|
| 235 |
-
}
|
| 236 |
-
@media (max-width: 760px) {
|
| 237 |
-
body {
|
| 238 |
-
padding: 18px 12px;
|
| 239 |
-
}
|
| 240 |
-
.grid {
|
| 241 |
-
grid-template-columns: 1fr;
|
| 242 |
-
}
|
| 243 |
-
section {
|
| 244 |
-
min-height: auto;
|
| 245 |
-
padding: 22px;
|
| 246 |
-
}
|
| 247 |
-
}
|
| 248 |
-
</style>
|
| 249 |
-
</head>
|
| 250 |
-
<body>
|
| 251 |
-
<main>
|
| 252 |
-
<header>
|
| 253 |
-
<div class="hero-copy">
|
| 254 |
-
<h1>Document Converter Pro</h1>
|
| 255 |
-
<p>Fast OCR and spreadsheet conversion in one focused workspace. Upload your file, watch the process complete, then download only when the processed document is ready.</p>
|
| 256 |
-
</div>
|
| 257 |
-
<div class="toolbar" aria-label="Capabilities">
|
| 258 |
-
<span class="pill">PDF OCR</span>
|
| 259 |
-
<span class="pill">Excel to CSV</span>
|
| 260 |
-
<span class="pill">Fast mode</span>
|
| 261 |
-
<span class="pill">Private processing</span>
|
| 262 |
-
</div>
|
| 263 |
-
</header>
|
| 264 |
-
{% if message %}
|
| 265 |
-
<div class="message">{{ message }}</div>
|
| 266 |
-
{% endif %}
|
| 267 |
-
<div class="grid">
|
| 268 |
-
<section>
|
| 269 |
-
<div class="panel-top">
|
| 270 |
-
<span class="tag">OCR engine</span>
|
| 271 |
-
<h2>PDF OCR Converter</h2>
|
| 272 |
-
<p>Turn scanned PDFs into searchable documents. Fast mode skips pages that already contain selectable text.</p>
|
| 273 |
-
</div>
|
| 274 |
-
<form action="/ocr" method="post" enctype="multipart/form-data" data-download-form>
|
| 275 |
-
<input type="file" name="pdf_file" accept="application/pdf,.pdf" required>
|
| 276 |
-
<select name="mode" aria-label="OCR mode">
|
| 277 |
-
<option value="fast" selected>Fast OCR - skip pages that already have text</option>
|
| 278 |
-
<option value="accurate">High accuracy - slower full OCR</option>
|
| 279 |
-
</select>
|
| 280 |
-
<button type="submit">Convert PDF with OCR</button>
|
| 281 |
-
<div class="progress-wrap" aria-hidden="true">
|
| 282 |
-
<div class="progress-label">
|
| 283 |
-
<span>Processing document</span>
|
| 284 |
-
<span data-progress-value>0%</span>
|
| 285 |
-
</div>
|
| 286 |
-
<div class="progress-track">
|
| 287 |
-
<div class="progress-bar"></div>
|
| 288 |
-
</div>
|
| 289 |
-
</div>
|
| 290 |
-
<div class="status" aria-live="polite"></div>
|
| 291 |
-
<a class="download-link" href="#" download>Download processed document</a>
|
| 292 |
-
</form>
|
| 293 |
-
<div class="details">
|
| 294 |
-
<span>Best for: scanned PDFs, contracts, reports, office paperwork.</span>
|
| 295 |
-
<span>Tip: use high accuracy only when fast mode misses text.</span>
|
| 296 |
-
</div>
|
| 297 |
-
</section>
|
| 298 |
-
<section>
|
| 299 |
-
<div class="panel-top">
|
| 300 |
-
<span class="tag">Spreadsheet export</span>
|
| 301 |
-
<h2>Excel to CSV Converter</h2>
|
| 302 |
-
<p>Convert the first worksheet into a clean CSV file using a memory-efficient streaming path for XLSX workbooks.</p>
|
| 303 |
-
</div>
|
| 304 |
-
<form action="/excel" method="post" enctype="multipart/form-data" data-download-form>
|
| 305 |
-
<input type="file" name="excel_file" accept=".xlsx,.xls" required>
|
| 306 |
-
<button type="submit">Convert to CSV</button>
|
| 307 |
-
<div class="progress-wrap" aria-hidden="true">
|
| 308 |
-
<div class="progress-label">
|
| 309 |
-
<span>Processing document</span>
|
| 310 |
-
<span data-progress-value>0%</span>
|
| 311 |
-
</div>
|
| 312 |
-
<div class="progress-track">
|
| 313 |
-
<div class="progress-bar"></div>
|
| 314 |
-
</div>
|
| 315 |
-
</div>
|
| 316 |
-
<div class="status" aria-live="polite"></div>
|
| 317 |
-
<a class="download-link" href="#" download>Download processed document</a>
|
| 318 |
-
</form>
|
| 319 |
-
<div class="details">
|
| 320 |
-
<span>Best for: tables, exports, lead lists, accounting sheets.</span>
|
| 321 |
-
<span>Output: first worksheet saved as UTF-8 CSV.</span>
|
| 322 |
-
</div>
|
| 323 |
-
</section>
|
| 324 |
-
</div>
|
| 325 |
-
</main>
|
| 326 |
-
<script>
|
| 327 |
-
function filenameFromDisposition(disposition, fallback) {
|
| 328 |
-
if (!disposition) return fallback;
|
| 329 |
-
const match = disposition.match(/filename="?([^"]+)"?/i);
|
| 330 |
-
return match ? match[1] : fallback;
|
| 331 |
-
}
|
| 332 |
-
|
| 333 |
-
async function errorFromResponse(response, fallback) {
|
| 334 |
-
const contentType = response.headers.get("content-type") || "";
|
| 335 |
-
if (contentType.includes("application/json")) {
|
| 336 |
-
const payload = await response.json();
|
| 337 |
-
return payload.error || fallback;
|
| 338 |
-
}
|
| 339 |
-
const message = await response.text();
|
| 340 |
-
return message.replace(/<[^>]*>/g, " ").replace(/\\s+/g, " ").trim() || fallback;
|
| 341 |
-
}
|
| 342 |
-
|
| 343 |
-
document.querySelectorAll("[data-download-form]").forEach((form) => {
|
| 344 |
-
let activeDownloadUrl = null;
|
| 345 |
-
|
| 346 |
-
form.addEventListener("submit", async (event) => {
|
| 347 |
-
event.preventDefault();
|
| 348 |
-
|
| 349 |
-
const button = form.querySelector("button");
|
| 350 |
-
const status = form.querySelector(".status");
|
| 351 |
-
const progressWrap = form.querySelector(".progress-wrap");
|
| 352 |
-
const progressBar = form.querySelector(".progress-bar");
|
| 353 |
-
const progressValue = form.querySelector("[data-progress-value]");
|
| 354 |
-
const downloadLink = form.querySelector(".download-link");
|
| 355 |
-
const originalText = button.textContent;
|
| 356 |
-
const isExcel = form.action.endsWith("/excel");
|
| 357 |
-
const fallbackName = isExcel ? "converted.csv" : "processed.pdf";
|
| 358 |
-
let progress = 0;
|
| 359 |
-
let progressTimer = null;
|
| 360 |
-
|
| 361 |
-
if (activeDownloadUrl) {
|
| 362 |
-
URL.revokeObjectURL(activeDownloadUrl);
|
| 363 |
-
activeDownloadUrl = null;
|
| 364 |
-
}
|
| 365 |
-
downloadLink.classList.remove("ready");
|
| 366 |
-
downloadLink.removeAttribute("href");
|
| 367 |
-
|
| 368 |
-
const setProgress = (value) => {
|
| 369 |
-
progress = Math.max(progress, Math.min(value, 100));
|
| 370 |
-
progressBar.style.width = `${progress}%`;
|
| 371 |
-
progressValue.textContent = `${Math.round(progress)}%`;
|
| 372 |
-
};
|
| 373 |
-
|
| 374 |
-
button.disabled = true;
|
| 375 |
-
button.textContent = "Processing...";
|
| 376 |
-
progressWrap.classList.add("active");
|
| 377 |
-
progressWrap.setAttribute("aria-hidden", "false");
|
| 378 |
-
setProgress(12);
|
| 379 |
-
status.classList.remove("error", "success");
|
| 380 |
-
status.textContent = isExcel
|
| 381 |
-
? "Uploading and converting spreadsheet. Large workbooks may take a moment."
|
| 382 |
-
: "Uploading and running OCR on PDF. Multi-page documents may take a moment.";
|
| 383 |
-
|
| 384 |
-
try {
|
| 385 |
-
const formData = new FormData(form);
|
| 386 |
-
|
| 387 |
-
progressTimer = window.setInterval(() => {
|
| 388 |
-
const next = progress < 55 ? progress + 7 : progress < 86 ? progress + 3 : progress + 0.6;
|
| 389 |
-
setProgress(Math.min(next, 94));
|
| 390 |
-
}, 650);
|
| 391 |
-
|
| 392 |
-
const response = await fetch(form.action, {
|
| 393 |
-
method: "POST",
|
| 394 |
-
body: formData,
|
| 395 |
-
});
|
| 396 |
-
|
| 397 |
-
if (!response.ok) {
|
| 398 |
-
throw new Error(await errorFromResponse(response, "Processing failed."));
|
| 399 |
-
}
|
| 400 |
-
|
| 401 |
-
const blob = await response.blob();
|
| 402 |
-
activeDownloadUrl = URL.createObjectURL(blob);
|
| 403 |
-
downloadLink.href = activeDownloadUrl;
|
| 404 |
-
downloadLink.download = filenameFromDisposition(response.headers.get("content-disposition"), fallbackName);
|
| 405 |
-
downloadLink.classList.add("ready");
|
| 406 |
-
|
| 407 |
-
setProgress(100);
|
| 408 |
-
status.classList.add("success");
|
| 409 |
-
status.textContent = "Processed. Your document is ready to download.";
|
| 410 |
-
} catch (error) {
|
| 411 |
-
setProgress(100);
|
| 412 |
-
status.classList.add("error");
|
| 413 |
-
status.textContent = error.message || "Processing failed.";
|
| 414 |
-
} finally {
|
| 415 |
-
if (progressTimer) {
|
| 416 |
-
window.clearInterval(progressTimer);
|
| 417 |
-
}
|
| 418 |
-
button.disabled = false;
|
| 419 |
-
button.textContent = originalText;
|
| 420 |
-
}
|
| 421 |
-
});
|
| 422 |
-
});
|
| 423 |
-
</script>
|
| 424 |
-
</body>
|
| 425 |
-
</html>
|
| 426 |
-
"""
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
def render_page(message=None):
|
| 430 |
-
return render_template_string(PAGE, message=message)
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
@app.get("/")
|
| 434 |
-
def index():
|
| 435 |
-
return render_page()
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
@app.get("/health")
|
| 439 |
-
def health():
|
| 440 |
-
return {"status": "ok"}
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
def remove_later(path):
|
| 444 |
-
def cleanup():
|
| 445 |
-
try:
|
| 446 |
-
os.remove(path)
|
| 447 |
-
except OSError:
|
| 448 |
-
pass
|
| 449 |
-
|
| 450 |
-
@after_this_request
|
| 451 |
-
def schedule_cleanup(response):
|
| 452 |
-
response.call_on_close(cleanup)
|
| 453 |
-
return response
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
def save_pdf_upload(uploaded):
|
| 457 |
-
if not uploaded or uploaded.filename == "":
|
| 458 |
-
raise ValueError("Please upload a PDF file.")
|
| 459 |
-
|
| 460 |
-
filename = secure_filename(uploaded.filename)
|
| 461 |
-
if not filename.lower().endswith(".pdf"):
|
| 462 |
-
raise ValueError("Please upload a valid PDF file.")
|
| 463 |
-
|
| 464 |
-
input_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
|
| 465 |
-
input_file.close()
|
| 466 |
-
uploaded.save(input_file.name)
|
| 467 |
-
return filename, input_file.name
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
def ocr_options(mode):
|
| 471 |
-
workers = max(1, min(4, multiprocessing.cpu_count()))
|
| 472 |
-
if mode == "accurate":
|
| 473 |
-
return {
|
| 474 |
-
"force_ocr": True,
|
| 475 |
-
"deskew": True,
|
| 476 |
-
"rotate_pages": True,
|
| 477 |
-
"oversample": 300,
|
| 478 |
-
"optimize": 1,
|
| 479 |
-
"jobs": workers,
|
| 480 |
-
"progress_bar": False,
|
| 481 |
-
}
|
| 482 |
-
|
| 483 |
-
return {
|
| 484 |
-
"skip_text": True,
|
| 485 |
-
"deskew": False,
|
| 486 |
-
"rotate_pages": False,
|
| 487 |
-
"oversample": 150,
|
| 488 |
-
"optimize": 1,
|
| 489 |
-
"jobs": workers,
|
| 490 |
-
"progress_bar": False,
|
| 491 |
-
}
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
@app.post("/ocr")
|
| 495 |
-
def ocr_pdf():
|
| 496 |
-
output_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
|
| 497 |
-
output_file.close()
|
| 498 |
-
output_path = output_file.name
|
| 499 |
-
output_sent = False
|
| 500 |
-
input_path = None
|
| 501 |
-
|
| 502 |
-
try:
|
| 503 |
-
filename, input_path = save_pdf_upload(request.files.get("pdf_file"))
|
| 504 |
-
os.remove(output_path)
|
| 505 |
-
ocrmypdf.ocr(
|
| 506 |
-
input_path,
|
| 507 |
-
output_path,
|
| 508 |
-
**ocr_options(request.form.get("mode", "fast")),
|
| 509 |
-
)
|
| 510 |
-
download_name = f"ocr_{filename}"
|
| 511 |
-
remove_later(output_path)
|
| 512 |
-
output_sent = True
|
| 513 |
-
return send_file(output_path, as_attachment=True, download_name=download_name)
|
| 514 |
-
except ValueError as exc:
|
| 515 |
-
return render_page(str(exc)), 400
|
| 516 |
-
except Exception as exc:
|
| 517 |
-
return render_page(f"OCR failed: {exc}"), 500
|
| 518 |
-
finally:
|
| 519 |
-
if input_path:
|
| 520 |
-
try:
|
| 521 |
-
os.remove(input_path)
|
| 522 |
-
except OSError:
|
| 523 |
-
pass
|
| 524 |
-
if not output_sent:
|
| 525 |
-
try:
|
| 526 |
-
os.remove(output_path)
|
| 527 |
-
except OSError:
|
| 528 |
-
pass
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
def convert_xlsx_to_csv(input_path, output_path):
|
| 532 |
-
workbook = load_workbook(input_path, read_only=True, data_only=True)
|
| 533 |
-
try:
|
| 534 |
-
sheet = workbook.active
|
| 535 |
-
with open(output_path, "w", newline="", encoding="utf-8") as csv_file:
|
| 536 |
-
writer = csv.writer(csv_file)
|
| 537 |
-
for row in sheet.iter_rows(values_only=True):
|
| 538 |
-
writer.writerow(["" if value is None else value for value in row])
|
| 539 |
-
finally:
|
| 540 |
-
workbook.close()
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
def convert_excel_file(input_path, output_path, extension):
|
| 544 |
-
if extension == ".xlsx":
|
| 545 |
-
convert_xlsx_to_csv(input_path, output_path)
|
| 546 |
-
return
|
| 547 |
-
|
| 548 |
-
df = pd.read_excel(
|
| 549 |
-
input_path,
|
| 550 |
-
dtype=str,
|
| 551 |
-
parse_dates=False,
|
| 552 |
-
keep_default_na=False,
|
| 553 |
-
)
|
| 554 |
-
df.to_csv(output_path, index=False, encoding="utf-8")
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
@app.post("/excel")
|
| 558 |
-
def excel_to_csv():
|
| 559 |
-
uploaded = request.files.get("excel_file")
|
| 560 |
-
if not uploaded or uploaded.filename == "":
|
| 561 |
-
return render_page("Please upload an Excel file."), 400
|
| 562 |
-
|
| 563 |
-
filename = secure_filename(uploaded.filename)
|
| 564 |
-
if not filename.lower().endswith((".xlsx", ".xls")):
|
| 565 |
-
return render_page("Please upload a valid Excel file."), 400
|
| 566 |
-
|
| 567 |
-
extension = os.path.splitext(filename)[1].lower()
|
| 568 |
-
input_file = tempfile.NamedTemporaryFile(suffix=extension, delete=False)
|
| 569 |
-
output_file = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
|
| 570 |
-
input_file.close()
|
| 571 |
-
output_file.close()
|
| 572 |
-
|
| 573 |
-
try:
|
| 574 |
-
uploaded.save(input_file.name)
|
| 575 |
-
convert_excel_file(input_file.name, output_file.name, extension)
|
| 576 |
-
download_name = f"{os.path.splitext(filename)[0]}.csv"
|
| 577 |
-
remove_later(output_file.name)
|
| 578 |
-
return send_file(output_file.name, as_attachment=True, download_name=download_name)
|
| 579 |
-
except Exception as exc:
|
| 580 |
-
return render_page(f"Conversion failed: {exc}"), 500
|
| 581 |
-
finally:
|
| 582 |
-
try:
|
| 583 |
-
os.remove(input_file.name)
|
| 584 |
-
except OSError:
|
| 585 |
-
pass
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
if __name__ == "__main__":
|
| 589 |
-
app.run(
|
| 590 |
-
host=os.getenv("HOST", "0.0.0.0"),
|
| 591 |
-
port=int(os.getenv("PORT", "7860")),
|
| 592 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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