brainbox-v1 / backend_app.py
cxjboby's picture
Rename backend_app.py.txt to backend_app.py
8eca2c8 verified
Raw History Blame Contribute Delete
2.56 kB
"""
Brainbox API proxy.
Run with:
pip install fastapi uvicorn openai python-dotenv
export HF_ENDPOINT_URL="https://YOUR-ENDPOINT.endpoints.huggingface.cloud/v1"
export HF_TOKEN="hf_your_secret_token"
uvicorn app:app --host 0.0.0.0 --port 8000
Keep HF_TOKEN on the server only. Never put it in index.html.
"""
import os
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from openai import OpenAI
endpoint = os.environ.get("HF_ENDPOINT_URL", "").rstrip("/")
token = os.environ.get("HF_TOKEN", "")
if not endpoint or not token:
raise RuntimeError("Set HF_ENDPOINT_URL and HF_TOKEN as server environment variables.")
client = OpenAI(base_url=endpoint + "/v1/", api_key=token)
app = FastAPI(title="Brainbox API")
# Replace * with your real website origin before production.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["POST"],
allow_headers=["*"],
)
SYSTEM_PROMPT = (
"You are Brainbox, a friendly and helpful AI assistant. Use simple English "
"and a warm Nigerian conversational style when appropriate. When a clear request "
"asks for a website, return a complete responsive HTML document. Be honest about "
"uncertainty. Never claim a frontend-only login is secure authentication."
)
class ChatRequest(BaseModel):
message: str
history: list[dict] = []
@app.get("/")
def health():
return {"status": "ok", "service": "Brainbox API"}
@app.post("/api/chat")
def chat(body: ChatRequest):
message = body.message.strip()
if not message:
raise HTTPException(status_code=400, detail="Message cannot be empty.")
# Keep only recent, valid user/assistant messages to limit prompt size.
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for item in body.history[-10:]:
role = item.get("role")
content = item.get("content")
if role in ("user", "assistant") and isinstance(content, str):
messages.append({"role": role, "content": content[:6000]})
messages.append({"role": "user", "content": message[:6000]})
try:
result = client.chat.completions.create(
model="tgi",
messages=messages,
temperature=0.7,
top_p=0.9,
max_tokens=1800
)
return {"reply": result.choices[0].message.content or ""}
except Exception as exc:
raise HTTPException(status_code=502, detail=f"Model request failed: {str(exc)[:300]}")