Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234 unmodified lines8910111213141 unmodified line161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081from fastapi import FastAPI, Request, HTTPException, Dependsfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials4 unmodified linesapp = FastAPI()security = HTTPBearer()# 1. CORS Configurationapp.add_middleware(CORSMiddleware,allow_origins=["*"],1 unmodified lineallow_headers=["*"],)# 2. RESTORED API KEYMY_API_KEY = "my-secret-key-456"# 3. LOAD MODEL (Q4 for Speed)print("--- Loading ChatMPT Engine (Q4) ---")llm = Llama(model_path="./model.gguf",n_ctx=2048,n_threads=2, # Optimized for HF Free Tiern_batch=128,verbose=False)def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):"""Validates the Bearer token: my-secret-key-456"""if credentials.credentials != MY_API_KEY:raise HTTPException(status_code=403, detail="Unauthorized: Invalid API Key")return credentials.credentials@app.get("/")def health():return {"status": "online", "brand": "ChatMPT"}@app.post("/v1/chat")async def chat(request: Request, _ = Depends(verify_token)):try:data = await request.json()user_input = data.get("prompt", "").strip()if not user_input:return JSONResponse(content={"reply": "I'm listening!"})# SYSTEM PROMPT: Forces the AI to be ChatMPT and stay concisesystem_msg = "You are ChatMPT, a helpful AI created by the ChatMPT Team. Be concise and direct."prompt = f"<s>[INST] {system_msg}\n\n{user_input} [/INST]"print(f"User: {user_input}")# GENERATION SETTINGSoutput = llm(prompt,max_tokens=300,temperature=0.7,stop=["</s>", "[INST]", "User:"],echo=False)reply = output["choices"][0]["text"].strip()# Identity Cleanupreply = reply.replace("ChatGPT", "ChatMPT").replace("OpenAI", "ChatMPT Team")if not reply:reply = "I'm here! Could you please rephrase that?"return JSONResponse(content={"reply": reply})except Exception as e:print(f"Error: {e}")return JSONResponse(status_code=500, content={"reply": "Server Error. Try again."})if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)12344 unmodified lines910111213141 unmodified line161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import osfrom fastapi import FastAPI, Request, HTTPException, Dependsfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentials4 unmodified linesapp = FastAPI()security = HTTPBearer()app.add_middleware(CORSMiddleware,allow_origins=["*"],1 unmodified lineallow_headers=["*"],)MY_API_KEY = "my-secret-key-456"# Load model without extra overheadllm = Llama(model_path="./model.gguf",n_ctx=2048,n_threads=2,n_batch=512,verbose=False)def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):if credentials.credentials != MY_API_KEY:raise HTTPException(status_code=403, detail="Unauthorized")return credentials.credentials@app.post("/v1/chat")async def chat(request: Request, _ = Depends(verify_token)):try:data = await request.json()user_input = data.get("prompt", "").strip()# REMOVED the extra <s> here to fix your warningprompt = f"[INST] {user_input} [/INST]"output = llm(prompt,max_tokens=512,temperature=0.7,stop=["[/INST]", "User:"],echo=False)reply = output["choices"][0]["text"].strip()return JSONResponse(content={"reply": reply or "..."})except Exception as e:print(f"Error: {e}")return JSONResponse(status_code=500, content={"reply": "SYSTEM_ERROR_RETRY"})if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)
app.py+11-32