Skip to content
kunalsangaPublic

About

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

21 Commits

Folders and files

Repository files navigation

Kepler AI πŸš€

A local-first, full-stack AI chat application β€” combining real-time LLM conversations with on-device AI image and video generation, all in a sleek ChatGPT-like interface.

Kepler AI runs entirely on your own hardware. No cloud subscriptions. No data leaving your machine. Just a powerful, privacy-first AI assistant powered by Ollama, ComfyUI, and Next.js 14.


✨ Features

Feature Description
πŸ’¬ Real-time Streaming Chat Token-by-token streaming responses from local LLMs via Server-Sent Events (SSE)
🎨 AI Image Generation Text-to-image powered by ComfyUI + SD Turbo (Stable Diffusion)
πŸŽ₯ AI Video Generation Text-to-video powered by ComfyUI + AnimateDiff
🧠 Intent Detection Auto-detects when you ask for images/videos β€” no special commands needed
πŸ”„ Live Job Polling Non-blocking background generation with real-time status updates
🌐 Remote Access Cloudflare Tunnel support β€” share your local AI from anywhere
☁️ Vercel Deployable Frontend deploys to Vercel; backend exposed via tunnel or cloud GPU
πŸ“± Fully Responsive Mobile-friendly chat UI with sidebar navigation
πŸŒ™ Dark Mode First-class dark mode support throughout the interface
✍️ Markdown + Code Highlighting Renders rich markdown with syntax-highlighted code blocks via Shiki

πŸ—οΈ System Architecture

Kepler is built as a microservice-style local stack. The frontend never calls AI services directly β€” everything routes through the AI Gateway.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     SSE / REST     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚             β”‚ ─────────────────► β”‚   Next.js 14 Frontend    β”‚
β”‚    User     β”‚                    β”‚   (Vercel / localhost)   β”‚
β”‚  (Browser)  β”‚ ◄───────────────── β”‚   App Router + TypeScriptβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     Streamed UI    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚ REST / SSE Proxy
                                              β–Ό
                                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                   β”‚    AI Gateway (Python)   β”‚
                                   β”‚    FastAPI @ port 9000   β”‚
                                   β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
                                   β”‚  β”‚ /chat  β†’ Ollama      β”‚β”‚
                                   β”‚  β”‚ /image β†’ ComfyUI     β”‚β”‚
                                   β”‚  β”‚ /video β†’ ComfyUI     β”‚β”‚
                                   β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
                                   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚          β”‚
                           SSE Stream   β”‚          β”‚  HTTP + WebSocket
                                        β–Ό          β–Ό
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚   Ollama    β”‚  β”‚   ComfyUI    β”‚
                              β”‚ qwen2.5 LLM β”‚  β”‚  SD Turbo /  β”‚
                              β”‚ port 11434  β”‚  β”‚  AnimateDiff β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  port 8188   β”‚
                                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Service Map

Service Technology Port Purpose
Frontend Next.js 14 (TypeScript) 3000 Chat UI, media rendering, polling
AI Gateway FastAPI (Python) 9000 Central API proxy & job orchestrator
LLM Engine Ollama (qwen2.5) 11434 Text generation & intent detection
Image/Video Engine ComfyUI 8188 Stable Diffusion & AnimateDiff inference

πŸ› οΈ Tech Stack

Frontend

Tech Version Purpose
Next.js ^14.2 App Router, SSR, API Routes
React ^18.3 UI component system
TypeScript ^5.5 Type-safe frontend
Tailwind CSS ^3.4 Utility-first styling
Shadcn UI / Radix UI Latest Accessible UI primitives
Lucide React ^0.400 Icon library
react-markdown ^9.0 Markdown rendering
Shiki ^1.0 Syntax highlighting for code blocks
remark-gfm ^4.0 GitHub Flavored Markdown support

Backend β€” AI Gateway

Tech Version Purpose
FastAPI >=0.111 Async Python web framework
Uvicorn >=0.30 ASGI server
httpx >=0.27 Async HTTP client for service calls
Pydantic >=2.7 Request/response validation

Backend β€” Generation Service (Legacy Direct)

Tech Version Purpose
FastAPI 0.109.2 Direct generation API
Websockets 12.0 ComfyUI WebSocket protocol
websocket-client 1.7.0 Synchronous WS connection
Requests 2.31.0 HTTP calls to Ollama

Local AI Services

Service Purpose
Ollama Runs local LLMs β€” default model: qwen2.5:latest
ComfyUI Node-graph AI inference engine
SD Turbo (sd_turbo.safetensors) Fast Stable Diffusion model for image generation
AnimateDiff (mm_sd_v15_v2.ckpt) Motion module for video/animation generation

Deployment

Tool Purpose
Vercel Frontend deployment (serverless Next.js)
Cloudflare Tunnel Expose local GPU services to the internet (free)
Windows Batch Scripts One-click local startup orchestration

πŸ“‚ Project Structure

Kepler/
β”œβ”€β”€ app/                          # Next.js 14 App Router
β”‚   β”œβ”€β”€ api/                      # Server-side API Routes (proxies to Gateway)
β”‚   β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   β”‚   └── route.ts          # SSE streaming proxy β†’ Gateway /chat
β”‚   β”‚   β”œβ”€β”€ image/
β”‚   β”‚   β”‚   β”œβ”€β”€ route.ts          # Trigger image generation β†’ Gateway /image
β”‚   β”‚   β”‚   └── status/[jobId]/
β”‚   β”‚   β”‚       └── route.ts      # Poll image job status
β”‚   β”‚   β”œβ”€β”€ video/
β”‚   β”‚   β”‚   β”œβ”€β”€ route.ts          # Trigger video generation β†’ Gateway /video
β”‚   β”‚   β”‚   └── status/[jobId]/
β”‚   β”‚   β”‚       └── route.ts      # Poll video job status
β”‚   β”‚   └── generated/            # (Static generated media serving)
β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   └── page.tsx              # Main chat UI page
β”‚   β”œβ”€β”€ globals.css               # Global styles + dark mode variables
β”‚   └── layout.tsx                # Root layout
β”‚
β”œβ”€β”€ components/                   # Reusable React components
β”‚   β”œβ”€β”€ ChatMessage.tsx           # Individual chat bubble (text/image/video)
β”‚   β”œβ”€β”€ ChatList.tsx              # Scrollable message list
β”‚   β”œβ”€β”€ ChatInput.tsx             # Text input with send button
β”‚   β”œβ”€β”€ MediaDisplay.tsx          # Image/video render with loading state
β”‚   β”œβ”€β”€ Sidebar.tsx               # Navigation sidebar
β”‚   β”œβ”€β”€ CodeBlock.tsx             # Syntax-highlighted code block
β”‚   └── ui/                       # Shadcn UI primitives
β”‚
β”œβ”€β”€ lib/                          # Core frontend utilities & clients
β”‚   β”œβ”€β”€ llm.ts                    # SSE streaming client, message types
β”‚   β”œβ”€β”€ generation.ts             # Intent parser, image/video trigger hooks
β”‚   β”œβ”€β”€ job-store.ts              # In-memory job tracking (client side)
β”‚   β”œβ”€β”€ comfyui.ts                # ComfyUI workflow definitions & API
β”‚   β”œβ”€β”€ cogvideo.ts               # CogVideo workflow integration
β”‚   └── system_prompt.ts          # System prompt configuration
β”‚
β”œβ”€β”€ gateway/                      # AI Gateway β€” FastAPI microservice
β”‚   β”œβ”€β”€ main.py                   # FastAPI app, route registration (port 9000)
β”‚   β”œβ”€β”€ utils.py                  # Job store, health checks
β”‚   β”œβ”€β”€ requirements.txt          # Python dependencies
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ chat.py               # POST /chat β€” SSE proxy to Ollama
β”‚   β”‚   β”œβ”€β”€ image.py              # POST /image + GET /image/status/{jobId}
β”‚   β”‚   └── video.py              # POST /video + GET /video/status/{jobId}
β”‚   └── services/
β”‚       β”œβ”€β”€ ollama_service.py     # Async SSE streaming from Ollama
β”‚       β”œβ”€β”€ comfy_service.py      # ComfyUI job trigger, poll, base64 fetch
β”‚       └── routing_service.py    # Optional intent-based routing logic
β”‚
β”œβ”€β”€ backend/                      # Legacy direct backend (FastAPI, port 8000)
β”‚   β”œβ”€β”€ main.py                   # Synchronous image/video generation endpoints
β”‚   β”œβ”€β”€ utils.py                  # WebSocket connection to ComfyUI
β”‚   β”œβ”€β”€ requirements.txt          # Python dependencies
β”‚   └── start_services.py         # Service manager
β”‚
β”œβ”€β”€ ComfyUI/                      # Local ComfyUI installation (git submodule / clone)
β”‚
β”œβ”€β”€ docs/                         # Documentation assets
β”‚
β”œβ”€β”€ START_ALL_SERVICES.bat        # πŸš€ One-click: starts all 4 services
β”œβ”€β”€ START_GATEWAY.bat             # Starts the AI Gateway only
β”œβ”€β”€ START_GENERATION_SERVICES.bat # Starts ComfyUI (and optionally backend)
β”œβ”€β”€ START_TUNNEL.bat              # Starts Cloudflare Tunnel for remote access
β”‚
β”œβ”€β”€ .env.local                    # Local environment configuration
β”œβ”€β”€ next.config.js                # Next.js config
β”œβ”€β”€ tailwind.config.js            # Tailwind theme config
β”œβ”€β”€ tsconfig.json                 # TypeScript config
└── package.json                  # Node.js dependencies

βš™οΈ How It Works

πŸ—¨οΈ Text Chat Flow

  1. User types a message in ChatInput.tsx
  2. lib/generation.ts::parseGenerationCommand() checks if it's an image/video request
  3. If it's plain text β†’ lib/llm.ts::streamLLMResponse() POSTs to /api/chat
  4. Next.js API route proxies the request to the AI Gateway (http://127.0.0.1:9000/chat)
  5. Gateway streams the response from Ollama as Server-Sent Events (SSE)
  6. Frontend decodes the token stream and progressively updates the message bubble
  7. If the LLM's response itself contains an image/video intent, generation is auto-triggered

🎨 Image / Video Generation Flow

User: "generate an image of a sunset"
        β”‚
        β–Ό
parseGenerationCommand() β†’ { type: 'image', prompt: 'a sunset' }
        β”‚
        β–Ό
POST /api/image  β†’  Gateway POST /image
        β”‚               β”‚
        β”‚               β–Ό
        β”‚       trigger_image_job()
        β”‚           - Builds SD Turbo ComfyUI workflow JSON
        β”‚           - POST to ComfyUI /prompt
        β”‚           - Returns { job_id }
        β”‚           - Starts background async poll_comfyui()
        β”‚
        β–Ό
Frontend gets { jobId }
Adds "Generating image..." placeholder message (MediaDisplay.tsx)
        β”‚
        β–Ό  every 2 seconds
GET /api/image/status/[jobId]  β†’  Gateway GET /image/status/{jobId}
        β”‚
        β–Ό (when completed)
ComfyUI output fetched as raw bytes
        β†’ base64 encoded Data URL (data:image/png;base64,...)
        β†’ stored in job store
        β”‚
        β–Ό
Frontend replaces placeholder with <img> rendered from base64 data URL

The base64 Data URL approach ensures cross-origin compatibility β€” works both locally and when the gateway is behind a Cloudflare tunnel.


πŸ”§ Prerequisites

  • Node.js 18+ and npm
  • Python 3.10+
  • Ollama installed and running
  • Ollama model pulled (default: qwen2.5)
    ollama pull qwen2.5
  • ComfyUI installed in ./ComfyUI
  • ComfyUI models placed in ComfyUI/models/:
    • checkpoints/sd_turbo.safetensors β€” for image generation
    • animatediff_models/mm_sd_v15_v2.ckpt β€” for video generation

πŸš€ Setup & Running

1. Install Node.js dependencies

npm install

2. Install Gateway dependencies

cd gateway
pip install -r requirements.txt
cd ..

3. Configure environment variables

Create .env.local in the project root:

# Local development defaults
AI_GATEWAY_URL=http://127.0.0.1:9000
LOCAL_LLM_URL=http://127.0.0.1:11434
COMFYUI_URL=http://127.0.0.1:8188

# For Vercel + Cloudflare Tunnel (production):
# AI_GATEWAY_URL=https://your-tunnel-id.trycloudflare.com

4. Start all services

Option A β€” One-click (Recommended for Windows):

START_ALL_SERVICES.bat

This script automatically:

  1. Checks if Ollama is running (starts it if not)
  2. Starts image & video generation services (ComfyUI)
  3. Starts the AI Gateway (port 9000)
  4. Starts Next.js dev server (port 3000)

Option B β€” Manual startup:

# Terminal 1: Start Ollama
ollama serve

# Terminal 2: Start ComfyUI
cd ComfyUI && python main.py

# Terminal 3: Start AI Gateway
cd gateway && uvicorn main:app --host 0.0.0.0 --port 9000 --reload

# Terminal 4: Start Next.js
npm run dev

Open http://localhost:3000 in your browser.


🌐 Deployment

Option 1: Vercel + Cloudflare Tunnel (Free β€” Recommended)

This is the recommended approach if you want to access Kepler from anywhere while keeping all AI inference on your local GPU.

Architecture:

Browser β†’ Vercel (Next.js frontend) β†’ Cloudflare Tunnel β†’ Your Local Machine (Gateway + Ollama + ComfyUI)

Steps:

Step 1: Install cloudflared

winget install --id Cloudflare.cloudflared

Step 2: Start your local services

START_ALL_SERVICES.bat

Step 3: Start the Cloudflare Tunnel (exposes Gateway port 9000)

START_TUNNEL.bat

Copy the generated URL (e.g., https://xxxx.trycloudflare.com).

Step 4: Deploy the frontend to Vercel

npx vercel --prod

Step 5: Set environment variable on Vercel In your Vercel dashboard β†’ Project Settings β†’ Environment Variables:

AI_GATEWAY_URL = https://xxxx.trycloudflare.com

Notes:

  • The Cloudflare Tunnel URL changes each time you restart START_TUNNEL.bat. For a permanent URL, use a named tunnel with a cloudflared config file.
  • All inference stays on your hardware β€” Vercel only serves the UI
  • Free tier on both Vercel and Cloudflare works perfectly for personal use

Option 2: Cloud GPU (Production / Always-On)

For 24/7 availability without keeping your local machine running:

  1. Spin up a GPU instance (e.g., RunPod, Vast.ai, Lambda Labs, AWS g4dn)
  2. Install Ollama + ComfyUI + the Gateway on the cloud instance
  3. Set AI_GATEWAY_URL in Vercel to point to your cloud instance's public IP/domain
  4. (Optional) Put NGINX in front of the Gateway as a reverse proxy with SSL

πŸŽ›οΈ Customization

Change the LLM Model

Edit gateway/services/ollama_service.py:

"model": "llama3.2",  # or mistral, gemma2, phi4, etc.

Then pull the model:

ollama pull llama3.2

Change Image Resolution

The default is 512Γ—512 (optimized for SD Turbo speed). Edit gateway/services/comfy_service.py:

def create_image_workflow(prompt: str, width: int = 768, height: int = 768):

Change the Image Model

In comfy_service.py, update the checkpoint name:

"4": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "your_model.safetensors"}},

Place the model in ComfyUI/models/checkpoints/.

Add a System Prompt

Edit lib/system_prompt.ts to give the AI a persona or set of instructions.

Theming

Modify app/globals.css for CSS custom properties and tailwind.config.js for the design token system.


πŸ”‘ Environment Variables Reference

Variable Default Description
AI_GATEWAY_URL http://127.0.0.1:9000 URL of the AI Gateway service
LOCAL_LLM_URL http://127.0.0.1:11434 Ollama API base URL
COMFYUI_URL http://127.0.0.1:8188 ComfyUI server URL

πŸ—ΊοΈ Roadmap

  • Persistent chat history (local SQLite or IndexedDB)
  • Multiple model selection UI
  • Image-to-image and inpainting support
  • File/document upload for RAG-style Q&A
  • Named Cloudflare Tunnel for a persistent public URL
  • Docker Compose setup for one-command deployment

πŸ“‹ License

MIT β€” Do whatever you want. Ship it, fork it, hack it.


Built with ❀️ using Ollama, ComfyUI, Next.js, and FastAPI.

About

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages