kiran78487/Rag_Symptoms_checker
๐ฅ Medical RAG System - Symptom-to-Disease Diagnosis
A production-grade medical RAG (Retrieval-Augmented Generation) system that provides intelligent differential diagnosis from natural language symptom descriptions.
โก Quick Start
API Endpoint
POST /api/v1/diagnoseExample Request
{
"text": "I have sharp chest pain radiating to my left arm with sweating"
}Example Response
{
"emergency_flag": true,
"emergency_message": "๐จ EMERGENCY: Possible heart attack. Call 911 immediately!",
"extracted_symptoms": [
{
"original": "sharp chest pain",
"canonical": "chest pain",
"confidence": 0.95
}
],
"top_differentials": [
{
"disease_name": "Myocardial infarction",
"confidence": 0.82,
"matched_symptoms": ["chest pain", "arm pain", "sweating"],
"reasoning": "Classic presentation..."
}
]
}๐๏ธ Architecture
This system implements 8 intelligent layers:
- Frontend Layer - REST API for mobile/web apps
- LLM Extraction - Structured symptom extraction from natural language
- Symptom Mapping - Dictionary + FAISS semantic matching to canonical terms
- Disease Retrieval - FAISS vector search across 1,394 diseases
- Scoring Engine - Hybrid 5-component formula:
- 40% Vector similarity
- 25% Symptom overlap
- 15% Coverage ratio
- 15% Modifier matching (OPQRST)
- 5% Risk adjustment
- Safety Layer - Emergency detection for life-threatening conditions
- LLM Explanation - Controlled reasoning generation
- Response Formatting - Structured JSON output
๐ Data & Models
Knowledge Base
- 1,394 diseases from authoritative sources (Mayo Clinic, NHS)
- 3,371 canonical symptoms with 15,000+ variant mappings
- Pre-built FAISS indices for fast semantic search
AI Models
- LLM: Qwen2.5-3B-reasoning-medical-symptoms
- Embeddings: BAAI/bge-small-en-v1.5 (384 dimensions)
- Vector Search: FAISS IndexFlatIP (exact search)
๐จ Safety Features
Emergency Detection
The system automatically detects and flags:
- โค๏ธ Heart attack symptoms (chest pain + radiation + sweating)
- ๐ง Stroke indicators (facial drooping, speech difficulty, numbness)
- ๐ซ Severe respiratory distress
- ๐ฉธ Severe bleeding/trauma
- ๐ต Loss of consciousness
- ๐ฆ Sepsis and meningitis patterns
When emergencies are detected, the system immediately returns an emergency alert and bypasses normal disease ranking.
๐ก API Documentation
Setup (HuggingFace Spaces)
- Add your HuggingFace API token as a Space secret:
- Go to your Space settings
- Navigate to "Variables and secrets"
- Add a new secret:
HF_TOKEN=your_huggingface_token - This is required for LLM inference API calls
- The Space will automatically build FAISS indices on first deployment
Endpoints
1. Diagnose Symptoms
POST /api/v1/diagnoseMain diagnostic endpoint for symptom analysis.
2. Search Symptoms
GET /api/v1/symptoms/search?query=headache&top_k=10Semantic search for canonical symptoms (useful for autocomplete).
3. Health Check
GET /api/v1/healthCheck if models are loaded and system is ready.
4. Interactive Docs
GET /docsSwagger UI for testing the API.
๐ง Technical Details
Scoring Formula
FINAL_SCORE =
(0.40 ร vector_similarity) +
(0.25 ร symptom_overlap) +
(0.15 ร coverage_ratio) +
(0.15 ร modifier_match) +
(0.05 ร risk_adjustment)Symptom Matching
- Dictionary lookup - Instant exact matching (98% confidence)
- FAISS semantic search - Fuzzy matching with threshold โฅ 0.80
- Gap validation - Ensures top match is significantly better
Disease Retrieval
- Combine canonical symptoms into query
- Embed using BGE model (384D)
- Search FAISS index for Top-20 candidates
- Re-rank using hybrid scoring
- Return Top-5 with explanations
โ ๏ธ Medical Disclaimer
CRITICAL: This system is designed for informational purposes only and is NOT a substitute for professional medical advice, diagnosis, or treatment.
- โ DO NOT use this for self-diagnosis
- โ DO NOT delay seeking medical care based on results
- โ DO NOT ignore emergency symptoms
- โ ALWAYS consult qualified healthcare professionals
- โ CALL 911 for emergencies indicated by the system
๐ฑ Mobile App Integration
React Native Example
async function diagnose(symptoms) {
const response = await fetch('https://barathvasan-rag-system-med-connect.hf.space/api/v1/diagnose', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: symptoms })
});
return await response.json();
}
const result = await diagnose("I have severe headache with stiff neck");
if (result.emergency_flag) {
alert(result.emergency_message);
}Flutter Example
Future<Map> diagnose(String symptoms) async {
final response = await http.post(
Uri.parse('https://barathvasan-rag-system-med-connect.hf.space/api/v1/diagnose'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'text': symptoms}),
);
return json.decode(response.body);
}๐งช Testing
Test the API with sample symptoms:
curl -X POST "https://barathvasan-rag-system-med-connect.hf.space/api/v1/diagnose" \
-H "Content-Type: application/json" \
-d '{"text": "Sharp chest pain radiating to left arm with sweating and nausea"}'๐ License
MIT License - See LICENSE file for details
๐จโ๐ป Developer
Built by Barathvasan
๐ Acknowledgments
- Medical data sources: Mayo Clinic, NHS
- LLM: Qwen2.5-3B-reasoning-medical-symptoms by dumbequation
- Embeddings: BGE by Beijing Academy of Artificial Intelligence
- Vector search: FAISS by Meta AI
Remember: This is a research/educational tool. For medical concerns, always consult healthcare professionals.
