dpv007 commited on
Commit
3554545
·
verified ·
1 Parent(s): 3c504bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -6,9 +6,7 @@ from fastapi.responses import StreamingResponse
6
  from pydantic import BaseModel
7
  import uvicorn
8
 
9
- # 1. Model Configuration
10
- # model_repo = "Qwen/Qwen2.5-1.5B-Instruct-GGUF"
11
- # model_file = "qwen2.5-1.5b-instruct-q4_k_m.gguf"
12
  model_repo = "Qwen/Qwen2.5-0.5B-Instruct-GGUF"
13
  model_file = "qwen2.5-0.5b-instruct-q4_k_m.gguf"
14
 
@@ -29,13 +27,12 @@ llm = Llama(
29
  )
30
 
31
  # 3. Attach Automatic Persistent KV Caching
32
- # This safely handles concurrent API requests and stores the KV cache to disk
33
  cache = LlamaDiskCache(capacity_bytes=536870912, cache_dir=PERSISTENT_DIR) # 512 MB cache
34
  llm.set_cache(cache)
35
  print(f"Disk cache mounted at {PERSISTENT_DIR}")
36
 
37
  # 4. FastAPI Setup
38
- app = FastAPI(title="Qwen-2.5 Local CPU API (Fully Optimized)")
39
 
40
  class QueryRequest(BaseModel):
41
  prompt: str
@@ -43,7 +40,6 @@ class QueryRequest(BaseModel):
43
  temperature: float = 0.7
44
 
45
  def format_prompt(user_text: str) -> str:
46
- # Qwen ChatML format. The disk cache will automatically recognize the static system prompt.
47
  return f"<|im_start|>system\nYou are a highly capable technical assistant.<|im_end|>\n<|im_start|>user\n{user_text}<|im_end|>\n<|im_start|>assistant\n"
48
 
49
  # ==========================================
@@ -80,7 +76,16 @@ def generate_stream(request: QueryRequest):
80
  for chunk in response_stream:
81
  yield chunk["choices"][0]["text"]
82
 
83
- return StreamingResponse(stream_generator(), media_type="text/event-stream")
 
 
 
 
 
 
 
 
 
84
 
85
  if __name__ == "__main__":
86
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
6
  from pydantic import BaseModel
7
  import uvicorn
8
 
9
+ # 1. Model Configuration (Using 0.5B for 2-3x faster streaming on 2 vCPUs)
 
 
10
  model_repo = "Qwen/Qwen2.5-0.5B-Instruct-GGUF"
11
  model_file = "qwen2.5-0.5b-instruct-q4_k_m.gguf"
12
 
 
27
  )
28
 
29
  # 3. Attach Automatic Persistent KV Caching
 
30
  cache = LlamaDiskCache(capacity_bytes=536870912, cache_dir=PERSISTENT_DIR) # 512 MB cache
31
  llm.set_cache(cache)
32
  print(f"Disk cache mounted at {PERSISTENT_DIR}")
33
 
34
  # 4. FastAPI Setup
35
+ app = FastAPI(title="Qwen-2.5 0.5B Local CPU API")
36
 
37
  class QueryRequest(BaseModel):
38
  prompt: str
 
40
  temperature: float = 0.7
41
 
42
  def format_prompt(user_text: str) -> str:
 
43
  return f"<|im_start|>system\nYou are a highly capable technical assistant.<|im_end|>\n<|im_start|>user\n{user_text}<|im_end|>\n<|im_start|>assistant\n"
44
 
45
  # ==========================================
 
76
  for chunk in response_stream:
77
  yield chunk["choices"][0]["text"]
78
 
79
+ # Return with strict anti-buffering headers so the HF Proxy doesn't block the stream
80
+ return StreamingResponse(
81
+ stream_generator(),
82
+ media_type="text/event-stream",
83
+ headers={
84
+ "X-Accel-Buffering": "no",
85
+ "Cache-Control": "no-cache",
86
+ "Connection": "keep-alive"
87
+ }
88
+ )
89
 
90
  if __name__ == "__main__":
91
  uvicorn.run(app, host="0.0.0.0", port=7860)