Daease - Medical AI Assistant Architecture
Overview
Daease is an AI-powered medical consultation application that enables users to describe their symptoms and receive AI-generated medical guidance. The application integrates with medical knowledge bases, provides sourced information, and can generate detailed medical reports.
Project Structure
.
βββ app.py # Main Streamlit application entry point
βββ model.py # AI model integration and medical evidence retrieval
βββ report_generator.py # Report generation functionality
βββ utils.py # Utility functions for text processing
βββ chat_display.py # Chat UI components and rendering
βββ session_state.py # Session state management
βββ database.py # Database integration with Supabase
βββ styles.py # Custom CSS styling
βββ sendgrid_service.py # Email service for sending reports
βββ __init__.py # Package initialization
βββ requirements.txt # Project dependencies
βββ README_DB_SETUP.md # Database setup instructions
Core Components
1. Main Application (app.py)
The entry point for the Streamlit web application.
Key Features:
- Streamlit UI setup with dark theme
- Chat interface for medical consultations
- Toggle for enabling/disabling medical database search (RAG)
- Report generation functionality with priority management
- Conversation lock mechanism to ensure uninterrupted user experience
- Session state management
- "End Conversation" functionality
Flow:
- Initializes session state and applies styling
- Sets up the sidebar with features toggle and report generation button
- Displays chat history and handles new user inputs
- Prioritizes conversation processing over report generation
- Processes user queries through the orchestrator model
- Updates the UI with AI responses
- Only processes report generation when no conversation is active
2. AI Model Integration (model.py)
Handles all AI interactions, medical evidence retrieval, and response processing.
Key Features:
- Medical evidence retrieval from multiple sources (PubMed, WHO, CORE)
- Orchestration of conversation flow
- Source extraction and linking
- Response parsing and formatting
Key Functions:
orchestrator_chat(): Coordinates the conversation flowfetch_medical_evidence(): Retrieves evidence from medical databasesextract_and_link_sources(): Processes and links sources in responses- Multiple API integrations: PubMed, PMC, WHO, CORE
3. Report Generation (report_generator.py)
Handles the creation of medical reports based on consultation history.
Key Features:
- Extraction of medical information from conversation
- PDF report generation with ReportLab
- File download functionality
- Email integration for report sharing
- Error handling for patient information validation
- Conversation-aware processing to prevent disruptions
Key Functions:
extract_medical_json(): Extracts structured medical data from conversationsbuild_medical_report(): Creates a formatted PDF reportgenerate_and_download_report(): Manages the report generation workflow with conversation awarenessshow_email_form(): Handles the email input and sending interface
4. Utility Functions (utils.py)
Provides helper functions for text processing and formatting.
Key Functions:
remove_reasoning_and_sources(): Cleans up AI responses for displayclean_explanation(): Formats explanation sectionsformat_conversation_history(): Prepares chat history for report generationget_image_base64(): Handles image encoding for the UI
5. Chat Display (chat_display.py)
Manages the chat interface components and rendering.
Key Functions:
display_chat_history(): Renders the chat messages from databaseshow_typing_indicator(): Shows a typing animation while processingdisplay_legal_disclaimer(): Shows medical disclaimerget_avatars(): Handles avatar images for the chat
6. Session State Management (session_state.py)
Manages the application's state across interactions and integrates with database.
Key Functions:
initialize_session_state(): Sets up all required session variablesadd_message_to_history(): Adds messages to both local cache and databaseget_full_history(): Retrieves complete conversation history from databaseend_conversation(): Deletes conversation data from database
State Variables:
history: Local cache of chat message historyconsultation_id: Unique ID for each consultationuse_rag: Toggle for retrieval-augmented generationprocessing: Flag for response generation statusconversation_lock: Flag to prioritize conversations over report generationshow_report_form: Report generation form visibilitypatient_info: Patient details for reportspdf_data: Generated PDF report datashow_email_form: Email form visibility
7. Database Integration (database.py)
Handles persistence of conversation data in Supabase.
Key Features:
- Supabase integration for conversation history storage
- Error handling and fallback mechanisms
- Conversation and message management
Key Functions:
create_conversation(): Creates a new conversation recordsave_message(): Saves individual messages to the databaseget_conversation_history(): Retrieves complete history for a consultationdelete_conversation(): Removes conversation data when no longer needed
8. Styling (styles.py)
Provides custom CSS styling for the application.
Key Features:
- Dark theme implementation
- Custom chat message styling (user vs. assistant)
- Responsive layout design
- Buttons and form elements styling
Key Function:
apply_styling(): Applies the custom CSS to the Streamlit app
9. Email Service (sendgrid_service.py)
Handles email functionality for sending reports.
Key Features:
- SendGrid API integration
- Email validation
- PDF attachment handling
- Error handling and logging
Key Methods:
send_report(): Sends the medical report via emailvalidate_email(): Performs basic email validation
Application Flow
Initialization:
- Load environment variables
- Initialize session state and database connection
- Apply custom styling
User Interaction:
- User inputs symptoms or medical queries
- Conversation lock is activated to prioritize conversation processing
- The application displays a typing indicator while processing
Query Processing:
orchestrator_chat()handles the conversation flow- If RAG is enabled,
fetch_medical_evidence()retrieves relevant medical information - The model generates a response with reasoning and evidence
Response Display:
- Main response is shown in the chat
- Detailed reasoning is available in an expandable section
- Sources are linked to their original content
- All data is persisted to the database
- Conversation lock is released after response is processed
Report Generation:
- User clicks "Generate Report" button
- System checks that no conversation is being processed
- Application retrieves full conversation history from database
- Input validation ensures all form fields have valid values
- PDF report is generated with patient information and consultation details
- User can download the report or send it via email
Email Functionality:
- User enters recipient email
- System validates email format
- Report is sent using the SendGrid service
End Conversation:
- User can end the conversation which deletes the data from the database
- A new conversation ID is generated for future interactions
Dependencies
- Core: openai, requests, beautifulsoup4, markdown, python-dotenv
- Report Generation: jinja2, python-docx, pydantic, reportlab
- Web App: streamlit
- Database: supabase, postgrest
Security and Compliance
- Medical disclaimer is prominently displayed
- Email functionality includes appropriate medical disclaimers
- Database storage is secured with row-level security
- Conversations can be deleted to ensure data privacy