Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
-
from fastapi.responses import StreamingResponse
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from typing import List
|
|
@@ -21,8 +20,9 @@ app.add_middleware(
|
|
| 21 |
)
|
| 22 |
|
| 23 |
# --- CONFIGURATION ---
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
CACHED_SYSTEM_PROMPT = ""
|
| 27 |
|
| 28 |
# 1. MongoDB Initialization
|
|
@@ -54,11 +54,13 @@ if not os.path.exists(MODEL_PATH):
|
|
| 54 |
MODEL_PATH = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME, local_dir=".")
|
| 55 |
|
| 56 |
print("Loading LLM into RAM (n_ctx=2048)...")
|
|
|
|
| 57 |
llm = Llama(
|
| 58 |
model_path=MODEL_PATH,
|
| 59 |
-
n_ctx=2048,
|
| 60 |
-
n_threads=
|
| 61 |
-
chat_format="chatml"
|
|
|
|
| 62 |
)
|
| 63 |
refresh_knowledge_cache()
|
| 64 |
|
|
@@ -75,7 +77,6 @@ class KnowledgeItem(BaseModel):
|
|
| 75 |
|
| 76 |
# --- ENDPOINTS ---
|
| 77 |
|
| 78 |
-
# Fixes the 404 Error:
|
| 79 |
@app.get("/")
|
| 80 |
async def root():
|
| 81 |
return {"status": "healthy", "message": "API is online and operational."}
|
|
@@ -83,7 +84,7 @@ async def root():
|
|
| 83 |
@app.post("/v1/chat/completions")
|
| 84 |
async def chat_completion(request: ChatRequest):
|
| 85 |
try:
|
| 86 |
-
# Construct history
|
| 87 |
history = [{"role": "system", "content": CACHED_SYSTEM_PROMPT}]
|
| 88 |
for msg in request.messages:
|
| 89 |
history.append({"role": msg.role, "content": msg.content})
|
|
@@ -91,7 +92,7 @@ async def chat_completion(request: ChatRequest):
|
|
| 91 |
def stream_generator():
|
| 92 |
response_stream = llm.create_chat_completion(
|
| 93 |
messages=history,
|
| 94 |
-
temperature=0.
|
| 95 |
stream=True
|
| 96 |
)
|
| 97 |
for chunk in response_stream:
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from typing import List
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
# --- CONFIGURATION ---
|
| 23 |
+
# SWAPPED TO 1.5B TO PREVENT EXIT CODE 139 (OOM CRASH)
|
| 24 |
+
REPO_ID = "Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF"
|
| 25 |
+
MODEL_FILENAME = "qwen2.5-coder-1.5b-instruct-q4_k_m.gguf"
|
| 26 |
CACHED_SYSTEM_PROMPT = ""
|
| 27 |
|
| 28 |
# 1. MongoDB Initialization
|
|
|
|
| 54 |
MODEL_PATH = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME, local_dir=".")
|
| 55 |
|
| 56 |
print("Loading LLM into RAM (n_ctx=2048)...")
|
| 57 |
+
# Reduced n_threads to 2 to ensure stability on shared hardware
|
| 58 |
llm = Llama(
|
| 59 |
model_path=MODEL_PATH,
|
| 60 |
+
n_ctx=2048,
|
| 61 |
+
n_threads=2,
|
| 62 |
+
chat_format="chatml",
|
| 63 |
+
verbose=False
|
| 64 |
)
|
| 65 |
refresh_knowledge_cache()
|
| 66 |
|
|
|
|
| 77 |
|
| 78 |
# --- ENDPOINTS ---
|
| 79 |
|
|
|
|
| 80 |
@app.get("/")
|
| 81 |
async def root():
|
| 82 |
return {"status": "healthy", "message": "API is online and operational."}
|
|
|
|
| 84 |
@app.post("/v1/chat/completions")
|
| 85 |
async def chat_completion(request: ChatRequest):
|
| 86 |
try:
|
| 87 |
+
# Construct history with injected system prompt
|
| 88 |
history = [{"role": "system", "content": CACHED_SYSTEM_PROMPT}]
|
| 89 |
for msg in request.messages:
|
| 90 |
history.append({"role": msg.role, "content": msg.content})
|
|
|
|
| 92 |
def stream_generator():
|
| 93 |
response_stream = llm.create_chat_completion(
|
| 94 |
messages=history,
|
| 95 |
+
temperature=0.2, # Slightly higher for creativity
|
| 96 |
stream=True
|
| 97 |
)
|
| 98 |
for chunk in response_stream:
|