Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394from fastapi import FastAPI, Request, HTTPException, Dependsfrom fastapi.middleware.cors import CORSMiddlewarefrom llama_cpp import Llamaimport uvicornapp = FastAPI()# 1. ALLOW EXTERNAL CONNECTIONS (CORS)# This allows your GitHub Pages or local HTML file to talk to the HF Spaceapp.add_middleware(CORSMiddleware,allow_origins=["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# ConfigurationMY_API_KEY = "my-secret-key-456"MODEL_PATH = "./model.gguf"# 2. INITIALIZE MISTRAL (Optimized for CPU)# We use 4 threads to utilize the full power of the HF Free Tier CPUllm = Llama(model_path=MODEL_PATH,n_ctx=2048, # Context windown_threads=4, # Parallel CPU coresn_batch=512, # Prompt processing speedverbose=True # Logs performance to the HF console)def verify_key(request: Request):3 unmodified lines@app.get("/")def home():return {"status": "Online","engine": "Mistral-7B-Q8_0","brand": "ChatMPT Official"}@app.post("/v1/chat")async def chat(request: Request, _ = Depends(verify_key)):try:body = await request.json()user_input = body.get("prompt", "")if not user_input:return {"reply": "System: No prompt received."}# 3. MISTRAL INSTRUCT FORMAT# llama-cpp-python adds the <s> automatically.# [INST] tags guide the model's logic.system_msg = "You are ChatMPT, a professional AI. Be concise and never repeat your own sentences."prompt = f"[INST] {system_msg}\n\n{user_input} [/INST]"# 4. ANTI-LOOP & SPEED SAMPLINGresponse = llm(prompt,max_tokens=256,stop=["</s>", "[INST]"], # Hard stops for Mistraltemperature=0.75, # Balanced creativity# --- THE "ANTI-LOOP" ARMOR ---repeat_penalty=1.25, # Heavily discourages repeating phrasesfrequency_penalty=0.5, # Discourages using the same word too oftenpresence_penalty=0.5, # Discourages returning to the same topics# --- MIROSTAT 2.0 (High Quality Control) ---mirostat_mode=2, # 2 is the modern standard for CPU stabilitymirostat_tau=5.0, # Higher = more interesting, Lower = more focusedmirostat_eta=0.1 # Learning rate for the AI's "internal mood")final_reply = response["choices"][0]["text"].strip()# 5. BRAND PROTECTION (Safety Filter)# Force-corrects common hallucinationshallucinations = ["ChatGPT", "Chat GPT", "OpenAI", "ChatPapers", "ChatPBT"]for item in hallucinations:final_reply = final_reply.replace(item, "ChatMPT")return {"reply": final_reply}except Exception as e:print(f"Error: {str(e)}")return {"reply": f"System Error: The engine is under heavy load. Please try again."}if __name__ == "__main__":# Standard Hugging Face portuvicorn.run(app, host="0.0.0.0", port=7860)12345678910111213141516171819202122232425262728293031323 unmodified lines36373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990from fastapi import FastAPI, Request, HTTPException, Dependsfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import JSONResponsefrom llama_cpp import Llamaimport uvicornimport osapp = FastAPI()# 1. CORS CONFIGURATION# This is crucial so your website can actually read the responseapp.add_middleware(CORSMiddleware,allow_origins=["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)MY_API_KEY = "my-secret-key-456"# 2. LOAD MISTRAL (Optimized for HF CPU)# We use 4 threads and a higher batch size for faster 'first thought'llm = Llama(model_path="./model.gguf",n_ctx=2048,n_threads=4,n_batch=512,verbose=True)def verify_key(request: Request):3 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", "").strip()if not user_input:return JSONResponse(content={"reply": "Please enter a message!"})# --- MISTRAL INSTRUCT FORMAT ---# llama-cpp adds <s> automatically, so we just wrap in [INST]system_msg = "You are ChatMPT, a helpful AI. Be direct and concise."prompt = f"[INST] {system_msg}\n\n{user_input} [/INST]"print(f"\n--- USER PROMPT: {user_input} ---")# --- GENERATION SETTINGS ---# We lower repeat_penalty slightly so it doesn't get 'scared' to talkresponse = llm(prompt,max_tokens=512,stop=["</s>", "[INST]", "[/INST]"],temperature=0.7,repeat_penalty=1.1,mirostat_mode=2)final_reply = response["choices"][0]["text"].strip()# --- DEBUG LOGGING ---# This will show up in your Hugging Face Logsprint(f"--- AI REPLY: {final_reply} ---")# --- FALLBACK IF EMPTY ---if not final_reply or len(final_reply) < 2:final_reply = "I'm here! Could you please rephrase that?"# Identity Safetyhallucinations = ["ChatGPT", "Chat GPT", "OpenAI", "ChatPapers"]for item in hallucinations:final_reply = final_reply.replace(item, "ChatMPT")return JSONResponse(content={"reply": final_reply})except Exception as e:print(f"SERVER ERROR: {str(e)}")return JSONResponse(status_code=500, content={"reply": "System Error: Brain is overloaded."})if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)
app.py+41-45