Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,17 +16,22 @@ app.add_middleware(
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# 1. Load
|
| 20 |
-
print("Loading model...
|
| 21 |
MODEL_PATH = hf_hub_download(
|
| 22 |
repo_id="Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF",
|
| 23 |
filename="qwen2.5-coder-1.5b-instruct-q4_k_m.gguf"
|
| 24 |
)
|
| 25 |
-
# Using n_ctx=512 to keep memory usage low and stable
|
| 26 |
-
llm = Llama(model_path=MODEL_PATH, n_ctx=512)
|
| 27 |
-
print("Model finished loading.")
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
class ChatMessage(BaseModel):
|
| 31 |
role: str
|
| 32 |
content: str
|
|
@@ -34,18 +39,18 @@ class ChatMessage(BaseModel):
|
|
| 34 |
class ChatRequest(BaseModel):
|
| 35 |
messages: List[ChatMessage]
|
| 36 |
|
| 37 |
-
# 3. Endpoints
|
| 38 |
@app.get("/")
|
| 39 |
def read_root():
|
| 40 |
-
return {"message": "
|
| 41 |
|
| 42 |
@app.post("/v1/chat/completions")
|
| 43 |
async def chat_completion(request: ChatRequest):
|
|
|
|
| 44 |
try:
|
| 45 |
-
# Convert request to history format
|
| 46 |
history = [{"role": msg.role, "content": msg.content} for msg in request.messages]
|
| 47 |
|
| 48 |
def stream_generator():
|
|
|
|
| 49 |
response_stream = llm.create_chat_completion(
|
| 50 |
messages=history,
|
| 51 |
temperature=0.2,
|
|
@@ -54,7 +59,9 @@ async def chat_completion(request: ChatRequest):
|
|
| 54 |
for chunk in response_stream:
|
| 55 |
yield f"data: {json.dumps(chunk)}\n\n"
|
| 56 |
yield "data: [DONE]\n\n"
|
|
|
|
| 57 |
|
| 58 |
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
| 59 |
except Exception as e:
|
|
|
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# 1. Load model with minimal resource allocation
|
| 20 |
+
print("Loading model...")
|
| 21 |
MODEL_PATH = hf_hub_download(
|
| 22 |
repo_id="Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF",
|
| 23 |
filename="qwen2.5-coder-1.5b-instruct-q4_k_m.gguf"
|
| 24 |
)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Crucial: n_gpu_layers=0 forces CPU, n_threads=1 saves RAM
|
| 27 |
+
llm = Llama(
|
| 28 |
+
model_path=MODEL_PATH,
|
| 29 |
+
n_ctx=512,
|
| 30 |
+
n_gpu_layers=0,
|
| 31 |
+
n_threads=1
|
| 32 |
+
)
|
| 33 |
+
print("Model loaded successfully.")
|
| 34 |
+
|
| 35 |
class ChatMessage(BaseModel):
|
| 36 |
role: str
|
| 37 |
content: str
|
|
|
|
| 39 |
class ChatRequest(BaseModel):
|
| 40 |
messages: List[ChatMessage]
|
| 41 |
|
|
|
|
| 42 |
@app.get("/")
|
| 43 |
def read_root():
|
| 44 |
+
return {"message": "Server is up!"}
|
| 45 |
|
| 46 |
@app.post("/v1/chat/completions")
|
| 47 |
async def chat_completion(request: ChatRequest):
|
| 48 |
+
print("Received chat request...") # Check logs for this
|
| 49 |
try:
|
|
|
|
| 50 |
history = [{"role": msg.role, "content": msg.content} for msg in request.messages]
|
| 51 |
|
| 52 |
def stream_generator():
|
| 53 |
+
print("Starting inference...") # Check logs for this
|
| 54 |
response_stream = llm.create_chat_completion(
|
| 55 |
messages=history,
|
| 56 |
temperature=0.2,
|
|
|
|
| 59 |
for chunk in response_stream:
|
| 60 |
yield f"data: {json.dumps(chunk)}\n\n"
|
| 61 |
yield "data: [DONE]\n\n"
|
| 62 |
+
print("Inference finished.") # Check logs for this
|
| 63 |
|
| 64 |
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f"ERROR: {e}")
|
| 67 |
raise HTTPException(status_code=500, detail=str(e))
|