Download agent/tools/ocr_reader.py from AlexTrinityBlock/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 3.76 kB
-
https://huggingface.co/spaces/AlexTrinityBlock/Final_Assignment_Template/resolve/main/agent/tools/ocr_reader.py
- Command line
-
hf download hf://spaces/AlexTrinityBlock/Final_Assignment_Template/agent/tools/ocr_reader.py
-
curl -L -o ocr_reader.py https://huggingface.co/spaces/AlexTrinityBlock/Final_Assignment_Template/resolve/main/agent/tools/ocr_reader.py
3.76 kB
| import base64 | |
| from pathlib import Path | |
| from colorama import Fore, Style # type: ignore[import] | |
| from langchain_core.messages import HumanMessage | |
| from langchain_core.tools import tool | |
| from agent.api.api import get_llm | |
| # Workspace directory where downloaded files are stored | |
| WORKSPACE_DIR = Path(__file__).resolve().parents[2] / "workspace" | |
| # Supported image extensions | |
| IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".tiff"} | |
| SYSTEM_PROMPT = ( | |
| "You are a precise OCR and image analysis assistant. " | |
| "If the image is a photo or illustration, describe the content in detail " | |
| "from left to right, top to bottom. " | |
| "If the image contains a table, document, chessboard, or any structured data, " | |
| "reproduce the full structure as a Markdown table. " | |
| "Always be thorough and accurate." | |
| ) | |
| def _encode_image(image_path: str) -> str: | |
| """Read and base64-encode an image file.""" | |
| with open(image_path, "rb") as f: | |
| return base64.b64encode(f.read()).decode("utf-8") | |
| def _guess_mime_type(path: str) -> str: | |
| """Return the MIME type based on file extension.""" | |
| ext = Path(path).suffix.lower() | |
| mime_map = { | |
| ".jpg": "image/jpeg", | |
| ".jpeg": "image/jpeg", | |
| ".png": "image/png", | |
| ".gif": "image/gif", | |
| ".bmp": "image/bmp", | |
| ".webp": "image/webp", | |
| ".tiff": "image/tiff", | |
| } | |
| return mime_map.get(ext, "image/jpeg") | |
| def ocr_reader(filename: str, query: str = "") -> str: | |
| """Read and extract content from an image file in the workspace directory. | |
| The image must be located in the workspace directory (where file_downloader | |
| saves files). Only provide the filename, not the full path. | |
| For photos or illustrations, the content is described from left to right, | |
| top to bottom in detail. For tables, documents, or chessboards, the full | |
| structure is returned as a Markdown table. | |
| Args: | |
| filename: The filename of the image in the workspace directory. | |
| Example: "photo.jpg", "table.png" | |
| query: An optional question to ask about the image. | |
| If provided, the model will focus on answering this question. | |
| If omitted, the model performs general OCR / description. | |
| Returns: | |
| A string containing the extracted or described content of the image. | |
| """ | |
| path = WORKSPACE_DIR / filename | |
| if not path.exists(): | |
| return f"Error: file not found: {path}" | |
| if path.suffix.lower() not in IMAGE_EXTENSIONS: | |
| return f"Error: unsupported image format '{path.suffix}'. Supported: {IMAGE_EXTENSIONS}" | |
| try: | |
| print( | |
| f"{Fore.YELLOW}[OCR] Reading image: {filename}" | |
| f"{f' | Query: {query}' if query else ''}{Style.RESET_ALL}" | |
| ) | |
| base64_image = _encode_image(str(path)) | |
| mime_type = _guess_mime_type(str(path)) | |
| llm = get_llm() | |
| prompt = f"{SYSTEM_PROMPT}\n\nUser question: {query}" if query else SYSTEM_PROMPT | |
| message = HumanMessage( | |
| content=[ | |
| {"type": "text", "text": prompt}, | |
| { | |
| "type": "image_url", | |
| "image_url": {"url": f"data:{mime_type};base64,{base64_image}"}, | |
| }, | |
| ] | |
| ) | |
| response = llm.invoke([message]) | |
| content = response.content | |
| if isinstance(content, list): | |
| result = "\n".join( | |
| block.get("text", "") for block in content if isinstance(block, dict) | |
| ) | |
| else: | |
| result = str(content) | |
| print(f"{Fore.YELLOW}[OCR] Result:\n{result}{Style.RESET_ALL}") | |
| return result | |
| except Exception as e: | |
| return f"Error processing image: {e}" | |