Spaces:
Build error
Build error
| from paddleocr import PaddleOCR | |
| import gradio as gr | |
| import os | |
| from typing import List, Optional | |
| # --- Config (same behavior as your original) --- | |
| TITLE = "US Vehicles Number Plate" | |
| DESC = "Upload an image and extract text with PaddleOCR." | |
| ARTICLE = """### Notes | |
| - This Space uses **PaddleOCR**. | |
| - Default language is **Chinese ('ch')** (same as your original code). | |
| - `use_gpu` is **False** by default (CPU). If you move to a GPU Space later, we can switch to `paddlepaddle-gpu`. | |
| """ | |
| # Cache model so it doesn't reload every click | |
| _POCR_MODEL: Optional[PaddleOCR] = None | |
| def _get_model() -> PaddleOCR: | |
| global _POCR_MODEL | |
| if _POCR_MODEL is None: | |
| _POCR_MODEL = PaddleOCR(use_angle_cls=True, lang='ch', use_gpu=False) | |
| return _POCR_MODEL | |
| def ocr_model(filepath: str, languages: List[str] = None) -> str: | |
| """Same signature + same output format as your original (line by line text).""" | |
| if not filepath: | |
| return "" | |
| pocr_model = _get_model() | |
| result = pocr_model.ocr(filepath) | |
| state_txt = "" | |
| # PaddleOCR output: result[0] -> list of [box, (text, score)] | |
| if result and len(result) > 0 and result[0]: | |
| for i in range(len(result[0])): | |
| text1 = result[0][i][1][0] | |
| state_txt += str(text1) + "\n" | |
| return state_txt.strip() | |
| css = """h1 { margin-bottom: 0.25rem; } | |
| .small { opacity: 0.8; } | |
| """ | |
| with gr.Blocks(css=css, title=TITLE) as demo: | |
| gr.Markdown(f"<h1 style='text-align:center'>{TITLE}</h1>") | |
| gr.Markdown(f"<p class='small' style='text-align:center'>{DESC}</p>") | |
| with gr.Row(): | |
| with gr.Column(): | |
| image = gr.Image(type="filepath", label="Input") | |
| with gr.Row(): | |
| btn_clear = gr.ClearButton([image]) | |
| btn_submit = gr.Button(value="Submit", variant="primary") | |
| with gr.Column(): | |
| text = gr.Textbox(label="Output") | |
| btn_submit.click(ocr_model, inputs=[image], outputs=text, api_name="PaddleOCR") | |
| btn_clear.add(text) | |
| gr.Markdown(ARTICLE) | |
| if __name__ == '__main__': | |
| demo.launch() | |