LibreTranslate-Marathi / scripts /healthcheck.py
SkyTraceX's picture
Deploy Marathi Translation API to Hugging Face Spaces
34059a0
#!/usr/bin/env python3
"""
Health check for LibreTranslate Marathi service.
Tests English β†’ Marathi translation.
"""
import requests
import os
import sys
def check_health():
"""Perform health check by testing translation."""
port = os.environ.get('PORT', os.environ.get('LT_PORT', '5000'))
url = f'http://localhost:{port}/translate'
try:
# Test English to Marathi translation
response = requests.post(
url=url,
headers={'Content-Type': 'application/json'},
json={
'q': 'Hello',
'source': 'en',
'target': 'mr'
},
timeout=30
)
response.raise_for_status()
# Verify response structure
data = response.json()
if 'translatedText' not in data:
print("βœ— Health check failed: No translatedText in response")
return False
print(f"βœ“ Health check passed: '{data['translatedText']}'")
return True
except requests.exceptions.Timeout:
print("βœ— Health check failed: Request timeout")
return False
except requests.exceptions.ConnectionError:
print("βœ— Health check failed: Cannot connect to service")
return False
except Exception as e:
print(f"βœ— Health check failed: {e}")
return False
if __name__ == "__main__":
success = check_health()
sys.exit(0 if success else 1)