Spaces:
Runtime error
Runtime error
File size: 19,799 Bytes
47c83f0 | 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | # Medical Predictor Chatbot - Developer Guides
## π All Documentation Files
### For Project Overview & Planning
- **PROJECT_ROADMAP.md** β Start here for complete timeline
- **ANALYSIS.md** β Current status & what's missing
- **GUIDES.md** β This file (detailed guides for each phase)
---
## π― Phase-by-Phase Implementation Guide
---
## PHASE 1: Environment & Infrastructure β
(85% Complete)
### What You've Done
- β
Created basic project structure
- β
Set up Gradio + Groq dependencies
- β
Created basic Groq API integration
- β
Implemented state management
### What You Need to Do (15% Remaining)
#### 1.1 Create `.env` File
```bash
cd /media/zunayed/HDD_code/chatbot\ with\ llm/medical-predictor-chatbot
echo "GROQ_API_KEY=gsk_your_api_key_here" > .env
```
Get your API key:
1. Visit https://console.groq.com/keys
2. Sign up (free)
3. Generate API key
4. Paste into `.env`
#### 1.2 Create `.gitignore`
```bash
cat > .gitignore << 'EOF'
.env
*.pkl
__pycache__/
venv/
.DS_Store
*.pyc
.pytest_cache/
.idea/
*.egg-info/
dist/
build/
app_state.json
EOF
```
#### 1.3 Create Folder Structure
```bash
mkdir -p app/services app/utils models
touch app/__init__.py app/services/__init__.py app/utils/__init__.py
```
#### 1.4 Verify Installation
```bash
pip install -r requirements.txt
python test_extractor.py # Should print JSON with features
```
**β
Phase 1 Complete When:**
- `.env` exists with valid GROQ_API_KEY
- `.gitignore` created
- Folder structure matches
- `test_extractor.py` runs without errors
---
## PHASE 2: Configuration & Schemas π² (0% Complete)
### What to Create
#### 2.1 Create `app/config.py`
```python
import os
from pathlib import Path
from dotenv import load_dotenv
# Load environment
load_dotenv()
# Paths
BASE_DIR = Path(__file__).parent.parent
MODEL_DIR = BASE_DIR / "models"
MODEL_PATH = MODEL_DIR / "GradientBoosting_model.pkl"
# Model configuration
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
GROQ_MODEL = "llama-3.1-8b-instant"
GROQ_TEMPERATURE = 0
# Medical features (all 16)
DEFAULT_MODEL_FEATURES = [
"LengthOfStay", "Smoking", "Family History", "HbA1c", "Glucose",
"Age", "Diet Score", "Alcohol", "Physical Activity", "Blood Pressure",
"BMI", "Cholesterol", "Sleep Hours", "Stress Level",
"Triglycerides", "Oxygen Saturation"
]
# Feature validation ranges (min, max, expected type)
FEATURE_RANGES = {
"Age": (0, 150, float),
"Glucose": (70, 400, float),
"HbA1c": (3, 15, float),
"BMI": (10, 60, float),
"Cholesterol": (100, 400, float),
"Triglycerides": (20, 500, float),
"Blood Pressure": (60, 200, float), # Systolic only simplified
"Physical Activity": (0, 24, float), # hours per week
"Sleep Hours": (0, 24, float),
"Stress Level": (1, 10, float),
"Diet Score": (1, 10, float),
"Smoking": (0, 1, int), # 0=No, 1=Yes
"Alcohol": (0, 1, int), # 0=No, 1=Yes
"Family History": (0, 1, int), # 0=No, 1=Yes
"LengthOfStay": (0, 365, int), # days
"Oxygen Saturation": (80, 100, float), # percentage
}
# API configuration
GROQ_TIMEOUT = 30
MAX_RETRIES = 3
# App configuration
DEBUG_MODE = True
CONVERSATION_MAX_TURNS = 20
```
#### 2.2 Create `app/schemas.py`
```python
from typing import Optional
from pydantic import BaseModel, Field, validator
class MedicalFeatures(BaseModel):
"""All 16 required medical features"""
Age: Optional[float] = None
Glucose: Optional[float] = None
HbA1c: Optional[float] = None
BMI: Optional[float] = None
Cholesterol: Optional[float] = None
Triglycerides: Optional[float] = None
BloodPressure: Optional[float] = None
PhysicalActivity: Optional[float] = None
SleepHours: Optional[float] = None
StressLevel: Optional[float] = None
DietScore: Optional[float] = None
Smoking: Optional[int] = None
Alcohol: Optional[int] = None
FamilyHistory: Optional[int] = None
LengthOfStay: Optional[int] = None
OxygenSaturation: Optional[float] = None
@validator("Age")
def validate_age(cls, v):
if v is not None and not (0 <= v <= 150):
raise ValueError("Age must be between 0 and 150")
return v
# Add similar validators for other fields...
class Config:
use_enum_values = True
arbitrary_types_allowed = True
class PredictionRequest(BaseModel):
"""Request for prediction"""
features: MedicalFeatures
class PredictionResponse(BaseModel):
"""Prediction response"""
prediction: int # 0 or 1 (or disease class)
probability: float # 0.0 to 1.0
risk_level: str # "Low", "Medium", "High"
explanation: str
class ExtractionResponse(BaseModel):
"""LLM extraction response"""
extracted_features: MedicalFeatures
confidence: float
```
**β
Phase 2 Complete When:**
- Both files exist and have no import errors
- Run: `python -c "from app.config import *; from app.schemas import *"`
- No errors appear
**Time Estimate**: 15-20 minutes
---
## PHASE 3: ML Model Preparation π² (0% Complete)
### Option A: Create a Synthetic Model (for testing)
Create `create_model.py`:
```python
import joblib
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from pathlib import Path
# Create synthetic data
np.random.seed(42)
X = np.random.randn(100, 16) # 16 features
y = np.random.randint(0, 2, 100) # Binary classification
# Train model
model = GradientBoostingClassifier(n_estimators=50, random_state=42)
model.fit(X, y)
# Save
Path("models").mkdir(exist_ok=True)
joblib.dump(model, "models/GradientBoosting_model.pkl")
print("β
Model saved to models/GradientBoosting_model.pkl")
print(f"Model expects {model.n_features_in_} features")
```
Run it:
```bash
python create_model.py
```
### Option B: Use Pre-trained Model
If you have an existing model file:
```bash
cp /path/to/your/GradientBoosting_model.pkl models/
```
**β
Phase 3 Complete When:**
- `models/GradientBoosting_model.pkl` exists
- Test with: `python -c "import joblib; m = joblib.load('models/GradientBoosting_model.pkl'); print(f'Model loaded! Features: {m.n_features_in_}')"`
**Time Estimate**: 20-40 minutes
---
## PHASE 4: Service Layer Implementation π² (50% Complete)
### 4.1 Implement `app/services/feature_builder.py`
```python
from typing import List, Dict, Any
from app.config import FEATURE_RANGES, DEFAULT_MODEL_FEATURES
from app.schemas import MedicalFeatures
def validate_feature(name: str, value: Any) -> Any:
"""Validate a single feature"""
if value is None:
return None
if name not in FEATURE_RANGES:
return None
min_val, max_val, expected_type = FEATURE_RANGES[name]
# Convert to expected type
try:
converted = expected_type(value)
except (ValueError, TypeError):
return None
# Check range
if not (min_val <= converted <= max_val):
return None
return converted
def prepare_features_dict(state: Dict[str, Any]) -> MedicalFeatures:
"""Convert state dict to MedicalFeatures model"""
validated = {}
for feature in DEFAULT_MODEL_FEATURES:
value = state.get(feature)
validated[feature] = validate_feature(feature, value)
return MedicalFeatures(**validated)
def prepare_feature_vector(state: Dict[str, Any]) -> List[float]:
"""
Convert state dict to feature vector for ML model.
Returns array in correct feature order.
Handles missing values with sensible defaults.
"""
# Define feature order (MUST match training data order)
feature_order = DEFAULT_MODEL_FEATURES
vector = []
for feature_name in feature_order:
value = state.get(feature_name)
if value is not None:
vector.append(float(value))
else:
# Use feature mean or 0 for missing values
vector.append(0.0) # Simple approach - can be improved
return vector
def is_ready_for_prediction(state: Dict[str, Any]) -> bool:
"""Check if enough features collected for prediction"""
missing = [k for k, v in state.items() if v is None]
# At least 14 out of 16 features
return len(missing) <= 2
```
### 4.2 Implement `app/services/predictor.py`
```python
import joblib
import logging
from pathlib import Path
from app.config import MODEL_PATH
from app.schemas import PredictionResponse
logger = logging.getLogger(__name__)
class Predictor:
"""Load and use ML model for predictions"""
def __init__(self, model_path: str = None):
if model_path is None:
model_path = MODEL_PATH
self.model_path = Path(model_path)
self.model = None
self.load_model()
def load_model(self):
"""Load model from disk"""
if not self.model_path.exists():
raise FileNotFoundError(f"Model not found at {self.model_path}")
try:
self.model = joblib.load(self.model_path)
logger.info(f"β
Model loaded from {self.model_path}")
except Exception as e:
logger.error(f"β Failed to load model: {e}")
raise
def predict(self, feature_vector: list) -> PredictionResponse:
"""
Make prediction on feature vector
Args:
feature_vector: List of 16 floats in correct order
Returns:
PredictionResponse with prediction, probability, risk level
"""
if self.model is None:
raise RuntimeError("Model not loaded")
try:
# Make prediction
prediction = self.model.predict([feature_vector])[0]
# Get probability
proba = self.model.predict_proba([feature_vector])[0]
probability = float(max(proba))
# Determine risk level
if probability >= 0.8:
risk_level = "High"
elif probability >= 0.5:
risk_level = "Medium"
else:
risk_level = "Low"
return PredictionResponse(
prediction=int(prediction),
probability=probability,
risk_level=risk_level,
explanation=f"Model predicts class {prediction} with {probability*100:.1f}% confidence"
)
except Exception as e:
logger.error(f"Prediction failed: {e}")
raise
# Global predictor instance
predictor = None
def get_predictor():
"""Get or create predictor instance"""
global predictor
if predictor is None:
predictor = Predictor()
return predictor
```
**β
Phase 4 Complete When:**
- Both services import without errors
- Test with: `python -c "from app.services.feature_builder import *; from app.services.predictor import *"`
**Time Estimate**: 30-40 minutes
---
## PHASE 5: Main Application Integration π² (20% Complete)
### Update `app/main.py`
Replace the current `main.py` with:
```python
import gradio as gr
import logging
from app.services.llm_extractor import extract_features_from_text
from app.services.feature_builder import prepare_feature_vector, is_ready_for_prediction
from app.services.predictor import get_predictor
from app.memory import initialize_state, update_state, get_missing_features
from app.utils.helpers import generate_question
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Global state
state = initialize_state()
conversation_history = []
def chat_fn(message, history):
"""Main chat function for Gradio"""
global state, conversation_history
try:
# Step 1: Extract features from user input
logger.info(f"User: {message}")
extracted = extract_features_from_text(message)
logger.info(f"Extracted: {extracted}")
# Step 2: Update memory with extracted features
state = update_state(state, extracted)
# Step 3: Check for missing features
missing = get_missing_features(state)
# Step 4: If all features collected, make prediction
if not missing or is_ready_for_prediction(state):
logger.info("All features collected! Making prediction...")
# Prepare features for model
feature_vector = prepare_feature_vector(state)
# Get prediction
predictor = get_predictor()
result = predictor.predict(feature_vector)
response = f"""
β
All information collected!
**Prediction Results:**
- **Prediction**: Class {result.prediction}
- **Confidence**: {result.probability*100:.1f}%
- **Risk Level**: {result.risk_level}
- **Details**: {result.explanation}
---
*Note: This is a demonstration. Always consult with a healthcare professional for medical advice.*
"""
return response
else:
# Ask for next missing feature
next_question = generate_question(missing[0])
remaining = len(missing)
response = f"Got it! {next_question}\n\n_(Missing {remaining} more features)_"
return response
except Exception as e:
logger.error(f"Error in chat: {e}")
return f"β Error: {str(e)}. Please try again."
# Create Gradio interface
demo = gr.ChatInterface(
fn=chat_fn,
title="π₯ Medical Predictor Chatbot",
description="Chat about your medical information. I'll ask follow-up questions and predict your health risk.",
examples=[
"I'm 45 years old and my glucose is 150",
"I smoke and my stress level is 8",
"My BMI is 28 and I exercise 5 hours per week"
]
)
if __name__ == "__main__":
demo.launch()
```
**β
Phase 5 Complete When:**
- `python app/main.py` runs without errors
- Gradio UI launches at http://localhost:7860
- Chat works (asks questions and eventually predicts)
**Time Estimate**: 25-35 minutes
---
## PHASE 6: Error Handling & Validation π² (0% Complete)
### Add Error Handling to `app/services/llm_extractor.py`
```python
def extract_features_from_text(user_input: str) -> dict:
"""
Uses Groq LLM to extract structured medical features from text.
Returns dictionary with all required keys.
"""
if not user_input or not user_input.strip():
return {feature: None for feature in DEFAULT_MODEL_FEATURES}
prompt = f"""
You are a medical information extraction system.
Extract the following features from the user input.
Return STRICT JSON only. No explanation.
Features:
{DEFAULT_MODEL_FEATURES}
Rules:
- If value is missing, use null
- Convert values to numbers where possible
- Smoking, Alcohol, Family History β 0 or 1
- Blood Pressure β numeric (e.g., 120)
- Output must be valid JSON
User Input:
\"\"\"{user_input}\"\"\"
"""
try:
response = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[
{"role": "system", "content": "You are a strict JSON generator."},
{"role": "user", "content": prompt}
],
temperature=0,
timeout=30
)
content = response.choices[0].message.content
try:
data = json.loads(content)
except json.JSONDecodeError:
# Try extracting JSON from markdown code blocks
if "```" in content:
content = content.split("```")[1]
if content.startswith("json"):
content = content[4:]
data = json.loads(content)
else:
logger.warning("Could not parse LLM response as JSON")
data = {feature: None for feature in DEFAULT_MODEL_FEATURES}
return data
except Exception as e:
logger.error(f"LLM extraction error: {e}")
return {feature: None for feature in DEFAULT_MODEL_FEATURES}
```
**β
Phase 6 Complete When:**
- No unhandled exceptions when running app
- Graceful error messages shown to user
**Time Estimate**: 15-20 minutes
---
## PHASE 7: Hugging Face Spaces Deployment π² (0% Complete)
### 7.1 Create HF Space
1. Go to https://huggingface.co/spaces/new
2. Fill in:
- **Space name**: medical-predictor-chatbot
- **License**: MIT
- **SDK**: Gradio
- **Visibility**: Public
### 7.2 Push Code to HF
```bash
# Navigate to your project
cd /media/zunayed/HDD_code/chatbot\ with\ llm/medical-predictor-chatbot
# Add HF remote (replace YOUR_USERNAME)
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/medical-predictor-chatbot
# Push to HF
git push hf main
```
### 7.3 Add Secrets in HF Space
1. Go to your Space page
2. Click "Settings" β "Repository Secrets"
3. Add secret:
- Key: `GROQ_API_KEY`
- Value: Your Groq API key from `console.groq.com/keys`
### 7.4 Update README.md
```markdown
---
title: Medical Predictor Chatbot
emoji: π₯
colorFrom: blue
colorTo: green
sdk: gradio
app_file: app/main.py
pinned: false
---
# Medical Predictor Chatbot
A conversational AI chatbot that collects medical information through natural conversation and predicts health risks using machine learning.
## Features
- π¬ Conversational interface using Groq Llama 3 LLM
- π₯ Extracts 16 medical features from free-form text
- π€ Makes predictions using scikit-learn GradientBoostingClassifier
- π Shows confidence levels and risk assessment
## How It Works
1. Chat naturally about your health
2. The AI extracts medical information from your responses
3. Asks follow-up questions for missing information
4. Makes a health risk prediction once all data is collected
## Example Usage
- "I'm 45 years old and my glucose is 150"
- "I smoke and my stress level is 8"
- "My BMI is 28 and I exercise 5 hours per week"
## Disclaimer
β οΈ This is a demonstration tool. Always consult with healthcare professionals for medical advice.
```
**β
Phase 7 Complete When:**
- Code deployed to HF Spaces
- App loads and works
- GROQ_API_KEY secret is set
**Time Estimate**: 15-30 minutes
---
## PHASE 8: Testing & Finalization π² (0% Complete)
### Test Scenarios
#### Test 1: Feature Extraction
```bash
python test_extractor.py
# Check if JSON is returned with features
```
#### Test 2: Gradio UI (Local)
```bash
python app/main.py
# Open http://localhost:7860
# Test conversation flow
```
#### Test 3: Full Prediction Flow
Chat sequence:
1. "I'm 45 years old"
2. "My glucose is 150"
3. "I smoke"
4. (Continue answering questions for other features)
5. (Expect: Prediction with risk level)
#### Test 4: Error Handling
- Send empty message β Should handle gracefully
- Send gibberish β Should extract what it can
- Invalid values β Should validate and reject
**β
Phase 8 Complete When:**
- All tests pass
- No errors in logs
- App works on HF Spaces
- README is complete
**Time Estimate**: 30-60 minutes
---
## π Success Criteria Checklist
- [ ] `.env` file created with GROQ_API_KEY
- [ ] `app/config.py` completed
- [ ] `app/schemas.py` completed
- [ ] `models/GradientBoosting_model.pkl` exists
- [ ] `app/services/feature_builder.py` completed
- [ ] `app/services/predictor.py` completed
- [ ] `app/main.py` updated with prediction
- [ ] Error handling added
- [ ] Logging configured
- [ ] Local testing successful
- [ ] HF Space created
- [ ] Code pushed to HF
- [ ] GROQ_API_KEY secret added to HF
- [ ] HF deployment successful
- [ ] README updated with documentation
---
## π Start Here!
**Next step**: Follow **PHASE 2** in this guide to create `config.py` and `schemas.py`
When ready, type: **"I'm ready for Phase 2"**
|