AI First-Aid Assistant

A Full-Stack RAG Application with a Multi-Stage Safety Pipeline

Main image for AI First-Aid Assistant

Problem Statement

In high-stress emergency situations, individuals require immediate, clear, and reliable first-aid guidance without friction. A critical challenge was to build an AI system capable of providing accurate, step-by-step instructions while also recognizing and appropriately escalating life-threatening scenarios.

Traditional Large Language Models (LLMs) can suffer from hallucinations or provide overly generic advice, which is highly problematic in a medical context. The objective was to engineer a robust system that prioritizes accuracy and user safety, ensuring professional standards for first-aid recommendations.

Furthermore, integrating a fast and reliable way to locate nearby medical facilities was essential, coupled with a seamless user experience that includes persistent chat history and secure user authentication.

Methodology & Approach

Backend Architecture (Python/FastAPI on Kaggle GPU): I designed and implemented a sophisticated, multi-stage AI pipeline using Python with FastAPI, hosted on Kaggle to leverage GPU acceleration via an ngrok tunnel for public access. This advanced agent processes user queries through three distinct stages:

1. The Gatekeeper (Intent Classification): Utilizing a zero-shot classification model (facebook/bart-large-mnli), this stage rigorously assesses every incoming message to determine if it's a medical query, a simple greeting, or off-topic. This prevents irrelevant responses and conserves resources by only engaging the main AI for relevant medical discussions.

2. The Brain (RAG and Response Generation): For medical queries, a Retrieval-Augmented Generation (RAG) system is activated. The user's input is converted into a vector embedding using the Qwen/Qwen3-Embedding-0.6B model. This embedding is then used to perform a rapid similarity search against a FAISS vector index (built from a comprehensive first-aid knowledge base) to retrieve highly relevant contextual information. This context, along with recent chat history, forms a detailed prompt for the microsoft/MediPhi-Clinical LLM (loaded in 4-bit for efficiency) to generate accurate, step-by-step guidance.

3. The Safety Analyst (Seriousness Classification): Crucially, after the primary AI generates its response, a secondary check is performed using the same facebook/bart-large-mnli classifier. This stage analyzes the AI's own output to detect indicators of an 'urgent medical emergency.' If detected, a show_hospital_modal flag is set to True in the JSON response, triggering an immediate and prominent call-to-action on the frontend.

Integrated Hospital Finder (PostGIS & Supabase): A dedicated 'Hospital Finder' feature provides rapid access to medical facilities. It leverages Supabase PostgreSQL with the PostGIS extension to perform highly accurate geospatial queries. A custom SQL function utilizes ST_Distance to calculate real-world distances between the user's geolocation and nearby hospitals, which are displayed and sortable by distance. The hospital data was seeded via a custom Node.js script and papaparse from a CSV file.

Frontend & User Experience (Next.js/TypeScript): The application's responsive UI is built with Next.js 15 (App Router) and TypeScript, styled with Material-UI (MUI) for core components and Tailwind CSS for fine-grained control. Markdown rendering for AI responses is handled by @tailwindcss/typography, ensuring clear formatting. Chat state is managed persistently via URL parameters, allowing conversations to resume seamlessly across page refreshes.

Authentication & Security: User accounts are managed by Supabase Auth, supporting both email/password and Google social logins. Row Level Security (RLS) is rigorously enforced on the database, ensuring users can only access their own conversations and data, highlighting a strong focus on data privacy and security.

Results & Insights

The deployed application successfully delivers immediate, context-aware, and accurately formatted first-aid guidance. The multi-stage AI pipeline proved effective in filtering non-medical queries, grounding responses in factual knowledge via RAG, and critically, acting as a robust Responsible AI safety layer by proactively identifying and escalating emergency scenarios.

A key outcome is the system's ability to intuitively guide users towards emergency medical services when facing critical conditions (e.g., 'chest pain and difficulty breathing'), while providing detailed, step-by-step advice for minor ailments (e.g., 'how to treat a small cut'). The show_hospital_modal flag demonstrates effective AI-driven UI adaptation.

Significant real-world development challenges were overcome, including fixing React race conditions for persistent chat state, debugging useEffect infinite loops, refining complex AI classification logic, and resolving intricate Vercel deployment issues stemming from Supabase URL misconfigurations. These efforts ensured a stable, responsive, and reliable user experience across the entire application.

Impact & Value

This project serves as a comprehensive proof-of-concept for how modern, production-ready AI systems can be architected with safety, accuracy, and user experience at their core. It demonstrates a complete end-to-end data science and full-stack engineering workflow, transforming a complex medical assistance problem into a genuinely useful and responsible AI application.

The integrated blend of LLMs, RAG, classification models, geospatial databases, and modern web frameworks showcases advanced capabilities in developing resilient and impactful AI solutions, directly applicable to real-world challenges in health tech or related fields.

Tech Stack

Next.jsPythonFastAPIRAGPostgreSQLSupabaseHugging FaceTailwindCSSPostGISTypeScript