File size: 4,090 Bytes
0a32234 |
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 |
#!/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") |