Spaces:
Sleeping
Sleeping
Update app.py
Browse files13 unmodified lines141516171819202 unmodified lines23242526272829303132333435363738394041424344454613 unmodified lines)MY_API_KEY = "my-secret-key-456"llm = Llama(model_path="./model.gguf", n_ctx=2048)def verify_key(request: Request):auth = request.headers.get("Authorization")2 unmodified lines@app.get("/")def home():return {"status": "Online", "mode": "Friendly Chat"}@app.post("/v1/chat")async def chat(request: Request, _ = Depends(verify_key)):body = await request.json()user_input = body.get("prompt", "")# Updated System Prompt in app.pysystem_text = "Assistant is a polite and helpful AI named ChatMPT. It was created by the ChatMPT Team."# This ensures that if you ask "Who made you?", it answers correctly.prompt = f"### Instruction:\n{system_text}\n\n### Input:\n{user_input}\n\n### Response:\n"response = llm(prompt,max_tokens=200,stop=["User:", "\n"], # Stops it from talking to itselftemperature=0.7)return {"reply": response["choices"][0]["text"].strip()}13 unmodified lines141516171819202122232425262 unmodified lines2930313233343536373839404142434445464748495051525354555657585960616263646566676813 unmodified lines)MY_API_KEY = "my-secret-key-456"# Load Mistral (Q8 is heavy, so we keep n_ctx at 2048 to save RAM)llm = Llama(model_path="./model.gguf",n_ctx=2048,n_threads=2 # Helps stability on HF Free CPU)def verify_key(request: Request):auth = request.headers.get("Authorization")2 unmodified lines@app.get("/")def home():return {"status": "Online", "mode": "Mistral-7B-Q8", "brand": "ChatMPT"}@app.post("/v1/chat")async def chat(request: Request, _ = Depends(verify_key)):try:body = await request.json()user_input = body.get("prompt", "")# --- MISTRAL SPECIFIC PROMPT FORMAT ---# Mistral uses <s>[INST] {System} \n {User} [/INST]system_msg = "You are ChatMPT, a professional AI created by the ChatMPT Team. You are a machine. Always identify as ChatMPT."prompt = f"<s>[INST] {system_msg}\n\n{user_input} [/INST]"response = llm(prompt,max_tokens=256, # Slightly more room for Q8's better logicstop=["</s>", "[INST]", "User:"],temperature=0.7)final_reply = response["choices"][0]["text"].strip()# --- SAFETY FILTER FOR IDENTITY ---# This stops the "ChatPapers/ChatGPT" hallucination by forcetypos = ["ChatPapers", "Chat GPT", "ChatGPT", "ChatPBT", "ChatPP"]for typo in typos:final_reply = final_reply.replace(typo, "ChatMPT")return {"reply": final_reply}except Exception as e:return {"reply": f"System Error: {str(e)}"}if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=7860)
app.py+41-19