Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Demo script for the EEG Motor Imagery Music Composer | |
| """ | |
| import sys | |
| import os | |
| from pathlib import Path | |
| # Add the current directory to Python path | |
| current_dir = Path(__file__).parent | |
| sys.path.insert(0, str(current_dir)) | |
| from data_processor import EEGDataProcessor | |
| from classifier import MotorImageryClassifier | |
| from sound_library import SoundManager | |
| from utils import setup_logging, create_classification_summary | |
| import numpy as np | |
| def run_demo(): | |
| """Run a simple demo of the system components.""" | |
| print("🧠 EEG Motor Imagery Music Composer - Demo") | |
| print("=" * 50) | |
| # Initialize components | |
| print("Initializing components...") | |
| data_processor = EEGDataProcessor() | |
| classifier = MotorImageryClassifier() | |
| sound_manager = SoundManager() | |
| # Create mock data for demo | |
| print("Creating mock EEG data...") | |
| mock_data = np.random.randn(10, 32, 384).astype(np.float32) # 10 samples, 32 channels, 384 time points | |
| mock_labels = np.random.randint(0, 6, 10) | |
| # Initialize classifier | |
| print("Loading classifier...") | |
| classifier.load_model(n_chans=32, n_times=384) | |
| print(f"Available sounds: {sound_manager.get_available_sounds()}") | |
| print() | |
| # Run classification demo | |
| print("Running classification demo...") | |
| print("-" * 30) | |
| for i in range(5): | |
| # Get random sample | |
| sample_idx = np.random.randint(0, len(mock_data)) | |
| eeg_sample = mock_data[sample_idx] | |
| true_label = mock_labels[sample_idx] | |
| # Classify | |
| predicted_class, confidence, probabilities = classifier.predict(eeg_sample) | |
| predicted_name = classifier.class_names[predicted_class] | |
| true_name = classifier.class_names[true_label] | |
| # Create summary | |
| summary = create_classification_summary(predicted_name, confidence, probabilities) | |
| print(f"Sample {i+1}:") | |
| print(f" True class: {true_name}") | |
| print(f" Predicted: {summary['emoji']} {predicted_name} ({summary['confidence_percent']})") | |
| # Add to composition if confidence is high | |
| if confidence > 0.7 and predicted_name != 'neutral': | |
| sound_manager.add_layer(predicted_name, confidence) | |
| print(f" ♪ Added to composition: {predicted_name}") | |
| else: | |
| print(f" - Not added (low confidence or neutral)") | |
| print() | |
| # Show composition summary | |
| composition_info = sound_manager.get_composition_info() | |
| print("Final Composition:") | |
| print("-" * 20) | |
| if composition_info: | |
| for i, layer in enumerate(composition_info, 1): | |
| print(f"{i}. {layer['class']} (confidence: {layer['confidence']:.2f})") | |
| else: | |
| print("No composition layers created") | |
| print() | |
| print("Demo completed! 🎵") | |
| print("To run the full Gradio interface, execute: python app.py") | |
| if __name__ == "__main__": | |
| try: | |
| run_demo() | |
| except KeyboardInterrupt: | |
| print("\nDemo interrupted by user") | |
| except Exception as e: | |
| print(f"Error running demo: {e}") | |
| import traceback | |
| traceback.print_exc() |