#!/usr/bin/env python3 """ Test script to verify the document translator setup """ import os import sys from pathlib import Path def test_environment(): """Test if environment is properly set up""" print("šŸ” Testing Document Translator Environment...") # Check Python version print(f"āœ… Python version: {sys.version}") # Check required modules required_modules = [ 'fastapi', 'uvicorn', 'aiohttp', 'aiofiles', 'docx', 'requests', 'PIL' ] missing_modules = [] for module in required_modules: try: __import__(module) print(f"āœ… {module} module available") except ImportError: missing_modules.append(module) print(f"āŒ {module} module missing") # Check OpenRouter API key api_key = os.getenv('OPENROUTER_API_KEY') if api_key: print(f"āœ… OpenRouter API key found (length: {len(api_key)})") else: print("āš ļø OpenRouter API key not found in environment") # Check LibreOffice installation try: import subprocess result = subprocess.run(['libreoffice', '--version'], capture_output=True, text=True, timeout=10) if result.returncode == 0: print(f"āœ… LibreOffice found: {result.stdout.strip()}") else: print("āŒ LibreOffice not found or not working") except Exception as e: print(f"āŒ LibreOffice check failed: {e}") # Check file structure required_files = [ 'app/main.py', 'translator.py', 'web/index.html', 'web/style.css', 'web/app.js', 'requirements.txt', 'Dockerfile' ] for file_path in required_files: if Path(file_path).exists(): print(f"āœ… {file_path} exists") else: print(f"āŒ {file_path} missing") print("\nšŸ Environment test complete!") if missing_modules: print(f"\nāš ļø Please install missing modules: {', '.join(missing_modules)}") print("Run: pip install -r requirements.txt") if not api_key: print("\nāš ļø Please set your OpenRouter API key:") print("export OPENROUTER_API_KEY='your_key_here'") def test_translator(): """Test translator functionality""" print("\nšŸ”§ Testing Translator Module...") try: from translator import DocumentTranslator translator = DocumentTranslator() if translator.is_ready(): print("āœ… Translator initialized successfully") else: print("āš ļø Translator not ready (missing API key)") # Test model fetching import asyncio async def test_models(): models = await translator.get_available_models() print(f"āœ… Found {len(models)} translation models") for model in models[:3]: # Show first 3 print(f" - {model['name']}") asyncio.run(test_models()) except Exception as e: print(f"āŒ Translator test failed: {e}") def test_fastapi(): """Test FastAPI application""" print("\n🌐 Testing FastAPI Application...") try: from app.main import app print("āœ… FastAPI app imported successfully") # Test if all routes are defined routes = [route.path for route in app.routes] expected_routes = ['/', '/models', '/translate', '/download/{file_path:path}', '/health'] for route in expected_routes: if any(route in r for r in routes): print(f"āœ… Route {route} defined") else: print(f"āŒ Route {route} missing") except Exception as e: print(f"āŒ FastAPI test failed: {e}") if __name__ == "__main__": test_environment() test_translator() test_fastapi() print("\nšŸš€ Ready to start the application!") print("Run: python app.py") print("Then open: http://localhost:7860")