Skip to content

A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.

audit: fix glm_ocr_pro - move to OCR folder, fix API endpoint bugs, add docstrings, add demo, fix README path

Tiioluwani45fddab2eca5
diff --git a/multimodal/glm_ocr_pro/.gitignore b/OCR/glm_ocr_pro/.gitignore
similarity index 100%
rename from multimodal/glm_ocr_pro/.gitignore
rename to OCR/glm_ocr_pro/.gitignore
diff --git a/multimodal/glm_ocr_pro/README.md b/OCR/glm_ocr_pro/README.md
similarity index 77%
rename from multimodal/glm_ocr_pro/README.md
rename to OCR/glm_ocr_pro/README.md
index 92dc3e8..31cf982 100644
--- a/multimodal/glm_ocr_pro/README.md
+++ b/OCR/glm_ocr_pro/README.md
@@ -2,18 +2,25 @@
**GLM-OCR Pro** is a high-performance, local-first Streamlit application designed for structured document extraction. It leverages the **GLM-OCR** model via Ollama to transform images and PDFs into cleanly formatted Markdown in real-time.
+## Demo
+
+![Demo](assets/demo.gif)
+
---
+
## Overview
+
GLM-OCR Pro solves the problem of extracting structured text from documents without sending sensitive data to external APIs. It runs entirely on your local machine using the GLM-OCR model via Ollama, making it ideal for confidential documents. The app streams results in real-time and outputs clean, structured Markdown that preserves headings, tables, and lists.
## Prerequisites
+
Before you begin, ensure you have:
- Python 3.8 or higher
- [Ollama](https://ollama.com) installed and running
- The GLM-OCR model pulled: `ollama pull glm-ocr`
- A GPU with at least 4GB VRAM (recommended)
-## ✨ Features
+## Features
- **100% Private & Local:** No data ever leaves your machine. Perfect for sensitive documents.
- **Real-time Streaming:** Watch text populate your screen as the model identifies it.
@@ -23,24 +30,29 @@ Before you begin, ensure you have:
---
-## 🚀 Quick Start
+## Quick Start
### 1. Clone the Repository
+
```bash
git clone https://github.com/Sumanth077/Hands-On-AI-Engineering.git
-cd Hands-On-AI-Engineering/multimodal_apps/glm-ocr-pro
+cd Hands-On-AI-Engineering/OCR/glm_ocr_pro
```
### 2. Install Ollama
+
Download and install Ollama from [ollama.com](https://ollama.com).
### 3. Pull the GLM-OCR Model
+
Open your terminal and run:
+
```bash
ollama pull glm-ocr
```
### 4. Setup the Python Environment
+
```bash
# Optional: Create a virtual environment
python -m venv .venv
@@ -51,13 +63,26 @@ pip install -r requirements.txt
```
### 5. Run the App
+
```bash
streamlit run app.py
```
---
-## ⚙️ Performance Tuning
+## Usage
+
+Once the app is running at `http://localhost:8501`:
+
+1. Check the **Ollama Status** indicator in the Settings panel. If the model is not loaded, click **Warm Up Model**.
+2. Upload a document (PNG, JPG, JPEG, or PDF) using the file uploader.
+3. Select a **Performance Profile** to match your GPU: Balanced, Fast, or Ultra Fast.
+4. Click **Extract Knowledge** to run OCR. Results stream in real time on the right panel.
+5. Download the extracted Markdown with the **Download .md** button or copy it from the raw source viewer.
+
+---
+
+## Performance Tuning
GLM-OCR Pro includes three "Performance Profiles" to help you balance speed and accuracy based on your hardware:
@@ -69,7 +94,7 @@ GLM-OCR Pro includes three "Performance Profiles" to help you balance speed and
---
-## 🛠️ Tech Stack
+## Tech Stack
- **UI Framework:** Streamlit
- **PDF Engine:** PyPDFium2
@@ -78,6 +103,10 @@ GLM-OCR Pro includes three "Performance Profiles" to help you balance speed and
---
-## 📝 License
+## License
This project is intended for personal and professional use. Ensure compliance with the GLM-OCR and Ollama licensing terms.
+
+---
+
+[Back to top](#glm-ocr-pro)
diff --git a/multimodal/glm_ocr_pro/app.py b/OCR/glm_ocr_pro/app.py
similarity index 96%
rename from multimodal/glm_ocr_pro/app.py
rename to OCR/glm_ocr_pro/app.py
index c6c4d7c..7770d4b 100644
--- a/multimodal/glm_ocr_pro/app.py
+++ b/OCR/glm_ocr_pro/app.py
@@ -1,3 +1,4 @@
+"""Streamlit application that uses GLM-OCR via Ollama to extract structured Markdown from uploaded images and PDFs."""
import streamlit as st
import os
@@ -12,6 +13,7 @@ if os.path.exists(_css_path):
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
def _looks_like_html_table(text: str) -> bool:
+ """Returns True if the text appears to contain an HTML table."""
lowered = text.lower()
return "<table" in lowered and ("<tr" in lowered or "<td" in lowered)
diff --git a/OCR/glm_ocr_pro/assets/demo.gif b/OCR/glm_ocr_pro/assets/demo.gif
new file mode 100644
index 0000000..4df9c86
Binary files /dev/null and b/OCR/glm_ocr_pro/assets/demo.gif differ
diff --git a/multimodal/glm_ocr_pro/ocr_local.py b/OCR/glm_ocr_pro/ocr_local.py
similarity index 89%
rename from multimodal/glm_ocr_pro/ocr_local.py
rename to OCR/glm_ocr_pro/ocr_local.py
index 7d16b90..ef9479b 100644
--- a/multimodal/glm_ocr_pro/ocr_local.py
+++ b/OCR/glm_ocr_pro/ocr_local.py
@@ -13,6 +13,7 @@ OLLAMA_BASE_URL = "http://localhost:11434"
def check_ollama_status() -> tuple[bool, str]:
+ """Probes the Ollama API to check whether it is reachable and returns a status message."""
try:
req = urllib.request.Request(f"{OLLAMA_BASE_URL}/api/tags", method="GET")
with urllib.request.urlopen(req, timeout=5) as resp:
@@ -24,6 +25,7 @@ def check_ollama_status() -> tuple[bool, str]:
def _iter_pdf_image_bytes(pdf_bytes: bytes, max_pages: int, render_scale: float = 2.0) -> Generator[bytes, None, None]:
+ """Renders each page of a PDF to a PNG byte string, up to max_pages."""
doc = pdfium.PdfDocument(pdf_bytes)
page_count = min(len(doc), max_pages)
for i in range(page_count):
@@ -60,6 +62,7 @@ def _optimize_image_bytes(image_bytes: bytes, max_side: int = 1400, quality: int
def warmup_local_model(timeout_seconds: int = 120) -> str:
+ """Sends a no-op prompt to Ollama to pre-load the GLM-OCR model weights into GPU memory."""
payload = {"model": "glm-ocr", "prompt": "ready", "stream": False, "keep_alive": "10m"}
req = urllib.request.Request(
f"{OLLAMA_BASE_URL}/api/generate",
@@ -84,23 +87,18 @@ def _run_single_image_local_stream(image_bytes: bytes, timeout_seconds: int, num
)
payload = {
"model": "glm-ocr",
+ "prompt": prompt,
+ "images": [image_bytes_to_base64(image_bytes)],
"stream": True,
"keep_alive": "10m",
"options": {
"temperature": 0.0,
"num_predict": num_predict,
},
- "messages": [
- {
- "role": "user",
- "content": prompt,
- "images": [image_bytes_to_base64(image_bytes)],
- }
- ],
}
req = urllib.request.Request(
- f"{OLLAMA_BASE_URL}/api/chat",
+ f"{OLLAMA_BASE_URL}/api/generate",
method="POST",
headers={"Content-Type": "application/json"},
data=json.dumps(payload).encode("utf-8"),
@@ -114,7 +112,7 @@ def _run_single_image_local_stream(image_bytes: bytes, timeout_seconds: int, num
continue
try:
chunk = json.loads(line.decode("utf-8"))
- content = chunk.get("message", {}).get("content", "")
+ content = chunk.get("response", "")
if content:
yield content
if chunk.get("done"):
@@ -145,9 +143,9 @@ def extract_markdown_local_stream(
yield f"## Page {idx}\n\n"
if optimize_images:
page_bytes = _optimize_image_bytes(page_bytes, max_side=max_side, grayscale=grayscale)
- yield from _run_single_image_local_stream(file_bytes, timeout_seconds=timeout_seconds, num_predict=num_predict)
+ yield from _run_single_image_local_stream(page_bytes, timeout_seconds=timeout_seconds, num_predict=num_predict)
yield "\n\n---\n\n"
else:
if optimize_images:
file_bytes = _optimize_image_bytes(file_bytes, max_side=max_side, grayscale=grayscale)
- yield from _run_single_image_local_stream(page_bytes, timeout_seconds=timeout_seconds, num_predict=num_predict)
+ yield from _run_single_image_local_stream(file_bytes, timeout_seconds=timeout_seconds, num_predict=num_predict)
diff --git a/multimodal/glm_ocr_pro/requirements.txt b/OCR/glm_ocr_pro/requirements.txt
similarity index 100%
rename from multimodal/glm_ocr_pro/requirements.txt
rename to OCR/glm_ocr_pro/requirements.txt
diff --git a/multimodal/glm_ocr_pro/style.css b/OCR/glm_ocr_pro/style.css
similarity index 100%
rename from multimodal/glm_ocr_pro/style.css
rename to OCR/glm_ocr_pro/style.css
diff --git a/multimodal/glm_ocr_pro/utils.py b/OCR/glm_ocr_pro/utils.py
similarity index 100%
rename from multimodal/glm_ocr_pro/utils.py
rename to OCR/glm_ocr_pro/utils.py