Spaces:
Sleeping
Sleeping
| """Test script for AI Radio - Verify all components work""" | |
| import sys | |
| def test_imports(): | |
| """Test that all required modules can be imported""" | |
| print("π§ͺ Testing imports...") | |
| try: | |
| import gradio as gr | |
| print(" β Gradio imported") | |
| except ImportError as e: | |
| print(f" β Gradio import failed: {e}") | |
| return False | |
| try: | |
| from openai import OpenAI | |
| print(" β OpenAI (Nebius) imported") | |
| except ImportError as e: | |
| print(f" β OpenAI import failed: {e}") | |
| return False | |
| try: | |
| from elevenlabs import ElevenLabs | |
| print(" β ElevenLabs imported") | |
| except ImportError as e: | |
| print(f" β ElevenLabs import failed: {e}") | |
| return False | |
| try: | |
| from llama_index.core import VectorStoreIndex | |
| print(" β LlamaIndex imported") | |
| except ImportError as e: | |
| print(f" β LlamaIndex import failed: {e}") | |
| return False | |
| try: | |
| import feedparser | |
| print(" β Feedparser imported") | |
| except ImportError as e: | |
| print(f" β Feedparser import failed: {e}") | |
| return False | |
| return True | |
| def test_config(): | |
| """Test configuration file""" | |
| print("\nβοΈ Testing configuration...") | |
| try: | |
| from config import get_config | |
| config = get_config() | |
| print(" β Config loaded") | |
| if config.elevenlabs_api_key: | |
| print(" β ElevenLabs API key present") | |
| else: | |
| print(" β οΈ ElevenLabs API key missing") | |
| if config.nebius_api_key: | |
| print(" β Nebius API key present") | |
| else: | |
| print(" β οΈ Nebius API key missing (REQUIRED!)") | |
| print(" Please add your Nebius API key to config.py") | |
| if config.llamaindex_api_key: | |
| print(" β LlamaIndex API key present") | |
| else: | |
| print(" β οΈ LlamaIndex API key missing") | |
| return True | |
| except Exception as e: | |
| print(f" β Config test failed: {e}") | |
| return False | |
| def test_mcp_servers(): | |
| """Test MCP server imports and initialization""" | |
| print("\nπ§ Testing MCP servers...") | |
| try: | |
| from mcp_servers import MusicMCPServer, NewsMCPServer, PodcastMCPServer | |
| print(" β MCP servers imported") | |
| music_server = MusicMCPServer() | |
| print(" β Music server initialized") | |
| news_server = NewsMCPServer() | |
| print(" β News server initialized") | |
| podcast_server = PodcastMCPServer() | |
| print(" β Podcast server initialized") | |
| # Test music server | |
| tracks = music_server.search_free_music("pop", "happy", 2) | |
| if tracks: | |
| print(f" β Music server returned {len(tracks)} tracks") | |
| else: | |
| print(" β οΈ Music server returned no tracks") | |
| # Test news server | |
| news = news_server.fetch_news("technology", 2) | |
| if news: | |
| print(f" β News server returned {len(news)} items") | |
| else: | |
| print(" β οΈ News server returned no items") | |
| # Test podcast server | |
| podcasts = podcast_server.get_trending_podcasts("technology", 2) | |
| if podcasts: | |
| print(f" β Podcast server returned {len(podcasts)} podcasts") | |
| else: | |
| print(" β οΈ Podcast server returned no podcasts") | |
| return True | |
| except Exception as e: | |
| print(f" β MCP server test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_rag_system(): | |
| """Test RAG system""" | |
| print("\nπΎ Testing RAG system...") | |
| try: | |
| from rag_system import RadioRAGSystem | |
| from config import get_config | |
| config = get_config() | |
| rag = RadioRAGSystem(config.nebius_api_key, config.nebius_api_base, config.nebius_model) | |
| print(" β RAG system initialized") | |
| # Test storing preferences | |
| test_prefs = { | |
| "name": "Test User", | |
| "favorite_genres": ["pop", "rock"], | |
| "interests": ["technology"] | |
| } | |
| rag.store_user_preferences(test_prefs) | |
| print(" β Preferences stored") | |
| # Test retrieving preferences | |
| prefs = rag.get_user_preferences() | |
| if prefs: | |
| print(f" β Preferences retrieved: {prefs.get('name', 'Unknown')}") | |
| else: | |
| print(" β οΈ No preferences found") | |
| # Test stats | |
| stats = rag.get_listening_stats() | |
| print(f" β Stats retrieved: {stats['total_sessions']} sessions") | |
| return True | |
| except Exception as e: | |
| print(f" β RAG system test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_radio_agent(): | |
| """Test radio agent""" | |
| print("\nπ€ Testing radio agent...") | |
| try: | |
| from radio_agent import RadioAgent | |
| from config import get_config | |
| config = get_config() | |
| agent = RadioAgent(config) | |
| print(" β Radio agent initialized") | |
| # Test show planning | |
| test_prefs = { | |
| "name": "Test User", | |
| "favorite_genres": ["pop"], | |
| "interests": ["technology"], | |
| "podcast_interests": ["technology"], | |
| "mood": "happy" | |
| } | |
| show_plan = agent.plan_radio_show(test_prefs, duration_minutes=10) | |
| if show_plan: | |
| print(f" β Show planned with {len(show_plan)} segments") | |
| # Check segment types | |
| segment_types = [seg['type'] for seg in show_plan] | |
| print(f" Segment types: {', '.join(set(segment_types))}") | |
| else: | |
| print(" β οΈ Show planning returned empty plan") | |
| # Test MCP tools | |
| tools = agent.get_all_mcp_tools() | |
| print(f" β Agent has {len(tools)} MCP tools available") | |
| return True | |
| except Exception as e: | |
| print(f" β Radio agent test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_tts_service(): | |
| """Test TTS service""" | |
| print("\nπ Testing TTS service...") | |
| try: | |
| from tts_service import TTSService | |
| from config import get_config | |
| config = get_config() | |
| tts = TTSService(config.elevenlabs_api_key) | |
| print(" β TTS service initialized") | |
| if tts.client: | |
| print(" β ElevenLabs client connected") | |
| # Note: Not actually generating audio to save API calls | |
| print(" βΉοΈ Skipping actual TTS generation to save API credits") | |
| else: | |
| print(" β οΈ ElevenLabs client not initialized (check API key)") | |
| return True | |
| except Exception as e: | |
| print(f" β TTS service test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_app_structure(): | |
| """Test that app file is valid""" | |
| print("\nπ± Testing app structure...") | |
| try: | |
| # Try importing without running | |
| import app | |
| print(" β App file is valid Python") | |
| # Check for key functions | |
| if hasattr(app, 'save_preferences'): | |
| print(" β save_preferences function found") | |
| if hasattr(app, 'start_radio_stream'): | |
| print(" β start_radio_stream function found") | |
| if hasattr(app, 'play_next_segment'): | |
| print(" β play_next_segment function found") | |
| if hasattr(app, 'demo'): | |
| print(" β Gradio demo object found") | |
| return True | |
| except Exception as e: | |
| print(f" β App structure test failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def main(): | |
| """Run all tests""" | |
| print("π΅ AI Radio - Component Test Suite\n") | |
| print("=" * 50) | |
| results = [] | |
| # Run tests | |
| results.append(("Imports", test_imports())) | |
| results.append(("Configuration", test_config())) | |
| results.append(("MCP Servers", test_mcp_servers())) | |
| results.append(("RAG System", test_rag_system())) | |
| results.append(("Radio Agent", test_radio_agent())) | |
| results.append(("TTS Service", test_tts_service())) | |
| results.append(("App Structure", test_app_structure())) | |
| # Print summary | |
| print("\n" + "=" * 50) | |
| print("π Test Summary\n") | |
| passed = sum(1 for _, result in results if result) | |
| total = len(results) | |
| for test_name, result in results: | |
| status = "β PASS" if result else "β FAIL" | |
| print(f" {status} - {test_name}") | |
| print(f"\nResults: {passed}/{total} tests passed") | |
| if passed == total: | |
| print("\nπ All tests passed! Your AI Radio is ready to launch! π") | |
| print("\nNext steps:") | |
| print(" 1. Make sure you've added your Nebius API key to config.py") | |
| print(" 2. Run: python run.py") | |
| print(" 3. Open: http://localhost:7867") | |
| return 0 | |
| else: | |
| print("\nβ οΈ Some tests failed. Please fix the issues above.") | |
| print("\nCommon fixes:") | |
| print(" - Run: pip install -r requirements.txt") | |
| print(" - Add your Nebius API key to config.py") | |
| print(" - Check that all files are in the correct location") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |