VsinghiAssociates commited on
Commit
f5021c2
·
verified ·
1 Parent(s): 902caa8

Sync from GitHub via hub-sync

Browse files
Files changed (1) hide show
  1. app.py +75 -2
app.py CHANGED
@@ -95,12 +95,24 @@ PAGE = """
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;
@@ -129,21 +141,77 @@ PAGE = """
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
  """
@@ -158,6 +226,11 @@ def index():
158
  return render_page()
159
 
160
 
 
 
 
 
 
161
  @app.post("/ocr")
162
  def ocr_pdf():
163
  uploaded = request.files.get("pdf_file")
 
95
  button:hover {
96
  background: #1d4ed8;
97
  }
98
+ button:disabled {
99
+ cursor: wait;
100
+ background: #94a3b8;
101
+ }
102
  .message {
103
  padding: 14px 16px;
104
  border-top: 1px solid #e6ecf5;
105
  color: #9f1239;
106
  background: #fff1f2;
107
  }
108
+ .status {
109
+ min-height: 22px;
110
+ color: #2563eb;
111
+ font-weight: 650;
112
+ }
113
+ .status.error {
114
+ color: #9f1239;
115
+ }
116
  @media (max-width: 760px) {
117
  .grid {
118
  grid-template-columns: 1fr;
 
141
  <section>
142
  <h2>PDF OCR Converter</h2>
143
  <p>Upload a PDF and download a searchable OCR-processed PDF.</p>
144
+ <form action="/ocr" method="post" enctype="multipart/form-data" data-download-form>
145
  <input type="file" name="pdf_file" accept="application/pdf,.pdf" required>
146
  <button type="submit">Process PDF</button>
147
+ <div class="status" aria-live="polite"></div>
148
  </form>
149
  </section>
150
  <section>
151
  <h2>Excel to CSV Converter</h2>
152
  <p>Upload an Excel workbook and download the first sheet as a CSV file.</p>
153
+ <form action="/excel" method="post" enctype="multipart/form-data" data-download-form>
154
  <input type="file" name="excel_file" accept=".xlsx,.xls" required>
155
  <button type="submit">Convert to CSV</button>
156
+ <div class="status" aria-live="polite"></div>
157
  </form>
158
  </section>
159
  </div>
160
  </main>
161
+ <script>
162
+ function filenameFromDisposition(disposition, fallback) {
163
+ if (!disposition) return fallback;
164
+ const match = disposition.match(/filename="?([^"]+)"?/i);
165
+ return match ? match[1] : fallback;
166
+ }
167
+
168
+ document.querySelectorAll("[data-download-form]").forEach((form) => {
169
+ form.addEventListener("submit", async (event) => {
170
+ event.preventDefault();
171
+
172
+ const button = form.querySelector("button");
173
+ const status = form.querySelector(".status");
174
+ const originalText = button.textContent;
175
+ const fallbackName = form.action.endsWith("/excel") ? "converted.csv" : "processed.pdf";
176
+
177
+ button.disabled = true;
178
+ button.textContent = "Processing...";
179
+ status.classList.remove("error");
180
+ status.textContent = "Uploading and processing. OCR can take a few minutes for large PDFs.";
181
+
182
+ try {
183
+ const response = await fetch(form.action, {
184
+ method: "POST",
185
+ body: new FormData(form),
186
+ });
187
+
188
+ if (!response.ok) {
189
+ const message = await response.text();
190
+ throw new Error(message.replace(/<[^>]*>/g, " ").replace(/\\s+/g, " ").trim() || "Processing failed.");
191
+ }
192
+
193
+ const blob = await response.blob();
194
+ const url = URL.createObjectURL(blob);
195
+ const link = document.createElement("a");
196
+ link.href = url;
197
+ link.download = filenameFromDisposition(response.headers.get("content-disposition"), fallbackName);
198
+ document.body.appendChild(link);
199
+ link.click();
200
+ link.remove();
201
+ URL.revokeObjectURL(url);
202
+
203
+ status.textContent = "Done. Your download should start automatically.";
204
+ form.reset();
205
+ } catch (error) {
206
+ status.classList.add("error");
207
+ status.textContent = error.message || "Processing failed.";
208
+ } finally {
209
+ button.disabled = false;
210
+ button.textContent = originalText;
211
+ }
212
+ });
213
+ });
214
+ </script>
215
  </body>
216
  </html>
217
  """
 
226
  return render_page()
227
 
228
 
229
+ @app.get("/health")
230
+ def health():
231
+ return {"status": "ok"}
232
+
233
+
234
  @app.post("/ocr")
235
  def ocr_pdf():
236
  uploaded = request.files.get("pdf_file")