enflow-api / test_hf_deployment.py
dhruv575
Pokemon
71ad4a3
"""
Test script to verify HuggingFace deployment configuration
This script checks the common issues that might occur in a Docker deployment.
"""
import os
import sys
import tempfile
import importlib.util
import requests
def check_tmp_writable():
"""Check if /tmp directory is writable"""
print("Checking if /tmp directory is writable...")
try:
with tempfile.NamedTemporaryFile(dir='/tmp', prefix='enflow_test_') as tmp:
tmp.write(b'test')
print(f"βœ… Successfully wrote to temp file: {tmp.name}")
print("βœ… /tmp is writable")
return True
except Exception as e:
print(f"❌ Could not write to /tmp: {str(e)}")
return False
def check_tmp_subdirs():
"""Check if we can create subdirectories in /tmp"""
print("\nChecking if we can create subdirectories in /tmp...")
try:
test_dir = '/tmp/enflow_test_dir'
if os.path.exists(test_dir):
os.rmdir(test_dir)
os.makedirs(test_dir)
print(f"βœ… Successfully created directory: {test_dir}")
# Try to write a file in the new directory
test_file = os.path.join(test_dir, 'test.txt')
with open(test_file, 'w') as f:
f.write('test')
print(f"βœ… Successfully wrote to file in test directory: {test_file}")
# Clean up
os.remove(test_file)
os.rmdir(test_dir)
print("βœ… Successfully cleaned up test directory")
return True
except Exception as e:
print(f"❌ Could not create/write to subdirectory in /tmp: {str(e)}")
return False
def check_env_variables():
"""Check if environment variables are accessible"""
print("\nChecking environment variables...")
required_vars = [
'MONGO_URI',
'JWT_SECRET',
'CLOUDINARY_CLOUD_NAME',
'CLOUDINARY_API_KEY',
'CLOUDINARY_API_SECRET',
'OPENAI_API_KEY'
]
all_present = True
for var in required_vars:
if os.environ.get(var):
print(f"βœ… {var} is set")
else:
print(f"❌ {var} is not set")
all_present = False
return all_present
def check_flask_app():
"""Check if Flask app can be imported"""
print("\nChecking if Flask app can be imported...")
try:
# Add current directory to sys.path if needed
if os.getcwd() not in sys.path:
sys.path.append(os.getcwd())
# Try importing the app module
spec = importlib.util.find_spec('app')
if spec is None:
print("❌ Could not find app module")
return False
from app import app
print("βœ… Flask app imported successfully")
return True
except Exception as e:
print(f"❌ Could not import Flask app: {str(e)}")
return False
def check_test_endpoint():
"""Try to start the Flask app and test the endpoint"""
print("\nChecking if the test endpoint works...")
# This is a simplified test - in a real scenario, you might want to
# start the Flask app in a separate process and then make the request
try:
# Check if the app is already running on localhost
response = requests.get('http://localhost:5000/api/test', timeout=2)
if response.status_code == 200:
print(f"βœ… Test endpoint returned: {response.json()}")
return True
except requests.exceptions.RequestException:
print("ℹ️ App not currently running on localhost, skipping endpoint test")
print("ℹ️ To fully test the endpoints, start the app separately with 'python app.py'")
return True # Return True even if we skipped the test to avoid failing the overall check
def main():
"""Run all checks"""
print("=== Enflow HuggingFace Deployment Test ===\n")
# Run checks
tmp_writable = check_tmp_writable()
tmp_subdirs = check_tmp_subdirs()
env_vars = check_env_variables()
flask_app = check_flask_app()
test_endpoint = check_test_endpoint()
# Summary
print("\n=== Test Summary ===")
print(f"βœ… /tmp writable: {tmp_writable}")
print(f"βœ… Can create subdirs in /tmp: {tmp_subdirs}")
print(f"βœ… Environment variables: {env_vars}")
print(f"βœ… Flask app importable: {flask_app}")
print(f"βœ… Test endpoint check: {test_endpoint}")
if all([tmp_writable, tmp_subdirs, env_vars, flask_app]):
print("\nβœ… All tests passed! The app should work on HuggingFace.")
else:
print("\n❌ Some tests failed. Please fix the issues before deploying.")
sys.exit(1)
if __name__ == "__main__":
main()