Update app.py
Browse files9 unmodified lines101112131415161717 unmodified lines3536373839404142434445464748495051525354555657585960619 unmodified lines# ββββββββββββββββββββββββββββββββββββββββββββββ# 1. Model Configuration# ββββββββββββββββββββββββββββββββββββββββββββββMODEL_REPO = "Qwen/Qwen2.5-0.5B-Instruct-GGUF"MODEL_FILE = "qwen2.5-0.5b-instruct-q4_k_m.gguf"SYSTEM_PROMPT = ("<|im_start|>system\n"17 unmodified linesprint("Loading model into memory...")llm = Llama(model_path=model_path,n_threads=4, # more threads = faster token generationn_ctx=8192, # 8k context (was 2048) β uses ~1 GB extra RAMn_keep=_system_token_count,n_batch=512, # larger batch = faster prompt processinguse_mmap=True, # memory-map the model file β faster cold loaduse_mlock=True, # lock model weights in RAM β prevents OS swappingverbose=False,)# 4 GB RAM cache (was 512 MB) β repeat queries return instantlycache = LlamaRAMCache(capacity_bytes=4_294_967_296)llm.set_cache(cache)print("RAM cache initialized (4 GB).")# More workers = handle parallel requests without queuing_executor = concurrent.futures.ThreadPoolExecutor(max_workers=2)# ββββββββββββββββββββββββββββββββββββββββββββββ# 3. FastAPI App# ββββββββββββββββββββββββββββββββββββββββββββββapp = FastAPI(title="Qwen-2.5 0.5B Local CPU API")class QueryRequest(BaseModel):9 unmodified lines1011121314151617181917 unmodified lines373839404142434445464748495051525354555657585960616263649 unmodified lines# ββββββββββββββββββββββββββββββββββββββββββββββ# 1. Model Configuration# ββββββββββββββββββββββββββββββββββββββββββββββ# Upgraded: 1.5B model β 3x smarter, fits easily in 16 GB free RAM# Switch back to 0.5B by changing these two lines if neededMODEL_REPO = "Qwen/Qwen2.5-1.5B-Instruct-GGUF"MODEL_FILE = "qwen2.5-1.5b-instruct-q4_k_m.gguf"SYSTEM_PROMPT = ("<|im_start|>system\n"17 unmodified linesprint("Loading model into memory...")llm = Llama(model_path=model_path,n_threads=8, # max out CPU threadsn_ctx=16384, # 16k context β long conversations, big documentsn_keep=_system_token_count,n_batch=1024, # large batch = fastest prompt ingestionn_ubatch=512, # micro-batch for KV computeuse_mmap=True, # memory-map weights β fast load, low overheaduse_mlock=True, # lock ALL weights in RAM β zero swap riskverbose=False,)# 8 GB RAM cache β massive response reuse, near-instant repeat queriescache = LlamaRAMCache(capacity_bytes=8_589_934_592)llm.set_cache(cache)print("RAM cache initialized (8 GB).")# 4 parallel workers_executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)# ββββββββββββββββββββββββββββββββββββββββββββββ# 3. FastAPI App# ββββββββββββββββββββββββββββββββββββββββββββββapp = FastAPI(title="Qwen-2.5 1.5B Local CPU API")class QueryRequest(BaseModel):
app.py+16-13