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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Configuration validation for HuggingFace Spaces deployment
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
def validate_hf_config():
|
| 11 |
+
"""Validate HuggingFace Spaces configuration."""
|
| 12 |
+
|
| 13 |
+
print("🔍 VALIDATING HUGGINGFACE SPACES CONFIGURATION")
|
| 14 |
+
print("=" * 55)
|
| 15 |
+
|
| 16 |
+
checks = []
|
| 17 |
+
|
| 18 |
+
# Check 1: app.py exists and has main block
|
| 19 |
+
print("1️⃣ CHECKING APP.PY")
|
| 20 |
+
print("-" * 25)
|
| 21 |
+
|
| 22 |
+
if os.path.exists("app.py"):
|
| 23 |
+
with open("app.py", "r") as f:
|
| 24 |
+
content = f.read()
|
| 25 |
+
if 'if __name__ == "__main__":' in content:
|
| 26 |
+
print("✅ app.py exists with main block")
|
| 27 |
+
checks.append(True)
|
| 28 |
+
else:
|
| 29 |
+
print("❌ app.py missing main execution block")
|
| 30 |
+
checks.append(False)
|
| 31 |
+
else:
|
| 32 |
+
print("❌ app.py not found")
|
| 33 |
+
checks.append(False)
|
| 34 |
+
|
| 35 |
+
print()
|
| 36 |
+
|
| 37 |
+
# Check 2: requirements.txt
|
| 38 |
+
print("2️⃣ CHECKING REQUIREMENTS.TXT")
|
| 39 |
+
print("-" * 30)
|
| 40 |
+
|
| 41 |
+
if os.path.exists("requirements.txt"):
|
| 42 |
+
with open("requirements.txt", "r") as f:
|
| 43 |
+
reqs = f.read()
|
| 44 |
+
required_packages = ["fastapi", "uvicorn", "pydantic"]
|
| 45 |
+
missing = []
|
| 46 |
+
|
| 47 |
+
for pkg in required_packages:
|
| 48 |
+
if pkg not in reqs.lower():
|
| 49 |
+
missing.append(pkg)
|
| 50 |
+
|
| 51 |
+
if not missing:
|
| 52 |
+
print("✅ All required packages present")
|
| 53 |
+
checks.append(True)
|
| 54 |
+
else:
|
| 55 |
+
print(f"❌ Missing packages: {missing}")
|
| 56 |
+
checks.append(False)
|
| 57 |
+
else:
|
| 58 |
+
print("❌ requirements.txt not found")
|
| 59 |
+
checks.append(False)
|
| 60 |
+
|
| 61 |
+
print()
|
| 62 |
+
|
| 63 |
+
# Check 3: Dockerfile
|
| 64 |
+
print("3️⃣ CHECKING DOCKERFILE")
|
| 65 |
+
print("-" * 25)
|
| 66 |
+
|
| 67 |
+
if os.path.exists("Dockerfile"):
|
| 68 |
+
with open("Dockerfile", "r") as f:
|
| 69 |
+
dockerfile = f.read()
|
| 70 |
+
if "EXPOSE 7860" in dockerfile and "python app.py" in dockerfile:
|
| 71 |
+
print("✅ Dockerfile properly configured")
|
| 72 |
+
checks.append(True)
|
| 73 |
+
else:
|
| 74 |
+
print("❌ Dockerfile missing HF configuration")
|
| 75 |
+
checks.append(False)
|
| 76 |
+
else:
|
| 77 |
+
print("❌ Dockerfile not found")
|
| 78 |
+
checks.append(False)
|
| 79 |
+
|
| 80 |
+
print()
|
| 81 |
+
|
| 82 |
+
# Check 4: README.md with HF frontmatter
|
| 83 |
+
print("4️⃣ CHECKING README.MD")
|
| 84 |
+
print("-" * 25)
|
| 85 |
+
|
| 86 |
+
if os.path.exists("README.md"):
|
| 87 |
+
with open("README.md", "r") as f:
|
| 88 |
+
readme = f.read()
|
| 89 |
+
if "---\ntitle:" in readme and "sdk: docker" in readme:
|
| 90 |
+
print("✅ README.md has HuggingFace frontmatter")
|
| 91 |
+
checks.append(True)
|
| 92 |
+
else:
|
| 93 |
+
print("❌ README.md missing HF frontmatter")
|
| 94 |
+
checks.append(False)
|
| 95 |
+
else:
|
| 96 |
+
print("❌ README.md not found")
|
| 97 |
+
checks.append(False)
|
| 98 |
+
|
| 99 |
+
print()
|
| 100 |
+
|
| 101 |
+
# Check 5: src directory structure
|
| 102 |
+
print("5️⃣ CHECKING SRC STRUCTURE")
|
| 103 |
+
print("-" * 30)
|
| 104 |
+
|
| 105 |
+
if os.path.exists("src"):
|
| 106 |
+
critical_dirs = ["agents", "core", "api"]
|
| 107 |
+
missing_dirs = []
|
| 108 |
+
|
| 109 |
+
for dir_name in critical_dirs:
|
| 110 |
+
if not os.path.exists(f"src/{dir_name}"):
|
| 111 |
+
missing_dirs.append(dir_name)
|
| 112 |
+
|
| 113 |
+
if not missing_dirs:
|
| 114 |
+
print("✅ Core src directories present")
|
| 115 |
+
checks.append(True)
|
| 116 |
+
else:
|
| 117 |
+
print(f"⚠️ Some directories missing: {missing_dirs}")
|
| 118 |
+
print(" (Using fallback mode - OK for HF)")
|
| 119 |
+
checks.append(True) # Still OK for HF deployment
|
| 120 |
+
else:
|
| 121 |
+
print("❌ src directory not found")
|
| 122 |
+
checks.append(False)
|
| 123 |
+
|
| 124 |
+
print()
|
| 125 |
+
|
| 126 |
+
# Summary
|
| 127 |
+
print("📊 VALIDATION SUMMARY")
|
| 128 |
+
print("-" * 25)
|
| 129 |
+
|
| 130 |
+
passed = sum(checks)
|
| 131 |
+
total = len(checks)
|
| 132 |
+
score = (passed / total) * 100
|
| 133 |
+
|
| 134 |
+
print(f"Checks passed: {passed}/{total}")
|
| 135 |
+
print(f"Configuration score: {score:.0f}%")
|
| 136 |
+
|
| 137 |
+
if score >= 80:
|
| 138 |
+
print("✅ READY FOR HUGGINGFACE DEPLOYMENT")
|
| 139 |
+
print("🚀 Configuration meets HuggingFace Spaces requirements")
|
| 140 |
+
elif score >= 60:
|
| 141 |
+
print("⚠️ DEPLOYMENT POSSIBLE WITH WARNINGS")
|
| 142 |
+
print("🔧 Some issues detected but deployment should work")
|
| 143 |
+
else:
|
| 144 |
+
print("❌ NEEDS FIXES BEFORE DEPLOYMENT")
|
| 145 |
+
print("🛠️ Critical issues must be resolved")
|
| 146 |
+
|
| 147 |
+
print()
|
| 148 |
+
print("🎯 Next step: Push to HuggingFace Space repository")
|
| 149 |
+
|
| 150 |
+
return score >= 60
|
| 151 |
+
|
| 152 |
+
if __name__ == "__main__":
|
| 153 |
+
print("🤖 CIDADÃO.AI BACKEND - HF VALIDATION")
|
| 154 |
+
print()
|
| 155 |
+
|
| 156 |
+
try:
|
| 157 |
+
is_ready = validate_hf_config()
|
| 158 |
+
if is_ready:
|
| 159 |
+
print("✅ Validation completed - Ready for deployment!")
|
| 160 |
+
else:
|
| 161 |
+
print("❌ Validation failed - Fix issues before deployment")
|
| 162 |
+
sys.exit(1)
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"❌ Validation error: {e}")
|
| 165 |
+
sys.exit(1)
|