Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516176 unmodified lines24252627282930311 unmodified line33343536373839404142434413 unmodified lines585960616263646566676869707172737475767778798081828313 unmodified lines979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import osimport ioimport jsonfrom typing import List, Tuple, Dict, Anyimport fitz # PyMuPDFfrom PIL import Imageimport gradio as gr# Lazy-load the OCR model to reduce startup time and memory_ocr_model = Nonedef get_ocr_model(lang: str = "en"):global _ocr_modelif _ocr_model is not None:6 unmodified lines_ocr_model = PaddleOCR(use_angle_cls=True, lang=lang, show_log=False)return _ocr_modeldef pdf_page_to_image(pdf_doc: fitz.Document, page_index: int, dpi: int = 170) -> Image.Image:page = pdf_doc.load_page(page_index)zoom = dpi / 72.0 # 72 dpi is PDF defaultmat = fitz.Matrix(zoom, zoom)1 unmodified lineimg = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)return imgdef run_paddle_ocr_on_image(image: Image.Image, lang: str = "en") -> Tuple[str, List[Dict[str, Any]]]:ocr = get_ocr_model(lang=lang)# Convert PIL image to numpy array for PaddleOCRimport numpy as npimg_np = np.array(image)result = ocr.ocr(img_np, cls=True)13 unmodified linesreturn "\n".join(lines), itemsdef extract_text_from_pdf(file_obj, dpi: int = 170, max_pages: int | None = None, lang: str = "en") -> Tuple[str, str]:"""Returns combined text and a JSON string with per-page OCR results."""if file_obj is None:return "", json.dumps({"pages": []}, ensure_ascii=False)# Gradio may pass a path or a tempfile.NamedTemporaryFile-like with .namepdf_path = file_obj if isinstance(file_obj, str) else getattr(file_obj, "name", None)if pdf_path is None or not os.path.exists(pdf_path):# If bytes were passed, fall back to reading from bufferfile_bytes = file_obj.read() if hasattr(file_obj, "read") else Noneif not file_bytes:return "", json.dumps({"pages": []}, ensure_ascii=False)pdf_doc = fitz.open(stream=file_bytes, filetype="pdf")else:pdf_doc = fitz.open(pdf_path)try:num_pages = pdf_doc.page_countif max_pages is not None:num_pages = min(num_pages, max_pages)13 unmodified linescombined_text = "\n\n".join([t for t in all_text_lines if t])json_payload = json.dumps({"pages": pages_payload}, ensure_ascii=False)return combined_text, json_payloadfinally:pdf_doc.close()def gradio_predict(pdf_file):# Always render at a high DPI for accuracy and use English OCR by defaulttext, _ = extract_text_from_pdf(pdf_file, dpi=300, max_pages=None, lang="en")return textwith gr.Blocks(title="PDF OCR with PaddleOCR + PyMuPDF") as demo:gr.Markdown("""# PDF OCR (PaddleOCR + PyMuPDF)Upload a PDF to extract text using OCR. The app renders pages with PyMuPDF at a high DPI and uses PaddleOCR for recognition.""")pdf_input = gr.File(label="PDF", file_types=[".pdf"], file_count="single")text_output = gr.Textbox(label="Extracted Text", lines=20)# Auto-run OCR when a PDF is uploadedpdf_input.change(fn=gradio_predict, inputs=[pdf_input], outputs=[text_output], api_name="predict")# Simple API notegr.Markdown("""## API usage- Use `gradio_client` to call this Space. Function signature: `gradio_predict(pdf_file)` β `text`.""")if __name__ == "__main__":# On Spaces, the host/port are managed by the platform. Locally, this runs on 7860 by default.demo.launch()12345678910111213141516171819202122236 unmodified lines303132333435361 unmodified line38394041424344454613 unmodified lines60616263646566676869707172737475767778798081828384858613 unmodified lines100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361import osimport ioimport jsonimport timefrom typing import List, Tuple, Dict, Any, Optionalimport fitz # PyMuPDFfrom PIL import Imageimport gradio as grimport numpy as np# =========================# Config# =========================LOGO_IMAGE_PATH = './assets/logo.jpg'GOOGLE_FONTS_URL = "<link href='https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap' rel='stylesheet'>"# Lazy-load the OCR model to reduce startup time and memory_ocr_model = Nonedef get_ocr_model(lang: str = "en"):global _ocr_modelif _ocr_model is not None:6 unmodified lines_ocr_model = PaddleOCR(use_angle_cls=True, lang=lang, show_log=False)return _ocr_modeldef pdf_page_to_image(pdf_doc: fitz.Document, page_index: int, dpi: int = 300) -> Image.Image:page = pdf_doc.load_page(page_index)zoom = dpi / 72.0 # 72 dpi is PDF defaultmat = fitz.Matrix(zoom, zoom)1 unmodified lineimg = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)return imgdef run_paddle_ocr_on_image(image: Image.Image, lang: str = "en") -> Tuple[str, List[Dict[str, Any]]]:ocr = get_ocr_model(lang=lang)# Convert PIL image to numpy array for PaddleOCRimg_np = np.array(image)result = ocr.ocr(img_np, cls=True)13 unmodified linesreturn "\n".join(lines), itemsdef extract_text_from_pdf(file_obj, dpi: int = 300, max_pages: int | None = None, lang: str = "en") -> Tuple[str, str, Dict[str, Any]]:"""Returns combined text, JSON string with per-page OCR results, and processing stats."""if file_obj is None:return "", json.dumps({"pages": []}, ensure_ascii=False), {"error": "No file provided"}start_time = time.time()try:# Gradio may pass a path or a tempfile.NamedTemporaryFile-like with .namepdf_path = file_obj if isinstance(file_obj, str) else getattr(file_obj, "name", None)if pdf_path is None or not os.path.exists(pdf_path):# If bytes were passed, fall back to reading from bufferfile_bytes = file_obj.read() if hasattr(file_obj, "read") else Noneif not file_bytes:return "", json.dumps({"pages": []}, ensure_ascii=False), {"error": "Could not read file"}pdf_doc = fitz.open(stream=file_bytes, filetype="pdf")else:pdf_doc = fitz.open(pdf_path)num_pages = pdf_doc.page_countif max_pages is not None:num_pages = min(num_pages, max_pages)13 unmodified linescombined_text = "\n\n".join([t for t in all_text_lines if t])json_payload = json.dumps({"pages": pages_payload}, ensure_ascii=False)processing_time = time.time() - start_timestats = {"pages_processed": num_pages,"total_pages": pdf_doc.page_count,"processing_time": round(processing_time, 2),"dpi": dpi,"language": lang}pdf_doc.close()return combined_text, json_payload, statsexcept Exception as e:return "", json.dumps({"pages": []}, ensure_ascii=False), {"error": str(e)}def handle_pdf_ocr(pdf_file: str) -> Tuple[str, str, str]:"""Main handler for PDF OCR processing"""if not pdf_file:raise gr.Error("Please upload a PDF file first.")try:print(f"Processing PDF: {pdf_file}")start_time = time.time()text, json_data, stats = extract_text_from_pdf(pdf_file, dpi=300, max_pages=None, lang="en")end_time = time.time()duration = end_time - start_timeprint(f"PDF processing completed in {duration:.2f} seconds.")if "error" in stats:raise gr.Error(f"Processing failed: {stats['error']}")# Format stats for displaystats_text = f"""**Processing Statistics:**- Pages processed: {stats.get('pages_processed', 0)}/{stats.get('total_pages', 0)}- Processing time: {stats.get('processing_time', 0)}s- DPI: {stats.get('dpi', 300)}- Language: {stats.get('language', 'en')}"""return text, json_data, stats_textexcept Exception as e:error_msg = f"Error processing PDF: {str(e)}"print(error_msg)raise gr.Error(error_msg)# =========================# CSS & UI# =========================custom_css = """/* Global fonts */body, .gradio-container {font-family: "Inter", "Segoe UI", "Roboto", sans-serif;}.app-header {text-align: center;max-width: 900px;margin: 0 auto 20px !important;padding: 20px;background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);border-radius: 12px;color: white;}.app-header h1 {margin: 0;font-size: 2.5rem;font-weight: 700;}.app-header p {margin: 10px 0 0 0;opacity: 0.9;font-size: 1.1rem;}.gradio-container {padding: 20px 0 !important;max-width: 1200px;margin: 0 auto;}.upload-section {background: #f8fafc;border: 2px dashed #cbd5e1;border-radius: 12px;padding: 30px;text-align: center;margin: 20px 0;}.upload-section:hover {border-color: #667eea;background: #f1f5f9;}.results-section {margin-top: 20px;}.stats-box {background: #f0f9ff;border: 1px solid #0ea5e9;border-radius: 8px;padding: 15px;margin: 10px 0;}#text_output {min-height: 300px;font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;font-size: 14px;line-height: 1.6;}#json_output {min-height: 200px;font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;font-size: 12px;}.process-btn {background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;color: white !important;border: none !important;padding: 12px 30px !important;border-radius: 8px !important;font-weight: 600 !important;font-size: 16px !important;}.process-btn:hover {transform: translateY(-2px);box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);}.notice {background: #fef3c7;border: 1px solid #f59e0b;border-radius: 8px;padding: 15px;margin: 20px 0;color: #92400e;}.api-section {background: #f1f5f9;border-radius: 8px;padding: 20px;margin: 20px 0;border-left: 4px solid #667eea;}"""with gr.Blocks(head=GOOGLE_FONTS_URL, css=custom_css, theme=gr.themes.Soft()) as demo:# Headergr.HTML("""<div class="app-header"><h1>π PDF OCR Extractor</h1><p>Extract text from PDF documents using PaddleOCR + PyMuPDF</p></div>""")# Noticegr.HTML("""<div class="notice"><strong>π‘ Tip:</strong> This tool processes PDFs by rendering each page as a high-resolution image (300 DPI) and then applying OCR.For best results, use clear, well-scanned PDFs with good contrast.</div>""")with gr.Row():with gr.Column(scale=1):# Upload sectiongr.HTML('<div class="upload-section">')pdf_input = gr.File(label="π Upload PDF File",file_types=[".pdf"],file_count="single",elem_id="pdf_upload")gr.HTML('</div>')# Process buttonprocess_btn = gr.Button("π Extract Text",variant="primary",elem_classes=["process-btn"],scale=2)# API sectiongr.HTML("""<div class="api-section"><h3>π API Usage</h3><p><strong>Endpoint:</strong> <code>/predict</code></p><p><strong>Input:</strong> PDF file</p><p><strong>Output:</strong> Extracted text</p></div>""")with gr.Column(scale=2):# Results sectiongr.HTML('<div class="results-section">')with gr.Tabs():with gr.Tab("π Extracted Text"):text_output = gr.Textbox(label="Extracted Text",lines=20,elem_id="text_output",placeholder="Extracted text will appear here...")with gr.Tab("π JSON Data"):json_output = gr.Code(label="Detailed OCR Results (JSON)",language="json",elem_id="json_output",placeholder="Detailed OCR results will appear here...")with gr.Tab("π Statistics"):stats_output = gr.Markdown(label="Processing Statistics",placeholder="Processing statistics will appear here...")gr.HTML('</div>')# Event handlersprocess_btn.click(fn=handle_pdf_ocr,inputs=[pdf_input],outputs=[text_output, json_output, stats_output],api_name="predict")# Auto-process on file uploadpdf_input.change(fn=handle_pdf_ocr,inputs=[pdf_input],outputs=[text_output, json_output, stats_output],api_name="predict")if __name__ == "__main__":port = int(os.getenv("PORT", "7860"))demo.queue(max_size=6).launch(server_name="0.0.0.0",server_port=port,share=False)
app.py+278-52