File size: 2,980 Bytes
ae43d88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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)}"