Spaces:
Sleeping
Sleeping
Sync from GitHub via hub-sync
Browse files- README.md +2 -2
- app.py +217 -78
- requirements.txt +2 -2
README.md
CHANGED
|
@@ -9,7 +9,7 @@ app_port: 7860
|
|
| 9 |
|
| 10 |
# Document Converter Pro
|
| 11 |
|
| 12 |
-
Document Converter Pro is a
|
| 13 |
|
| 14 |
- PDF OCR conversion with `ocrmypdf`
|
| 15 |
- Excel to CSV conversion with `pandas` and `openpyxl`
|
|
@@ -53,7 +53,7 @@ Open `http://localhost:7860`.
|
|
| 53 |
|
| 54 |
## Files
|
| 55 |
|
| 56 |
-
- `app.py`: deployable
|
| 57 |
- `Dockerfile`: container build for deployment
|
| 58 |
- `requirements.txt`: Python dependencies
|
| 59 |
- `.github/workflows/deploy-huggingface.yml`: optional GitHub Actions deployment workflow
|
|
|
|
| 9 |
|
| 10 |
# Document Converter Pro
|
| 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`
|
|
|
|
| 53 |
|
| 54 |
## Files
|
| 55 |
|
| 56 |
+
- `app.py`: deployable Flask app
|
| 57 |
- `Dockerfile`: container build for deployment
|
| 58 |
- `requirements.txt`: Python dependencies
|
| 59 |
- `.github/workflows/deploy-huggingface.yml`: optional GitHub Actions deployment workflow
|
app.py
CHANGED
|
@@ -1,98 +1,237 @@
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
|
| 4 |
-
import gradio as gr
|
| 5 |
import ocrmypdf
|
| 6 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
| 12 |
-
return None, "Please upload a PDF file"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def convert_excel_to_csv(input_excel):
|
| 30 |
-
"""Convert Excel file to CSV."""
|
| 31 |
-
if input_excel is None:
|
| 32 |
-
return None, "Please upload an Excel file"
|
| 33 |
|
| 34 |
try:
|
|
|
|
| 35 |
df = pd.read_excel(
|
| 36 |
-
|
| 37 |
engine="openpyxl",
|
| 38 |
dtype=str,
|
| 39 |
parse_dates=False,
|
| 40 |
keep_default_na=False,
|
| 41 |
)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
except Exception as e:
|
| 53 |
-
return None, f"Error converting Excel: {str(e)}"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
with gr.Blocks(title="Document Converter Pro", theme=gr.themes.Soft()) as demo:
|
| 57 |
-
gr.Markdown("# Document Converter Pro")
|
| 58 |
-
gr.Markdown("Convert your documents with ease using our powerful online tools")
|
| 59 |
-
|
| 60 |
-
with gr.Tabs():
|
| 61 |
-
with gr.TabItem("PDF OCR Converter"):
|
| 62 |
-
gr.Markdown("### Convert scanned PDFs to searchable, text-based PDFs")
|
| 63 |
-
with gr.Row():
|
| 64 |
-
with gr.Column():
|
| 65 |
-
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 66 |
-
pdf_btn = gr.Button("Process PDF with OCR")
|
| 67 |
-
with gr.Column():
|
| 68 |
-
pdf_output = gr.File(label="Download OCRed PDF")
|
| 69 |
-
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 70 |
-
|
| 71 |
-
pdf_btn.click(
|
| 72 |
-
fn=ocr_pdf,
|
| 73 |
-
inputs=pdf_input,
|
| 74 |
-
outputs=[pdf_output, pdf_status],
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
with gr.TabItem("Excel to CSV Converter"):
|
| 78 |
-
gr.Markdown("### Convert Excel files to CSV format quickly and efficiently")
|
| 79 |
-
with gr.Row():
|
| 80 |
-
with gr.Column():
|
| 81 |
-
excel_input = gr.File(label="Upload Excel", file_types=[".xlsx", ".xls"])
|
| 82 |
-
excel_btn = gr.Button("Convert to CSV")
|
| 83 |
-
with gr.Column():
|
| 84 |
-
excel_output = gr.File(label="Download CSV")
|
| 85 |
-
excel_status = gr.Textbox(label="Status", interactive=False)
|
| 86 |
-
|
| 87 |
-
excel_btn.click(
|
| 88 |
-
fn=convert_excel_to_csv,
|
| 89 |
-
inputs=excel_input,
|
| 90 |
-
outputs=[excel_output, excel_status],
|
| 91 |
-
)
|
| 92 |
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
|
|
|
|
| 4 |
import ocrmypdf
|
| 5 |
import pandas as pd
|
| 6 |
+
from flask import Flask, render_template_string, request, send_file
|
| 7 |
+
from werkzeug.utils import secure_filename
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
PAGE = """
|
| 15 |
+
<!doctype html>
|
| 16 |
+
<html lang="en">
|
| 17 |
+
<head>
|
| 18 |
+
<meta charset="utf-8">
|
| 19 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 20 |
+
<title>Document Converter Pro</title>
|
| 21 |
+
<style>
|
| 22 |
+
:root {
|
| 23 |
+
color-scheme: light;
|
| 24 |
+
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 25 |
+
color: #172033;
|
| 26 |
+
background: #eef2f7;
|
| 27 |
+
}
|
| 28 |
+
body {
|
| 29 |
+
margin: 0;
|
| 30 |
+
min-height: 100vh;
|
| 31 |
+
display: grid;
|
| 32 |
+
place-items: center;
|
| 33 |
+
padding: 32px 16px;
|
| 34 |
+
}
|
| 35 |
+
main {
|
| 36 |
+
width: min(960px, 100%);
|
| 37 |
+
background: #ffffff;
|
| 38 |
+
border: 1px solid #dbe3ef;
|
| 39 |
+
border-radius: 8px;
|
| 40 |
+
box-shadow: 0 18px 50px rgba(23, 32, 51, 0.12);
|
| 41 |
+
overflow: hidden;
|
| 42 |
+
}
|
| 43 |
+
header {
|
| 44 |
+
padding: 28px 32px;
|
| 45 |
+
border-bottom: 1px solid #e6ecf5;
|
| 46 |
+
background: #f8fafc;
|
| 47 |
+
}
|
| 48 |
+
h1 {
|
| 49 |
+
margin: 0 0 8px;
|
| 50 |
+
font-size: clamp(1.6rem, 4vw, 2.4rem);
|
| 51 |
+
letter-spacing: 0;
|
| 52 |
+
}
|
| 53 |
+
p {
|
| 54 |
+
margin: 0;
|
| 55 |
+
color: #526173;
|
| 56 |
+
line-height: 1.5;
|
| 57 |
+
}
|
| 58 |
+
.grid {
|
| 59 |
+
display: grid;
|
| 60 |
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 61 |
+
gap: 0;
|
| 62 |
+
}
|
| 63 |
+
section {
|
| 64 |
+
padding: 32px;
|
| 65 |
+
}
|
| 66 |
+
section + section {
|
| 67 |
+
border-left: 1px solid #e6ecf5;
|
| 68 |
+
}
|
| 69 |
+
h2 {
|
| 70 |
+
margin: 0 0 10px;
|
| 71 |
+
font-size: 1.15rem;
|
| 72 |
+
}
|
| 73 |
+
form {
|
| 74 |
+
display: grid;
|
| 75 |
+
gap: 16px;
|
| 76 |
+
margin-top: 22px;
|
| 77 |
+
}
|
| 78 |
+
input[type="file"] {
|
| 79 |
+
width: 100%;
|
| 80 |
+
box-sizing: border-box;
|
| 81 |
+
padding: 12px;
|
| 82 |
+
border: 1px solid #cbd5e1;
|
| 83 |
+
border-radius: 6px;
|
| 84 |
+
background: #f8fafc;
|
| 85 |
+
}
|
| 86 |
+
button {
|
| 87 |
+
min-height: 44px;
|
| 88 |
+
border: 0;
|
| 89 |
+
border-radius: 6px;
|
| 90 |
+
background: #2563eb;
|
| 91 |
+
color: white;
|
| 92 |
+
font-weight: 700;
|
| 93 |
+
cursor: pointer;
|
| 94 |
+
}
|
| 95 |
+
button:hover {
|
| 96 |
+
background: #1d4ed8;
|
| 97 |
+
}
|
| 98 |
+
.message {
|
| 99 |
+
padding: 14px 16px;
|
| 100 |
+
border-top: 1px solid #e6ecf5;
|
| 101 |
+
color: #9f1239;
|
| 102 |
+
background: #fff1f2;
|
| 103 |
+
}
|
| 104 |
+
@media (max-width: 760px) {
|
| 105 |
+
.grid {
|
| 106 |
+
grid-template-columns: 1fr;
|
| 107 |
+
}
|
| 108 |
+
section + section {
|
| 109 |
+
border-left: 0;
|
| 110 |
+
border-top: 1px solid #e6ecf5;
|
| 111 |
+
}
|
| 112 |
+
header,
|
| 113 |
+
section {
|
| 114 |
+
padding: 24px;
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
</style>
|
| 118 |
+
</head>
|
| 119 |
+
<body>
|
| 120 |
+
<main>
|
| 121 |
+
<header>
|
| 122 |
+
<h1>Document Converter Pro</h1>
|
| 123 |
+
<p>Convert scanned PDFs into searchable PDFs, or export Excel sheets as CSV files.</p>
|
| 124 |
+
</header>
|
| 125 |
+
{% if message %}
|
| 126 |
+
<div class="message">{{ message }}</div>
|
| 127 |
+
{% endif %}
|
| 128 |
+
<div class="grid">
|
| 129 |
+
<section>
|
| 130 |
+
<h2>PDF OCR Converter</h2>
|
| 131 |
+
<p>Upload a PDF and download a searchable OCR-processed PDF.</p>
|
| 132 |
+
<form action="/ocr" method="post" enctype="multipart/form-data">
|
| 133 |
+
<input type="file" name="pdf_file" accept="application/pdf,.pdf" required>
|
| 134 |
+
<button type="submit">Process PDF</button>
|
| 135 |
+
</form>
|
| 136 |
+
</section>
|
| 137 |
+
<section>
|
| 138 |
+
<h2>Excel to CSV Converter</h2>
|
| 139 |
+
<p>Upload an Excel workbook and download the first sheet as a CSV file.</p>
|
| 140 |
+
<form action="/excel" method="post" enctype="multipart/form-data">
|
| 141 |
+
<input type="file" name="excel_file" accept=".xlsx,.xls" required>
|
| 142 |
+
<button type="submit">Convert to CSV</button>
|
| 143 |
+
</form>
|
| 144 |
+
</section>
|
| 145 |
+
</div>
|
| 146 |
+
</main>
|
| 147 |
+
</body>
|
| 148 |
+
</html>
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def render_page(message=None):
|
| 153 |
+
return render_template_string(PAGE, message=message)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
@app.get("/")
|
| 157 |
+
def index():
|
| 158 |
+
return render_page()
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
@app.post("/ocr")
|
| 162 |
+
def ocr_pdf():
|
| 163 |
+
uploaded = request.files.get("pdf_file")
|
| 164 |
+
if not uploaded or uploaded.filename == "":
|
| 165 |
+
return render_page("Please upload a PDF file."), 400
|
| 166 |
+
|
| 167 |
+
filename = secure_filename(uploaded.filename)
|
| 168 |
+
if not filename.lower().endswith(".pdf"):
|
| 169 |
+
return render_page("Please upload a valid PDF file."), 400
|
| 170 |
+
|
| 171 |
+
input_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
|
| 172 |
+
output_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
|
| 173 |
+
input_file.close()
|
| 174 |
+
output_file.close()
|
| 175 |
|
| 176 |
+
try:
|
| 177 |
+
uploaded.save(input_file.name)
|
| 178 |
+
ocrmypdf.ocr(
|
| 179 |
+
input_file.name,
|
| 180 |
+
output_file.name,
|
| 181 |
+
force_ocr=True,
|
| 182 |
+
deskew=True,
|
| 183 |
+
rotate_pages=True,
|
| 184 |
+
oversample=300,
|
| 185 |
+
)
|
| 186 |
+
download_name = f"ocr_{filename}"
|
| 187 |
+
return send_file(output_file.name, as_attachment=True, download_name=download_name)
|
| 188 |
+
except Exception as exc:
|
| 189 |
+
return render_page(f"OCR failed: {exc}"), 500
|
| 190 |
+
finally:
|
| 191 |
+
try:
|
| 192 |
+
os.remove(input_file.name)
|
| 193 |
+
except OSError:
|
| 194 |
+
pass
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
@app.post("/excel")
|
| 198 |
+
def excel_to_csv():
|
| 199 |
+
uploaded = request.files.get("excel_file")
|
| 200 |
+
if not uploaded or uploaded.filename == "":
|
| 201 |
+
return render_page("Please upload an Excel file."), 400
|
| 202 |
+
|
| 203 |
+
filename = secure_filename(uploaded.filename)
|
| 204 |
+
if not filename.lower().endswith((".xlsx", ".xls")):
|
| 205 |
+
return render_page("Please upload a valid Excel file."), 400
|
| 206 |
+
|
| 207 |
+
input_file = tempfile.NamedTemporaryFile(suffix=os.path.splitext(filename)[1], delete=False)
|
| 208 |
+
output_file = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
|
| 209 |
+
input_file.close()
|
| 210 |
+
output_file.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
try:
|
| 213 |
+
uploaded.save(input_file.name)
|
| 214 |
df = pd.read_excel(
|
| 215 |
+
input_file.name,
|
| 216 |
engine="openpyxl",
|
| 217 |
dtype=str,
|
| 218 |
parse_dates=False,
|
| 219 |
keep_default_na=False,
|
| 220 |
)
|
| 221 |
+
df.to_csv(output_file.name, index=False, encoding="utf-8")
|
| 222 |
+
download_name = f"{os.path.splitext(filename)[0]}.csv"
|
| 223 |
+
return send_file(output_file.name, as_attachment=True, download_name=download_name)
|
| 224 |
+
except Exception as exc:
|
| 225 |
+
return render_page(f"Conversion failed: {exc}"), 500
|
| 226 |
+
finally:
|
| 227 |
+
try:
|
| 228 |
+
os.remove(input_file.name)
|
| 229 |
+
except OSError:
|
| 230 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|
| 234 |
+
app.run(
|
| 235 |
+
host=os.getenv("HOST", "0.0.0.0"),
|
| 236 |
+
port=int(os.getenv("PORT", "7860")),
|
| 237 |
)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
pandas==2.2.2
|
| 2 |
openpyxl==3.1.2
|
| 3 |
-
|
| 4 |
-
|
| 5 |
ocrmypdf==16.11.1
|
|
|
|
| 1 |
pandas==2.2.2
|
| 2 |
openpyxl==3.1.2
|
| 3 |
+
Flask==3.0.3
|
| 4 |
+
Werkzeug==3.0.6
|
| 5 |
ocrmypdf==16.11.1
|