Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Syntax validation for MCP-enabled STT service | |
| """ | |
| import ast | |
| import sys | |
| import os | |
| def validate_python_syntax(file_path): | |
| """Validate Python syntax of a file""" | |
| print(f"π§ͺ Validating syntax of {file_path}...") | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| source_code = f.read() | |
| # Parse the AST to check for syntax errors | |
| ast.parse(source_code) | |
| print(f"β {file_path} has valid Python syntax") | |
| return True | |
| except SyntaxError as e: | |
| print(f"β Syntax error in {file_path}:") | |
| print(f" Line {e.lineno}: {e.text}") | |
| print(f" {' ' * (e.offset-1)}^") | |
| print(f" {e.msg}") | |
| return False | |
| except Exception as e: | |
| print(f"β Error reading {file_path}: {e}") | |
| return False | |
| def check_mcp_integration_structure(file_path): | |
| """Check that MCP integration follows expected structure""" | |
| print(f"π Checking MCP integration structure in {file_path}...") | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # Check for required MCP components | |
| required_elements = [ | |
| "from mcp.server import Server", | |
| "from mcp.types import Tool, TextContent", | |
| "mcp_server = Server(", | |
| "@mcp_server.list_tools()", | |
| "@mcp_server.call_tool()", | |
| "stt_transcribe", | |
| "stt_batch_transcribe", | |
| "stt_get_info", | |
| "MCP_AVAILABLE", | |
| "--mcp-only" | |
| ] | |
| missing_elements = [] | |
| for element in required_elements: | |
| if element not in content: | |
| missing_elements.append(element) | |
| if missing_elements: | |
| print(f"β Missing MCP elements:") | |
| for element in missing_elements: | |
| print(f" - {element}") | |
| return False | |
| print("β All required MCP elements found") | |
| # Check for proper error handling | |
| if "try:" in content and "except ImportError:" in content: | |
| print("β Proper import error handling found") | |
| else: | |
| print("β οΈ Warning: Import error handling may be missing") | |
| # Check for dual mode support | |
| if "if __name__ == \"__main__\":" in content and "sys.argv" in content: | |
| print("β Dual mode support (Gradio + MCP) implemented") | |
| else: | |
| print("β οΈ Warning: Dual mode support may be incomplete") | |
| return True | |
| except Exception as e: | |
| print(f"β Error checking structure: {e}") | |
| return False | |
| def validate_requirements(file_path): | |
| """Validate requirements.txt has MCP dependency""" | |
| print(f"π¦ Validating requirements in {file_path}...") | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| requirements = f.read() | |
| if "mcp>=" in requirements: | |
| print("β MCP dependency found in requirements.txt") | |
| return True | |
| else: | |
| print("β MCP dependency missing from requirements.txt") | |
| return False | |
| except Exception as e: | |
| print(f"β Error reading requirements: {e}") | |
| return False | |
| def main(): | |
| """Run validation tests""" | |
| print("π MCP Integration Validation") | |
| print("=" * 50) | |
| base_dir = os.path.dirname(os.path.abspath(__file__)) | |
| tests = [ | |
| ("App Syntax", lambda: validate_python_syntax(os.path.join(base_dir, "app.py"))), | |
| ("Test Syntax", lambda: validate_python_syntax(os.path.join(base_dir, "test_mcp_integration.py"))), | |
| ("MCP Structure", lambda: check_mcp_integration_structure(os.path.join(base_dir, "app.py"))), | |
| ("Requirements", lambda: validate_requirements(os.path.join(base_dir, "requirements.txt"))) | |
| ] | |
| results = [] | |
| for test_name, test_func in tests: | |
| try: | |
| result = test_func() | |
| results.append((test_name, result)) | |
| except Exception as e: | |
| print(f"β {test_name} crashed: {e}") | |
| results.append((test_name, False)) | |
| print() # Add spacing | |
| # Summary | |
| print("=" * 50) | |
| print("π Validation Results") | |
| print("=" * 50) | |
| passed = 0 | |
| total = len(results) | |
| for test_name, result in results: | |
| status = "β PASS" if result else "β FAIL" | |
| print(f"{status}: {test_name}") | |
| if result: | |
| passed += 1 | |
| print(f"\nπ― Results: {passed}/{total} validations passed") | |
| if passed == total: | |
| print("π All validations passed! Code is ready for deployment.") | |
| print("\nπ Integration Summary:") | |
| print("- β MCP server capabilities added") | |
| print("- β Three MCP tools implemented (transcribe, batch, info)") | |
| print("- β Dual protocol support (Gradio + MCP)") | |
| print("- β Backwards compatibility maintained") | |
| print("- β Proper error handling and fallbacks") | |
| print("\nπ Next Steps:") | |
| print("1. Deploy to Hugging Face Spaces") | |
| print("2. Test MCP client connectivity") | |
| print("3. Integrate with ChatCal voice pipeline") | |
| else: | |
| print("β Some validations failed. Please review the code.") | |
| return 1 | |
| return 0 | |
| if __name__ == "__main__": | |
| exit_code = main() | |
| sys.exit(exit_code) |