#!/usr/bin/env python import os import tempfile import gradio as gr import warnings from crew import DocProcessing warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd") def determine_file_type(file_path): """ Determine the file type based on the file extension. Args: file_path (str): Path to the file Returns: str: 'pdf' if the file is a PDF, 'image' otherwise """ _, ext = os.path.splitext(file_path) if ext.lower() == '.pdf': return 'pdf' return 'image' def process_document(file, anthropic_api_key, landing_ai_api_key): """ Process the uploaded document using CrewAI. Args: file: Uploaded file from Gradio anthropic_api_key (str): Anthropic API key landing_ai_api_key (str): LandingAI API key Returns: str: Processing results or error message """ try: # Validate inputs if file is None: return "❌ Please upload a file first." if not anthropic_api_key.strip(): return "❌ Please provide your Anthropic API key." if not landing_ai_api_key.strip(): return "❌ Please provide your LandingAI API key." # Set environment variables securely for this session os.environ["ANTHROPIC_API_KEY"] = anthropic_api_key.strip() os.environ["LANDING_AI_API_KEY"] = landing_ai_api_key.strip() # Get file path and determine type file_path = file.name file_type = determine_file_type(file_path) print(f"Processing file: {file_path} (type: {file_type})") # Prepare inputs for CrewAI inputs = { "file_path": file_path, "file_type": file_type, } # Process with CrewAI result = DocProcessing().crew().kickoff(inputs=inputs) # Clean up environment variables for security if "ANTHROPIC_API_KEY" in os.environ: del os.environ["ANTHROPIC_API_KEY"] if "LANDING_AI_API_KEY" in os.environ: del os.environ["LANDING_AI_API_KEY"] return f"✅ **Processing Complete!**\n\n{result}" except Exception as e: # Clean up environment variables even on error if "ANTHROPIC_API_KEY" in os.environ: del os.environ["ANTHROPIC_API_KEY"] if "LANDING_AI_API_KEY" in os.environ: del os.environ["LANDING_AI_API_KEY"] error_msg = f"❌ **Error occurred:** {str(e)}" print(error_msg) return error_msg # Create Gradio interface def create_interface(): """Create and return the Gradio interface.""" with gr.Blocks( title="Document Analysis with CrewAI", theme=gr.themes.Soft(), css=""" .container { max-width: 800px; margin: auto; } .header { text-align: center; margin-bottom: 30px; } .api-section { background-color: #f8f9fa; padding: 20px; border-radius: 10px; margin-bottom: 20px; } """ ) as demo: gr.HTML("""
Upload your documents for intelligent analysis using AI agents