Spaces:
Sleeping
Sleeping
YanBoChen
commited on
Commit
·
9e8cbc8
1
Parent(s):
5888ce4
feat and fix: update medical advice extraction logic to prioritize raw response
Browse files- README.md +264 -0
- app.py +517 -0
- src/generation.py +3 -3
README.md
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OnCall.ai - Medical Emergency Assistant
|
| 2 |
+
|
| 3 |
+
A RAG-based medical assistant system that provides evidence-based clinical guidance for emergency medical situations using real medical guidelines and advanced language models.
|
| 4 |
+
|
| 5 |
+
## 🎯 Project Overview
|
| 6 |
+
|
| 7 |
+
OnCall.ai helps healthcare professionals by:
|
| 8 |
+
- Processing medical queries through multi-level validation
|
| 9 |
+
- Retrieving relevant medical guidelines from curated datasets
|
| 10 |
+
- Generating evidence-based clinical advice using specialized medical LLMs
|
| 11 |
+
- Providing transparent, traceable medical guidance
|
| 12 |
+
|
| 13 |
+
## ✅ Current Implementation Status
|
| 14 |
+
|
| 15 |
+
### **🎉 COMPLETED MODULES (2025-07-31)**
|
| 16 |
+
|
| 17 |
+
#### **1. Multi-Level Query Processing System**
|
| 18 |
+
- ✅ **UserPromptProcessor** (`src/user_prompt.py`)
|
| 19 |
+
- Level 1: Predefined medical condition mapping (instant response)
|
| 20 |
+
- Level 2: LLM-based condition extraction (Llama3-Med42-70B)
|
| 21 |
+
- Level 3: Semantic search fallback
|
| 22 |
+
- Level 4: Medical query validation (100% non-medical rejection)
|
| 23 |
+
- Level 5: Generic medical search for rare conditions
|
| 24 |
+
|
| 25 |
+
#### **2. Dual-Index Retrieval System**
|
| 26 |
+
- ✅ **BasicRetrievalSystem** (`src/retrieval.py`)
|
| 27 |
+
- Emergency medical guidelines index (emergency.ann)
|
| 28 |
+
- Treatment protocols index (treatment.ann)
|
| 29 |
+
- Vector-based similarity search using PubMedBERT embeddings
|
| 30 |
+
- Intelligent deduplication and result ranking
|
| 31 |
+
|
| 32 |
+
#### **3. Medical Knowledge Base**
|
| 33 |
+
- ✅ **MedicalConditions** (`src/medical_conditions.py`)
|
| 34 |
+
- Predefined condition-keyword mappings
|
| 35 |
+
- Medical terminology validation
|
| 36 |
+
- Extensible condition database
|
| 37 |
+
|
| 38 |
+
#### **4. LLM Integration**
|
| 39 |
+
- ✅ **Med42-70B Client** (`src/llm_clients.py`)
|
| 40 |
+
- Specialized medical language model integration
|
| 41 |
+
- Dual-layer rejection detection for non-medical queries
|
| 42 |
+
- Robust error handling and timeout management
|
| 43 |
+
|
| 44 |
+
#### **5. Medical Advice Generation**
|
| 45 |
+
- ✅ **MedicalAdviceGenerator** (`src/generation.py`)
|
| 46 |
+
- RAG-based prompt construction
|
| 47 |
+
- Intention-aware chunk selection (treatment/diagnosis)
|
| 48 |
+
- Confidence scoring and response formatting
|
| 49 |
+
- Integration with Med42-70B for clinical advice generation
|
| 50 |
+
|
| 51 |
+
#### **6. Data Processing Pipeline**
|
| 52 |
+
- ✅ **Processed Medical Guidelines** (`src/data_processing.py`)
|
| 53 |
+
- ~4000 medical guidelines from EPFL-LLM dataset
|
| 54 |
+
- Emergency subset: ~2000-2500 records
|
| 55 |
+
- Treatment subset: ~2000-2500 records
|
| 56 |
+
- PubMedBERT embeddings (768 dimensions)
|
| 57 |
+
- ANNOY vector indices for fast retrieval
|
| 58 |
+
|
| 59 |
+
## 📊 **System Performance (Validated)**
|
| 60 |
+
|
| 61 |
+
### **Test Results Summary**
|
| 62 |
+
```
|
| 63 |
+
🎯 Multi-Level Fallback Validation: 69.2% success rate
|
| 64 |
+
- Level 1 (Predefined): 100% success (instant response)
|
| 65 |
+
- Level 4a (Non-medical rejection): 100% success
|
| 66 |
+
- Level 4b→5 (Rare medical): 100% success
|
| 67 |
+
|
| 68 |
+
📈 End-to-End Pipeline: 100% technical completion
|
| 69 |
+
- Condition extraction: 2.6s average
|
| 70 |
+
- Medical guideline retrieval: 0.3s average
|
| 71 |
+
- Total pipeline: 15.5s average (including generation)
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### **Quality Metrics**
|
| 75 |
+
```
|
| 76 |
+
🔍 Retrieval Performance:
|
| 77 |
+
- Guidelines retrieved: 8-9 per query
|
| 78 |
+
- Relevance scores: 0.245-0.326 (good for medical domain)
|
| 79 |
+
- Emergency/Treatment balance: Correctly maintained
|
| 80 |
+
|
| 81 |
+
🧠 Generation Quality:
|
| 82 |
+
- Confidence scores: 0.90 for successful generations
|
| 83 |
+
- Evidence-based responses with specific guideline references
|
| 84 |
+
- Appropriate medical caution and clinical judgment emphasis
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
## 🛠️ **Technical Architecture**
|
| 88 |
+
|
| 89 |
+
### **Data Flow**
|
| 90 |
+
```
|
| 91 |
+
User Query → Multi-Level Processing → Dual-Index Retrieval → RAG Generation
|
| 92 |
+
↓ ↓ ↓ ↓
|
| 93 |
+
Validation Condition Mapping Guidelines Search Medical Advice
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
### **Core Technologies**
|
| 97 |
+
- **Embeddings**: NeuML/pubmedbert-base-embeddings (768D)
|
| 98 |
+
- **Vector Search**: ANNOY indices with angular distance
|
| 99 |
+
- **LLM**: m42-health/Llama3-Med42-70B (medical specialist)
|
| 100 |
+
- **Dataset**: EPFL-LLM medical guidelines (~4000 documents)
|
| 101 |
+
|
| 102 |
+
### **Fallback Mechanism**
|
| 103 |
+
```
|
| 104 |
+
Level 1: Predefined Mapping (0.001s) → Success: Direct return
|
| 105 |
+
Level 2: LLM Extraction (8-15s) → Success: Condition mapping
|
| 106 |
+
Level 3: Semantic Search (1-2s) → Success: Sliding window chunks
|
| 107 |
+
Level 4: Medical Validation (8-10s) → Fail: Return rejection
|
| 108 |
+
Level 5: Generic Search (1s) → Final: General medical guidance
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
## 🚀 **NEXT PHASE: Interactive Interface**
|
| 112 |
+
|
| 113 |
+
### **🎯 Immediate Goals (Next 1-2 Days)**
|
| 114 |
+
|
| 115 |
+
#### **Phase 1: Gradio Interface Development**
|
| 116 |
+
- [ ] **Create `app.py`** - Interactive web interface
|
| 117 |
+
- [ ] Complete pipeline integration
|
| 118 |
+
- [ ] Multi-output display (advice + guidelines + technical details)
|
| 119 |
+
- [ ] Environment-controlled debug mode
|
| 120 |
+
- [ ] User-friendly error handling
|
| 121 |
+
|
| 122 |
+
#### **Phase 2: Local Validation Testing**
|
| 123 |
+
- [ ] **Manual testing** with 20-30 realistic medical queries
|
| 124 |
+
- [ ] Emergency scenarios (cardiac arrest, stroke, MI)
|
| 125 |
+
- [ ] Diagnostic queries (chest pain, respiratory distress)
|
| 126 |
+
- [ ] Treatment protocols (medication management, procedures)
|
| 127 |
+
- [ ] Edge cases (rare conditions, complex symptoms)
|
| 128 |
+
|
| 129 |
+
#### **Phase 3: HuggingFace Spaces Deployment**
|
| 130 |
+
- [ ] **Create requirements.txt** for deployment
|
| 131 |
+
- [ ] **Deploy to HF Spaces** for public testing
|
| 132 |
+
- [ ] **Production mode configuration** (limited technical details)
|
| 133 |
+
- [ ] **Performance monitoring** and user feedback collection
|
| 134 |
+
|
| 135 |
+
### **🔮 Future Enhancements (Next 1-2 Weeks)**
|
| 136 |
+
|
| 137 |
+
#### **Audio Input Integration**
|
| 138 |
+
- [ ] **Whisper ASR integration** for voice queries
|
| 139 |
+
- [ ] **Audio preprocessing** and quality validation
|
| 140 |
+
- [ ] **Multi-modal interface** (text + audio input)
|
| 141 |
+
|
| 142 |
+
#### **Evaluation & Metrics**
|
| 143 |
+
- [ ] **Faithfulness scoring** implementation
|
| 144 |
+
- [ ] **Automated evaluation pipeline**
|
| 145 |
+
- [ ] **Clinical validation** with medical professionals
|
| 146 |
+
- [ ] **Performance benchmarking** against target metrics
|
| 147 |
+
|
| 148 |
+
#### **Dataset Expansion (Future)**
|
| 149 |
+
- [ ] **Dataset B integration** (symptom/diagnosis subsets)
|
| 150 |
+
- [ ] **Multi-dataset RAG** architecture
|
| 151 |
+
- [ ] **Enhanced medical knowledge** coverage
|
| 152 |
+
|
| 153 |
+
## 📋 **Target Performance Metrics**
|
| 154 |
+
|
| 155 |
+
### **Response Quality**
|
| 156 |
+
- [ ] Physician satisfaction: ≥ 4/5
|
| 157 |
+
- [ ] RAG content coverage: ≥ 80%
|
| 158 |
+
- [ ] Retrieval precision (P@5): ≥ 0.7
|
| 159 |
+
- [ ] Medical advice faithfulness: ≥ 0.8
|
| 160 |
+
|
| 161 |
+
### **System Performance**
|
| 162 |
+
- [ ] Total response latency: ≤ 30 seconds
|
| 163 |
+
- [ ] Condition extraction: ≤ 5 seconds
|
| 164 |
+
- [ ] Guideline retrieval: ≤ 2 seconds
|
| 165 |
+
- [ ] Medical advice generation: ≤ 25 seconds
|
| 166 |
+
|
| 167 |
+
### **User Experience**
|
| 168 |
+
- [ ] Non-medical query rejection: 100%
|
| 169 |
+
- [ ] System availability: ≥ 99%
|
| 170 |
+
- [ ] Error handling: Graceful degradation
|
| 171 |
+
- [ ] Interface responsiveness: Immediate feedback
|
| 172 |
+
|
| 173 |
+
## 🏗️ **Project Structure**
|
| 174 |
+
```
|
| 175 |
+
OnCall.ai/
|
| 176 |
+
├── src/ # Core modules (✅ Complete)
|
| 177 |
+
│ ├── user_prompt.py # Multi-level query processing
|
| 178 |
+
│ ├── retrieval.py # Dual-index vector search
|
| 179 |
+
│ ├── generation.py # RAG-based advice generation
|
| 180 |
+
│ ├── llm_clients.py # Med42-70B integration
|
| 181 |
+
│ ├── medical_conditions.py # Medical knowledge configuration
|
| 182 |
+
│ └── data_processing.py # Dataset preprocessing
|
| 183 |
+
├── models/ # Pre-processed data (✅ Complete)
|
| 184 |
+
│ ├── embeddings/ # Vector embeddings and chunks
|
| 185 |
+
│ └── indices/ # ANNOY vector indices
|
| 186 |
+
├── tests/ # Validation tests (✅ Complete)
|
| 187 |
+
│ ├── test_multilevel_fallback_validation.py
|
| 188 |
+
│ ├── test_end_to_end_pipeline.py
|
| 189 |
+
│ └── test_userinput_userprompt_medical_*.py
|
| 190 |
+
├── docs/ # Documentation and planning
|
| 191 |
+
│ ├── next/ # Current implementation docs
|
| 192 |
+
│ └── next_gradio_evaluation/ # Interface planning
|
| 193 |
+
├── app.py # 🎯 NEXT: Gradio interface
|
| 194 |
+
├── requirements.txt # 🎯 NEXT: Deployment dependencies
|
| 195 |
+
└── README.md # This file
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
## 🧪 **Testing Validation**
|
| 199 |
+
|
| 200 |
+
### **Completed Tests**
|
| 201 |
+
- ✅ **Multi-level fallback validation**: 13 test cases, 69.2% success
|
| 202 |
+
- ✅ **End-to-end pipeline testing**: 6 scenarios, 100% technical completion
|
| 203 |
+
- ✅ **Component integration**: All modules working together
|
| 204 |
+
- ✅ **Error handling**: Graceful degradation and user-friendly messages
|
| 205 |
+
|
| 206 |
+
### **Key Findings**
|
| 207 |
+
- **Predefined mapping**: Instant response for known conditions
|
| 208 |
+
- **LLM extraction**: Reliable for complex symptom descriptions
|
| 209 |
+
- **Non-medical rejection**: Perfect accuracy with updated prompt engineering
|
| 210 |
+
- **Retrieval quality**: High-relevance medical guidelines (0.2-0.4 relevance scores)
|
| 211 |
+
- **Generation capability**: Evidence-based advice with proper medical caution
|
| 212 |
+
|
| 213 |
+
## 🤝 **Contributing & Development**
|
| 214 |
+
|
| 215 |
+
### **Environment Setup**
|
| 216 |
+
```bash
|
| 217 |
+
# Clone repository
|
| 218 |
+
git clone [repository-url]
|
| 219 |
+
cd OnCall.ai
|
| 220 |
+
|
| 221 |
+
# Setup virtual environment
|
| 222 |
+
python -m venv genAIvenv
|
| 223 |
+
source genAIvenv/bin/activate # On Windows: genAIvenv\Scripts\activate
|
| 224 |
+
|
| 225 |
+
# Install dependencies
|
| 226 |
+
pip install -r requirements.txt
|
| 227 |
+
|
| 228 |
+
# Run tests
|
| 229 |
+
python tests/test_end_to_end_pipeline.py
|
| 230 |
+
|
| 231 |
+
# Start Gradio interface (coming soon)
|
| 232 |
+
python app.py
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
### **API Configuration**
|
| 236 |
+
```bash
|
| 237 |
+
# Set up HuggingFace token for LLM access
|
| 238 |
+
export HF_TOKEN=your_huggingface_token
|
| 239 |
+
|
| 240 |
+
# Enable debug mode for development
|
| 241 |
+
export ONCALL_DEBUG=true
|
| 242 |
+
```
|
| 243 |
+
|
| 244 |
+
## ⚠️ **Important Notes**
|
| 245 |
+
|
| 246 |
+
### **Medical Disclaimer**
|
| 247 |
+
This system is designed for **research and educational purposes only**. It should not replace professional medical consultation, diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.
|
| 248 |
+
|
| 249 |
+
### **Current Limitations**
|
| 250 |
+
- **API Dependencies**: Requires HuggingFace API access for LLM functionality
|
| 251 |
+
- **Dataset Scope**: Currently focused on emergency and treatment guidelines
|
| 252 |
+
- **Language Support**: English medical terminology only
|
| 253 |
+
- **Validation Stage**: System under active development and testing
|
| 254 |
+
|
| 255 |
+
## 📞 **Contact & Support**
|
| 256 |
+
|
| 257 |
+
**Development Team**: OnCall.ai Team
|
| 258 |
+
**Last Updated**: 2025-07-31
|
| 259 |
+
**Version**: 0.9.0 (Pre-release)
|
| 260 |
+
**Status**: 🚧 Ready for Interactive Testing Phase
|
| 261 |
+
|
| 262 |
+
---
|
| 263 |
+
|
| 264 |
+
*Built with ❤️ for healthcare professionals*
|
app.py
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
OnCall.ai - Interactive Medical Emergency Assistant
|
| 4 |
+
|
| 5 |
+
A Gradio-based web interface for the OnCall.ai medical query processing system.
|
| 6 |
+
Provides real-time medical guidance based on evidence from medical guidelines.
|
| 7 |
+
|
| 8 |
+
Features:
|
| 9 |
+
- Complete pipeline: Query → Condition Extraction → Retrieval → Generation
|
| 10 |
+
- Multi-level fallback validation system
|
| 11 |
+
- Evidence-based medical advice with source attribution
|
| 12 |
+
- Environment-controlled debug mode
|
| 13 |
+
- Audio input ready (future enhancement)
|
| 14 |
+
|
| 15 |
+
Author: OnCall.ai Team
|
| 16 |
+
Date: 2025-07-31
|
| 17 |
+
Version: 0.9.0
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
import gradio as gr
|
| 23 |
+
import json
|
| 24 |
+
import traceback
|
| 25 |
+
from datetime import datetime
|
| 26 |
+
from typing import Dict, List, Any, Tuple, Optional
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
|
| 29 |
+
# Add src directory to Python path
|
| 30 |
+
current_dir = Path(__file__).parent
|
| 31 |
+
src_dir = current_dir / "src"
|
| 32 |
+
sys.path.insert(0, str(src_dir))
|
| 33 |
+
|
| 34 |
+
# Import OnCall.ai modules
|
| 35 |
+
try:
|
| 36 |
+
from user_prompt import UserPromptProcessor
|
| 37 |
+
from retrieval import BasicRetrievalSystem
|
| 38 |
+
from llm_clients import llm_Med42_70BClient
|
| 39 |
+
from generation import MedicalAdviceGenerator
|
| 40 |
+
from medical_conditions import CONDITION_KEYWORD_MAPPING
|
| 41 |
+
except ImportError as e:
|
| 42 |
+
print(f"❌ Failed to import OnCall.ai modules: {e}")
|
| 43 |
+
print("Please ensure you're running from the project root directory")
|
| 44 |
+
sys.exit(1)
|
| 45 |
+
|
| 46 |
+
# Configuration
|
| 47 |
+
DEBUG_MODE = os.getenv('ONCALL_DEBUG', 'false').lower() == 'true'
|
| 48 |
+
print(f"🔧 Debug mode: {'ON' if DEBUG_MODE else 'OFF'}")
|
| 49 |
+
|
| 50 |
+
class OnCallAIInterface:
|
| 51 |
+
"""
|
| 52 |
+
Main interface class for OnCall.ai Gradio application
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
def __init__(self):
|
| 56 |
+
"""Initialize the complete OnCall.ai pipeline"""
|
| 57 |
+
self.initialized = False
|
| 58 |
+
self.initialization_error = None
|
| 59 |
+
|
| 60 |
+
# Pipeline components
|
| 61 |
+
self.llm_client = None
|
| 62 |
+
self.retrieval_system = None
|
| 63 |
+
self.user_prompt_processor = None
|
| 64 |
+
self.medical_generator = None
|
| 65 |
+
|
| 66 |
+
# Initialize pipeline
|
| 67 |
+
self._initialize_pipeline()
|
| 68 |
+
|
| 69 |
+
def _initialize_pipeline(self):
|
| 70 |
+
"""Initialize all pipeline components with error handling"""
|
| 71 |
+
try:
|
| 72 |
+
print("🔧 Initializing OnCall.ai Pipeline...")
|
| 73 |
+
|
| 74 |
+
# Initialize LLM client
|
| 75 |
+
print(" 1. Loading Med42-70B client...")
|
| 76 |
+
self.llm_client = llm_Med42_70BClient()
|
| 77 |
+
|
| 78 |
+
# Initialize retrieval system
|
| 79 |
+
print(" 2. Loading medical guidelines indices...")
|
| 80 |
+
self.retrieval_system = BasicRetrievalSystem()
|
| 81 |
+
|
| 82 |
+
# Initialize user prompt processor
|
| 83 |
+
print(" 3. Setting up multi-level query processor...")
|
| 84 |
+
self.user_prompt_processor = UserPromptProcessor(
|
| 85 |
+
llm_client=self.llm_client,
|
| 86 |
+
retrieval_system=self.retrieval_system
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Initialize medical advice generator
|
| 90 |
+
print(" 4. Preparing medical advice generator...")
|
| 91 |
+
self.medical_generator = MedicalAdviceGenerator(
|
| 92 |
+
llm_client=self.llm_client
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
self.initialized = True
|
| 96 |
+
print("✅ OnCall.ai pipeline initialized successfully!")
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
self.initialization_error = str(e)
|
| 100 |
+
print(f"❌ Pipeline initialization failed: {e}")
|
| 101 |
+
print(f"Traceback: {traceback.format_exc()}")
|
| 102 |
+
|
| 103 |
+
def process_medical_query(self, user_query: str, intention_override: Optional[str] = None) -> Tuple[str, str, str, str]:
|
| 104 |
+
"""
|
| 105 |
+
Complete medical query processing pipeline
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
user_query: User's medical query
|
| 109 |
+
intention_override: Optional intention override for testing
|
| 110 |
+
|
| 111 |
+
Returns:
|
| 112 |
+
Tuple of (medical_advice, processing_steps, retrieved_guidelines, technical_details)
|
| 113 |
+
"""
|
| 114 |
+
if not self.initialized:
|
| 115 |
+
error_msg = f"❌ System not initialized: {self.initialization_error}"
|
| 116 |
+
return error_msg, error_msg, "{}", "{}"
|
| 117 |
+
|
| 118 |
+
if not user_query or not user_query.strip():
|
| 119 |
+
return "Please enter a medical query to get started.", "", "{}", "{}"
|
| 120 |
+
|
| 121 |
+
processing_start = datetime.now()
|
| 122 |
+
processing_steps = []
|
| 123 |
+
technical_details = {}
|
| 124 |
+
|
| 125 |
+
try:
|
| 126 |
+
# STEP 1: Query Processing and Condition Extraction
|
| 127 |
+
processing_steps.append("🎯 Step 1: Processing medical query and extracting conditions...")
|
| 128 |
+
step1_start = datetime.now()
|
| 129 |
+
|
| 130 |
+
condition_result = self.user_prompt_processor.extract_condition_keywords(user_query)
|
| 131 |
+
step1_time = (datetime.now() - step1_start).total_seconds()
|
| 132 |
+
|
| 133 |
+
processing_steps.append(f" ✅ Condition: {condition_result.get('condition', 'None')}")
|
| 134 |
+
processing_steps.append(f" 📋 Emergency Keywords: {condition_result.get('emergency_keywords', 'None')}")
|
| 135 |
+
processing_steps.append(f" 💊 Treatment Keywords: {condition_result.get('treatment_keywords', 'None')}")
|
| 136 |
+
processing_steps.append(f" ⏱️ Processing Time: {step1_time:.3f}s")
|
| 137 |
+
|
| 138 |
+
# Handle non-medical queries
|
| 139 |
+
if condition_result.get('type') == 'invalid_query':
|
| 140 |
+
non_medical_msg = condition_result.get('message', 'This appears to be a non-medical query.')
|
| 141 |
+
processing_steps.append(" 🚫 Query identified as non-medical")
|
| 142 |
+
return non_medical_msg, '\n'.join(processing_steps), "{}", "{}"
|
| 143 |
+
|
| 144 |
+
# STEP 2: User Confirmation (Auto-simulated)
|
| 145 |
+
processing_steps.append("\n🤝 Step 2: User confirmation (auto-confirmed for demo)")
|
| 146 |
+
confirmation = self.user_prompt_processor.handle_user_confirmation(condition_result)
|
| 147 |
+
|
| 148 |
+
if not condition_result.get('condition'):
|
| 149 |
+
no_condition_msg = "Unable to identify a specific medical condition. Please rephrase your query with more specific medical terms."
|
| 150 |
+
processing_steps.append(" ⚠️ No medical condition identified")
|
| 151 |
+
return no_condition_msg, '\n'.join(processing_steps), "{}", "{}"
|
| 152 |
+
|
| 153 |
+
processing_steps.append(f" ✅ Confirmed condition: {condition_result.get('condition')}")
|
| 154 |
+
|
| 155 |
+
# STEP 3: Medical Guidelines Retrieval
|
| 156 |
+
processing_steps.append("\n🔍 Step 3: Retrieving relevant medical guidelines...")
|
| 157 |
+
step3_start = datetime.now()
|
| 158 |
+
|
| 159 |
+
# Construct search query
|
| 160 |
+
search_query = f"{condition_result.get('emergency_keywords', '')} {condition_result.get('treatment_keywords', '')}".strip()
|
| 161 |
+
if not search_query:
|
| 162 |
+
search_query = condition_result.get('condition', user_query)
|
| 163 |
+
|
| 164 |
+
retrieval_results = self.retrieval_system.search(search_query, top_k=5)
|
| 165 |
+
step3_time = (datetime.now() - step3_start).total_seconds()
|
| 166 |
+
|
| 167 |
+
processed_results = retrieval_results.get('processed_results', [])
|
| 168 |
+
emergency_count = len([r for r in processed_results if r.get('type') == 'emergency'])
|
| 169 |
+
treatment_count = len([r for r in processed_results if r.get('type') == 'treatment'])
|
| 170 |
+
|
| 171 |
+
processing_steps.append(f" 📊 Found {len(processed_results)} relevant guidelines")
|
| 172 |
+
processing_steps.append(f" 🚨 Emergency guidelines: {emergency_count}")
|
| 173 |
+
processing_steps.append(f" 💊 Treatment guidelines: {treatment_count}")
|
| 174 |
+
processing_steps.append(f" ⏱️ Retrieval time: {step3_time:.3f}s")
|
| 175 |
+
|
| 176 |
+
# Format retrieved guidelines for display
|
| 177 |
+
guidelines_display = self._format_guidelines_display(processed_results)
|
| 178 |
+
|
| 179 |
+
# STEP 4: Medical Advice Generation
|
| 180 |
+
processing_steps.append("\n🧠 Step 4: Generating evidence-based medical advice...")
|
| 181 |
+
step4_start = datetime.now()
|
| 182 |
+
|
| 183 |
+
# Determine intention (use override if provided, otherwise detect)
|
| 184 |
+
intention = intention_override or self._detect_query_intention(user_query)
|
| 185 |
+
|
| 186 |
+
medical_advice_result = self.medical_generator.generate_medical_advice(
|
| 187 |
+
user_query=user_query,
|
| 188 |
+
retrieval_results=retrieval_results,
|
| 189 |
+
intention=intention
|
| 190 |
+
)
|
| 191 |
+
step4_time = (datetime.now() - step4_start).total_seconds()
|
| 192 |
+
|
| 193 |
+
# Extract medical advice
|
| 194 |
+
medical_advice = medical_advice_result.get('medical_advice', 'Unable to generate medical advice.')
|
| 195 |
+
confidence_score = medical_advice_result.get('confidence_score', 0.0)
|
| 196 |
+
|
| 197 |
+
processing_steps.append(f" 🎯 Intention: {intention}")
|
| 198 |
+
processing_steps.append(f" 📈 Confidence: {confidence_score:.2f}")
|
| 199 |
+
processing_steps.append(f" ⏱️ Generation time: {step4_time:.3f}s")
|
| 200 |
+
|
| 201 |
+
# STEP 5: Final Summary
|
| 202 |
+
total_time = (datetime.now() - processing_start).total_seconds()
|
| 203 |
+
processing_steps.append(f"\n✅ Complete pipeline finished in {total_time:.3f}s")
|
| 204 |
+
|
| 205 |
+
# Prepare technical details
|
| 206 |
+
technical_details = {
|
| 207 |
+
"condition_extraction": {
|
| 208 |
+
"method": self._determine_extraction_source(condition_result),
|
| 209 |
+
"condition": condition_result.get('condition', ''),
|
| 210 |
+
"processing_time": step1_time
|
| 211 |
+
},
|
| 212 |
+
"retrieval": {
|
| 213 |
+
"search_query": search_query if DEBUG_MODE else "[Hidden in production]",
|
| 214 |
+
"total_results": len(processed_results),
|
| 215 |
+
"emergency_results": emergency_count,
|
| 216 |
+
"treatment_results": treatment_count,
|
| 217 |
+
"processing_time": step3_time
|
| 218 |
+
},
|
| 219 |
+
"generation": {
|
| 220 |
+
"intention": intention,
|
| 221 |
+
"confidence_score": confidence_score,
|
| 222 |
+
"chunks_used": medical_advice_result.get('query_metadata', {}).get('total_chunks_used', 0),
|
| 223 |
+
"processing_time": step4_time
|
| 224 |
+
},
|
| 225 |
+
"performance": {
|
| 226 |
+
"total_pipeline_time": total_time,
|
| 227 |
+
"debug_mode": DEBUG_MODE
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
# Apply security filtering for production
|
| 232 |
+
if not DEBUG_MODE:
|
| 233 |
+
technical_details = self._sanitize_technical_details(technical_details)
|
| 234 |
+
|
| 235 |
+
return (
|
| 236 |
+
medical_advice,
|
| 237 |
+
'\n'.join(processing_steps),
|
| 238 |
+
guidelines_display,
|
| 239 |
+
json.dumps(technical_details, indent=2)
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
except Exception as e:
|
| 243 |
+
error_msg = f"❌ System error: {str(e)}"
|
| 244 |
+
processing_steps.append(f"\n❌ Error occurred: {str(e)}")
|
| 245 |
+
|
| 246 |
+
error_details = {
|
| 247 |
+
"error": str(e),
|
| 248 |
+
"timestamp": datetime.now().isoformat(),
|
| 249 |
+
"query": user_query
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
return (
|
| 253 |
+
"I apologize, but I encountered an error while processing your medical query. Please try rephrasing your question or contact technical support.",
|
| 254 |
+
'\n'.join(processing_steps),
|
| 255 |
+
"{}",
|
| 256 |
+
json.dumps(error_details, indent=2)
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
def _format_guidelines_display(self, processed_results: List[Dict]) -> str:
|
| 260 |
+
"""Format retrieved guidelines for user-friendly display"""
|
| 261 |
+
if not processed_results:
|
| 262 |
+
return json.dumps({"message": "No guidelines retrieved"}, indent=2)
|
| 263 |
+
|
| 264 |
+
guidelines = []
|
| 265 |
+
for i, result in enumerate(processed_results[:6], 1): # Show top 6
|
| 266 |
+
guideline = {
|
| 267 |
+
"guideline_id": i,
|
| 268 |
+
"source_type": result.get('type', 'unknown').title(),
|
| 269 |
+
"relevance_score": f"{1 - result.get('distance', 1):.3f}",
|
| 270 |
+
"content_preview": result.get('text', '')[:200] + "..." if len(result.get('text', '')) > 200 else result.get('text', ''),
|
| 271 |
+
"matched_keywords": result.get('matched', '') if DEBUG_MODE else "[Keywords used for matching]"
|
| 272 |
+
}
|
| 273 |
+
guidelines.append(guideline)
|
| 274 |
+
|
| 275 |
+
return json.dumps({
|
| 276 |
+
"total_guidelines": len(processed_results),
|
| 277 |
+
"displayed_guidelines": guidelines
|
| 278 |
+
}, indent=2)
|
| 279 |
+
|
| 280 |
+
def _detect_query_intention(self, user_query: str) -> str:
|
| 281 |
+
"""Simple intention detection based on query content"""
|
| 282 |
+
query_lower = user_query.lower()
|
| 283 |
+
|
| 284 |
+
treatment_indicators = ['treat', 'treatment', 'manage', 'therapy', 'protocol', 'how to']
|
| 285 |
+
diagnosis_indicators = ['diagnos', 'differential', 'symptoms', 'signs', 'what is']
|
| 286 |
+
|
| 287 |
+
treatment_score = sum(1 for indicator in treatment_indicators if indicator in query_lower)
|
| 288 |
+
diagnosis_score = sum(1 for indicator in diagnosis_indicators if indicator in query_lower)
|
| 289 |
+
|
| 290 |
+
if treatment_score > diagnosis_score:
|
| 291 |
+
return "treatment"
|
| 292 |
+
elif diagnosis_score > treatment_score:
|
| 293 |
+
return "diagnosis"
|
| 294 |
+
else:
|
| 295 |
+
return "treatment" # Default to treatment for emergency scenarios
|
| 296 |
+
|
| 297 |
+
def _determine_extraction_source(self, condition_result: Dict) -> str:
|
| 298 |
+
"""Determine how the condition was extracted"""
|
| 299 |
+
if condition_result.get('semantic_confidence') is not None:
|
| 300 |
+
return "semantic_search"
|
| 301 |
+
elif condition_result.get('generic_confidence') is not None:
|
| 302 |
+
return "generic_search"
|
| 303 |
+
elif condition_result.get('condition') in CONDITION_KEYWORD_MAPPING:
|
| 304 |
+
return "predefined_mapping"
|
| 305 |
+
else:
|
| 306 |
+
return "llm_extraction"
|
| 307 |
+
|
| 308 |
+
def _sanitize_technical_details(self, technical_details: Dict) -> Dict:
|
| 309 |
+
"""Remove sensitive technical information for production mode"""
|
| 310 |
+
sanitized = {
|
| 311 |
+
"processing_summary": {
|
| 312 |
+
"total_time": technical_details["performance"]["total_pipeline_time"],
|
| 313 |
+
"confidence": technical_details["generation"]["confidence_score"],
|
| 314 |
+
"guidelines_found": technical_details["retrieval"]["total_results"]
|
| 315 |
+
},
|
| 316 |
+
"medical_context": {
|
| 317 |
+
"condition_identified": bool(technical_details["condition_extraction"]["condition"]),
|
| 318 |
+
"intention_detected": technical_details["generation"]["intention"],
|
| 319 |
+
"evidence_sources": f"{technical_details['retrieval']['emergency_results']} emergency + {technical_details['retrieval']['treatment_results']} treatment"
|
| 320 |
+
},
|
| 321 |
+
"system_status": {
|
| 322 |
+
"all_components_operational": True,
|
| 323 |
+
"processing_mode": "production"
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
return sanitized
|
| 327 |
+
|
| 328 |
+
def create_oncall_interface():
|
| 329 |
+
"""Create and configure the Gradio interface"""
|
| 330 |
+
|
| 331 |
+
# Initialize OnCall.ai system
|
| 332 |
+
oncall_system = OnCallAIInterface()
|
| 333 |
+
|
| 334 |
+
# Define interface theme and styling
|
| 335 |
+
theme = gr.themes.Soft(
|
| 336 |
+
primary_hue="blue",
|
| 337 |
+
secondary_hue="green",
|
| 338 |
+
neutral_hue="slate"
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
# Create Gradio interface
|
| 342 |
+
with gr.Blocks(
|
| 343 |
+
theme=theme,
|
| 344 |
+
title="OnCall.ai - Medical Emergency Assistant",
|
| 345 |
+
css="""
|
| 346 |
+
.main-container { max-width: 1200px; margin: 0 auto; }
|
| 347 |
+
.medical-advice { font-size: 16px; line-height: 1.6; }
|
| 348 |
+
.processing-steps { font-family: monospace; font-size: 14px; }
|
| 349 |
+
.guidelines-display { max-height: 400px; overflow-y: auto; }
|
| 350 |
+
"""
|
| 351 |
+
) as interface:
|
| 352 |
+
|
| 353 |
+
# Header
|
| 354 |
+
gr.Markdown("""
|
| 355 |
+
# 🏥 OnCall.ai - Medical Emergency Assistant
|
| 356 |
+
|
| 357 |
+
**Evidence-based clinical guidance for healthcare professionals**
|
| 358 |
+
|
| 359 |
+
⚠️ **Medical Disclaimer**: This system is for research and educational purposes only.
|
| 360 |
+
Always consult qualified healthcare providers for medical decisions.
|
| 361 |
+
""")
|
| 362 |
+
|
| 363 |
+
# Main interface
|
| 364 |
+
with gr.Row():
|
| 365 |
+
with gr.Column(scale=1):
|
| 366 |
+
# Input section
|
| 367 |
+
gr.Markdown("## 📝 Medical Query Input")
|
| 368 |
+
|
| 369 |
+
user_input = gr.Textbox(
|
| 370 |
+
label="Enter your medical query",
|
| 371 |
+
placeholder="Example: How to treat acute myocardial infarction in emergency department?",
|
| 372 |
+
lines=3,
|
| 373 |
+
max_lines=5
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
# Optional intention override for testing
|
| 377 |
+
if DEBUG_MODE:
|
| 378 |
+
intention_override = gr.Dropdown(
|
| 379 |
+
choices=[None, "treatment", "diagnosis"],
|
| 380 |
+
label="🎯 Override Intention (Debug Mode)",
|
| 381 |
+
value=None
|
| 382 |
+
)
|
| 383 |
+
else:
|
| 384 |
+
intention_override = gr.State(None)
|
| 385 |
+
|
| 386 |
+
submit_btn = gr.Button("🔍 Get Medical Guidance", variant="primary", size="lg")
|
| 387 |
+
|
| 388 |
+
# Example queries
|
| 389 |
+
gr.Markdown("""
|
| 390 |
+
### 💡 Example Queries
|
| 391 |
+
- "How to treat acute myocardial infarction?"
|
| 392 |
+
- "Patient with severe chest pain and shortness of breath"
|
| 393 |
+
- "Emergency protocols for acute stroke management"
|
| 394 |
+
- "Differential diagnosis for sudden onset chest pain"
|
| 395 |
+
""")
|
| 396 |
+
|
| 397 |
+
# Output sections
|
| 398 |
+
gr.Markdown("## 📋 Medical Guidance Results")
|
| 399 |
+
|
| 400 |
+
with gr.Row():
|
| 401 |
+
with gr.Column(scale=2):
|
| 402 |
+
# Primary output - Medical Advice
|
| 403 |
+
medical_advice_output = gr.Textbox(
|
| 404 |
+
label="🩺 Medical Advice",
|
| 405 |
+
lines=10,
|
| 406 |
+
max_lines=15,
|
| 407 |
+
elem_classes="medical-advice"
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
# Processing steps
|
| 411 |
+
processing_steps_output = gr.Textbox(
|
| 412 |
+
label="📊 Processing Steps",
|
| 413 |
+
lines=8,
|
| 414 |
+
max_lines=12,
|
| 415 |
+
elem_classes="processing-steps"
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
with gr.Column(scale=1):
|
| 419 |
+
# Retrieved guidelines
|
| 420 |
+
guidelines_output = gr.JSON(
|
| 421 |
+
label="📚 Retrieved Medical Guidelines",
|
| 422 |
+
elem_classes="guidelines-display"
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
+
# Technical details (collapsible in production)
|
| 426 |
+
if DEBUG_MODE:
|
| 427 |
+
technical_output = gr.JSON(
|
| 428 |
+
label="⚙️ Technical Details (Debug Mode)",
|
| 429 |
+
elem_classes="technical-details"
|
| 430 |
+
)
|
| 431 |
+
else:
|
| 432 |
+
with gr.Accordion("🔧 System Information", open=False):
|
| 433 |
+
technical_output = gr.JSON(
|
| 434 |
+
label="Processing Information",
|
| 435 |
+
elem_classes="technical-details"
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
# Audio input section (placeholder for future)
|
| 439 |
+
with gr.Accordion("🎙️ Audio Input (Coming Soon)", open=False):
|
| 440 |
+
gr.Markdown("""
|
| 441 |
+
**Future Enhancement**: Voice input capability will be available soon.
|
| 442 |
+
You'll be able to:
|
| 443 |
+
- Record audio queries directly in the interface
|
| 444 |
+
- Upload audio files for processing
|
| 445 |
+
- Receive audio responses (Text-to-Speech)
|
| 446 |
+
""")
|
| 447 |
+
|
| 448 |
+
# Placeholder components for audio (inactive)
|
| 449 |
+
audio_input = gr.Audio(
|
| 450 |
+
label="Audio Query (Not yet functional)",
|
| 451 |
+
type="filepath",
|
| 452 |
+
interactive=False
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
# Event handlers
|
| 456 |
+
submit_btn.click(
|
| 457 |
+
fn=oncall_system.process_medical_query,
|
| 458 |
+
inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
|
| 459 |
+
outputs=[medical_advice_output, processing_steps_output, guidelines_output, technical_output]
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
# Enter key support
|
| 463 |
+
user_input.submit(
|
| 464 |
+
fn=oncall_system.process_medical_query,
|
| 465 |
+
inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
|
| 466 |
+
outputs=[medical_advice_output, processing_steps_output, guidelines_output, technical_output]
|
| 467 |
+
)
|
| 468 |
+
|
| 469 |
+
# Footer
|
| 470 |
+
gr.Markdown("""
|
| 471 |
+
---
|
| 472 |
+
**OnCall.ai v0.9.0** | Built with ❤️ for healthcare professionals |
|
| 473 |
+
[GitHub](https://github.com/your-username/oncall-ai) |
|
| 474 |
+
**⚠️ Research Use Only**
|
| 475 |
+
""")
|
| 476 |
+
|
| 477 |
+
return interface
|
| 478 |
+
|
| 479 |
+
def main():
|
| 480 |
+
"""Main application entry point"""
|
| 481 |
+
print("🏥 Starting OnCall.ai Interactive Interface...")
|
| 482 |
+
print(f"🔧 Debug Mode: {'ON' if DEBUG_MODE else 'OFF'}")
|
| 483 |
+
|
| 484 |
+
try:
|
| 485 |
+
# Create interface
|
| 486 |
+
interface = create_oncall_interface()
|
| 487 |
+
|
| 488 |
+
# Launch configuration
|
| 489 |
+
launch_config = {
|
| 490 |
+
"server_name": "0.0.0.0", # Allow external connections
|
| 491 |
+
"server_port": 7860, # Standard Gradio port
|
| 492 |
+
"share": False, # Set to True for public links
|
| 493 |
+
"debug": DEBUG_MODE,
|
| 494 |
+
"show_error": DEBUG_MODE
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
print("🚀 Launching OnCall.ai interface...")
|
| 498 |
+
print(f"🌐 Interface will be available at: http://localhost:7860")
|
| 499 |
+
|
| 500 |
+
if DEBUG_MODE:
|
| 501 |
+
print("🔧 Debug mode active - Technical details will be visible")
|
| 502 |
+
else:
|
| 503 |
+
print("🛡️ Production mode - Limited technical information displayed")
|
| 504 |
+
|
| 505 |
+
# Launch interface
|
| 506 |
+
interface.launch(**launch_config)
|
| 507 |
+
|
| 508 |
+
except Exception as e:
|
| 509 |
+
print(f"❌ Failed to launch interface: {e}")
|
| 510 |
+
print(f"Traceback: {traceback.format_exc()}")
|
| 511 |
+
return 1
|
| 512 |
+
|
| 513 |
+
return 0
|
| 514 |
+
|
| 515 |
+
if __name__ == "__main__":
|
| 516 |
+
exit_code = main()
|
| 517 |
+
sys.exit(exit_code)
|
src/generation.py
CHANGED
|
@@ -348,10 +348,10 @@ Your response should be concise but comprehensive, suitable for immediate clinic
|
|
| 348 |
Returns:
|
| 349 |
Structured medical advice response
|
| 350 |
"""
|
| 351 |
-
# Extract generated content
|
| 352 |
-
advice_content = generated_advice.get('
|
| 353 |
if not advice_content:
|
| 354 |
-
advice_content = generated_advice.get('
|
| 355 |
|
| 356 |
# Calculate confidence based on available factors
|
| 357 |
confidence_score = self._calculate_confidence_score(generated_advice, chunks_used)
|
|
|
|
| 348 |
Returns:
|
| 349 |
Structured medical advice response
|
| 350 |
"""
|
| 351 |
+
# Extract generated content - use raw_response for complete medical advice
|
| 352 |
+
advice_content = generated_advice.get('raw_response', '')
|
| 353 |
if not advice_content:
|
| 354 |
+
advice_content = generated_advice.get('extracted_condition', 'Unable to generate medical advice.')
|
| 355 |
|
| 356 |
# Calculate confidence based on available factors
|
| 357 |
confidence_score = self._calculate_confidence_score(generated_advice, chunks_used)
|