# =========================
# Project Structure
# =========================
ai-aggregator/
├─ backend/
│ ├─ main.py
│ ├─ models.py
│ ├─ auth.py
│ ├─ ai_router.py
│ ├─ payment.py
│ ├─ daily_reset.py
│ ├─ requirements.txt
│ └─ Dockerfile
├─ ai_worker/
│ ├─ ai_worker.py
│ ├─ requirements.txt
│ └─ Dockerfile
├─ frontend/
│ ├─ package.json
│ └─ src/
│ ├─ App.js
│ ├─ Login.js
│ ├─ index.js
│ └─ index.css
├─ docker-compose.yml
├─ k8s/
│ ├─ backend-deploy.yaml
│ ├─ worker-deploy.yaml
│ ├─ frontend-deploy.yaml
│ ├─ redis-deploy.yaml
│ ├─ postgres-deploy.yaml
│ └─ reset-cron.yaml
├─ .env.example
└─ deploy.sh
# =========================
# backend/main.py
# =========================
from fastapi import FastAPI, HTTPException
from ai_router import route
from models import User, SessionLocal
from payment import create_checkout_session
app = FastAPI()
@app.post("/chat")
async def chat(message: str, user_email: str):
db = SessionLocal()
user = db.query(User).filter(User.email == user_email).first()
if not user:
raise HTTPException(status_code=404, detail="User not found")
responses = await route(message, user)
return {"responses": responses}
@app.post("/pay")
def pay(user_email: str):
url = create_checkout_session(user_email)
return {"checkout_url": url}
# =========================
# backend/models.py
# =========================
from sqlalchemy import Column, Integer, String, Boolean
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
email = Column(String, unique=True)
hashed_password = Column(String)
plan = Column(String, default="free")
daily_tokens = Column(Integer, default=0)
# =========================
# backend/ai_router.py
# =========================
import asyncio
async def route(message, user):
user.daily_tokens += len(message.split())
return [{"model": "demo", "response": f"Echo: {message}"}]
# =========================
# ai_worker/ai_worker.py
# =========================
def ask_model(name, message):
return f"[{name}] response to: {message}"
if __name__ == "__main__":
print("AI Worker ready")
# =========================
# frontend/src/App.js
# =========================
import React, { useState } from "react";
import axios from "axios";
function App() {
const [msg, setMsg] = useState("");
const [res, setRes] = useState("");
const send = async () => {
const r = await axios.post("http://localhost:8000/chat", {
message: msg,
user_email: "demo@demo.com",
});
setRes(JSON.stringify(r.data.responses));
};
return (
);
}
export default App;
# =========================
# Quick Deploy Instructions
# =========================
1. Copy this entire block into your local folder structure as shown above.
2. Fill .env.example with:
SECRET_KEY, STRIPE_SECRET_KEY, DATABASE_URL, REDIS_HOST
3. Build and run locally:
docker-compose build
docker-compose up
4. Deploy to Cloud/Kubernetes:
chmod +x deploy.sh
./deploy.sh
Comments
Post a Comment