Skip to content

About

Production-grade AI document platform that enables PDF uploads, semantic retrieval, and conversational Q&A using a scalable RAG pipeline powered by FastAPI, FAISS, Celery, Redis, and React.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

46 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 AI-Powered Document API

Upload PDFs. Ask questions. Get AI-generated answers grounded in your documents.

FastAPI React PostgreSQL OpenAI Render


πŸ“Œ Overview

AI-Powered Document API is a full-stack application that enables users to upload PDF documents, automatically extract and index their content, and then ask natural-language questions that are answered by an AI β€” with responses grounded exclusively in the uploaded document's content.

Key Capabilities

Feature Description
PDF Upload & Storage Upload PDFs that are stored on Cloudinary with metadata persisted in PostgreSQL
Text Extraction Automatic text extraction from uploaded PDFs using pypdf
Semantic Chunking Documents are split into overlapping chunks for optimal retrieval
Vector Embeddings Chunks are embedded using sentence-transformers/all-MiniLM-L6-v2
FAISS Indexing Embeddings are indexed with Facebook AI Similarity Search for fast retrieval
AI Q&A Users ask questions and receive answers generated by OpenAI gpt-4o-mini, grounded in retrieved document chunks
Auth System JWT-based authentication with bcrypt password hashing
Response Caching In-memory cache to avoid redundant AI calls for repeated queries
3D Animated UI Immersive React frontend with Three.js 3D backgrounds and Framer Motion animations

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         React Frontend                             β”‚
β”‚   Landing Page β†’ Login/Register β†’ Dashboard (Upload + Ask AI)      β”‚
β”‚   Three.js 3D Background Β· Framer Motion Animations                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚ HTTPS (REST API)
                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       FastAPI Backend                               β”‚
β”‚                                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚ Auth API β”‚   β”‚ Document API β”‚   β”‚       AI Query API       β”‚     β”‚
β”‚  β”‚ /auth/*  β”‚   β”‚  /docs/*     β”‚   β”‚       /ai/ask            β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β”‚       β”‚                β”‚                       β”‚                    β”‚
β”‚       β–Ό                β–Ό                       β–Ό                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚  β”‚  JWT +  β”‚   β”‚  Cloudinary  β”‚   β”‚  FAISS Vector Search   β”‚       β”‚
β”‚  β”‚ bcrypt  β”‚   β”‚  + pypdf     β”‚   β”‚  + OpenAI gpt-4o-mini  β”‚       β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
β”‚                                                                     β”‚
β”‚              PostgreSQL (Users + Document Metadata)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Document Processing Pipeline

PDF Upload β†’ Save Locally β†’ Upload to Cloudinary β†’ Extract Text (pypdf)
          β†’ Chunk Text (500 chars, 50 overlap) β†’ Generate Embeddings
          β†’ Store in FAISS Index β†’ Save Metadata to PostgreSQL

AI Query Pipeline

User Question β†’ Check Cache β†’ Embed Query β†’ FAISS Similarity Search
             β†’ Retrieve Top-5 Chunks β†’ Send to OpenAI with Context
             β†’ Cache & Return Answer

πŸ› οΈ Tech Stack

Backend

Technology Purpose
FastAPI High-performance async web framework
SQLAlchemy ORM for PostgreSQL
psycopg2-binary PostgreSQL adapter
python-jose JWT token encoding/decoding
passlib + bcrypt Secure password hashing
pypdf PDF text extraction
sentence-transformers Text embeddings (all-MiniLM-L6-v2)
FAISS (faiss-cpu) Vector similarity search
OpenAI Python SDK GPT-4o-mini for answer generation
Cloudinary Cloud-based file storage for uploaded PDFs
python-dotenv Environment variable management

Frontend

Technology Purpose
React 19 UI library
Vite Build tool and dev server
React Router v7 Client-side routing
Framer Motion Page transitions and micro-animations
Three.js + @react-three/fiber 3D animated backgrounds
Lucide React Icon library
Axios HTTP client

πŸš€ Getting Started

Prerequisites

  • Python 3.10+
  • Node.js 18+
  • PostgreSQL database (local or hosted)
  • Cloudinary account (sign up free)
  • OpenAI API key (get one here)

1. Clone the Repository

git clone https://github.com/Maherimtiyaz/AI_Powered_Doc_API.git
cd AI-Powered-Doc-API

2. Backend Setup

# Create and activate virtual environment
python -m venv venv

# Windows
venv\Scripts\activate

# macOS / Linux
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

3. Configure Environment Variables

Copy the example file and fill in your values:

cp .env.example .env

Edit .env with your credentials:

DATABASE_URL=postgresql://user:password@localhost:5432/doc_ai_db
SECRET_KEY=your-super-secret-key
ALGORITHM=HS256

OPENAI_API_KEY=sk-...your-openai-key

CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret

FRONTEND_URL=http://localhost:5173
FAISS_BASE_PATH=faiss_indexes

4. Run the Backend

uvicorn app.main:app --reload --port 8000

The API will be live at http://localhost:8000. Visit http://localhost:8000/docs for interactive Swagger documentation.

5. Frontend Setup

cd frontend
npm install
npm run dev

The frontend will be live at http://localhost:5173.


πŸ“‘ API Reference

Health Check

Method Endpoint Description
GET / Returns {"message": "API is running"}

Authentication β€” /auth

Method Endpoint Parameters Description
POST /auth/register email, password (query) Register a new user
POST /auth/login email, password (query) Login and receive a JWT access_token

Documents β€” /docs

Method Endpoint Headers Body Description
POST /docs/upload token (query param) file (multipart PDF) Upload and process a PDF document

Response:

{
  "file_id": "uuid-string",
  "filename": "report.pdf",
  "cloudinary_url": "https://res.cloudinary.com/...",
  "chunks": 42,
  "message": "Document processed successfully"
}

AI Query β€” /ai

Method Endpoint Body (JSON) Description
POST /ai/ask {"query": "...", "file_id": "..."} Ask a question about a specific document

Response:

{
  "answer": "Based on the document, ...",
  "chunks_used": 5,
  "source": "generated"
}

Cached responses return "source": "cache" instead.


πŸ“‚ Project Structure

AI-Powered-Doc-API/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                  # FastAPI entry point, CORS, route registration
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ auth_routes.py       # /auth/register, /auth/login
β”‚   β”‚   β”œβ”€β”€ document_auth.py     # /docs/upload (authenticated)
β”‚   β”‚   β”œβ”€β”€ ai_routes.py         # /ai/ask
β”‚   β”‚   └── deps.py              # Shared dependencies (get_db, get_current_user)
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py            # Environment variable loading
β”‚   β”‚   β”œβ”€β”€ database.py          # SQLAlchemy engine, session, Base
β”‚   β”‚   β”œβ”€β”€ security.py          # JWT creation, password hashing
β”‚   β”‚   β”œβ”€β”€ cache.py             # In-memory query cache
β”‚   β”‚   └── rate_limiter.py      # Rate limiting (placeholder)
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.py              # User SQLAlchemy model
β”‚   β”‚   └── document.py          # Document SQLAlchemy model
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”œβ”€β”€ user_schema.py       # Pydantic schemas for auth
β”‚   β”‚   └── document_schema.py   # Pydantic schemas for documents
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ auth_service.py      # Registration & login business logic
β”‚   β”‚   β”œβ”€β”€ ai_service.py        # RAG pipeline: retrieve chunks β†’ generate answer
β”‚   β”‚   └── document_service.py  # Full document processing pipeline
β”‚   └── utils/
β”‚       β”œβ”€β”€ chunking.py          # Text chunking with overlap
β”‚       β”œβ”€β”€ embeddings.py        # Sentence-transformer embeddings
β”‚       β”œβ”€β”€ faiss_store.py       # FAISS index read/write
β”‚       └── cloudinary_helper.py # Cloudinary upload helper
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ conftest.py              # Test fixtures (SQLite test DB, test client)
β”‚   β”œβ”€β”€ test_auth.py             # Auth endpoint tests
β”‚   β”œβ”€β”€ test_docs.py             # Document upload tests
β”‚   └── test_ai.py               # AI query tests
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx              # Router, AnimatePresence, 3D scene
β”‚   β”‚   β”œβ”€β”€ main.jsx             # React entry point
β”‚   β”‚   β”œβ”€β”€ index.css            # Global styles & design system
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   └── Scene3D.jsx      # Three.js animated 3D background
β”‚   β”‚   └── pages/
β”‚   β”‚       β”œβ”€β”€ Landing.jsx      # Hero landing page
β”‚   β”‚       β”œβ”€β”€ Login.jsx        # Login / Register form
β”‚   β”‚       └── Dashboard.jsx    # Upload PDFs + Ask AI interface
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.js
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ Dockerfile               # Docker image definition
β”‚   └── docker-compose.yml       # Docker Compose configuration
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml               # GitHub Actions CI pipeline
β”œβ”€β”€ .env.example                 # Template for environment variables
β”œβ”€β”€ .gitignore
β”œβ”€β”€ render.yaml                  # Render Blueprint (backend + frontend + DB)
β”œβ”€β”€ requirements.txt             # Pinned Python dependencies
└── README.md

☁️ Deployment on Render

This project includes a render.yaml Blueprint for one-click deployment.

Render Services Created

Service Type Runtime Description
doc-ai-db PostgreSQL β€” Free-tier managed database
doc-ai-backend Web Service Python FastAPI backend on port 10000
doc-ai-frontend Static Site Node Vite-built React SPA

Steps

  1. Push to GitHub β€” Ensure all changes are committed and pushed.

  2. Create Blueprint on Render

    • Go to Render Dashboard β†’ New β†’ Blueprint
    • Connect your GitHub repository
    • Render auto-detects render.yaml
  3. Set Manual Environment Variables These variables are marked sync: false in render.yaml and must be set manually in the Render dashboard:

    Variable Where to Get It
    OPENAI_API_KEY OpenAI API Keys
    CLOUDINARY_CLOUD_NAME Cloudinary Console
    CLOUDINARY_API_KEY Cloudinary Console β†’ Settings
    CLOUDINARY_API_SECRET Cloudinary Console β†’ Settings
  4. Deploy β€” Render builds and deploys both services automatically.

Auto-Configured Variables

These are handled by render.yaml automatically:

  • DATABASE_URL β€” Injected from the managed PostgreSQL instance
  • SECRET_KEY β€” Auto-generated secure random value
  • ALGORITHM β€” Set to HS256
  • FRONTEND_URL β€” Injected from the static site URL
  • VITE_API_URL β€” Injected into the frontend from the backend URL
  • PYTHON_VERSION β€” Set to 3.10.0

⚠️ Important Notes

  • Ephemeral Filesystem: Render's free tier has an ephemeral disk. FAISS indexes stored locally will be lost on every deploy or restart. For persistent vector storage, consider upgrading to a paid tier with a persistent disk or migrating to a managed vector database (e.g., Pinecone, Weaviate).
  • Cold Starts: Free-tier web services spin down after 15 minutes of inactivity. The first request after a cold start may take 30–60 seconds as the sentence-transformers model loads into memory.

πŸ§ͺ Running Tests

# Activate virtual environment
venv\Scripts\activate  # Windows
source venv/bin/activate  # macOS / Linux

# Run all tests
PYTHONPATH=. python -m pytest tests/ -v

Tests use an in-memory SQLite database and mock external services, so no external credentials are needed.


πŸ”§ Environment Variables Reference

Variable Required Description Default
DATABASE_URL βœ… PostgreSQL connection string β€”
SECRET_KEY βœ… JWT signing secret change-me-in-production
ALGORITHM βœ… JWT algorithm HS256
OPENAI_API_KEY βœ… OpenAI API key for answer generation β€”
CLOUDINARY_CLOUD_NAME βœ… Cloudinary cloud name β€”
CLOUDINARY_API_KEY βœ… Cloudinary API key β€”
CLOUDINARY_API_SECRET βœ… Cloudinary API secret β€”
FRONTEND_URL ⬜ Frontend URL for CORS ""
FAISS_BASE_PATH ⬜ Directory for FAISS indexes faiss_indexes

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“œ License

This project is open source and available under the MIT License.


Built with ❀️ using FastAPI, React, and OpenAI

About

Production-grade AI document platform that enables PDF uploads, semantic retrieval, and conversational Q&A using a scalable RAG pipeline powered by FastAPI, FAISS, Celery, Redis, and React.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages