Spaces:
Starting
Starting
File size: 804 Bytes
488dc3e 1bbca12 488dc3e 1bbca12 488dc3e 1bbca12 488dc3e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from langchain_core.tools import tool
import easyocr
import logging
import os
logger = logging.getLogger(__name__)
reader = easyocr.Reader(['en'])
@tool
async def image_parser_tool(file_path: str, task: str = "describe", match_query: str = "") -> str:
"""Parse text from an image"""
try:
if not os.path.exists(file_path):
logger.warning(f"Image not found: {file_path}")
return "Image not found"
results = reader.readtext(file_path)
text = " ".join(result[1] for result in results)
if task == "match" and match_query:
return str(match_query.lower() in text.lower())
return text
except Exception as e:
logger.error(f"Error parsing image {file_path}: {e}")
return f"Error: {str(e)}" |