| |
| """ |
| 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...") |
| |
| |
| print(f"β
Python version: {sys.version}") |
| |
| |
| 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") |
| |
| |
| 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") |
| |
| |
| 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}") |
| |
| |
| 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)") |
| |
| |
| import asyncio |
| async def test_models(): |
| models = await translator.get_available_models() |
| print(f"β
Found {len(models)} translation models") |
| for model in models[: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") |
| |
| |
| 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") |