File size: 6,920 Bytes
73c6377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
---
title: HBV AI Assistant - Patient Selection System
emoji: πŸ₯
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
app_port: 7860
---

# HBV AI Assistant - Patient Selection System

A specialized AI-powered clinical decision support system for hepatologists and healthcare professionals managing Hepatitis B Virus (HBV) patients. The system evaluates patient eligibility for treatment according to SASLT 2021 guidelines and provides evidence-based recommendations.

## 🎯 Features

### Core Capabilities
- **Patient Eligibility Assessment**: Evaluates HBV patients for treatment eligibility based on SASLT 2021 guidelines
- **Evidence-Based Guidance**: Provides treatment recommendations according to authoritative medical guidelines
- **Comprehensive Input Validation**: Validates patient data including HBV DNA levels, ALT levels, fibrosis stage, and more
- **Treatment Options**: Recommends preferred regimens (ETV, TDF, TAF) based on patient profile
- **AI-Powered Chat**: Interactive AI bot for exploring guideline recommendations
- **JSON API**: RESTful POST endpoint for programmatic integration

### Technical Features
- **FastAPI Backend**: High-performance async API
- **Structured Input/Output**: Well-defined schemas for patient data and eligibility results
- **Real-time Processing**: Fast eligibility determination
- **Authentication**: Secure session-based authentication
- **Rate Limiting**: Built-in API rate limiting
- **CORS Support**: Cross-origin resource sharing enabled

## πŸš€ Deployment

### Live API
The API is deployed at: **http://127.0.0.1:7860**

### Quick Start

1. **Access the API**:
   - API Docs: http://127.0.0.1:7860/docs
   - Health Check: http://127.0.0.1:7860/health

2. **Submit Patient Data**:
   - Use the POST `/assess` endpoint to evaluate patient eligibility
   - Provide patient information according to the input schema
   - Receive eligibility determination and treatment recommendations

### Deploy Your Own Instance

See [DEPLOYMENT.md](DEPLOYMENT.md) for detailed deployment instructions.

## πŸ“š API Endpoints

### Health & Status
- `GET /` - API information
- `GET /health` - Health check

### HBV Patient Assessment
- `POST /assess` - Evaluate patient eligibility for HBV treatment using structured data
- `POST /assess/text` - Text-based patient assessment (provide clinical notes in free text format)
  - **Input**: Patient data (sex, age, HBV DNA, ALT, fibrosis stage, etc.)
  - **Output**: Eligibility status and treatment recommendations

### AI Chat 
- `POST /ask` - Ask guideline-related questions with optional patient context
  - **Input**: 
    - `query` (string): The question or message
    - `session_id` (string, optional): Session identifier for conversation history
    - `patient_context` (object, optional): Patient data for context-aware responses
    - `assessment_result` (object, optional): Previous assessment results for reference
  
- `POST /ask/stream` - Streaming chat responses with optional patient context
  - **Input**: Same as `/ask` endpoint
  - **Output**: Stream of text chunks for real-time display

## πŸ’» Local Development

### Prerequisites
- Python 3.11+
- OpenAI API key (optional, for AI chat feature)

### Setup

1. **Clone the repository**:
```bash
git clone https://github.com/your-repo/hbv-ai-assistant.git
cd hbv-ai-assistant
```

2. **Install dependencies**:
```bash
pip install -r requirements.txt
```

3. **Configure environment variables**:
```bash
cp .env.example .env
# Edit .env with your API keys
```

4. **Run the application**:
```bash
python app.py
```

5. **Access the application**:
   - API: http://localhost:7860
   - Docs: http://localhost:7860/docs
   - Test the `/assess` endpoint with patient data

## πŸ”§ Configuration

### Environment Variables

See `.env.example` for all configuration options:

- `OPENAI_API_KEY`: Your OpenAI API key (optional, for AI chat)
- `PORT`: Server port (default: 7860)
- `ALLOWED_ORIGINS`: CORS allowed origins

### Authentication

Default credentials (change in production):
- Username: `admin`
- Password: `admin123`

Update in `api/routers/auth.py` or via environment variables.

## πŸ“– Usage Examples

### Assessing Patient Eligibility

```python
import requests

# Login
response = requests.post(
    "http://127.0.0.1:7860/auth/login",
    json={"username": "admin", "password": "admin123"}
)
cookies = response.cookies

# Assess patient eligibility
patient_data = {
    "sex": "Male",
    "age": 45,
    "pregnancy_status": "Not pregnant",
    "hbsag_status": "Positive",
    "duration_hbsag_months": 12,
    "hbv_dna_level": 50000,
    "hbeag_status": "Positive",
    "alt_level": 60,
    "fibrosis_stage": "F2-F3",
    "necroinflammatory_activity": "A2",
    "extrahepatic_manifestations": False,
    "coinfections": [],
    "family_history_cirrhosis_hcc": False
}

response = requests.post(
    "http://127.0.0.1:7860/assess",
    json=patient_data,
    cookies=cookies
)
result = response.json()
print(f"Eligible: {result['eligible']}")
print(f"Recommendations: {result['recommendations']}")
```

## πŸ—οΈ Architecture

### Components

- **FastAPI Backend**: RESTful API with async support
- **Eligibility Engine**: Evaluates patient data against SASLT 2021 criteria
- **AI Chat (Optional)**: LangChain-powered conversational interface for guideline exploration
- **Validation Layer**: Ensures data integrity and completeness

### Assessment Logic

The system evaluates patients based on SASLT 2021 criteria:

1. **HBV DNA > 2,000 IU/mL** + **ALT > ULN** + moderate necroinflammation/fibrosis (β‰₯F2 or β‰₯A2)
2. **Cirrhosis** (F4) with any detectable HBV DNA
3. **HBV DNA > 20,000 IU/mL** + **ALT > 2Γ—ULN** regardless of fibrosis
4. **Age > 30** with HBeAg-positive chronic infection (normal ALT, high HBV DNA)
5. **Family history** of HCC/cirrhosis + HBV DNA > 2,000 + ALT > ULN
6. **Extrahepatic manifestations**

## πŸ“Š Response Format

The API returns:
- **Eligibility Status**: Eligible / Not Eligible
- **Guideline Recommendations**: Specific criteria met and treatment options
- **Treatment Choices**: Preferred regimens (ETV, TDF, TAF)

Example Response:
```json
{
  "eligible": true,
  "recommendations": "Patient meets SASLT 2021 criteria: HBV DNA > 2,000 IU/mL, ALT > ULN, and fibrosis stage F2-F3 (Grade A)",
  "treatment_options": ["ETV", "TDF", "TAF"],
  "guideline": "SASLT 2021"
}
```

## πŸ”’ Security

- Session-based authentication
- Rate limiting (100 requests/minute)
- CORS protection
- Input validation
- Secure cookie handling

## πŸ“ License

[Add your license here]

## 🀝 Contributing

Contributions are welcome! Please read the contributing guidelines first.

## πŸ“§ Support

For issues or questions:
- Check the [DEPLOYMENT.md](DEPLOYMENT.md) guide
- Review API docs at `/docs`
- Open an issue on GitHub

## πŸ™ Acknowledgments

Built with:
- FastAPI
- Pydantic
- Python 3.11+
- SASLT 2021 Guidelines