Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
|
@@ -5,22 +5,22 @@ import os
|
|
| 5 |
import json
|
| 6 |
import time
|
| 7 |
import uuid
|
|
|
|
| 8 |
|
| 9 |
api = Blueprint("api", __name__)
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Load config
|
| 12 |
with open("llm_config.json", "r") as f:
|
| 13 |
config = json.load(f).get("openai", {})
|
| 14 |
|
| 15 |
-
# Model details
|
| 16 |
REPO_ID = "mradermacher/distilabeled-Hermes-2.5-Mistral-7B-GGUF"
|
| 17 |
MODEL_FILENAME = "distilabeled-Hermes-2.5-Mistral-7B.Q2_K.gguf"
|
| 18 |
HF_TOKEN = os.environ.get("HF_API_TOKEN")
|
| 19 |
CACHE_DIR = "/app/.cache/huggingface"
|
| 20 |
-
|
| 21 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
-
# Download model if not already cached
|
| 24 |
MODEL_PATH = hf_hub_download(
|
| 25 |
repo_id=REPO_ID,
|
| 26 |
filename=MODEL_FILENAME,
|
|
@@ -28,18 +28,16 @@ MODEL_PATH = hf_hub_download(
|
|
| 28 |
token=HF_TOKEN
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# Load model
|
| 32 |
llm = Llama(
|
| 33 |
model_path=MODEL_PATH,
|
| 34 |
n_ctx=2048,
|
| 35 |
n_threads=4,
|
| 36 |
-
n_gpu_layers=0
|
| 37 |
)
|
| 38 |
|
| 39 |
@api.route("/v1/chat/completions", methods=["POST"])
|
| 40 |
def chat_completions():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
try:
|
| 44 |
data = request.get_json(force=True)
|
| 45 |
messages = data.get("messages", [])
|
|
@@ -48,19 +46,20 @@ def chat_completions():
|
|
| 48 |
top_p = float(data.get("top_p", config.get("top_p", 0.95)))
|
| 49 |
stop = data.get("stop", config.get("stop", ["User:", "Assistant:"]))
|
| 50 |
|
| 51 |
-
# Build prompt text from chat messages
|
| 52 |
prompt_lines = []
|
| 53 |
for msg in messages:
|
| 54 |
role = msg.get("role", "").lower()
|
| 55 |
content = msg.get("content", "").strip()
|
|
|
|
|
|
|
| 56 |
if role == "user":
|
| 57 |
prompt_lines.append(f"User: {content}")
|
| 58 |
elif role == "assistant":
|
| 59 |
prompt_lines.append(f"Assistant: {content}")
|
|
|
|
| 60 |
prompt_lines.append("Assistant:")
|
| 61 |
prompt = "\n".join(prompt_lines)
|
| 62 |
|
| 63 |
-
# Generate text from the model
|
| 64 |
response = llm(
|
| 65 |
prompt,
|
| 66 |
max_tokens=max_tokens,
|
|
@@ -69,10 +68,11 @@ def chat_completions():
|
|
| 69 |
stop=stop
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
# Return response in OpenAI chat completion format
|
| 76 |
return jsonify({
|
| 77 |
"id": f"chatcmpl-{uuid.uuid4().hex}",
|
| 78 |
"object": "chat.completion",
|
|
@@ -89,5 +89,5 @@ def chat_completions():
|
|
| 89 |
})
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
-
|
| 93 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 5 |
import json
|
| 6 |
import time
|
| 7 |
import uuid
|
| 8 |
+
import logging
|
| 9 |
|
| 10 |
api = Blueprint("api", __name__)
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# Load config
|
| 15 |
with open("llm_config.json", "r") as f:
|
| 16 |
config = json.load(f).get("openai", {})
|
| 17 |
|
|
|
|
| 18 |
REPO_ID = "mradermacher/distilabeled-Hermes-2.5-Mistral-7B-GGUF"
|
| 19 |
MODEL_FILENAME = "distilabeled-Hermes-2.5-Mistral-7B.Q2_K.gguf"
|
| 20 |
HF_TOKEN = os.environ.get("HF_API_TOKEN")
|
| 21 |
CACHE_DIR = "/app/.cache/huggingface"
|
|
|
|
| 22 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 23 |
|
|
|
|
| 24 |
MODEL_PATH = hf_hub_download(
|
| 25 |
repo_id=REPO_ID,
|
| 26 |
filename=MODEL_FILENAME,
|
|
|
|
| 28 |
token=HF_TOKEN
|
| 29 |
)
|
| 30 |
|
|
|
|
| 31 |
llm = Llama(
|
| 32 |
model_path=MODEL_PATH,
|
| 33 |
n_ctx=2048,
|
| 34 |
n_threads=4,
|
| 35 |
+
n_gpu_layers=0
|
| 36 |
)
|
| 37 |
|
| 38 |
@api.route("/v1/chat/completions", methods=["POST"])
|
| 39 |
def chat_completions():
|
| 40 |
+
logger.info("Received request at /v1/chat/completions")
|
|
|
|
| 41 |
try:
|
| 42 |
data = request.get_json(force=True)
|
| 43 |
messages = data.get("messages", [])
|
|
|
|
| 46 |
top_p = float(data.get("top_p", config.get("top_p", 0.95)))
|
| 47 |
stop = data.get("stop", config.get("stop", ["User:", "Assistant:"]))
|
| 48 |
|
|
|
|
| 49 |
prompt_lines = []
|
| 50 |
for msg in messages:
|
| 51 |
role = msg.get("role", "").lower()
|
| 52 |
content = msg.get("content", "").strip()
|
| 53 |
+
if not content:
|
| 54 |
+
continue
|
| 55 |
if role == "user":
|
| 56 |
prompt_lines.append(f"User: {content}")
|
| 57 |
elif role == "assistant":
|
| 58 |
prompt_lines.append(f"Assistant: {content}")
|
| 59 |
+
|
| 60 |
prompt_lines.append("Assistant:")
|
| 61 |
prompt = "\n".join(prompt_lines)
|
| 62 |
|
|
|
|
| 63 |
response = llm(
|
| 64 |
prompt,
|
| 65 |
max_tokens=max_tokens,
|
|
|
|
| 68 |
stop=stop
|
| 69 |
)
|
| 70 |
|
| 71 |
+
result_text = ""
|
| 72 |
+
choices = response.get("choices")
|
| 73 |
+
if choices and isinstance(choices, list):
|
| 74 |
+
result_text = choices[0].get("text", "").strip()
|
| 75 |
|
|
|
|
| 76 |
return jsonify({
|
| 77 |
"id": f"chatcmpl-{uuid.uuid4().hex}",
|
| 78 |
"object": "chat.completion",
|
|
|
|
| 89 |
})
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
logger.error(f"Error in chat_completions: {e}", exc_info=True)
|
| 93 |
return jsonify({"error": str(e)}), 500
|