Skip to content

KeyValueSoftwareSystems/safehands-py-be

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SafeHands Senior AI Assistant Backend

A production-ready Python backend for the SafeHands Senior AI Assistant, providing real-time WebSocket communication, intelligent AI orchestration, and comprehensive assistance for seniors.

πŸš€ Features

  • Real-time WebSocket Communication: Always-on connection for instant assistance
  • Intelligent AI Workflow: LangGraph-based orchestration with conditional branching
  • Multi-modal Processing: Voice, screen, and command message handling
  • Session Management: Redis-based session tracking and management
  • Error Recovery: Smart error detection and recovery strategies
  • Learning System: Continuous learning from user interactions
  • Performance Optimization: Caching, monitoring, and analytics

πŸ›  Tech Stack

  • FastAPI: Modern, fast web framework for building APIs
  • WebSockets: Real-time bidirectional communication
  • LangGraph: Intelligent AI agent orchestration
  • OpenAI: LLM integration (GPT-3.5-turbo, GPT-4-Vision, Whisper, TTS)
  • Redis: Session management and caching
  • PostgreSQL: Persistent data storage

πŸ“‹ Prerequisites

  • Python 3.11+
  • Redis server
  • PostgreSQL database
  • OpenAI API key

πŸš€ Quick Start

1. Setup Environment

# Clone the repository
git clone <repository-url>
cd safehands-py-be

# Run the development setup script
python setup_dev.py

# Or manually set your API key
export OPENAI_API_KEY="sk-your-openai-api-key-here"
cp env.example .env

2. Start with Docker (Recommended)

# Start all services
docker-compose up -d

# Check if services are running
curl http://localhost:8000/health

3. Manual Setup

# Install dependencies
pip install -r requirements.txt

# Start Redis and PostgreSQL
# (Install and start these services on your system)

# Start the backend
./start.sh

πŸ§ͺ Testing

Web-Based Test Interface

# Start the backend
./start.sh

# Start the test interface (in another terminal)
./start_test_interface.sh

# Open your browser to: http://localhost:3000

Test Interface Features:

  • πŸ€– Real-time chat with AI assistant
  • πŸ“± Screen upload and analysis
  • 🎯 Quick test scenarios (food ordering, messaging, payments)
  • πŸ“Š Performance monitoring

πŸ— Architecture

Core Components

  • app/main.py: FastAPI application with WebSocket endpoints
  • app/agents/: AI agents for intent recognition, guidance generation, etc.
  • app/services/: Core services (AI, voice, screen analysis, RAG)
  • app/websocket/: WebSocket connection and message management
  • app/models/: Data models and schemas

Intelligent Workflow

The system uses LangGraph for intelligent AI orchestration:

Intent Recognition β†’ Context Analysis β†’ RAG Enhancement β†’ 
Guidance Generation β†’ Step Verification β†’ Error Detection β†’ 
Learning β†’ Response Generation

With conditional branching based on:

  • Intent complexity (simple vs complex)
  • User skill level (beginner/intermediate/advanced)
  • Error types and recovery strategies

πŸ“‘ API Endpoints

REST API

  • GET /health - Health check
  • GET /stats - System statistics
  • POST /sessions - Create session
  • GET /sessions/{session_id} - Get session info

WebSocket API

  • ws://localhost:8000/ws/{session_id} - Real-time communication

Message Types:

  • voice - Voice input processing
  • screen - Screen analysis
  • command - Text commands
  • heartbeat - Connection health

πŸ”§ Configuration

Key environment variables in .env:

# AI Services
OPENAI_API_KEY=sk-your-openai-api-key-here
OPENAI_MODEL=gpt-3.5-turbo
VISION_MODEL=gpt-4-vision-preview

# Database
DATABASE_URL=postgresql+asyncpg://user:password@localhost/safehands
REDIS_URL=redis://localhost:6379

# Server
HOST=0.0.0.0
PORT=8000

πŸš€ Production Deployment

Docker Compose

# Production deployment
docker-compose -f docker-compose.yml up -d

# Check logs
docker-compose logs -f backend

Manual Deployment

# Install dependencies
pip install -r requirements.txt

# Set production environment variables
export ENVIRONMENT=production
export DEBUG=false

# Start with production settings
./start.sh

πŸ“Š Monitoring

The system includes comprehensive monitoring:

  • Real-time Metrics: Request counts, response times, error rates
  • Session Analytics: User interactions, success rates, learning patterns
  • Performance Monitoring: System health, resource usage
  • Error Tracking: Detailed error logging and recovery

Access monitoring at:

  • GET /monitoring/dashboard - Real-time dashboard
  • GET /monitoring/analytics - Analytics reports
  • GET /monitoring/performance - Performance metrics

🀝 Development

Project Structure

safehands-py-be/
β”œβ”€β”€ app/                    # Main application code
β”‚   β”œβ”€β”€ agents/            # AI agents and workflows
β”‚   β”œβ”€β”€ services/          # Core services
β”‚   β”œβ”€β”€ websocket/         # WebSocket handling
β”‚   β”œβ”€β”€ models/            # Data models
β”‚   └── main.py           # FastAPI application
β”œβ”€β”€ web_interface/         # Test interface
β”œβ”€β”€ requirements.txt       # Dependencies
β”œβ”€β”€ docker-compose.yml     # Container orchestration
└── README.md             # This file

Key Features for Development

  1. Intelligent Routing: Conditional workflow based on user context
  2. Error Recovery: Multiple strategies for handling user errors
  3. Learning System: Continuous improvement from interactions
  4. Performance Optimization: Caching and monitoring
  5. Real-time Communication: WebSocket-based instant responses

πŸ“ License

This project is part of the SafeHands Senior AI Assistant system.

πŸ†˜ Support

For development support and questions, please refer to the API documentation in API.md.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors