Spaces:
Paused
Paused
#!/usr/bin/env python3 | |
""" | |
Deployment Validation Script for Hugging Face Spaces | |
=================================================== | |
This script validates the essential components needed for successful deployment. | |
""" | |
import os | |
import sys | |
from pathlib import Path | |
import json | |
def check_file_structure(): | |
"""Check that all required files exist for deployment""" | |
print("π Checking file structure...") | |
required_files = [ | |
"huggingface_space/app.py", | |
"huggingface_space/Spacefile", | |
"huggingface_space/README.md", | |
"requirements.txt", | |
"app/services/ocr_service.py", | |
"app/services/ai_service.py", | |
"app/services/database_service.py", | |
"app/models/document_models.py", | |
"data/sample_persian.pdf" | |
] | |
missing_files = [] | |
for file_path in required_files: | |
if not os.path.exists(file_path): | |
missing_files.append(file_path) | |
else: | |
print(f"β {file_path}") | |
if missing_files: | |
print(f"\nβ Missing files: {missing_files}") | |
return False | |
else: | |
print("\nβ All required files exist") | |
return True | |
def check_requirements(): | |
"""Check requirements.txt for deployment compatibility""" | |
print("\nπ Checking requirements.txt...") | |
try: | |
with open("requirements.txt", "r") as f: | |
requirements = f.read() | |
# Check for essential packages | |
essential_packages = [ | |
"gradio", | |
"transformers", | |
"torch", | |
"fastapi", | |
"uvicorn", | |
"PyMuPDF", | |
"Pillow" | |
] | |
missing_packages = [] | |
for package in essential_packages: | |
if package not in requirements: | |
missing_packages.append(package) | |
if missing_packages: | |
print(f"β Missing packages: {missing_packages}") | |
return False | |
else: | |
print("β All essential packages found in requirements.txt") | |
return True | |
except Exception as e: | |
print(f"β Error reading requirements.txt: {e}") | |
return False | |
def check_spacefile(): | |
"""Check Spacefile configuration""" | |
print("\nπ Checking Spacefile...") | |
try: | |
with open("huggingface_space/Spacefile", "r") as f: | |
spacefile_content = f.read() | |
# Check for essential configurations | |
required_configs = [ | |
"runtime: python", | |
"run: python app.py", | |
"gradio" | |
] | |
missing_configs = [] | |
for config in required_configs: | |
if config not in spacefile_content: | |
missing_configs.append(config) | |
if missing_configs: | |
print(f"β Missing configurations: {missing_configs}") | |
return False | |
else: | |
print("β Spacefile properly configured") | |
return True | |
except Exception as e: | |
print(f"β Error reading Spacefile: {e}") | |
return False | |
def check_app_entry_point(): | |
"""Check the main app.py entry point""" | |
print("\nπ Checking app.py entry point...") | |
try: | |
with open("huggingface_space/app.py", "r") as f: | |
app_content = f.read() | |
# Check for essential components | |
required_components = [ | |
"import gradio", | |
"gr.Blocks", | |
"demo.launch" | |
] | |
missing_components = [] | |
for component in required_components: | |
if component not in app_content: | |
missing_components.append(component) | |
if missing_components: | |
print(f"β Missing components: {missing_components}") | |
return False | |
else: | |
print("β App entry point properly configured") | |
return True | |
except Exception as e: | |
print(f"β Error reading app.py: {e}") | |
return False | |
def check_sample_data(): | |
"""Check that sample data exists""" | |
print("\nπ Checking sample data...") | |
sample_files = [ | |
"data/sample_persian.pdf" | |
] | |
missing_files = [] | |
for file_path in sample_files: | |
if not os.path.exists(file_path): | |
missing_files.append(file_path) | |
else: | |
file_size = os.path.getsize(file_path) | |
print(f"β {file_path} ({file_size} bytes)") | |
if missing_files: | |
print(f"β Missing sample files: {missing_files}") | |
return False | |
else: | |
print("β Sample data available") | |
return True | |
def generate_deployment_summary(): | |
"""Generate deployment summary""" | |
print("\nπ Deployment Summary") | |
print("=" * 50) | |
summary = { | |
"project_name": "Legal Dashboard OCR", | |
"deployment_type": "Hugging Face Spaces", | |
"framework": "Gradio", | |
"entry_point": "huggingface_space/app.py", | |
"requirements": "requirements.txt", | |
"configuration": "huggingface_space/Spacefile", | |
"documentation": "huggingface_space/README.md", | |
"sample_data": "data/sample_persian.pdf" | |
} | |
for key, value in summary.items(): | |
print(f"{key.replace('_', ' ').title()}: {value}") | |
return summary | |
def main(): | |
"""Main validation function""" | |
print("π Legal Dashboard OCR - Deployment Validation") | |
print("=" * 60) | |
# Run all checks | |
checks = [ | |
check_file_structure, | |
check_requirements, | |
check_spacefile, | |
check_app_entry_point, | |
check_sample_data | |
] | |
results = [] | |
for check in checks: | |
try: | |
result = check() | |
results.append(result) | |
except Exception as e: | |
print(f"β Check failed with exception: {e}") | |
results.append(False) | |
# Generate summary | |
summary = generate_deployment_summary() | |
# Final results | |
print("\n" + "=" * 60) | |
print("π Validation Results") | |
print("=" * 60) | |
passed = sum(results) | |
total = len(results) | |
print(f"β Passed: {passed}/{total}") | |
print(f"β Failed: {total - passed}/{total}") | |
if all(results): | |
print("\nπ All validation checks passed!") | |
print("β Project is ready for Hugging Face Spaces deployment") | |
print("\nπ Next Steps:") | |
print("1. Create a new Space on Hugging Face") | |
print("2. Upload the huggingface_space/ directory") | |
print("3. Set HF_TOKEN environment variable") | |
print("4. Deploy and test the application") | |
return 0 | |
else: | |
print("\nβ οΈ Some validation checks failed.") | |
print("Please fix the issues above before deployment.") | |
return 1 | |
if __name__ == "__main__": | |
sys.exit(main()) | |