File size: 10,388 Bytes
65be7f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
#!/usr/bin/env python3
"""
Test HF Integration
Comprehensive test script to validate that the HF deployment works correctly
across all tracks and tools.
"""
import asyncio
import json
import sys
import time
from datetime import datetime
from typing import Any
import httpx
# HF Space endpoints to test
HF_ENDPOINTS = {
"Summarizer Tool": "https://basalganglia-mcp-summarizer-tool.hf.space",
"Sentiment Analyzer": "https://basalganglia-mcp-sentiment-analyzer.hf.space",
"Code Analyzer": "https://basalganglia-mcp-code-analyzer.hf.space",
"File Processor": "https://basalganglia-mcp-file-processor.hf.space",
"Math Calculator": "https://basalganglia-mcp-math-calculator.hf.space",
"Web Scraper": "https://basalganglia-mcp-web-scraper.hf.space",
"Image Analyzer": "https://basalganglia-mcp-image-analyzer.hf.space",
"Main Platform": "https://basalganglia-kgraph-mcp-agent-platform.hf.space"
}
class HFIntegrationTester:
"""Test the entire HF ecosystem integration."""
def __init__(self):
"""Initialize the tester."""
self.client = httpx.AsyncClient(timeout=30.0)
self.results = {}
async def test_space_availability(self, name: str, url: str) -> dict[str, Any]:
"""Test if a HF Space is available and responding."""
result = {
"name": name,
"url": url,
"available": False,
"response_time": None,
"status_code": None,
"error": None
}
try:
start_time = time.time()
response = await self.client.get(url)
end_time = time.time()
result["response_time"] = round((end_time - start_time) * 1000, 2)
result["status_code"] = response.status_code
result["available"] = response.status_code == 200
except Exception as e:
result["error"] = str(e)
return result
async def test_mcp_endpoint(self, name: str, base_url: str) -> dict[str, Any]:
"""Test if MCP endpoint is working."""
result = {
"name": name,
"mcp_endpoint": f"{base_url}/gradio_api/mcp/sse",
"mcp_working": False,
"response_time": None,
"error": None
}
try:
mcp_url = f"{base_url}/gradio_api/mcp/sse"
# Test with a simple POST request
start_time = time.time()
response = await self.client.post(
mcp_url,
json={"data": ["test input"]},
headers={"Content-Type": "application/json"}
)
end_time = time.time()
result["response_time"] = round((end_time - start_time) * 1000, 2)
result["mcp_working"] = response.status_code in [200, 422] # 422 might be expected for invalid input
except Exception as e:
result["error"] = str(e)
return result
async def test_all_spaces(self) -> dict[str, list[dict[str, Any]]]:
"""Test all HF Spaces."""
print("π§ͺ Testing HF Spaces Availability")
print("=" * 40)
print()
availability_tests = []
mcp_tests = []
for name, url in HF_ENDPOINTS.items():
print(f"Testing {name}...")
# Test basic availability
availability_result = await self.test_space_availability(name, url)
availability_tests.append(availability_result)
# Test MCP endpoint for tool spaces (not main platform)
if name != "Main Platform":
mcp_result = await self.test_mcp_endpoint(name, url)
mcp_tests.append(mcp_result)
# Display result
if availability_result["available"]:
print(f" β
Available ({availability_result['response_time']}ms)")
else:
error = availability_result.get("error", "Unknown error")
print(f" β Unavailable: {error}")
print()
return {
"availability": availability_tests,
"mcp_endpoints": mcp_tests
}
async def test_main_platform_integration(self) -> dict[str, Any]:
"""Test main platform integration with tools."""
print("π Testing Main Platform Integration")
print("=" * 35)
print()
main_url = HF_ENDPOINTS["Main Platform"]
result = {
"main_platform_available": False,
"integration_test": False,
"error": None
}
try:
# Test main platform availability
response = await self.client.get(main_url)
result["main_platform_available"] = response.status_code == 200
if result["main_platform_available"]:
print("β
Main platform is available")
# TODO: Add more sophisticated integration tests
# For now, just check that it's responsive
result["integration_test"] = True
print("β
Basic integration test passed")
else:
print(f"β Main platform unavailable (status: {response.status_code})")
except Exception as e:
result["error"] = str(e)
print(f"β Main platform test failed: {e}")
print()
return result
def generate_report(self, test_results: dict[str, Any]) -> None:
"""Generate a comprehensive test report."""
print("π HF INTEGRATION TEST REPORT")
print("=" * 50)
print(f"Test Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print()
# Availability summary
availability_tests = test_results["space_tests"]["availability"]
available_count = sum(1 for test in availability_tests if test["available"])
total_count = len(availability_tests)
print(f"π SPACE AVAILABILITY: {available_count}/{total_count}")
print("-" * 30)
for test in availability_tests:
status = "β
" if test["available"] else "β"
response_time = f"({test['response_time']}ms)" if test["response_time"] else ""
print(f"{status} {test['name']}: {test['url']} {response_time}")
if test.get("error"):
print(f" Error: {test['error']}")
print()
# MCP endpoints summary
mcp_tests = test_results["space_tests"]["mcp_endpoints"]
mcp_working_count = sum(1 for test in mcp_tests if test["mcp_working"])
mcp_total_count = len(mcp_tests)
print(f"π§ MCP ENDPOINTS: {mcp_working_count}/{mcp_total_count}")
print("-" * 25)
for test in mcp_tests:
status = "β
" if test["mcp_working"] else "β"
response_time = f"({test['response_time']}ms)" if test["response_time"] else ""
print(f"{status} {test['name']} MCP {response_time}")
if test.get("error"):
print(f" Error: {test['error']}")
print()
# Integration summary
integration_result = test_results["integration_test"]
print("π INTEGRATION TEST")
print("-" * 20)
if integration_result["main_platform_available"]:
print("β
Main platform available")
else:
print("β Main platform unavailable")
if integration_result["integration_test"]:
print("β
Basic integration working")
else:
print("β Integration test failed")
if integration_result.get("error"):
print(f"Error: {integration_result['error']}")
print()
# Overall assessment
print("π― HACKATHON READINESS ASSESSMENT")
print("-" * 35)
total_score = 0
max_score = 0
# Space availability scoring (40 points)
availability_score = (available_count / total_count) * 40
total_score += availability_score
max_score += 40
print(f"Space Availability: {availability_score:.1f}/40 ({available_count}/{total_count} spaces)")
# MCP endpoint scoring (40 points)
mcp_score = (mcp_working_count / mcp_total_count) * 40 if mcp_total_count > 0 else 0
total_score += mcp_score
max_score += 40
print(f"MCP Endpoints: {mcp_score:.1f}/40 ({mcp_working_count}/{mcp_total_count} endpoints)")
# Integration scoring (20 points)
integration_score = 20 if integration_result["integration_test"] else 0
total_score += integration_score
max_score += 20
print(f"Integration: {integration_score}/20")
print()
print(f"TOTAL SCORE: {total_score:.1f}/{max_score}")
# Final assessment
if total_score >= 90:
print("π EXCELLENT - Ready for hackathon submission!")
elif total_score >= 70:
print("β
GOOD - Minor issues to address")
elif total_score >= 50:
print("π‘ FAIR - Several issues need fixing")
else:
print("β POOR - Major deployment issues")
print()
async def run_comprehensive_test(self) -> None:
"""Run the complete test suite."""
print("π KGraph-MCP HF Integration Test Suite")
print("=" * 45)
print()
try:
# Test all spaces
space_tests = await self.test_all_spaces()
# Test main platform integration
integration_test = await self.test_main_platform_integration()
# Compile results
test_results = {
"space_tests": space_tests,
"integration_test": integration_test,
"timestamp": datetime.now().isoformat()
}
# Generate report
self.generate_report(test_results)
# Save results to file
with open("hf_integration_test_results.json", "w") as f:
json.dump(test_results, f, indent=2)
print("πΎ Test results saved to: hf_integration_test_results.json")
except Exception as e:
print(f"β Test suite failed: {e}")
sys.exit(1)
finally:
await self.client.aclose()
async def main():
"""Main test runner."""
tester = HFIntegrationTester()
await tester.run_comprehensive_test()
if __name__ == "__main__":
asyncio.run(main())
|