Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.responses import
|
|
|
|
| 5 |
from llama_cpp import Llama
|
| 6 |
-
import time
|
| 7 |
|
| 8 |
app = FastAPI(title="Qwen 2.5 Coder OpenAI Server")
|
| 9 |
|
| 10 |
-
# Enable CORS
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
@@ -16,11 +17,19 @@ app.add_middleware(
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
llm = Llama(
|
| 21 |
-
model_path=
|
| 22 |
-
n_ctx=4096, # Expanded
|
| 23 |
-
n_threads=2, #
|
| 24 |
n_batch=512,
|
| 25 |
verbose=False
|
| 26 |
)
|
|
@@ -32,9 +41,8 @@ async def chat_completions(request: Request):
|
|
| 32 |
messages = body.get("messages", [])
|
| 33 |
temperature = body.get("temperature", 0.2)
|
| 34 |
max_tokens = body.get("max_tokens", 1024)
|
| 35 |
-
stream = body.get("stream", False)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
formatted_prompt = ""
|
| 39 |
for msg in messages:
|
| 40 |
role = msg.get("role", "user")
|
|
@@ -42,13 +50,7 @@ async def chat_completions(request: Request):
|
|
| 42 |
formatted_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
|
| 43 |
formatted_prompt += "<|im_start|>assistant\n"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
if stream:
|
| 47 |
-
# High-performance streaming on 2 vCPUs can stall the event loop;
|
| 48 |
-
# Forcing non-stream mode keeps the CPU connection robust
|
| 49 |
-
pass
|
| 50 |
-
|
| 51 |
-
# 3. Generate completion
|
| 52 |
response = llm(
|
| 53 |
formatted_prompt,
|
| 54 |
max_tokens=max_tokens,
|
|
@@ -56,9 +58,8 @@ async def chat_completions(request: Request):
|
|
| 56 |
temperature=temperature
|
| 57 |
)
|
| 58 |
|
| 59 |
-
text_output = response["choices"][
|
| 60 |
|
| 61 |
-
# 4. Construct a compliant OpenAI JSON response structure
|
| 62 |
return JSONResponse(content={
|
| 63 |
"id": f"chatcmpl-{int(time.time())}",
|
| 64 |
"object": "chat.completion",
|
|
@@ -86,3 +87,4 @@ def index():
|
|
| 86 |
"model": "qwen2.5-coder-7b-instruct-q4_k_m",
|
| 87 |
"endpoint": "/v1/chat/completions"
|
| 88 |
}
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
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 |
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 |
)
|
|
|
|
| 41 |
messages = body.get("messages", [])
|
| 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 |
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,
|
|
|
|
| 58 |
temperature=temperature
|
| 59 |
)
|
| 60 |
|
| 61 |
+
text_output = response["choices"]["text"]
|
| 62 |
|
|
|
|
| 63 |
return JSONResponse(content={
|
| 64 |
"id": f"chatcmpl-{int(time.time())}",
|
| 65 |
"object": "chat.completion",
|
|
|
|
| 87 |
"model": "qwen2.5-coder-7b-instruct-q4_k_m",
|
| 88 |
"endpoint": "/v1/chat/completions"
|
| 89 |
}
|
| 90 |
+
|