mohamedhassan22/WHEC_ERI_experimental
๐ฅ US Army Medical Research RAG System
A Retrieval-Augmented Generation (RAG) system for querying US Army medical research papers using state-of-the-art AI models - completely free and open-source!
๐ Features
- ๐ค LLM: Uses OpenAI GPT-3.5 Turbo for intelligent answers
- ๐ Semantic Search: OpenAI text-embedding-3-small for accurate retrieval
- ๐ Document Support: Upload JSONL or TXT files
- โ๏ธ Cloud Powered: Leveraging OpenAI API for high performance
- ๐ REST API: Full FastAPI implementation with automatic docs
- ๐พ Persistent Index: Build once, query many times
๐ Quick Start
Using the API
1. Check System Health
curl https://your-space-name.hf.space/health2. Upload Documents
curl -X POST "https://your-space-name.hf.space/upload" \
-F "files=@your_document.jsonl"3. Build Index
curl -X POST "https://your-space-name.hf.space/build-index"4. Query the System
curl -X POST "https://your-space-name.hf.space/query" \
-H "Content-Type: application/json" \
-d '{
"question": "What are the main causes of exertional injuries in US Army soldiers?",
"top_k": 5
}'Using Python
import requests
# Base URL
BASE_URL = "https://your-space-name.hf.space"
# Query the RAG system
response = requests.post(
f"{BASE_URL}/query",
json={
"question": "What injury prevention strategies are recommended?",
"top_k": 5
}
)
result = response.json()
print(f"Answer: {result['answer']}")
print(f"\nSources: {len(result['sources'])}")Using JavaScript
const BASE_URL = "https://your-space-name.hf.space";
async function queryRAG(question) {
const response = await fetch(`${BASE_URL}/query`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
question: question,
top_k: 5
})
});
const data = await response.json();
return data;
}
// Use it
queryRAG("What are the risk factors for heat illness?")
.then(result => console.log(result.answer));๐ API Endpoints
Core Endpoints
Interactive Documentation
Visit /docs for full Swagger UI documentation with interactive testing.
๐ API Request/Response Examples
Query Request
POST /query
{
"question": "What are the main causes of exertional injuries?",
"top_k": 5
}Query Response
{
"answer": "The main causes of exertional injuries in US Army soldiers include...",
"sources": [
{
"text": "Excerpt from the paper...",
"score": 0.85,
"metadata": {
"title": "Risk factors for musculoskeletal injuries...",
"journal": "Military Medical Research",
"year": "2021",
"authors": "Author1, Author2, Author3",
"pmcid": "PMC123456"
}
}
],
"question": "What are the main causes of exertional injuries?"
}๐ Document Format
JSONL Format (Recommended)
{"pmcid": "PMC123456", "title": "Paper Title", "text": "Full paper text...", "metadata": {"authors": ["Author1", "Author2"], "journal": "Journal Name", "year": "2021", "doi": "10.1234/example", "keywords": ["keyword1", "keyword2"]}}Plain Text Format
Simply upload .txt files containing your documents. Metadata will be extracted from filenames.
๐ ๏ธ Configuration
The system uses the following default configuration:
EMBEDDING_MODEL = "text-embedding-3-small"
LLM_MODEL = "gpt-3.5-turbo"
CHUNK_SIZE = 1024
CHUNK_OVERLAP = 200
TOP_K = 5
TEMPERATURE = 0.1๐ง Local Development
Prerequisites
- Python 3.10+
- NVIDIA GPU with CUDA support (recommended)
- 16GB+ RAM
- Docker (for containerized deployment)
Setup
- Clone the repository
git clone https://huggingface.co/spaces/your-username/your-space-name
cd your-space-name- Install dependencies
pip install -r requirements.txt- Run the application
uvicorn app:app --host 0.0.0.0 --port 7860- Access the API
- API: http://localhost:7860
- Docs: http://localhost:7860/docs
Docker Build
docker build -t rag-system .
docker run -p 7860:7860 --gpus all rag-system๐ฆ Project Structure
.
โโโ Dockerfile # Docker configuration
โโโ requirements.txt # Python dependencies
โโโ app.py # FastAPI application
โโโ rag_pipeline.py # Core RAG logic
โโโ README.md # This file
โโโ data/ # Document storage (created automatically)
โโโ rag_index/ # Vector index storage (created automatically)๐ง How It Works
- Document Processing: Documents are split into chunks using sentence-based splitting
- Embedding: Each chunk is embedded using OpenAI's text-embedding-3-small
- Indexing: Embeddings are stored in a vector index
- Query: User questions are embedded and similar chunks are retrieved
- Generation: GPT-3.5 Turbo generates answers based on retrieved context
๐ฏ Use Cases
- ๐ Research paper analysis
- ๐ Medical literature search
- ๐ก Knowledge extraction from documents
- ๐ Educational Q&A systems
- ๐ Data-driven insights
โ๏ธ System Requirements
Minimum
- CPU: 4 cores
- RAM: 16GB
- Storage: 20GB
Recommended
- GPU: NVIDIA GPU with 8GB+ VRAM
- CPU: 8+ cores
- RAM: 32GB+
- Storage: 50GB+ SSD
๐จ Troubleshooting
Index Not Found
Issue: RAG system returns "not initialized" Solution: Upload documents and build the index using /build-index
API Key Missing
Issue: RAG system errors with authentication failure Solution: Ensure OPENAI_API_KEY is set in your environment variables or Space settings.
Rate Limits
Issue: Queries failing continuously Solution: Check your OpenAI API usage and limits.
๐ Example Questions
- "What are the main causes of exertional injuries in US Army soldiers?"
- "How does heat illness affect military readiness?"
- "What injury prevention strategies are recommended for military training?"
- "What are the risk factors for musculoskeletal injuries?"
- "How can training programs reduce injury rates?"
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- OpenAI for LLM and Embedding models
- LlamaIndex for the RAG framework
- Hugging Face for hosting and model infrastructure
๐ง Contact
For questions or support, please open an issue on the repository.
Note: This system is designed for research and educational purposes. Always verify critical information with primary sources.
