Skip to content

ArabianCowboy/SFDA_copilot

Repository files navigation

SFDA Copilot

An AI-powered regulatory guidance system for pharmaceutical regulations in Saudi Arabia, built with Flask, Supabase, and modern web technologies.

🌟 Features

  • AI-Powered Chat Interface: Get intelligent answers about SFDA regulations
  • Comprehensive FAQ System: Browse categorized regulatory guidelines
  • User Authentication: Secure login/signup with Supabase
  • Profile Management: User profiles with theme preferences
  • Dark/Light Theme: Accessible theme toggle with system preference detection
  • Responsive Design: Works seamlessly across desktop and mobile devices
  • Real-time Updates: Live chat with typing indicators and suggested questions

🎨 Theme Toggle System

The SFDA Copilot application uses an HTML-first approach for theme toggles, ensuring better accessibility and maintainability.

Features

  • βœ… Light/dark theme support
  • βœ… System preference detection
  • βœ… User preference persistence
  • βœ… Accessible toggle buttons with ARIA labels
  • βœ… Bootstrap 5 integration with data-bs-theme
  • βœ… Keyboard navigation support
  • βœ… Screen reader compatibility

Implementation Details

HTML Structure

Theme toggle buttons are defined directly in the HTML with proper accessibility attributes:

<!-- Landing page theme toggle -->
<button
    id="landing-theme-toggle"
    class="theme-toggle-btn btn btn-outline-secondary btn-sm ms-2"
    aria-label="Toggle theme between light and dark"
    title="Toggle theme between light and dark"
>
    <i class="bi bi-moon-fill"></i>
</button>

<!-- Sidebar theme toggle -->
<button
    id="sidebar-theme-toggle"
    class="theme-toggle-btn btn btn-outline-secondary btn-sm ms-2"
    aria-label="Toggle theme between light and dark"
    title="Toggle theme between light and dark"
>
    <i class="bi bi-moon-fill"></i>
</button>

<!-- Offcanvas theme toggle -->
<button
    id="offcanvas-theme-toggle"
    class="theme-toggle-btn btn btn-outline-secondary btn-sm ms-2"
    aria-label="Toggle theme between light and dark"
    title="Toggle theme between light and dark"
>
    <i class="bi bi-moon-fill"></i>
</button>

JavaScript Implementation

The theme system uses event delegation and DOMCache for optimal performance:

// Event delegation for better performance
document.addEventListener('click', (e) => {
    if (e.target.closest('.theme-toggle-btn')) {
        e.preventDefault();
        toggleTheme();
    }
});

// Keyboard navigation support
document.addEventListener('keydown', (e) => {
    if (e.target.closest('.theme-toggle-btn') && (e.key === 'Enter' || e.key === ' ')) {
        e.preventDefault();
        toggleTheme();
    }
});

Theme Persistence

  • Uses Bootstrap 5's native data-bs-theme attribute
  • Stores preference in localStorage
  • Respects system color scheme preference
  • Synchronizes across all toggle buttons

Accessibility Features

  • ARIA Labels: Clear descriptions for screen readers
  • Keyboard Navigation: Full keyboard accessibility with Enter and Space keys
  • Focus Management: Proper focus handling during theme changes
  • Screen Reader Announcements: Theme change notifications
  • High Contrast: Maintains readability in both themes

πŸš€ Getting Started

Prerequisites

  • Python 3.8+
  • Node.js 16+
  • Supabase account

Installation

  1. Clone the repository

    git clone https://github.com/your-username/sfda-copilot.git
    cd sfda-copilot
  2. Set up the backend

    # Create virtual environment
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
    # Install dependencies
    pip install -r requirements.txt
    
    # Set up environment variables
    cp .env.example .env
    # Edit .env with your Supabase credentials
  3. Set up the frontend

    # Install Node.js dependencies
    npm install
    
    # Build frontend assets (if needed)
    npm run build
  4. Configure Supabase

    • Create a new Supabase project
    • Set up the database schema (see web/migrations/)
    • Get your project URL and anon key
    • Update .env with your credentials
  5. Run the application

    # Start Flask development server
    python web/app.py
    
    # Open your browser to http://localhost:5000

πŸ“ Project Structure

sfda-copilot/
β”œβ”€β”€ README.md                 # This file
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ package.json             # Node.js dependencies
β”œβ”€β”€ .env.example            # Environment variables template
β”œβ”€β”€ faq.yaml                # FAQ data configuration
β”œβ”€β”€ static/                 # Static frontend assets
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── style.css       # Custom CSS styles
β”‚   β”œβ”€β”€ js/
β”‚   β”‚   └── app.js          # Main JavaScript application
β”‚   └── images/             # Image assets
β”œβ”€β”€ web/                    # Flask backend
β”‚   β”œβ”€β”€ app.py             # Flask application entry point
β”‚   β”œβ”€β”€ api/               # API endpoints
β”‚   β”œβ”€β”€ services/          # Business logic services
β”‚   β”œβ”€β”€ utils/             # Utility functions
β”‚   β”œβ”€β”€ templates/         # HTML templates
β”‚   β”‚   └── index.html     # Main application template
β”‚   └── tests/             # Test files
β”œβ”€β”€ data/                  # Regulatory guideline data
β”‚   β”œβ”€β”€ regulatory/
β”‚   β”œβ”€β”€ pharmacovigilance/
β”‚   β”œβ”€β”€ Veterinary_Medicines/
β”‚   └── Biological_Products_and_Quality_Control/
└── memory-bank/           # Project documentation and notes

Architecture Boundaries

  • SearchEngine composes the index, query processor, semantic search, TF-IDF lexical search, and result combiner. Those search responsibilities remain separate by design.
  • web.utils.embedding_helpers is the only embedding-provider factory. Provider initialization fails clearly rather than switching vector spaces behind an existing FAISS index.
  • Frontend Services owns only Supabase and HTTP operations. Event handlers own user-facing recovery, state.js owns runtime state, and auth-view.js owns authenticated/unauthenticated view transitions.

πŸ§ͺ Testing

Install Development Test Tools

python -m pip install -r requirements-dev.txt
python -m playwright install chromium

Running Tests

# Fast backend suite
python -m pytest -m "not browser and not integration"

# Browser suite (pytest starts an ephemeral Flask test server)
python -m pytest -m browser --browser chromium

# Integration tests requiring generated artifacts or external services
python -m pytest -m integration

# Backend coverage
python -m pytest -m "not browser and not integration" --cov=web

Playwright starts an ephemeral Flask test server from web/tests/conftest.py. GitHub Actions installs Chromium and runs the browser suite as a separate merge gate.

Test Coverage

  • Theme Toggle Tests: Verify theme persistence, accessibility, and functionality
  • Profile Integration Tests: Test theme preference synchronization with user profiles
  • Authentication Tests: Test user authentication and session management
  • API Tests: Test backend API endpoints

πŸ”§ Configuration

Environment Variables

# Supabase Configuration
SUPABASE_URL=your_supabase_project_url
SUPABASE_ANON_KEY=your_supabase_anon_key

# Flask Configuration
FLASK_ENV=development
FLASK_DEBUG=True
SECRET_KEY=your_secret_key

# Database Configuration (if not using Supabase)
DATABASE_URL=your_database_url

FAQ Configuration

Edit faq.yaml to customize the FAQ categories and questions:

regulatory:
  title: "Regulatory Guidelines"
  icon: "bi-shield-check"
  questions:
    - short: "Drug Registration"
      text: "What are the requirements for drug registration in Saudi Arabia?"

πŸ“š Documentation

Component Documentation

Theme Toggle Refactoring

API Documentation

API endpoints are documented in the code with OpenAPI/Swagger annotations.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass (python -m pytest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Development Guidelines

  • Follow the existing code style and patterns
  • Write comprehensive tests for new features
  • Update documentation for significant changes
  • Ensure accessibility compliance
  • Test across different browsers and devices

πŸ“ Changelog

Recent Updates

  • Theme Toggle Refactoring: Implemented HTML-first approach with improved accessibility
  • Profile Integration: Enhanced theme preference synchronization with user profiles
  • Testing Suite: Added comprehensive tests for theme toggle functionality
  • Documentation: Updated all documentation with new implementation details

Version History

  • v2.0.0: Complete theme toggle refactoring with accessibility improvements
  • v1.0.0: Initial release with basic functionality

πŸ› Troubleshooting

Common Issues

Theme Toggle Not Working

  • Check that JavaScript is enabled in your browser
  • Clear browser cache and localStorage
  • Verify that all theme toggle buttons have the correct class="theme-toggle-btn" attribute
  • Check browser console for JavaScript errors

Authentication Issues

  • Verify Supabase credentials in .env file
  • Ensure Supabase project is properly configured
  • Check network connectivity to Supabase services

Mobile Responsiveness

  • Test on actual devices, not just browser dev tools
  • Check viewport meta tag settings
  • Verify CSS media queries are working correctly

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘₯ Team

  • Mohamed Fouda - Lead Developer & Designer
  • SFDA Copilot Team - Development & Testing

πŸ™ Acknowledgments

  • Saudi Food and Drug Authority (SFDA) for regulatory guidelines
  • Bootstrap team for the excellent UI framework
  • Supabase team for the backend-as-a-service platform
  • OpenAI for AI capabilities

πŸ“ž Support

For support, please open an issue in the GitHub repository or contact the development team.


Note: This is a documentation file for the SFDA Copilot project. For the most up-to-date information, please refer to the project's GitHub repository and the latest code commits.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages