# test_multimodal_tools.py import sys import os from dotenv import load_dotenv import tempfile import base64 # Load environment variables load_dotenv() # Add tools to path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) def create_test_image(): """Create a simple test image for testing""" try: from PIL import Image, ImageDraw # Create a simple test image img = Image.new('RGB', (200, 100), color='white') draw = ImageDraw.Draw(img) draw.text((10, 40), "Test Image", fill='black') # Save to temporary file temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png') img.save(temp_file.name) return temp_file.name except ImportError: print(" ⚠️ PIL not available, creating mock image file") # Create a mock image file for testing temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt') temp_file.write(b"This is a mock image file for testing") temp_file.close() return temp_file.name def test_multimodal_tools(): """Test multimodal tool functions""" try: from tools.multimodal_tools import MultimodalTools, analyze_image, extract_text, analyze_transcript # Check OpenRouter API key openrouter_key = os.getenv("OPENROUTER_API_KEY") if not openrouter_key: print(" ❌ OpenRouter API key not found!") print(" Please add OPENROUTER_API_KEY to your .env file") return print(" 🎨 Initializing multimodal tools...") multimodal_tools = MultimodalTools() print(f" ✅ API key loaded: {openrouter_key[:8]}...") # Test with transcript (doesn't require image) print(" 🎙️ Testing audio transcript analysis...") test_transcript = "Hello, this is a test transcript. We're discussing artificial intelligence and machine learning technologies." transcript_result = multimodal_tools.analyze_audio_transcript( test_transcript, "What is the main topic of this transcript?" ) if transcript_result and "Error" not in transcript_result: print(" ✅ Transcript analysis: PASSED") print(f" Result: {transcript_result[:100]}...") else: print(f" ❌ Transcript analysis failed: {transcript_result}") # Test convenience function print(" 🔧 Testing convenience functions...") convenience_result = analyze_transcript( "This is a test about Python programming and software development.", "Summarize the main topics" ) if convenience_result and "Error" not in convenience_result: print(" ✅ Convenience function: PASSED") else: print(f" ⚠️ Convenience function result: {convenience_result}") # Test image analysis (if we can create a test image) print(" 🖼️ Testing image analysis...") try: test_image_path = create_test_image() # Test image description image_result = multimodal_tools.analyze_image( test_image_path, "Describe what you see in this image" ) if image_result and "Error" not in image_result: print(" ✅ Image analysis: PASSED") print(f" Result: {image_result[:100]}...") else: print(f" ⚠️ Image analysis result: {image_result}") # Test OCR functionality ocr_result = multimodal_tools.extract_text_from_image(test_image_path) if ocr_result and "Error" not in ocr_result: print(" ✅ OCR extraction: PASSED") print(f" Extracted: {ocr_result[:50]}...") else: print(f" ⚠️ OCR result: {ocr_result}") # Clean up test image os.unlink(test_image_path) except Exception as e: print(f" ⚠️ Image testing skipped: {str(e)}") # Test error handling print(" 🚨 Testing error handling...") error_result = multimodal_tools.analyze_image( "nonexistent_file.jpg", "This should fail" ) if "Error" in error_result: print(" ✅ Error handling: PASSED") else: print(" ⚠️ Error handling unexpected result") # Test empty transcript empty_result = multimodal_tools.analyze_audio_transcript("", "Question") if "Error" in empty_result: print(" ✅ Empty input handling: PASSED") print(" ✅ MULTIMODAL TESTS COMPLETED!") # Demo output print("\n 📋 Multimodal Tools Demo:") demo_transcript = "We discussed the implementation of AI agents using CrewAI framework for automated workflows." demo_result = analyze_transcript(demo_transcript, "What framework was mentioned?") print(f" Question: What framework was mentioned?") print(f" Answer: {demo_result[:150]}...") except ImportError as e: print(f" ❌ Import error: {e}") print(" Make sure all dependencies are installed") except Exception as e: print(f" ❌ Test failed: {e}") import traceback traceback.print_exc() if __name__ == "__main__": test_multimodal_tools()