Local AI with Ollama + Python Web Apps 2026
Run powerful LLMs completely offline on your own hardware and expose them as secure web apps/APIs using FastAPI or Flask.
Why Run AI Locally in 2026?
- Zero cost after hardware (no per-token API fees)
- 100% privacy – data never leaves your machine
- No rate limits or censorship
- Offline access (air-gapped, remote sites, travel)
- 2026 reality: Llama 3.1/3.2 8B/70B runs fast on RTX 4090 / M3 Max / Ryzen AI
Prerequisites
- Python 3.11+ (3.14 recommended)
- Ollama installed – https://ollama.com/download
- GPU: NVIDIA (≥12 GB VRAM) or Apple M-series (≥16 GB unified memory)
- Models: Llama 3.1 8B / Mistral Nemo / Phi-3.5 / Gemma 2
1. Install & Run Ollama Locally
# Install Ollama (one-time)
# Go to https://ollama.com/download → install for your OS
# Start Ollama server in terminal
ollama serve
# In another terminal – pull a model (8B is good balance)
ollama pull llama3.1:8b
# Test it
ollama run llama3.1:8b
>>> Hello! How can I help you today?
2. Expose Ollama as FastAPI API
# app/main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain_ollama import ChatOllama
import os
app = FastAPI(title="Local Ollama API 2026")
llm = ChatOllama(model="llama3.1:8b", base_url="http://localhost:11434")
class ChatRequest(BaseModel):
prompt: str
system: str = "You are a helpful assistant."
@app.post("/chat")
async def chat(request: ChatRequest):
try:
response = llm.invoke(
request.prompt,
system=request.system
)
return {"response": response.content}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Run: uvicorn main:app --reload
3. Alternative: Expose via Flask (Simpler)
# app.py (Flask version)
from flask import Flask, request, jsonify
from langchain_ollama import ChatOllama
app = Flask(__name__)
llm = ChatOllama(model="llama3.1:8b")
@app.route("/chat", methods=["POST"])
def chat():
data = request.json
prompt = data.get("prompt")
system = data.get("system", "You are a helpful assistant.")
if not prompt:
return jsonify({"error": "No prompt provided"}), 400
try:
response = llm.invoke(prompt, system=system)
return jsonify({"response": response.content})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True, port=5000)
4. Simple Web Chat Frontend
Local AI Chat
Local Ollama Chat
5. Advanced Tips & Deployment 2026
- Use LangChain + Ollama for RAG, agents, memory
- Quantize models: 4-bit/8-bit for 70B on 24 GB GPU
- Run multiple models: Ollama + Open WebUI (nice UI)
- Secure API: Add API keys, HTTPS, rate limiting
- Deploy: Docker + Railway/Fly.io/Render (free tier)