Skip to content

About

A hospital analytics system that preprocesses structured healthcare data from multiple sources, loads it into a relational database with enforced constraints, and enables insights through a Natural Language Query (NLQ) interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

MediQuery 🏥

MediQuery is a natural language to SQL engine for hospital analytics. Ask questions about hospital data in plain English — the app generates SQL using Groq LLM and returns results from a structured hospital database.

Live Demo: https://mediquery.vercel.app Backend API Docs: https://mediquery-api-uu2g.onrender.com/docs


How It Works

User Question (plain English)
        │
        ▼
FastAPI Backend (Render)
        │
        ▼
LangChain SQLDatabaseChain
        │
        ├── Groq LLM generates SQL query
        │
        ▼
SQLite Hospital Database
        │
        ▼
Results returned as JSON
        │
        ▼
Next.js Frontend renders table

Tech Stack

Layer Tool
Frontend Next.js + Tailwind CSS
Frontend Hosting Vercel
Backend FastAPI
Backend Hosting Render
NL to SQL LangChain SQLDatabaseChain
LLM Groq (llama-3.1-8b-instant)
Database SQLite
Containerisation Docker

Database Schema

Hospital data warehouse with 5 tables:

dim_patient      — patient demographics (1,751 records)
dim_staff        — staff records (262 members)
dim_dept         — hospital departments (10 departments)
dim_bed          — bed assignments
fact_treatment   — treatment records (cost, LOS, rating, feedback)

Example Queries

"Show total treatment cost by department"
"How many patients are in each status category?"
"What is the average rating per department?"
"List top 5 patients by length of stay"

Project Structure

mediquery/
├── core/
│   └── chain.py          ← LangChain NL-to-SQL chain + DB stats
├── api/
│   └── routes.py         ← FastAPI route handlers
├── frontend/             ← Next.js app (deployed on Vercel)
│   ├── app/
│   │   ├── page.tsx      ← main page
│   │   └── layout.tsx    ← root layout
│   └── components/
├── main.py               ← FastAPI entry point
├── hospital_warehouse.db ← SQLite hospital database
├── Dockerfile
├── requirements.txt
└── .env.example

Run Locally

Prerequisites

Backend

git clone https://github.com/rohitsahayy/mediquery.git
cd mediquery

cp .env.example .env
# add GROQ_API_KEY to .env

pip install -r requirements.txt
uvicorn main:app --reload
# API running at http://localhost:8000
# Docs at http://localhost:8000/docs

Frontend

cd frontend
npm install

echo "NEXT_PUBLIC_API_URL=http://localhost:8000" > .env.local

npm run dev
# UI running at http://localhost:3000

Docker (backend only)

docker build -t mediquery .
docker run -p 8000:8000 --env-file .env mediquery

API

POST /api/query

// Request
{ "question": "Show total treatment cost by department" }

// Response
{
  "sql": "SELECT dd.Dept_Name, SUM(ft.Treatment_Cost) ...",
  "columns": ["Dept_Name", "Total_Cost"],
  "rows": [["Cardiology", 245000.0], ["Neurology", 198000.0]],
  "row_count": 10
}

GET /api/stats

{
  "total_patients": 1751,
  "departments": 10,
  "staff": 262,
  "treatment_records": 1751
}

GET /api/examples

{
  "examples": [
    "Show total treatment cost by department",
    "List top 5 patients by length of stay",
    "How many patients are in each status category?",
    "What is the average rating per department?"
  ]
}

Full interactive docs: https://mediquery-api-uu2g.onrender.com/docs


Environment Variables

Backend .env:

Variable Description
GROQ_API_KEY Groq API key for LLM inference

Frontend .env.local:

Variable Description
NEXT_PUBLIC_API_URL Backend API base URL

Deployment

Service Platform URL
Frontend Vercel https://mediquery.vercel.app
Backend Render https://mediquery-api-uu2g.onrender.com

Auto-deploys on every push to main.

Note: Backend is on Render free tier — first request after inactivity may take 30–50 seconds to wake up.

About

A hospital analytics system that preprocesses structured healthcare data from multiple sources, loads it into a relational database with enforced constraints, and enables insights through a Natural Language Query (NLQ) interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages