mchinea commited on
Commit
d9c930b
·
1 Parent(s): 5ef9706
Files changed (2) hide show
  1. requirements.txt +1 -0
  2. tools.py +33 -0
requirements.txt CHANGED
@@ -12,3 +12,4 @@ Pillow
12
  pydub
13
  tavily-python
14
  wikipedia
 
 
12
  pydub
13
  tavily-python
14
  wikipedia
15
+ pip pytesseract
tools.py CHANGED
@@ -325,6 +325,39 @@ def download_file_from_url(url: str, filename: str) -> str:
325
  return f"Error downloading file: {str(e)}"
326
 
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  level1_tools = [
329
  multiply,
330
  add,
 
325
  return f"Error downloading file: {str(e)}"
326
 
327
 
328
+ @tool
329
+ def extract_text_from_image(image_path: str) -> str:
330
+ """
331
+ Extracts text from an image using pytesseract OCR.
332
+
333
+ Args:
334
+ image_path: Path to the image file.
335
+
336
+ Returns:
337
+ A string with the extracted text or an error message.
338
+ """
339
+ try:
340
+ from PIL import Image
341
+ import pytesseract
342
+
343
+ # Load the image
344
+ image = Image.open(image_path)
345
+
346
+ # Perform OCR
347
+ text = pytesseract.image_to_string(image)
348
+
349
+ return f"Extracted text from image:\n\n{text.strip()}"
350
+ except ImportError:
351
+ return (
352
+ "Error: pytesseract or PIL is not installed. "
353
+ "Install them with 'pip install pytesseract pillow' and ensure Tesseract OCR is installed."
354
+ )
355
+ except FileNotFoundError:
356
+ return f"Error: File not found at '{image_path}'."
357
+ except Exception as e:
358
+ return f"Unexpected error during OCR: {str(e)}"
359
+
360
+
361
  level1_tools = [
362
  multiply,
363
  add,