CoolFace
Apppublic

kiran78487/Rag_Symptoms_checker

sourceHugging Facemitupdated 7mo agoView on Hugging Face
0likes
App README

๐Ÿฅ 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/diagnose

Example Request

json
{
  "text": "I have sharp chest pain radiating to my left arm with sweating"
}

Example Response

json
{
  "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:

  1. 1.Frontend Layer - REST API for mobile/web apps
  2. 2.LLM Extraction - Structured symptom extraction from natural language
  3. 3.Symptom Mapping - Dictionary + FAISS semantic matching to canonical terms
  4. 4.Disease Retrieval - FAISS vector search across 1,394 diseases
  5. 5.Scoring Engine - Hybrid 5-component formula:
  6. 6.40% Vector similarity
  7. 7.25% Symptom overlap
  8. 8.15% Coverage ratio
  9. 9.15% Modifier matching (OPQRST)
  10. 10.5% Risk adjustment
  11. 11.Safety Layer - Emergency detection for life-threatening conditions
  12. 12.LLM Explanation - Controlled reasoning generation
  13. 13.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

๐Ÿšจ 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)

  1. 1.Add your HuggingFace API token as a Space secret:
  2. 2.Go to your Space settings
  3. 3.Navigate to "Variables and secrets"
  4. 4.Add a new secret: HF_TOKEN = your_huggingface_token
  5. 5.This is required for LLM inference API calls
  1. 1.The Space will automatically build FAISS indices on first deployment

Endpoints

1. Diagnose Symptoms
POST /api/v1/diagnose

Main diagnostic endpoint for symptom analysis.

2. Search Symptoms
GET /api/v1/symptoms/search?query=headache&top_k=10

Semantic search for canonical symptoms (useful for autocomplete).

3. Health Check
GET /api/v1/health

Check if models are loaded and system is ready.

4. Interactive Docs
GET /docs

Swagger 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

  1. 1.Dictionary lookup - Instant exact matching (98% confidence)
  2. 2.FAISS semantic search - Fuzzy matching with threshold โ‰ฅ 0.80
  3. 3.Gap validation - Ensures top match is significantly better

Disease Retrieval

  1. 1.Combine canonical symptoms into query
  2. 2.Embed using BGE model (384D)
  3. 3.Search FAISS index for Top-20 candidates
  4. 4.Re-rank using hybrid scoring
  5. 5.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

javascript
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

dart
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:

bash
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.