CoolFace
Apppublic

twoimo/glm-ocr-demo

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
app.py73 linesDownload Raw Back to root
1import streamlit as st2from paddleocr import PaddleOCR3from PIL import Image4import numpy as np5 6st.set_page_config(page_title="OCR Demo", layout="centered")7 8st.title("๐Ÿ“ Simple OCR Demo")9st.markdown("""10This is a lightweight OCR demo using PaddleOCR.11 12**Note**: Originally intended for GLM-OCR, but that model requires GPU resources.13This demo uses PaddleOCR instead, which works on CPU.14""")15 16# Initialize PaddleOCR17@st.cache_resource18def load_ocr():19    try:20        ocr = PaddleOCR(use_textline_orientation=True, lang='en', use_gpu=False)21        return ocr22    except Exception as e:23        st.error(f"Error loading OCR: {e}")24        return None25 26with st.spinner("Loading OCR model..."):27    ocr = load_ocr()28 29if ocr is None:30    st.error("Failed to load OCR model. Please try refreshing.")31    st.stop()32 33# File uploader34uploaded_file = st.file_uploader(35    "Upload an image",36    type=["jpg", "jpeg", "png", "bmp"],37)38 39if uploaded_file is not None:40    # Display image41    image = Image.open(uploaded_file)42    st.image(image, caption="Uploaded Image", use_column_width=True)43    44    if st.button("Extract Text", type="primary"):45        with st.spinner("Processing..."):46            try:47                # Convert to numpy array48                img_array = np.array(image)49                50                # Run OCR51                result = ocr.ocr(img_array, cls=True)52                53                if result and result[0]:54                    st.success("Text extraction completed!")55                    56                    # Extract text57                    extracted_text = "\n".join([line[1][0] for line in result[0]])58                    59                    st.text_area("Extracted Text", value=extracted_text, height=300)60                else:61                    st.warning("No text found in the image.")62                    63            except Exception as e:64                st.error(f"Error: {str(e)}")65 66st.markdown("---")67st.markdown("""68**About GLM-OCR**: 69The original [GLM-OCR model](https://huggingface.co/zai-org/GLM-OCR) is a powerful 0.9B parameter 70multimodal OCR model, but requires GPU resources to run efficiently.71 72For CPU-only environments like Hugging Face CPU Spaces, lighter alternatives like PaddleOCR are more suitable.73""")