AI-RADIO / src /test_app.py
Nikita Makarov
works cool
0df1705
"""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())