Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from llama_cpp import Llama
|
|
| 8 |
|
| 9 |
app = FastAPI(title="Qwen 2.5 Coder OpenAI Server")
|
| 10 |
|
|
|
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
@@ -20,13 +21,14 @@ 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,6 +41,7 @@ async def chat_completions(request: Request):
|
|
| 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,6 +49,7 @@ async def chat_completions(request: Request):
|
|
| 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,
|
|
@@ -55,6 +59,7 @@ async def chat_completions(request: Request):
|
|
| 55 |
|
| 56 |
text_output = response["choices"]["text"]
|
| 57 |
|
|
|
|
| 58 |
return JSONResponse(content={
|
| 59 |
"id": f"chatcmpl-{int(time.time())}",
|
| 60 |
"object": "chat.completion",
|
|
@@ -83,3 +88,4 @@ def index():
|
|
| 83 |
"endpoint": "/v1/chat/completions"
|
| 84 |
}
|
| 85 |
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI(title="Qwen 2.5 Coder OpenAI Server")
|
| 10 |
|
| 11 |
+
# Enable CORS for local VS Code extension connections (Cline, Continue, Cursor)
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
|
|
| 21 |
MODEL_FILE = "qwen2.5-coder-7b-instruct-q4_k_m.gguf"
|
| 22 |
|
| 23 |
print("Checking and downloading Qwen 2.5 Coder 7B GGUF model...")
|
| 24 |
+
# Downloads dynamically on container boot time via the HF library (cached across container restarts)
|
| 25 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 26 |
|
| 27 |
print(f"Loading model into memory from: {model_path}")
|
| 28 |
llm = Llama(
|
| 29 |
model_path=model_path,
|
| 30 |
+
n_ctx=4096, # Safely fits within 16GB memory bounds
|
| 31 |
+
n_threads=2, # Perfectly matches your 2 vCPU resource cap
|
| 32 |
n_batch=512,
|
| 33 |
verbose=False
|
| 34 |
)
|
|
|
|
| 41 |
temperature = body.get("temperature", 0.2)
|
| 42 |
max_tokens = body.get("max_tokens", 1024)
|
| 43 |
|
| 44 |
+
# Convert standard OpenAI messages array into Qwen ChatML formatting
|
| 45 |
formatted_prompt = ""
|
| 46 |
for msg in messages:
|
| 47 |
role = msg.get("role", "user")
|
|
|
|
| 49 |
formatted_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
|
| 50 |
formatted_prompt += "<|im_start|>assistant\n"
|
| 51 |
|
| 52 |
+
# Synchronous processing avoids thread-locking/stalling on weak CPUs
|
| 53 |
response = llm(
|
| 54 |
formatted_prompt,
|
| 55 |
max_tokens=max_tokens,
|
|
|
|
| 59 |
|
| 60 |
text_output = response["choices"]["text"]
|
| 61 |
|
| 62 |
+
# Construct structured JSON compliant with OpenAI specs
|
| 63 |
return JSONResponse(content={
|
| 64 |
"id": f"chatcmpl-{int(time.time())}",
|
| 65 |
"object": "chat.completion",
|
|
|
|
| 88 |
"endpoint": "/v1/chat/completions"
|
| 89 |
}
|
| 90 |
|
| 91 |
+
|