Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script to verify MCP integration with STT service | |
| """ | |
| import asyncio | |
| import json | |
| import sys | |
| import os | |
| # Add current directory to path for imports | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| def test_imports(): | |
| """Test that all required imports work""" | |
| print("π§ͺ Testing imports...") | |
| try: | |
| import gradio as gr | |
| print("β Gradio import successful") | |
| except ImportError as e: | |
| print(f"β Gradio import failed: {e}") | |
| return False | |
| try: | |
| import mcp | |
| from mcp.server import Server | |
| from mcp.types import Tool, TextContent | |
| print("β MCP imports successful") | |
| return True | |
| except ImportError as e: | |
| print(f"β MCP imports failed: {e}") | |
| print("π‘ Install with: pip install mcp>=1.0.0") | |
| return False | |
| def test_mcp_server_creation(): | |
| """Test MCP server creation""" | |
| print("\nπ§ͺ Testing MCP server creation...") | |
| try: | |
| from app import mcp_server, MCP_AVAILABLE | |
| if not MCP_AVAILABLE: | |
| print("β MCP not available in app") | |
| return False | |
| if mcp_server is None: | |
| print("β MCP server not initialized") | |
| return False | |
| print("β MCP server created successfully") | |
| return True | |
| except Exception as e: | |
| print(f"β MCP server creation failed: {e}") | |
| return False | |
| async def test_mcp_tools(): | |
| """Test MCP tool listing""" | |
| print("\nπ§ͺ Testing MCP tool definitions...") | |
| try: | |
| from app import mcp_server, MCP_AVAILABLE | |
| if not MCP_AVAILABLE: | |
| print("β MCP not available") | |
| return False | |
| # Get the list_tools handler | |
| tools = await mcp_server.list_tools()() | |
| print(f"β Found {len(tools)} MCP tools:") | |
| for tool in tools: | |
| print(f" - {tool.name}: {tool.description}") | |
| # Verify expected tools exist | |
| tool_names = [tool.name for tool in tools] | |
| expected_tools = ["stt_transcribe", "stt_batch_transcribe", "stt_get_info"] | |
| for expected in expected_tools: | |
| if expected in tool_names: | |
| print(f"β Tool '{expected}' found") | |
| else: | |
| print(f"β Tool '{expected}' missing") | |
| return False | |
| return True | |
| except Exception as e: | |
| print(f"β MCP tools test failed: {e}") | |
| return False | |
| async def test_system_info_tool(): | |
| """Test the system info MCP tool""" | |
| print("\nπ§ͺ Testing stt_get_info tool...") | |
| try: | |
| from app import mcp_server, MCP_AVAILABLE | |
| if not MCP_AVAILABLE: | |
| print("β MCP not available") | |
| return False | |
| # Call the get_info tool | |
| result = await mcp_server.call_tool()("stt_get_info", {}) | |
| if not result or len(result) == 0: | |
| print("β No result from stt_get_info") | |
| return False | |
| info_text = result[0].text | |
| info_data = json.loads(info_text) | |
| print("β System info tool response:") | |
| print(f" - MCP Status: {info_data.get('mcp_status', 'Unknown')}") | |
| print(f" - Available Tools: {info_data.get('available_tools', [])}") | |
| print(f" - Supported Languages: {len(info_data.get('supported_languages', []))} languages") | |
| print(f" - Supported Models: {info_data.get('supported_models', [])}") | |
| return True | |
| except Exception as e: | |
| print(f"β System info tool test failed: {e}") | |
| return False | |
| def test_gradio_functions(): | |
| """Test that original Gradio functions still work""" | |
| print("\nπ§ͺ Testing original Gradio functions...") | |
| try: | |
| from app import get_system_info, LANGUAGES, MODEL_SIZES | |
| # Test system info function | |
| info = get_system_info() | |
| if "MCP Server" in info: | |
| print("β get_system_info() includes MCP status") | |
| else: | |
| print("β get_system_info() missing MCP status") | |
| return False | |
| # Test language and model definitions | |
| print(f"β Languages available: {len(LANGUAGES)}") | |
| print(f"β Model sizes available: {len(MODEL_SIZES)}") | |
| return True | |
| except Exception as e: | |
| print(f"β Gradio functions test failed: {e}") | |
| return False | |
| async def main(): | |
| """Run all tests""" | |
| print("π STT Service MCP Integration Tests") | |
| print("=" * 50) | |
| tests = [ | |
| ("Import Test", test_imports), | |
| ("MCP Server Creation", test_mcp_server_creation), | |
| ("MCP Tools", test_mcp_tools), | |
| ("System Info Tool", test_system_info_tool), | |
| ("Gradio Functions", test_gradio_functions) | |
| ] | |
| results = [] | |
| for test_name, test_func in tests: | |
| try: | |
| if asyncio.iscoroutinefunction(test_func): | |
| result = await test_func() | |
| else: | |
| result = test_func() | |
| results.append((test_name, result)) | |
| except Exception as e: | |
| print(f"β {test_name} crashed: {e}") | |
| results.append((test_name, False)) | |
| # Summary | |
| print("\n" + "=" * 50) | |
| print("π Test Results Summary") | |
| 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} tests passed") | |
| if passed == total: | |
| print("π All tests passed! MCP integration is working correctly.") | |
| print("\nπ Next steps:") | |
| print("1. Deploy updated service to Hugging Face") | |
| print("2. Test with actual MCP client") | |
| print("3. Integrate with ChatCal voice pipeline") | |
| else: | |
| print("β Some tests failed. Please check the implementation.") | |
| return 1 | |
| return 0 | |
| if __name__ == "__main__": | |
| exit_code = asyncio.run(main()) | |
| sys.exit(exit_code) |