Spaces:
Sleeping
Sleeping
File size: 9,435 Bytes
9c60c5f |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# #crew_tool.py
import json
import os
import requests
from crewai.tools import tool
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def get_landing_ai_api_key():
"""
Get Landing AI API key from environment variables.
Tries multiple sources for compatibility with different deployment environments.
Returns:
str: The API key if found, None otherwise
"""
# Try different environment variable names for flexibility
api_key = (
os.getenv("LANDING_AI_API_KEY") or # HuggingFace secrets or production
os.getenv("LANDING_AI_API_KEY_LOCAL") or # Local development alternative
os.getenv("LANDINGAI_API_KEY") or # Alternative naming
os.getenv("LANDING_API_KEY") # Another alternative
)
return api_key
def safe_format_with_indicators(text):
"""Replace emojis with text indicators"""
if not isinstance(text, str):
text = str(text)
emoji_replacements = {
'π': '[BATTERY]',
'β
': '[SUCCESS]',
'β': '[ERROR]',
'π': '[DOCUMENT]',
'π': '[CHART]',
'β οΈ': '[WARNING]',
'π': '[SEARCH]',
'π‘': '[INSIGHT]',
'π―': '[TARGET]',
'β¨': '[HIGHLIGHT]',
'π': '[LAUNCH]',
'π': '[GROWTH]',
'π': '[DECLINE]',
'π§': '[TOOL]',
'π»': '[CODE]',
'π': '[STAR]',
'π¨': '[DESIGN]',
'π±': '[MOBILE]',
'π₯οΈ': '[DESKTOP]',
'π': '[LINK]',
'π': '[NOTE]',
'π': '[ACHIEVEMENT]',
'π°': '[MONEY]',
'π': '[CELEBRATION]',
'π': '[SECURE]',
'π': '[UNLOCKED]',
'β': '[RATING]',
'β€οΈ': '[FAVORITE]',
'π': '[THUMBS_UP]',
'π': '[THUMBS_DOWN]',
'π₯': '[HOT]',
'βοΈ': '[COLD]',
'π‘οΈ': '[TEMPERATURE]'
}
for emoji, replacement in emoji_replacements.items():
text = text.replace(emoji, replacement)
return text
def format_api_response(response_data):
"""
Format API response and replace emojis with safe indicators.
Args:
response_data: Raw API response (dict, str, or other)
Returns:
str: Formatted response with safe emoji replacements
"""
try:
if isinstance(response_data, dict):
# Convert dict to readable format
formatted_parts = []
for key, value in response_data.items():
if isinstance(value, (str, int, float)):
safe_value = safe_format_with_indicators(str(value))
formatted_parts.append(f"{key}: {safe_value}")
elif isinstance(value, list):
safe_items = [safe_format_with_indicators(str(item)) for item in value]
formatted_parts.append(f"{key}: {', '.join(safe_items)}")
elif isinstance(value, dict):
# Handle nested dictionaries
nested_items = []
for nested_key, nested_value in value.items():
safe_nested = safe_format_with_indicators(str(nested_value))
nested_items.append(f"{nested_key}: {safe_nested}")
formatted_parts.append(f"{key}: {'; '.join(nested_items)}")
result = "\n".join(formatted_parts)
return safe_format_with_indicators(result)
else:
return safe_format_with_indicators(str(response_data))
except Exception as e:
error_msg = f"Error formatting response: {str(e)}"
return safe_format_with_indicators(error_msg)
@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
"""
try:
# Get API key with improved error handling
api_key = get_landing_ai_api_key()
if not api_key:
error_msg = "Landing AI API key not found. Please ensure LANDING_AI_API_KEY is set in environment variables or HuggingFace secrets."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
# Validate file exists
if not os.path.exists(file_path):
error_msg = f"File not found: {file_path}"
return safe_format_with_indicators(f"[ERROR] {error_msg}")
# API endpoint
url = "https://api.va.landing.ai/v1/tools/agentic-document-analysis"
# Prepare the file for upload based on file_type
try:
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 with timeout
response = requests.post(
url,
files=files,
headers=headers,
timeout=60 # 60 second timeout
)
# Handle different response status codes
if response.status_code == 200:
try:
response_json = response.json()
formatted_result = format_api_response(response_json)
return safe_format_with_indicators(formatted_result)
except json.JSONDecodeError:
# If response isn't JSON, format the text response
safe_text = safe_format_with_indicators(response.text)
return safe_format_with_indicators(safe_text)
elif response.status_code == 401:
error_msg = "Authentication failed. Please check your Landing AI API key."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
elif response.status_code == 403:
error_msg = "Access forbidden. Your API key may not have the required permissions."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
elif response.status_code == 429:
error_msg = "Rate limit exceeded. Please try again later."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
elif response.status_code == 500:
error_msg = "Server error from Landing AI. Please try again later."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
else:
error_msg = f"API request failed with status code {response.status_code}: {response.text}"
return safe_format_with_indicators(f"[ERROR] {error_msg}")
except FileNotFoundError:
error_msg = f"File not found: {file_path}"
return safe_format_with_indicators(f"[ERROR] {error_msg}")
except requests.exceptions.Timeout:
error_msg = "Request timed out. The file may be too large or the service is slow."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
except requests.exceptions.ConnectionError:
error_msg = "Connection error. Please check your internet connection."
return safe_format_with_indicators(f"[ERROR] {error_msg}")
except requests.exceptions.RequestException as e:
error_msg = f"Request failed: {str(e)}"
return safe_format_with_indicators(f"[ERROR] {error_msg}")
except Exception as e:
error_msg = f"Unexpected error in document analysis: {str(e)}"
return safe_format_with_indicators(f"[ERROR] {error_msg}")
def test_landing_ai_connection():
"""
Test function to verify Landing AI API connection.
This can be called to verify the setup before processing documents.
Returns:
bool: True if API key is available and valid format, False otherwise
"""
api_key = get_landing_ai_api_key()
if not api_key:
print("β Landing AI API key not found in environment variables")
print("Available environment variables:")
for key in os.environ.keys():
if 'LANDING' in key.upper() or 'API' in key.upper():
print(f" - {key}: {'SET' if os.environ[key] else 'EMPTY'}")
return False
# Basic format validation (adjust based on Landing AI key format)
if len(api_key) < 10:
print("β Landing AI API key appears to be too short")
return False
print("β
Landing AI API key found and appears valid")
return True
# Test the connection when module is imported (optional)
if __name__ == "__main__":
test_landing_ai_connection() |