Sourav122005 commited on
Commit
3087da9
·
verified ·
1 Parent(s): c9503bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -17,22 +17,23 @@ app.add_middleware(
17
  )
18
 
19
  print("Downloading highly optimized VibeThinker-3B Q4_K_M GGUF model...")
20
- # Fixed typo: changed hyphen to dot before Q4_K_M to match the repository file system
21
  model_path = hf_hub_download(
22
  repo_id="prithivMLmods/VibeThinker-3B-GGUF",
23
  filename="VibeThinker-3B.Q4_K_M.gguf"
24
  )
25
 
26
- print("Initializing high-speed llama.cpp execution runtime...")
 
27
  llm = Llama(
28
  model_path=model_path,
29
- n_ctx=8192, # Optimized context window to keep memory footprint light on CPU
30
- n_threads=2 # Formatted to maximize dual-core free container allocation limits
 
31
  )
32
 
33
  @app.get("/")
34
  def read_root():
35
- return {"status": "online", "engine": "llama.cpp Optimized C++ Core"}
36
 
37
  @app.post("/v1/chat/completions")
38
  async def chat_completions(request: Request):
@@ -41,20 +42,23 @@ async def chat_completions(request: Request):
41
 
42
  response = llm.create_chat_completion(
43
  messages=messages,
44
- temperature=0.7,
45
  top_p=0.95,
46
  stream=True,
47
- max_tokens=2048
48
  )
49
 
50
  def stream_generator():
51
- for chunk in response:
52
- # Extract raw token changes
53
- delta = chunk.get("choices", [{}])[0].get("delta", {})
54
- if "content" in delta:
55
- # Direct transparent formatting pass-through
56
- yield f"data: {json.dumps(chunk)}\n\n"
57
- yield "data: [DONE]\n\n"
 
 
 
58
 
59
  return StreamingResponse(stream_generator(), media_type="text/event-stream")
60
 
 
17
  )
18
 
19
  print("Downloading highly optimized VibeThinker-3B Q4_K_M GGUF model...")
 
20
  model_path = hf_hub_download(
21
  repo_id="prithivMLmods/VibeThinker-3B-GGUF",
22
  filename="VibeThinker-3B.Q4_K_M.gguf"
23
  )
24
 
25
+ print("Initializing memory-optimized llama.cpp execution runtime...")
26
+ # Hardened configurations specifically tuned to avoid OOM crashes on free shared CPU tiers
27
  llm = Llama(
28
  model_path=model_path,
29
+ n_ctx=4096, # Shifting context window to 4K slashes active memory requirements in half
30
+ n_batch=32, # Drop processing batches from 512 to 32 to eliminate peak memory spikes
31
+ n_threads=2 # Explicitly bounds CPU thread contention blocks
32
  )
33
 
34
  @app.get("/")
35
  def read_root():
36
+ return {"status": "online", "engine": "llama.cpp Memory-Hardened Core"}
37
 
38
  @app.post("/v1/chat/completions")
39
  async def chat_completions(request: Request):
 
42
 
43
  response = llm.create_chat_completion(
44
  messages=messages,
45
+ temperature=0.3,
46
  top_p=0.95,
47
  stream=True,
48
+ max_tokens=1536
49
  )
50
 
51
  def stream_generator():
52
+ try:
53
+ for chunk in response:
54
+ delta = chunk.get("choices", [{}])[0].get("delta", {})
55
+ if "content" in delta:
56
+ yield f"data: {json.dumps(chunk)}\n\n"
57
+ yield "data: [DONE]\n\n"
58
+ except Exception as e:
59
+ # Catch silent disconnects or timeouts cleanly without crashing the Uvicorn thread
60
+ print(f"Streaming trace intercepted safely: {str(e)}")
61
+ return
62
 
63
  return StreamingResponse(stream_generator(), media_type="text/event-stream")
64