|
|
import sys |
|
|
import os |
|
|
import logging |
|
|
from typing import Optional |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
current_dir = Path(__file__).parent.absolute() |
|
|
sys.path.insert(0, str(current_dir)) |
|
|
|
|
|
|
|
|
try: |
|
|
from main import main, validate_environment |
|
|
from audio_utils import validate_environment as validate_audio_env |
|
|
except ImportError as e: |
|
|
print(f"Error importing functional modules: {e}") |
|
|
print("Please ensure all required dependencies are installed.") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
def setup_logging(level: str = "INFO") -> logging.Logger: |
|
|
"""Set up application logging.""" |
|
|
log_level = getattr(logging, level.upper(), logging.INFO) |
|
|
|
|
|
logging.basicConfig( |
|
|
level=log_level, |
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
|
|
handlers=[ |
|
|
logging.StreamHandler(sys.stdout), |
|
|
logging.FileHandler('wisal_app.log', mode='a') |
|
|
] |
|
|
) |
|
|
|
|
|
return logging.getLogger('WisalApp') |
|
|
|
|
|
def check_dependencies() -> bool: |
|
|
"""Check if all required dependencies are available.""" |
|
|
required_packages = [ |
|
|
'gradio', |
|
|
'numpy', |
|
|
'soundfile', |
|
|
'webrtcvad', |
|
|
'fastrtc', |
|
|
'google.genai', |
|
|
'openai', |
|
|
'weaviate', |
|
|
'langdetect', |
|
|
'requests', |
|
|
'langchain', |
|
|
'pypdf', |
|
|
'docx', |
|
|
'dotenv' |
|
|
] |
|
|
|
|
|
missing_packages = [] |
|
|
|
|
|
for package in required_packages: |
|
|
try: |
|
|
__import__(package.replace('-', '_').replace('.', '.')) |
|
|
except ImportError: |
|
|
missing_packages.append(package) |
|
|
|
|
|
if missing_packages: |
|
|
print(f"Missing required packages: {', '.join(missing_packages)}") |
|
|
print("Please install them using: pip install " + " ".join(missing_packages)) |
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
def check_environment_files() -> bool: |
|
|
"""Check if required environment files exist.""" |
|
|
required_files = ['.env'] |
|
|
missing_files = [] |
|
|
|
|
|
for file_path in required_files: |
|
|
if not os.path.exists(file_path): |
|
|
missing_files.append(file_path) |
|
|
|
|
|
if missing_files: |
|
|
print(f"Missing required files: {', '.join(missing_files)}") |
|
|
print("Please create a .env file with the required API keys.") |
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
def print_startup_banner(): |
|
|
"""Print application startup banner.""" |
|
|
banner = """ |
|
|
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
|
|
β β |
|
|
β π€ Wisal: Autism AI Assistant β |
|
|
β β |
|
|
β Functional Programming Version β |
|
|
β Developed by Compumacy AI β |
|
|
β β |
|
|
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
|
|
""" |
|
|
print(banner) |
|
|
|
|
|
def print_feature_overview(): |
|
|
"""Print overview of available features.""" |
|
|
features = """ |
|
|
π§ Available Features: |
|
|
βββ π¬ Text-based chat with autism expertise |
|
|
βββ π€ Voice input with transcription |
|
|
βββ π Text-to-speech responses |
|
|
βββ π Document upload and Q&A |
|
|
βββ π Web search integration |
|
|
βββ π§ RAG (Retrieval Augmented Generation) |
|
|
βββ π― Functional programming architecture |
|
|
βββ π Real-time voice chat (WebRTC) |
|
|
""" |
|
|
print(features) |
|
|
|
|
|
def run_pre_flight_checks() -> bool: |
|
|
"""Run all pre-flight checks before starting the application.""" |
|
|
app_logger = setup_logging() |
|
|
|
|
|
print_startup_banner() |
|
|
|
|
|
app_logger.info("Starting pre-flight checks...") |
|
|
|
|
|
|
|
|
app_logger.info("Checking dependencies...") |
|
|
if not check_dependencies(): |
|
|
app_logger.error("Dependency check failed") |
|
|
return False |
|
|
app_logger.info("β
Dependencies check passed") |
|
|
|
|
|
|
|
|
app_logger.info("Checking environment files...") |
|
|
if not check_environment_files(): |
|
|
app_logger.error("Environment files check failed") |
|
|
return False |
|
|
app_logger.info("β
Environment files check passed") |
|
|
|
|
|
|
|
|
app_logger.info("Validating environment variables...") |
|
|
if not validate_environment(): |
|
|
app_logger.warning("β οΈ Some environment variables are missing - features may be limited") |
|
|
else: |
|
|
app_logger.info("β
Environment variables validated") |
|
|
|
|
|
|
|
|
app_logger.info("Checking audio environment...") |
|
|
if not validate_audio_env(): |
|
|
app_logger.warning("β οΈ Audio environment not fully configured - voice features may be limited") |
|
|
else: |
|
|
app_logger.info("β
Audio environment validated") |
|
|
|
|
|
app_logger.info("Pre-flight checks completed") |
|
|
print_feature_overview() |
|
|
|
|
|
return True |
|
|
|
|
|
def handle_graceful_shutdown(): |
|
|
"""Handle graceful application shutdown.""" |
|
|
print("\nπ Shutting down Wisal gracefully...") |
|
|
print("π Thank you for using Wisal!") |
|
|
|
|
|
def run_application(): |
|
|
"""Run the main application with error handling.""" |
|
|
try: |
|
|
print("π Starting Wisal application...") |
|
|
main() |
|
|
except KeyboardInterrupt: |
|
|
handle_graceful_shutdown() |
|
|
except Exception as e: |
|
|
print(f"β Application error: {e}") |
|
|
logging.error(f"Application error: {e}", exc_info=True) |
|
|
sys.exit(1) |
|
|
|
|
|
def main_runner(): |
|
|
"""Main runner function.""" |
|
|
try: |
|
|
|
|
|
if not run_pre_flight_checks(): |
|
|
print("β Pre-flight checks failed. Cannot start application.") |
|
|
sys.exit(1) |
|
|
|
|
|
print("β
All checks passed. Starting application...") |
|
|
|
|
|
|
|
|
run_application() |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Fatal error during startup: {e}") |
|
|
logging.error(f"Fatal startup error: {e}", exc_info=True) |
|
|
sys.exit(1) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main_runner() |