File size: 1,622 Bytes
b879e43
f0f547d
3320b3e
967e0b2
48fde5c
3320b3e
7d837d2
2142f68
f0f547d
 
f043989
2142f68
f0f547d
 
 
48fde5c
2142f68
 
 
 
3320b3e
2142f68
 
 
ad138c6
2257bba
2142f68
 
 
3320b3e
2142f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00a3720
 
3320b3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from fastapi import FastAPI, Request, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from llama_cpp import Llama
import uvicorn

app = FastAPI()

# --- CORS SETTINGS (Crucial for GitHub Pages) ---
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# --- CONFIGURATION ---
MY_API_KEY = "my-secret-key-456" 
# Optimized for HF Free Tier CPU
llm = Llama(model_path="./model.gguf", n_ctx=2048, n_threads=4, n_gpu_layers=0)

def verify_key(request: Request):
    auth = request.headers.get("Authorization")
    if auth != f"Bearer {MY_API_KEY}":
        raise HTTPException(status_code=403, detail="Unauthorized")

@app.get("/")
def home():
    return {"status": "Online", "branding": "ChatMPT Team"}

@app.post("/v1/chat")
async def chat(request: Request, _ = Depends(verify_key)):
    body = await request.json()
    user_input = body.get("prompt", "")
    
    # Instruction-based prompt to fix the identity
    prompt = (
        "Assistant is a polite AI named ChatMPT, created by the ChatMPT Team.\n"
        f"User: {user_input}\n"
        "Assistant:"
    )
    
    response = llm(
        prompt, 
        max_tokens=500, 
        stop=["User:", "\n", "Assistant:"], 
        temperature=0.7 
    )
    
    raw_reply = response["choices"][0]["text"].strip()
    
    # Anti-Typo Safety Filter
    final_reply = raw_reply.replace("ChatPBT", "ChatMPT").replace("ChatPP", "ChatMPT")
    
    return {"reply": final_reply}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)