Upload PDFs. Ask questions. Get AI-generated answers grounded in your documents.
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.
| 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 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 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) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
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
User Question β Check Cache β Embed Query β FAISS Similarity Search
β Retrieve Top-5 Chunks β Send to OpenAI with Context
β Cache & Return Answer
| 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 |
| 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 |
- Python 3.10+
- Node.js 18+
- PostgreSQL database (local or hosted)
- Cloudinary account (sign up free)
- OpenAI API key (get one here)
git clone https://github.com/Maherimtiyaz/AI_Powered_Doc_API.git
cd AI-Powered-Doc-API# 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.txtCopy the example file and fill in your values:
cp .env.example .envEdit .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_indexesuvicorn app.main:app --reload --port 8000The API will be live at http://localhost:8000. Visit http://localhost:8000/docs for interactive Swagger documentation.
cd frontend
npm install
npm run devThe frontend will be live at http://localhost:5173.
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Returns {"message": "API is running"} |
| 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 |
| 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"
}| 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.
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
This project includes a render.yaml Blueprint for one-click deployment.
| 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 |
-
Push to GitHub β Ensure all changes are committed and pushed.
-
Create Blueprint on Render
- Go to Render Dashboard β New β Blueprint
- Connect your GitHub repository
- Render auto-detects
render.yaml
-
Set Manual Environment Variables These variables are marked
sync: falseinrender.yamland must be set manually in the Render dashboard:Variable Where to Get It OPENAI_API_KEYOpenAI API Keys CLOUDINARY_CLOUD_NAMECloudinary Console CLOUDINARY_API_KEYCloudinary Console β Settings CLOUDINARY_API_SECRETCloudinary Console β Settings -
Deploy β Render builds and deploys both services automatically.
These are handled by render.yaml automatically:
DATABASE_URLβ Injected from the managed PostgreSQL instanceSECRET_KEYβ Auto-generated secure random valueALGORITHMβ Set toHS256FRONTEND_URLβ Injected from the static site URLVITE_API_URLβ Injected into the frontend from the backend URLPYTHON_VERSIONβ Set to3.10.0
- 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-transformersmodel loads into memory.
# Activate virtual environment
venv\Scripts\activate # Windows
source venv/bin/activate # macOS / Linux
# Run all tests
PYTHONPATH=. python -m pytest tests/ -vTests use an in-memory SQLite database and mock external services, so no external credentials are needed.
| 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 |
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source and available under the MIT License.
Built with β€οΈ using FastAPI, React, and OpenAI