version 6 | DB
Browse files- src/architecture.md +52 -14
src/architecture.md
CHANGED
@@ -14,10 +14,12 @@ Daease is an AI-powered medical consultation application that enables users to d
|
|
14 |
βββ utils.py # Utility functions for text processing
|
15 |
βββ chat_display.py # Chat UI components and rendering
|
16 |
βββ session_state.py # Session state management
|
|
|
17 |
βββ styles.py # Custom CSS styling
|
18 |
βββ sendgrid_service.py # Email service for sending reports
|
19 |
βββ __init__.py # Package initialization
|
20 |
-
|
|
|
21 |
```
|
22 |
|
23 |
## Core Components
|
@@ -30,15 +32,19 @@ The entry point for the Streamlit web application.
|
|
30 |
- Streamlit UI setup with dark theme
|
31 |
- Chat interface for medical consultations
|
32 |
- Toggle for enabling/disabling medical database search (RAG)
|
33 |
-
- Report generation functionality
|
|
|
34 |
- Session state management
|
|
|
35 |
|
36 |
**Flow:**
|
37 |
1. Initializes session state and applies styling
|
38 |
2. Sets up the sidebar with features toggle and report generation button
|
39 |
3. Displays chat history and handles new user inputs
|
40 |
-
4.
|
41 |
-
5.
|
|
|
|
|
42 |
|
43 |
### 2. AI Model Integration (`model.py`)
|
44 |
|
@@ -65,11 +71,13 @@ Handles the creation of medical reports based on consultation history.
|
|
65 |
- PDF report generation with ReportLab
|
66 |
- File download functionality
|
67 |
- Email integration for report sharing
|
|
|
|
|
68 |
|
69 |
**Key Functions:**
|
70 |
- `extract_medical_json()`: Extracts structured medical data from conversations
|
71 |
- `build_medical_report()`: Creates a formatted PDF report
|
72 |
-
- `generate_and_download_report()`: Manages the report generation workflow
|
73 |
- `show_email_form()`: Handles the email input and sending interface
|
74 |
|
75 |
### 4. Utility Functions (`utils.py`)
|
@@ -87,29 +95,48 @@ Provides helper functions for text processing and formatting.
|
|
87 |
Manages the chat interface components and rendering.
|
88 |
|
89 |
**Key Functions:**
|
90 |
-
- `display_chat_history()`: Renders the chat messages
|
91 |
- `show_typing_indicator()`: Shows a typing animation while processing
|
92 |
- `display_legal_disclaimer()`: Shows medical disclaimer
|
93 |
- `get_avatars()`: Handles avatar images for the chat
|
94 |
|
95 |
### 6. Session State Management (`session_state.py`)
|
96 |
|
97 |
-
Manages the application's state across interactions.
|
98 |
|
99 |
-
**Key
|
100 |
- `initialize_session_state()`: Sets up all required session variables
|
|
|
|
|
|
|
101 |
|
102 |
**State Variables:**
|
103 |
-
- `history`:
|
104 |
- `consultation_id`: Unique ID for each consultation
|
105 |
- `use_rag`: Toggle for retrieval-augmented generation
|
106 |
- `processing`: Flag for response generation status
|
|
|
107 |
- `show_report_form`: Report generation form visibility
|
108 |
- `patient_info`: Patient details for reports
|
109 |
- `pdf_data`: Generated PDF report data
|
110 |
- `show_email_form`: Email form visibility
|
111 |
|
112 |
-
### 7.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
Provides custom CSS styling for the application.
|
115 |
|
@@ -122,7 +149,7 @@ Provides custom CSS styling for the application.
|
|
122 |
**Key Function:**
|
123 |
- `apply_styling()`: Applies the custom CSS to the Streamlit app
|
124 |
|
125 |
-
###
|
126 |
|
127 |
Handles email functionality for sending reports.
|
128 |
|
@@ -140,11 +167,12 @@ Handles email functionality for sending reports.
|
|
140 |
|
141 |
1. **Initialization:**
|
142 |
- Load environment variables
|
143 |
-
- Initialize session state
|
144 |
- Apply custom styling
|
145 |
|
146 |
2. **User Interaction:**
|
147 |
- User inputs symptoms or medical queries
|
|
|
148 |
- The application displays a typing indicator while processing
|
149 |
|
150 |
3. **Query Processing:**
|
@@ -156,10 +184,14 @@ Handles email functionality for sending reports.
|
|
156 |
- Main response is shown in the chat
|
157 |
- Detailed reasoning is available in an expandable section
|
158 |
- Sources are linked to their original content
|
|
|
|
|
159 |
|
160 |
5. **Report Generation:**
|
161 |
- User clicks "Generate Report" button
|
162 |
-
-
|
|
|
|
|
163 |
- PDF report is generated with patient information and consultation details
|
164 |
- User can download the report or send it via email
|
165 |
|
@@ -168,14 +200,20 @@ Handles email functionality for sending reports.
|
|
168 |
- System validates email format
|
169 |
- Report is sent using the SendGrid service
|
170 |
|
|
|
|
|
|
|
|
|
171 |
## Dependencies
|
172 |
|
173 |
- **Core:** openai, requests, beautifulsoup4, markdown, python-dotenv
|
174 |
- **Report Generation:** jinja2, python-docx, pydantic, reportlab
|
175 |
- **Web App:** streamlit
|
|
|
176 |
|
177 |
## Security and Compliance
|
178 |
|
179 |
- Medical disclaimer is prominently displayed
|
180 |
- Email functionality includes appropriate medical disclaimers
|
181 |
-
-
|
|
|
|
14 |
βββ utils.py # Utility functions for text processing
|
15 |
βββ chat_display.py # Chat UI components and rendering
|
16 |
βββ session_state.py # Session state management
|
17 |
+
βββ database.py # Database integration with Supabase
|
18 |
βββ styles.py # Custom CSS styling
|
19 |
βββ sendgrid_service.py # Email service for sending reports
|
20 |
βββ __init__.py # Package initialization
|
21 |
+
βββ requirements.txt # Project dependencies
|
22 |
+
βββ README_DB_SETUP.md # Database setup instructions
|
23 |
```
|
24 |
|
25 |
## Core Components
|
|
|
32 |
- Streamlit UI setup with dark theme
|
33 |
- Chat interface for medical consultations
|
34 |
- Toggle for enabling/disabling medical database search (RAG)
|
35 |
+
- Report generation functionality with priority management
|
36 |
+
- Conversation lock mechanism to ensure uninterrupted user experience
|
37 |
- Session state management
|
38 |
+
- "End Conversation" functionality
|
39 |
|
40 |
**Flow:**
|
41 |
1. Initializes session state and applies styling
|
42 |
2. Sets up the sidebar with features toggle and report generation button
|
43 |
3. Displays chat history and handles new user inputs
|
44 |
+
4. Prioritizes conversation processing over report generation
|
45 |
+
5. Processes user queries through the orchestrator model
|
46 |
+
6. Updates the UI with AI responses
|
47 |
+
7. Only processes report generation when no conversation is active
|
48 |
|
49 |
### 2. AI Model Integration (`model.py`)
|
50 |
|
|
|
71 |
- PDF report generation with ReportLab
|
72 |
- File download functionality
|
73 |
- Email integration for report sharing
|
74 |
+
- Error handling for patient information validation
|
75 |
+
- Conversation-aware processing to prevent disruptions
|
76 |
|
77 |
**Key Functions:**
|
78 |
- `extract_medical_json()`: Extracts structured medical data from conversations
|
79 |
- `build_medical_report()`: Creates a formatted PDF report
|
80 |
+
- `generate_and_download_report()`: Manages the report generation workflow with conversation awareness
|
81 |
- `show_email_form()`: Handles the email input and sending interface
|
82 |
|
83 |
### 4. Utility Functions (`utils.py`)
|
|
|
95 |
Manages the chat interface components and rendering.
|
96 |
|
97 |
**Key Functions:**
|
98 |
+
- `display_chat_history()`: Renders the chat messages from database
|
99 |
- `show_typing_indicator()`: Shows a typing animation while processing
|
100 |
- `display_legal_disclaimer()`: Shows medical disclaimer
|
101 |
- `get_avatars()`: Handles avatar images for the chat
|
102 |
|
103 |
### 6. Session State Management (`session_state.py`)
|
104 |
|
105 |
+
Manages the application's state across interactions and integrates with database.
|
106 |
|
107 |
+
**Key Functions:**
|
108 |
- `initialize_session_state()`: Sets up all required session variables
|
109 |
+
- `add_message_to_history()`: Adds messages to both local cache and database
|
110 |
+
- `get_full_history()`: Retrieves complete conversation history from database
|
111 |
+
- `end_conversation()`: Deletes conversation data from database
|
112 |
|
113 |
**State Variables:**
|
114 |
+
- `history`: Local cache of chat message history
|
115 |
- `consultation_id`: Unique ID for each consultation
|
116 |
- `use_rag`: Toggle for retrieval-augmented generation
|
117 |
- `processing`: Flag for response generation status
|
118 |
+
- `conversation_lock`: Flag to prioritize conversations over report generation
|
119 |
- `show_report_form`: Report generation form visibility
|
120 |
- `patient_info`: Patient details for reports
|
121 |
- `pdf_data`: Generated PDF report data
|
122 |
- `show_email_form`: Email form visibility
|
123 |
|
124 |
+
### 7. Database Integration (`database.py`)
|
125 |
+
|
126 |
+
Handles persistence of conversation data in Supabase.
|
127 |
+
|
128 |
+
**Key Features:**
|
129 |
+
- Supabase integration for conversation history storage
|
130 |
+
- Error handling and fallback mechanisms
|
131 |
+
- Conversation and message management
|
132 |
+
|
133 |
+
**Key Functions:**
|
134 |
+
- `create_conversation()`: Creates a new conversation record
|
135 |
+
- `save_message()`: Saves individual messages to the database
|
136 |
+
- `get_conversation_history()`: Retrieves complete history for a consultation
|
137 |
+
- `delete_conversation()`: Removes conversation data when no longer needed
|
138 |
+
|
139 |
+
### 8. Styling (`styles.py`)
|
140 |
|
141 |
Provides custom CSS styling for the application.
|
142 |
|
|
|
149 |
**Key Function:**
|
150 |
- `apply_styling()`: Applies the custom CSS to the Streamlit app
|
151 |
|
152 |
+
### 9. Email Service (`sendgrid_service.py`)
|
153 |
|
154 |
Handles email functionality for sending reports.
|
155 |
|
|
|
167 |
|
168 |
1. **Initialization:**
|
169 |
- Load environment variables
|
170 |
+
- Initialize session state and database connection
|
171 |
- Apply custom styling
|
172 |
|
173 |
2. **User Interaction:**
|
174 |
- User inputs symptoms or medical queries
|
175 |
+
- Conversation lock is activated to prioritize conversation processing
|
176 |
- The application displays a typing indicator while processing
|
177 |
|
178 |
3. **Query Processing:**
|
|
|
184 |
- Main response is shown in the chat
|
185 |
- Detailed reasoning is available in an expandable section
|
186 |
- Sources are linked to their original content
|
187 |
+
- All data is persisted to the database
|
188 |
+
- Conversation lock is released after response is processed
|
189 |
|
190 |
5. **Report Generation:**
|
191 |
- User clicks "Generate Report" button
|
192 |
+
- System checks that no conversation is being processed
|
193 |
+
- Application retrieves full conversation history from database
|
194 |
+
- Input validation ensures all form fields have valid values
|
195 |
- PDF report is generated with patient information and consultation details
|
196 |
- User can download the report or send it via email
|
197 |
|
|
|
200 |
- System validates email format
|
201 |
- Report is sent using the SendGrid service
|
202 |
|
203 |
+
7. **End Conversation:**
|
204 |
+
- User can end the conversation which deletes the data from the database
|
205 |
+
- A new conversation ID is generated for future interactions
|
206 |
+
|
207 |
## Dependencies
|
208 |
|
209 |
- **Core:** openai, requests, beautifulsoup4, markdown, python-dotenv
|
210 |
- **Report Generation:** jinja2, python-docx, pydantic, reportlab
|
211 |
- **Web App:** streamlit
|
212 |
+
- **Database:** supabase, postgrest
|
213 |
|
214 |
## Security and Compliance
|
215 |
|
216 |
- Medical disclaimer is prominently displayed
|
217 |
- Email functionality includes appropriate medical disclaimers
|
218 |
+
- Database storage is secured with row-level security
|
219 |
+
- Conversations can be deleted to ensure data privacy
|