Spaces:
Sleeping
Sleeping
Sync from GitHub via hub-sync
Browse files
README.md
CHANGED
|
@@ -48,6 +48,12 @@ RAZORPAY_KEY_SECRET=your_key_secret
|
|
| 48 |
|
| 49 |
The OCR price is configured in `app.py` as `OCR_PRICE_PER_PAGE_PAISE = 50`, which is INR 0.50 per page.
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
## Run locally
|
| 52 |
|
| 53 |
Install system dependencies first:
|
|
|
|
| 48 |
|
| 49 |
The OCR price is configured in `app.py` as `OCR_PRICE_PER_PAGE_PAISE = 50`, which is INR 0.50 per page.
|
| 50 |
|
| 51 |
+
The owner bypass key defaults to `215836`. To change it without editing code, set this optional secret:
|
| 52 |
+
|
| 53 |
+
```bash
|
| 54 |
+
OCR_BYPASS_KEY=your_private_access_key
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
## Run locally
|
| 58 |
|
| 59 |
Install system dependencies first:
|
app.py
CHANGED
|
@@ -20,6 +20,7 @@ app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024
|
|
| 20 |
|
| 21 |
OCR_PRICE_PER_PAGE_PAISE = 50
|
| 22 |
PENDING_ORDERS = {}
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
PAGE = """
|
|
@@ -134,7 +135,9 @@ PAGE = """
|
|
| 134 |
margin-top: 4px;
|
| 135 |
}
|
| 136 |
input[type="file"],
|
| 137 |
-
select
|
|
|
|
|
|
|
| 138 |
width: 100%;
|
| 139 |
min-height: 48px;
|
| 140 |
padding: 12px 14px;
|
|
@@ -303,10 +306,11 @@ PAGE = """
|
|
| 303 |
<option value="fast" selected>Fast OCR - skip pages that already have text</option>
|
| 304 |
<option value="accurate">High accuracy - slower full OCR</option>
|
| 305 |
</select>
|
|
|
|
| 306 |
<button type="submit">Pay and Process PDF</button>
|
| 307 |
<div class="billing-summary" aria-live="polite">
|
| 308 |
<span data-billing-total></span>
|
| 309 |
-
<span>Rate: INR 0.50 per PDF page.
|
| 310 |
</div>
|
| 311 |
<div class="progress-wrap" aria-hidden="true">
|
| 312 |
<div class="progress-label">
|
|
@@ -465,23 +469,34 @@ PAGE = """
|
|
| 465 |
const formData = new FormData(form);
|
| 466 |
|
| 467 |
if (requiresPayment) {
|
| 468 |
-
const
|
| 469 |
-
if (
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
}
|
| 473 |
-
|
| 474 |
-
setProgress(18);
|
| 475 |
-
button.textContent = "Waiting for payment...";
|
| 476 |
-
status.textContent = `Complete INR ${order.display_amount} payment to start OCR.`;
|
| 477 |
-
const payment = await openRazorpayCheckout(order);
|
| 478 |
-
|
| 479 |
-
formData.append("razorpay_payment_id", payment.razorpay_payment_id);
|
| 480 |
-
formData.append("razorpay_order_id", payment.razorpay_order_id);
|
| 481 |
-
formData.append("razorpay_signature", payment.razorpay_signature);
|
| 482 |
-
setProgress(26);
|
| 483 |
-
button.textContent = "Processing...";
|
| 484 |
-
status.textContent = "Payment verified by Razorpay. OCR is processing now.";
|
| 485 |
}
|
| 486 |
|
| 487 |
progressTimer = window.setInterval(() => {
|
|
@@ -587,6 +602,11 @@ def verify_razorpay_signature(order_id, payment_id, signature):
|
|
| 587 |
return hmac.compare_digest(expected, signature or "")
|
| 588 |
|
| 589 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
def ocr_options(mode):
|
| 591 |
workers = max(1, min(4, multiprocessing.cpu_count()))
|
| 592 |
if mode == "accurate":
|
|
@@ -680,14 +700,15 @@ def ocr_pdf():
|
|
| 680 |
signature = request.form.get("razorpay_signature", "")
|
| 681 |
order = PENDING_ORDERS.get(order_id)
|
| 682 |
|
| 683 |
-
if not
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
|
|
|
| 691 |
|
| 692 |
os.remove(output_path)
|
| 693 |
ocrmypdf.ocr(
|
|
@@ -697,7 +718,8 @@ def ocr_pdf():
|
|
| 697 |
)
|
| 698 |
download_name = f"ocr_{filename}"
|
| 699 |
remove_later(output_path)
|
| 700 |
-
|
|
|
|
| 701 |
output_sent = True
|
| 702 |
return send_file(output_path, as_attachment=True, download_name=download_name)
|
| 703 |
except ValueError as exc:
|
|
|
|
| 20 |
|
| 21 |
OCR_PRICE_PER_PAGE_PAISE = 50
|
| 22 |
PENDING_ORDERS = {}
|
| 23 |
+
BYPASS_ACCESS_KEY = os.getenv("OCR_BYPASS_KEY", "215836").strip()
|
| 24 |
|
| 25 |
|
| 26 |
PAGE = """
|
|
|
|
| 135 |
margin-top: 4px;
|
| 136 |
}
|
| 137 |
input[type="file"],
|
| 138 |
+
select,
|
| 139 |
+
input[type="password"],
|
| 140 |
+
input[type="text"] {
|
| 141 |
width: 100%;
|
| 142 |
min-height: 48px;
|
| 143 |
padding: 12px 14px;
|
|
|
|
| 306 |
<option value="fast" selected>Fast OCR - skip pages that already have text</option>
|
| 307 |
<option value="accurate">High accuracy - slower full OCR</option>
|
| 308 |
</select>
|
| 309 |
+
<input type="password" name="access_key" inputmode="numeric" autocomplete="off" placeholder="Access key for free processing">
|
| 310 |
<button type="submit">Pay and Process PDF</button>
|
| 311 |
<div class="billing-summary" aria-live="polite">
|
| 312 |
<span data-billing-total></span>
|
| 313 |
+
<span>Rate: INR 0.50 per PDF page. A valid access key skips payment.</span>
|
| 314 |
</div>
|
| 315 |
<div class="progress-wrap" aria-hidden="true">
|
| 316 |
<div class="progress-label">
|
|
|
|
| 469 |
const formData = new FormData(form);
|
| 470 |
|
| 471 |
if (requiresPayment) {
|
| 472 |
+
const accessKey = formData.get("access_key");
|
| 473 |
+
if (accessKey && accessKey.trim()) {
|
| 474 |
+
if (billingSummary && billingTotal) {
|
| 475 |
+
billingTotal.textContent = "Access key entered. Payment will be skipped after server verification.";
|
| 476 |
+
billingSummary.classList.add("ready");
|
| 477 |
+
}
|
| 478 |
+
setProgress(20);
|
| 479 |
+
button.textContent = "Processing...";
|
| 480 |
+
status.textContent = "Checking access key and starting OCR.";
|
| 481 |
+
} else {
|
| 482 |
+
const order = await createOcrOrder(form);
|
| 483 |
+
if (billingSummary && billingTotal) {
|
| 484 |
+
billingTotal.textContent = `${order.pages} page${order.pages === 1 ? "" : "s"} x INR 0.50 = INR ${order.display_amount}`;
|
| 485 |
+
billingSummary.classList.add("ready");
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
setProgress(18);
|
| 489 |
+
button.textContent = "Waiting for payment...";
|
| 490 |
+
status.textContent = `Complete INR ${order.display_amount} payment to start OCR.`;
|
| 491 |
+
const payment = await openRazorpayCheckout(order);
|
| 492 |
+
|
| 493 |
+
formData.append("razorpay_payment_id", payment.razorpay_payment_id);
|
| 494 |
+
formData.append("razorpay_order_id", payment.razorpay_order_id);
|
| 495 |
+
formData.append("razorpay_signature", payment.razorpay_signature);
|
| 496 |
+
setProgress(26);
|
| 497 |
+
button.textContent = "Processing...";
|
| 498 |
+
status.textContent = "Payment verified by Razorpay. OCR is processing now.";
|
| 499 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
}
|
| 501 |
|
| 502 |
progressTimer = window.setInterval(() => {
|
|
|
|
| 602 |
return hmac.compare_digest(expected, signature or "")
|
| 603 |
|
| 604 |
|
| 605 |
+
def has_valid_bypass_key():
|
| 606 |
+
access_key = request.form.get("access_key", "").strip()
|
| 607 |
+
return bool(BYPASS_ACCESS_KEY and hmac.compare_digest(access_key, BYPASS_ACCESS_KEY))
|
| 608 |
+
|
| 609 |
+
|
| 610 |
def ocr_options(mode):
|
| 611 |
workers = max(1, min(4, multiprocessing.cpu_count()))
|
| 612 |
if mode == "accurate":
|
|
|
|
| 700 |
signature = request.form.get("razorpay_signature", "")
|
| 701 |
order = PENDING_ORDERS.get(order_id)
|
| 702 |
|
| 703 |
+
if not has_valid_bypass_key():
|
| 704 |
+
if not order_id or not payment_id or not signature:
|
| 705 |
+
return render_page("Complete the Razorpay payment before OCR processing."), 402
|
| 706 |
+
if not order:
|
| 707 |
+
return render_page("Payment order expired. Please calculate payment and try again."), 402
|
| 708 |
+
if order["pages"] != page_count:
|
| 709 |
+
return render_page("Uploaded PDF page count changed after payment. Please pay again for this file."), 400
|
| 710 |
+
if not verify_razorpay_signature(order_id, payment_id, signature):
|
| 711 |
+
return render_page("Payment verification failed. Please try again."), 402
|
| 712 |
|
| 713 |
os.remove(output_path)
|
| 714 |
ocrmypdf.ocr(
|
|
|
|
| 718 |
)
|
| 719 |
download_name = f"ocr_{filename}"
|
| 720 |
remove_later(output_path)
|
| 721 |
+
if order_id:
|
| 722 |
+
PENDING_ORDERS.pop(order_id, None)
|
| 723 |
output_sent = True
|
| 724 |
return send_file(output_path, as_attachment=True, download_name=download_name)
|
| 725 |
except ValueError as exc:
|