# import os # import requests # from crewai.tools import tool # from dotenv import load_dotenv # # Load environment variables # load_dotenv() # @tool("LandingAI Document Analysis") # def landing_ai_document_analysis(file_path: str, file_type: str = "image") -> str: # """ # Analyze images or PDFs using LandingAI's document analysis API. # Args: # file_path (str): Path to the image or PDF file to analyze # file_type (str): Type of file, either "image" or "pdf" # Returns: # str: Analysis results from the API # """ # # Get API key from environment variable # api_key = os.getenv("LANDING_AI_API_KEY") # # API endpoint # url = "https://api.va.landing.ai/v1/tools/agentic-document-analysis" # # Prepare the file for upload based on file_type # with open(file_path, "rb") as file_obj: # if file_type.lower() == "pdf": # files = {"pdf": file_obj} # else: # files = {"image": file_obj} # # Prepare headers with authentication # headers = {"Authorization": f"Basic {api_key}"} # # Make the API request # response = requests.post(url, files=files, headers=headers) # return response.json() import os import requests from crewai.tools import tool @tool("LandingAI Document Analysis") def landing_ai_document_analysis(file_path: str, file_type: str = "image") -> str: """ Analyze images or PDFs using LandingAI's document analysis API. Args: file_path (str): Path to the image or PDF file to analyze file_type (str): Type of file, either "image" or "pdf" Returns: str: Analysis results from the API """ # Get API key from environment variable api_key = os.getenv("LANDING_AI_API_KEY") if not api_key: return "Error: LANDING_AI_API_KEY environment variable is not set" # API endpoint url = "https://api.va.landing.ai/v1/tools/agentic-document-analysis" try: # Prepare the file for upload based on file_type with open(file_path, "rb") as file_obj: if file_type.lower() == "pdf": files = {"pdf": file_obj} else: files = {"image": file_obj} # Prepare headers with authentication headers = {"Authorization": f"Basic {api_key}"} # Make the API request response = requests.post(url, files=files, headers=headers) # Check if request was successful if response.status_code == 200: return response.json() else: return f"API Error: {response.status_code} - {response.text}" except FileNotFoundError: return f"Error: File not found at path: {file_path}" except Exception as e: return f"Error processing file: {str(e)}"