trabb / test_setup.py
fokan's picture
Initial commit
0a32234
#!/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")