Spaces:
Sleeping
Sleeping
Download app.py from afueth/malayalam-ocr: direct link, hf CLI and curl.
- Browser
- Download file 4.59 kB
-
https://huggingface.co/spaces/afueth/malayalam-ocr/resolve/main/app.py
- Command line
-
hf download hf://spaces/afueth/malayalam-ocr/app.py
-
curl -L -o app.py https://huggingface.co/spaces/afueth/malayalam-ocr/resolve/main/app.py
4.59 kB
| from __future__ import annotations | |
| import io | |
| import streamlit as st | |
| import fitz # PyMuPDF | |
| from PIL import Image | |
| from ocr.ocr_engine import configure_tesseract, ocr_pil_image | |
| from ocr.pdf_utils import pdf_has_native_text, extract_native_text, render_page_to_pil | |
| from ocr.image_utils import preprocess_image | |
| st.set_page_config(page_title="Malayalam OCR", page_icon="favicon.ico", layout="wide") | |
| st.title("Malayalam OCR") | |
| configure_tesseract() | |
| with st.sidebar: | |
| st.header("OCR Settings") | |
| lang = st.selectbox("Language", ["mal", "mal+eng"], index=0) | |
| psm = st.selectbox("PSM (layout mode)", [3, 4, 6, 11], index=2) | |
| zoom = st.slider("PDF render zoom (OCR quality)", 1.0, 3.5, 2.0, 0.1) | |
| st.divider() | |
| st.header("Preprocessing") | |
| grayscale = st.checkbox("Grayscale", value=True) | |
| contrast = st.slider("Contrast", 1.0, 3.0, 2.0, 0.1) | |
| use_threshold = st.checkbox("Threshold", value=True) | |
| threshold = st.slider("Threshold value", 0, 255, 160, 5) if use_threshold else None | |
| invert = st.checkbox("Invert colors", value=False) | |
| st.divider() | |
| st.header("PDF Behavior") | |
| force_ocr = st.checkbox("Force OCR (use for scanned PDFs)", value=False) | |
| max_pages = st.slider("Max pages to process", 1, 200, 20) | |
| def do_ocr_on_pil(img: Image.Image) -> str: | |
| img2 = preprocess_image( | |
| img, | |
| grayscale=grayscale, | |
| contrast=contrast, | |
| threshold=threshold, | |
| invert=invert, | |
| ) | |
| return ocr_pil_image(img2, lang=lang, psm=int(psm), oem=1) | |
| def process_image_bytes(img_bytes: bytes, settings_key: str) -> str: | |
| img = Image.open(io.BytesIO(img_bytes)) | |
| return do_ocr_on_pil(img) | |
| def process_pdf_bytes(pdf_bytes: bytes, settings_key: str) -> str: | |
| doc = fitz.open(stream=pdf_bytes, filetype="pdf") | |
| out = [] | |
| total = min(doc.page_count, max_pages) | |
| for i in range(total): | |
| page = doc.load_page(i) | |
| if (not force_ocr) and pdf_has_native_text(page, min_chars=50): | |
| txt = extract_native_text(page) | |
| out.append(f"\n\n--- Page {i+1} (native text) ---\n{txt}") | |
| else: | |
| pil_img = render_page_to_pil(page, zoom=zoom) | |
| txt = do_ocr_on_pil(pil_img) | |
| out.append(f"\n\n--- Page {i+1} (OCR) ---\n{txt}") | |
| return "\n".join(out).strip() | |
| uploaded = st.file_uploader("Upload PDF or Image", type=["pdf", "png", "jpg", "jpeg", "webp"]) | |
| if uploaded: | |
| settings_key = f"{lang}|{psm}|{zoom}|{grayscale}|{contrast}|{threshold}|{invert}|{force_ocr}|{max_pages}" | |
| file_bytes = uploaded.getvalue() | |
| name = uploaded.name.lower() | |
| col1, col2 = st.columns([1, 1]) | |
| with col1: | |
| st.subheader("Input Preview") | |
| if name.endswith(".pdf"): | |
| st.info("PDF uploaded. Previewing first page as image.") | |
| doc = fitz.open(stream=file_bytes, filetype="pdf") | |
| page0 = doc.load_page(0) | |
| img0 = render_page_to_pil(page0, zoom=1.5) | |
| st.image(img0, use_container_width=True) | |
| else: | |
| img = Image.open(io.BytesIO(file_bytes)) | |
| st.image(img, use_container_width=True) | |
| with col2: | |
| st.subheader("Extracted Text") | |
| with st.spinner("Extracting text..."): | |
| if name.endswith(".pdf"): | |
| text = process_pdf_bytes(file_bytes, settings_key) | |
| else: | |
| text = process_image_bytes(file_bytes, settings_key) | |
| st.text_area("Copy text from here", value=text, height=420, key="result_text_area") | |
| # ✅ Copy button (clipboard) — works in browser | |
| copy_disabled = not bool(text.strip()) | |
| copy_label = "Copy" if not copy_disabled else "Copy (no text)" | |
| clicked = st.button(copy_label, use_container_width=True, disabled=copy_disabled) | |
| if clicked and not copy_disabled: | |
| st.components.v1.html( | |
| f""" | |
| <script> | |
| navigator.clipboard.writeText({text!r}); | |
| </script> | |
| """, | |
| height=0, | |
| ) | |
| st.success("Copied to clipboard!") | |
| st.download_button( | |
| "Download as .txt", | |
| data=text.encode("utf-8"), | |
| file_name=uploaded.name.rsplit(".", 1)[0] + "_ocr.txt", | |
| mime="text/plain", | |
| use_container_width=True, | |
| ) | |
| st.caption("Tip: If results are messy, try PSM=4 or PSM=3, increase Zoom to 2.5–3.0, or adjust Threshold.") | |
| else: | |
| st.write("Upload a PDF or image to extract Malayalam printed text.") | |