Instructions to use nsr51324/CortexRAG with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use nsr51324/CortexRAG with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("nsr51324/CortexRAG") query = "Which planet is known as the Red Planet?" passages = [ "Venus is often called Earth's twin because of its similar size and proximity.", "Mars, known for its reddish appearance, is often referred to as the Red Planet.", "Jupiter, the largest planet in our solar system, has a prominent red spot.", "Saturn, famous for its rings, is sometimes mistaken for the Red Planet." ] scores = model.predict([(query, passage) for passage in passages]) print(scores) - Notebooks
- Google Colab
- Kaggle
File size: 8,712 Bytes
76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af 76b2481 dac03af | 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 | ---
title: CortexRAG Medical RAG API
emoji: π©Ί
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 8000
tags:
- rag
- medical-ai
- sentence-transformers
- faiss
- fastapi
- cross-encoder
- groq
---
# π©Ί CortexRAG - Advanced Medical RAG API
**CortexRAG** is a high-performance, domain-specific Retrieval-Augmented Generation (RAG) system engineered for medical and clinical question answering. It combines semantic vector search (FAISS + Sentence Transformers), cross-encoder re-ranking, and high-speed LLM inference (Groq / Llama) wrapped in a lightweight **FastAPI** REST interface.
---
## π Features
- **Semantic Embedding Engine**: `all-MiniLM-L6-v2` dense vector retrieval using FAISS indexing.
- **Precision Re-ranking**: Cross-encoder scoring (`cross-encoder/ms-marco-MiniLM-L-6-v2`) for optimal document relevance.
- **Medical Synonym Expansion**: Context-aware synonym mapping for expanded search recall.
- **Ultra-Fast REST API**: Built on FastAPI with asynchronous request handling and Pydantic validation.
- **Cloudflare Tunnel Ready**: Zero-trust public exposure without complex firewall configuration.
---
## π System Architecture
```
βββββββββββββββββββββββββββββ
β Client Request β
βββββββββββββββ¬ββββββββββββββ
β POST /query
βΌ
βββββββββββββββββββββββββββββ
β FastAPI Web Server β
βββββββββββββββ¬ββββββββββββββ
β
βββββββββββββββββββββββ΄ββββββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββ
β Query Vectorization β β Medical Synonym Expansion β
β (all-MiniLM-L6-v2) β βββββββββββββββ¬ββββββββββββββ
βββββββββββββββ¬ββββββββββββββ β
β β
βββββββββββββββββββββββ¬ββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββ
β FAISS Vector Index β
βββββββββββββββ¬ββββββββββββββ
β Top-N Candidate Docs
βΌ
βββββββββββββββββββββββββββββ
β Cross-Encoder Reranker β
β (ms-marco-MiniLM-L-6-v2) β
βββββββββββββββ¬ββββββββββββββ
β Top-K Ranked Context
βΌ
βββββββββββββββββββββββββββββ
β LLM Synthesis (Groq) β
βββββββββββββββ¬ββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββ
β JSON API Response β
βββββββββββββββββββββββββββββ
```
---
## π Repository Structure
```
CortexRAG/
βββ API_DEPLOYMENT_PLAN.md # Step-by-step API & tunnel setup documentation
βββ README.md # Hugging Face & GitHub Project Card
βββ question_embeddings.npy # Pre-computed dense embeddings matrix
βββ questions.index # Binary FAISS vector search index
βββ notebooks/ # Experimental notebooks & cleaning scripts
β βββ Medical_RAG_Sytem.ipynb
β βββ rag_data_cleaning.ipynb
βββ rag_model/ # Core RAG engine configurations & resources
βββ rag_config.json # Search, score & model parameters
βββ requirements.txt # Python dependency specifications
βββ models/ # Synonyms & model metadata
βββ medical_synonyms.json
```
---
## β‘ Quick Start & Installation
### 1. Prerequisites
- Python 3.9+
- Pip package manager
### 2. Environment Setup
```bash
# Clone repository
git clone https://huggingface.co/spaces/YOUR_USERNAME/CortexRAG
cd CortexRAG
# Create virtual environment
python -m venv venv
# Activate on Windows:
venv\Scripts\activate
# Activate on Linux/macOS:
source venv/bin/activate
# Install dependencies
pip install -r rag_model/requirements.txt fastapi uvicorn pydantic
```
### 3. Environment Variables
Set your Groq API Key (or other LLM provider keys):
```bash
# Windows PowerShell
$env:GROQ_API_KEY="your_groq_api_key_here"
# Linux/macOS
export GROQ_API_KEY="your_groq_api_key_here"
```
---
## π Running the Local API
Start the server using `uvicorn`:
```bash
uvicorn app:app --host 127.0.0.1 --port 8000 --reload
```
Interactive API Documentation (Swagger UI) is available at:
π **`http://127.0.0.1:8000/docs`**
---
## π Exposing Publicly via Cloudflare Tunnel
To expose your local FastAPI server securely to the internet without port forwarding:
1. Download [cloudflared](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/).
2. Run the tunnel pointing to your local port:
```bash
cloudflared tunnel --url http://127.0.0.1:8000
```
3. Use the generated URL (e.g. `https://xxx.trycloudflare.com`) as your public API endpoint.
---
## π API Reference & Integration Guide
### Endpoint
`POST /query`
### Request Headers
```http
Content-Type: application/json
```
### Request Payload Example
```json
{
"question": "What are the first-line treatments for type 2 diabetes?",
"top_k": 6
}
```
### Response Payload Example
```json
{
"status": "success",
"question": "What are the first-line treatments for type 2 diabetes?",
"answer": "First-line pharmacological management for type 2 diabetes typically includes Metformin alongside lifestyle modifications...",
"retrieved_context": [
{
"doc_id": 42,
"text": "Metformin remains the initial drug of choice for monotherapy...",
"rerank_score": 4.85
}
],
"execution_time_sec": 0.38
}
```
### Python Integration Example
```python
import requests
url = "https://your-cloudflare-url.trycloudflare.com/query"
payload = {
"question": "What are the common causes of chest pain?",
"top_k": 5
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
```
---
## π Configuration Parameters (`rag_config.json`)
| Parameter | Default | Description |
| :--- | :--- | :--- |
| `embedding_model` | `all-MiniLM-L6-v2` | SentenceTransformer embedding model |
| `reranker_model` | `cross-encoder/ms-marco-MiniLM-L-6-v2` | Precision reranking cross-encoder model |
| `retrieve_top_n` | `20` | Initial FAISS vector retrieval candidate count |
| `rerank_top_k` | `6` | Number of context snippets passed to LLM |
| `min_similarity_floor` | `0.4` | Cosine similarity threshold |
---
## π License
This project is released under the **MIT License**.
|