varsshancoder commited on
Commit
7334d2e
·
verified ·
1 Parent(s): 6d78fb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -7
app.py CHANGED
@@ -8,7 +8,6 @@ from llama_cpp import Llama
8
 
9
  app = FastAPI(title="Qwen 2.5 Coder OpenAI Server")
10
 
11
- # Enable CORS for local VS Code extension requests
12
  app.add_middleware(
13
  CORSMiddleware,
14
  allow_origins=["*"],
@@ -17,19 +16,17 @@ app.add_middleware(
17
  allow_headers=["*"],
18
  )
19
 
20
- # Safe Hugging Face runtime model download path
21
  MODEL_REPO = "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF"
22
  MODEL_FILE = "qwen2.5-coder-7b-instruct-q4_k_m.gguf"
23
 
24
  print("Checking and downloading Qwen 2.5 Coder 7B GGUF model...")
25
- # Downloads dynamically on container boot time via the HF library (cached across restarts)
26
  model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
27
 
28
  print(f"Loading model into memory from: {model_path}")
29
  llm = Llama(
30
  model_path=model_path,
31
- n_ctx=4096, # Expanded safely within 16GB memory bounds
32
- n_threads=2, # Perfectly matches your 2 vCPU resource cap
33
  n_batch=512,
34
  verbose=False
35
  )
@@ -42,7 +39,6 @@ async def chat_completions(request: Request):
42
  temperature = body.get("temperature", 0.2)
43
  max_tokens = body.get("max_tokens", 1024)
44
 
45
- # Convert incoming standard OpenAI messages array to Qwen ChatML formatting
46
  formatted_prompt = ""
47
  for msg in messages:
48
  role = msg.get("role", "user")
@@ -50,7 +46,6 @@ async def chat_completions(request: Request):
50
  formatted_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
51
  formatted_prompt += "<|im_start|>assistant\n"
52
 
53
- # Synchronous processing avoids lock stalls on CPU
54
  response = llm(
55
  formatted_prompt,
56
  max_tokens=max_tokens,
 
8
 
9
  app = FastAPI(title="Qwen 2.5 Coder OpenAI Server")
10
 
 
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
 
16
  allow_headers=["*"],
17
  )
18
 
 
19
  MODEL_REPO = "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF"
20
  MODEL_FILE = "qwen2.5-coder-7b-instruct-q4_k_m.gguf"
21
 
22
  print("Checking and downloading Qwen 2.5 Coder 7B GGUF model...")
 
23
  model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
24
 
25
  print(f"Loading model into memory from: {model_path}")
26
  llm = Llama(
27
  model_path=model_path,
28
+ n_ctx=4096,
29
+ n_threads=2,
30
  n_batch=512,
31
  verbose=False
32
  )
 
39
  temperature = body.get("temperature", 0.2)
40
  max_tokens = body.get("max_tokens", 1024)
41
 
 
42
  formatted_prompt = ""
43
  for msg in messages:
44
  role = msg.get("role", "user")
 
46
  formatted_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
47
  formatted_prompt += "<|im_start|>assistant\n"
48
 
 
49
  response = llm(
50
  formatted_prompt,
51
  max_tokens=max_tokens,