mcp-helper-tool / tools.py
agharsallah
addding simple llama parse tool
f928c5e
"""
tools.py
This module contains modular tools for the Gradio app, including:
- Letter Counter Tool
- PDF to Text Tool (using LlamaParse)
"""
from llama_cloud_services import LlamaParse
import os
# --- Tool: Letter Counter ---
def letter_counter(word: str, letter: str) -> int:
"""
Count the occurrences of a specific letter in a word.
Args:
word (str): The word or phrase to analyze.
letter (str): The letter to count occurrences of.
Returns:
int: The number of times the letter appears in the word.
"""
return word.lower().count(letter.lower())
# --- Tool: PDF to Text ---
def pdf_to_text(pdf_path: str) -> str | None:
"""
Extracts text from a PDF file using LlamaParse.
Args:
pdf_path (str): Path to the PDF file.
Returns:
str: The extracted text from the PDF.
None: If no text could be extracted.
"""
parser = LlamaParse(
api_key=os.getenv("LLAMA_CLOUD_API_KEY"),
num_workers=1,
verbose=True,
language="en",
)
result = parser.parse(pdf_path)
# Get all text as a single string
text_documents = result.get_text_documents(split_by_page=False)
if text_documents:
return "\n".join([doc.text for doc in text_documents])
return None