Spaces:
Running on Zero
Running on Zero
File size: 7,094 Bytes
ba70497 33c0c25 ba70497 33c0c25 ba70497 33c0c25 ba70497 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | """A stand-in for Ollama on hosts that cannot run it (a free Hugging Face Space).
Runs Qwen3 1.7B (the same Q4_K_M build Ollama serves as qwen3:1.7b) in-process with llama.cpp and
answers the two Ollama calls the tower makes, so the tower's code does not change. With GROQ_API_KEY
set, each call goes to Groq first (groq_client.py) and this model answers only when Groq cannot:
GET /api/tags the model list (the tower checks the model is there)
POST /api/chat one chat turn: messages, an optional JSON schema ("format") the answer must
follow, optional tools, options temperature / seed / num_predict, think false
python deploy/space/local_model.py listens on 127.0.0.1:11434 (LOCAL_MODEL_PORT)
"""
from __future__ import annotations
import json
import os
import re
import threading
import uuid
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
REPO = os.environ.get("LOCAL_MODEL_REPO", "unsloth/Qwen3-1.7B-GGUF")
FILE = os.environ.get("LOCAL_MODEL_FILE", "Qwen3-1.7B-Q4_K_M.gguf")
NAME = os.environ.get("TOWER_LLM_MODEL", "qwen3:1.7b") # the name the tower asks for
CTX = int(os.environ.get("LOCAL_MODEL_CTX", "8192"))
PORT = int(os.environ.get("LOCAL_MODEL_PORT", "11434"))
_llm = None
_template = None
_lock = threading.Lock() # one generation at a time: llama.cpp's model object is not thread-safe
_TOOL_CALL = re.compile(r"<tool_call>\s*(\{.*?\})\s*</tool_call>", re.S)
_THINK = re.compile(r"<think>.*?</think>\s*", re.S)
def load(path: str | None = None) -> None:
"""Download the model from Hugging Face (cached after the first time) and load it."""
global _llm, _template
from llama_cpp import Llama
import jinja2
if path is None:
from huggingface_hub import hf_hub_download
path = hf_hub_download(REPO, FILE)
_llm = Llama(model_path=path, n_ctx=CTX, n_threads=os.cpu_count() or 2, n_gpu_layers=-1, verbose=False)
# The model's own chat template, rendered with thinking off (as Ollama does with think: false).
source = _llm.metadata["tokenizer.chat_template"]
env = jinja2.Environment(loader=jinja2.BaseLoader(), trim_blocks=True, lstrip_blocks=True)
env.globals["raise_exception"] = lambda msg: (_ for _ in ()).throw(ValueError(msg))
_template = env.from_string(source)
def _prompt(messages: list[dict], tools: list | None) -> str:
msgs = []
for m in messages:
m = dict(m)
if m.get("tool_calls"):
# Ollama sends arguments as objects; the template accepts objects or strings.
m["tool_calls"] = [{"type": "function", "function": c.get("function", c)} for c in m["tool_calls"]]
m.setdefault("content", "")
msgs.append(m)
return _template.render(messages=msgs, tools=tools or None, add_generation_prompt=True, enable_thinking=False)
def chat(body: dict) -> dict:
"""One /api/chat request -> Ollama's reply shape. Groq first when a key is set (fast), then the
model on this machine. Without a local model loaded (GROQ_ONLY=1, for tests), Groq or an error."""
import groq_client
if groq_client.enabled():
while True:
try:
return groq_client.answer(body)
except groq_client.GroqBusy:
if _llm is not None:
break
# Groq only (tests): wait for a model's allowance instead of failing the reading.
import time
time.sleep(min(90.0, groq_client.next_free() + 0.5))
return _local_chat(body)
def _local_chat(body: dict) -> dict:
from llama_cpp.llama_grammar import LlamaGrammar
opts = body.get("options") or {}
tools = body.get("tools")
prompt = _prompt(body.get("messages") or [], tools)
fmt = body.get("format")
grammar = None
if isinstance(fmt, dict):
grammar = LlamaGrammar.from_json_schema(json.dumps(fmt), verbose=False)
elif fmt == "json":
grammar = LlamaGrammar.from_json_schema(json.dumps({"type": "object"}), verbose=False)
with _lock:
out = _llm.create_completion(
prompt, grammar=grammar, max_tokens=int(opts.get("num_predict") or 1024),
temperature=float(opts.get("temperature", 0)), seed=int(opts.get("seed", 7)),
stop=["<|im_end|>", "<|endoftext|>"],
)
text = _THINK.sub("", out["choices"][0]["text"]).strip()
message = {"role": "assistant", "content": text}
if tools:
calls = []
# The tags are special tokens that llama.cpp may leave out of the text: then each call is a
# bare {"name", "arguments"} object on its own line.
found = _TOOL_CALL.findall(text) or [ln for ln in text.splitlines() if ln.strip().startswith("{") and '"name"' in ln]
for raw in found:
try:
call = json.loads(raw)
except json.JSONDecodeError:
continue
args = call.get("arguments") or {}
if isinstance(args, str):
try:
args = json.loads(args)
except json.JSONDecodeError:
args = {}
calls.append({"id": uuid.uuid4().hex[:8], "function": {"name": call.get("name", ""), "arguments": args}})
if calls:
message = {"role": "assistant", "content": "", "tool_calls": calls}
return {"model": body.get("model", NAME), "message": message, "done": True}
class _Handler(BaseHTTPRequestHandler):
def log_message(self, *args): # quiet: the tower logs what matters
pass
def _send(self, code: int, obj: dict) -> None:
data = json.dumps(obj).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
if self.path.startswith("/api/tags"):
return self._send(200, {"models": [{"name": NAME, "model": NAME}]})
self._send(404, {"error": "not found"})
def do_POST(self):
if not self.path.startswith("/api/chat"):
return self._send(404, {"error": "not found"})
try:
body = json.loads(self.rfile.read(int(self.headers.get("Content-Length") or 0)) or b"{}")
self._send(200, chat(body))
except Exception as exc: # report it; the tower treats a failed call as a failed reading
self._send(500, {"error": str(exc)})
def serve(host: str = "127.0.0.1", port: int = PORT) -> ThreadingHTTPServer:
"""Start answering in a background thread; returns the server."""
server = ThreadingHTTPServer((host, port), _Handler)
server.daemon_threads = True
threading.Thread(target=server.serve_forever, daemon=True).start()
return server
if __name__ == "__main__":
import sys
if os.environ.get("GROQ_ONLY") != "1":
load(sys.argv[1] if len(sys.argv) > 1 else None)
serve()
print(f"model ready on http://127.0.0.1:{PORT} as {NAME}", flush=True)
threading.Event().wait()
|