Spaces:
Running on Zero
Running on Zero
File size: 20,219 Bytes
6e7902e 1a220a8 d2bc8de 1a220a8 65287ee 154253c 65287ee 66415d8 d2bc8de 3e7e8e0 66415d8 65287ee d2bc8de 3e7e8e0 8f3f117 d2bc8de 65287ee d2bc8de 65287ee d2bc8de 8f3f117 d2bc8de 66415d8 d2bc8de 8f3f117 d2bc8de b71e6e4 d2bc8de 65287ee d2bc8de 6e7902e d2bc8de 1a220a8 d2bc8de 8763daa d2bc8de 6e7902e d2bc8de 66415d8 d2bc8de 1a220a8 d2bc8de 1a220a8 d2bc8de 1a220a8 d2bc8de 1a220a8 d2bc8de 6e7902e d2bc8de 6e7902e d2bc8de 6e7902e d2bc8de 6e7902e 1a220a8 6e7902e d2bc8de 6e7902e 3e7e8e0 d2bc8de 1a220a8 d2bc8de 65287ee d2bc8de 3e7e8e0 6e7902e 7734b88 | 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | import os
import re
import time
import traceback
from pathlib import Path
from typing import Any, Dict, List, Tuple
from config import *
import torch
from fastapi import Request
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from gradio import Server
from transformers import AutoModelForCausalLM, AutoTokenizer
# ββ ZeroGPU Detection βββββββββββββββββββββββββββββββββββββββββββββββββββ
IS_ZEROGPU = False
try:
import spaces
from spaces.config import Config as _ZeroGPUConfig
IS_ZEROGPU = bool(_ZeroGPUConfig.zero_gpu)
except ImportError:
class _DummySpaces:
@staticmethod
def GPU(fn):
return fn
spaces = _DummySpaces()
# ββ Configuration βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
DEFAULT_SYSTEM_PROMPT = (
"You are ChatUI, a helpful local AI assistant. "
"You provide accurate, detailed, and thoughtful responses."
)
STATIC_DIR = Path(__file__).parent / "static"
os.makedirs(STATIC_DIR, exist_ok=True)
# ββ Fallback chat templates for tokenizers missing .chat_template βββββββ
_FALLBACK_TEMPLATES = {
"chatml": (
"{% for message in messages %}"
"{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}"
"{% endfor %}"
"{% if add_generation_prompt %}"
"{{'<|im_start|>assistant\n'}}"
"{% endif %}"
),
"llama2": (
"{% if messages[0]['role'] == 'system' %}"
"{{'<<SYS>>\n' + messages[0]['content'] + '\n<</SYS>>\n\n'}}"
"{% set loop_messages = messages[1:] %}"
"{% else %}"
"{% set loop_messages = messages %}"
"{% endif %}"
"{% for message in loop_messages %}"
"{% if message['role'] == 'user' %}"
"{{'[INST] ' + message['content'] + ' [/INST]'}}"
"{% elif message['role'] == 'assistant' %}"
"{{message['content'] + '</s>'}}"
"{% endif %}"
"{% endfor %}"
),
"phi3": (
"{% for message in messages %}"
"{% if message['role'] == 'system' %}"
"{{'<|system|>\n' + message['content'] + '<|end|>\n'}}"
"{% elif message['role'] == 'user' %}"
"{{'<|user|>\n' + message['content'] + '<|end|>\n'}}"
"{% elif message['role'] == 'assistant' %}"
"{{'<|assistant|>\n' + message['content'] + '<|end|>\n'}}"
"{% endif %}"
"{% endfor %}"
"{% if add_generation_prompt %}"
"{{'<|assistant|>\n'}}"
"{% endif %}"
),
"qwen": (
"{% for message in messages %}"
"{% if message['role'] == 'system' %}"
"{{'<|im_start|>system\n' + message['content'] + '<|im_end|>\n'}}"
"{% elif message['role'] == 'user' %}"
"{{'<|im_start|>user\n' + message['content'] + '<|im_end|>\n'}}"
"{% elif message['role'] == 'assistant' %}"
"{{'<|im_start|>assistant\n' + message['content'] + '<|im_end|>\n'}}"
"{% endif %}"
"{% endfor %}"
"{% if add_generation_prompt %}"
"{{'<|im_start|>assistant\n'}}"
"{% endif %}"
),
}
def _pick_fallback_template(model_id: str) -> str:
mid = model_id.lower()
if "phi" in mid:
return _FALLBACK_TEMPLATES["phi3"]
if "qwen" in mid:
return _FALLBACK_TEMPLATES["qwen"]
if "tinyllama" in mid or "llama" in mid:
return _FALLBACK_TEMPLATES["llama2"]
return _FALLBACK_TEMPLATES["chatml"]
def _ensure_chat_template(tokenizer, model_id: str):
if getattr(tokenizer, "chat_template", None):
return
tpl = _pick_fallback_template(model_id)
tokenizer.chat_template = tpl
print(f"[chat_template] injected fallback template for {model_id}")
# ββ Model cache (lazy) ββββββββββββββββββββββββββββββββββββββββββββββββββ
_MODEL_CACHE: Dict[str, Dict[str, Any]] = {}
def _device() -> str:
if IS_ZEROGPU:
return "cuda"
if torch.cuda.is_available():
return "cuda"
if getattr(torch.backends, "mps", None) and torch.backends.mps.is_available():
return "mps"
return "cpu"
def _dtype():
if IS_ZEROGPU or torch.cuda.is_available():
return torch.float16
return torch.float32
def get_model(model_id: str) -> Dict[str, Any]:
if model_id in _MODEL_CACHE:
return _MODEL_CACHE[model_id]
if model_id not in MODEL_REGISTRY:
raise ValueError(f"Unknown model: {model_id}")
device = _device()
dtype = _dtype()
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
_ensure_chat_template(tokenizer, model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=dtype,
trust_remote_code=True,
low_cpu_mem_usage=True,
).to(device)
model.eval()
_MODEL_CACHE[model_id] = {
"tokenizer": tokenizer,
"model": model,
"device": device,
"loaded_at": time.time(),
}
return _MODEL_CACHE[model_id]
# ββ Generation core βββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _flatten_content(content) -> str:
if isinstance(content, str):
return content
parts: List[str] = []
for c in content or []:
if isinstance(c, dict) and c.get("type") == "text":
parts.append(c.get("text", ""))
return "\n".join(parts).strip()
def _build_prompt(model_id: str, messages: List[Dict[str, Any]],
system_prompt: str) -> str:
bundle = get_model(model_id)
tokenizer = bundle["tokenizer"]
flat_messages: List[Dict[str, str]] = []
if system_prompt and system_prompt.strip():
flat_messages.append({"role": "system", "content": system_prompt.strip()})
for m in messages:
role = m.get("role", "user")
if role == "system":
continue
text = _flatten_content(m.get("content", ""))
if text:
flat_messages.append({"role": role, "content": text})
if getattr(tokenizer, "chat_template", None):
try:
return tokenizer.apply_chat_template(
flat_messages, tokenize=False, add_generation_prompt=True
)
except Exception as e:
print(f"[chat_template] apply failed for {model_id}: {e}; using manual fallback")
# Manual fallback
lines = [f"{m['role']}: {m['content']}" for m in flat_messages]
lines.append("assistant:")
return "\n".join(lines) + "\n"
@spaces.GPU
def generate(model_id: str, messages: List[Dict[str, Any]],
system_prompt: str, max_tokens: int, temperature: float,
top_p: float = 0.95) -> Tuple[str, str]:
bundle = get_model(model_id)
tokenizer = bundle["tokenizer"]
model = bundle["model"]
device = bundle["device"]
prompt = _build_prompt(model_id, messages, system_prompt)
inputs = tokenizer(prompt, return_tensors="pt").to(device)
do_sample = temperature > 0.01
with torch.inference_mode():
out = model.generate(
**inputs,
max_new_tokens=max_tokens,
do_sample=do_sample,
temperature=max(temperature, 1e-3) if do_sample else 1.0,
top_p=top_p if do_sample else 1.0,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
new_tokens = out[0][inputs["input_ids"].shape[1]:]
text = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
reasoning = ""
think_match = re.search(r"<think[^>]*>(.*?)</think\s*>", text, re.DOTALL | re.IGNORECASE)
if think_match:
reasoning = think_match.group(1).strip()
text = re.sub(r"<think[^>]*>.*?</think\s*>", "", text, flags=re.DOTALL | re.IGNORECASE).strip()
return text, reasoning
# ββ Agent tools βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _tool_calculator(expression: str) -> str:
try:
if not re.fullmatch(r"[\d+\-*/().%\s]+", expression):
return "Error: only numeric expressions are allowed."
result = eval(expression, {"__builtins__": {}}, {})
return f"= {result}"
except Exception as e:
return f"Error: {e}"
def _tool_web_search(query: str) -> str:
return (
f"[stub search] I cannot reach the web (no API key configured), "
f"but for query '{query}' you might want to check Wikipedia, "
f"Stack Overflow, or arXiv. Use the calculator for any maths."
)
def _tool_file_read(path: str) -> str:
safe_root = Path(__file__).parent.resolve()
try:
target = (safe_root / path).resolve()
target.relative_to(safe_root)
if not target.exists() or not target.is_file():
return f"Error: file not found: {path}"
text = target.read_text(encoding="utf-8", errors="ignore")
if len(text) > 4000:
text = text[:4000] + "\nβ¦[truncated]"
return text
except Exception as e:
return f"Error: {e}"
def _tool_now(_arg: str = "") -> str:
return time.strftime("%Y-%m-%d %H:%M:%S %Z")
AGENT_TOOLS = {
"calculator": _tool_calculator,
"web_search": _tool_web_search,
"file_read": _tool_file_read,
"now": _tool_now,
}
AGENT_TOOL_DESCRIPTION = """You have access to the following tools. To use one,
reply with a single line that starts with `TOOL:` followed by the tool name and
its argument in parentheses, e.g.:
TOOL: calculator(2 * (3 + 4))
TOOL: web_search(latest news about mars)
TOOL: file_read(static/index.html)
TOOL: now()
After receiving the tool output, you may use another tool or give your final
answer. When you are done, start your final message with `FINAL:`.
"""
@spaces.GPU
def run_agent(model_id: str, user_text: str, system_prompt: str,
max_tokens: int, temperature: float,
max_iters: int = 4) -> Dict[str, Any]:
history = [
{"role": "system", "content": system_prompt + "\n\n" + AGENT_TOOL_DESCRIPTION},
{"role": "user", "content": user_text},
]
trace: List[Dict[str, str]] = []
for i in range(max_iters):
answer, _reason = generate(
model_id, history,
system_prompt="",
max_tokens=max_tokens, temperature=temperature,
)
answer_stripped = answer.strip()
if answer_stripped.upper().startswith("FINAL:"):
final = answer_stripped[6:].strip()
trace.append({"step": i + 1, "type": "final", "content": final})
return {"final": final, "trace": trace}
m = re.match(r"\s*TOOL:\s*([A-Za-z_]+)\s*\((.*)\)\s*$",
answer_stripped, re.DOTALL)
if not m:
trace.append({"step": i + 1, "type": "final", "content": answer_stripped})
return {"final": answer_stripped, "trace": trace}
tool_name = m.group(1).strip().lower()
tool_arg = m.group(2).strip()
trace.append({
"step": i + 1,
"type": "tool_call",
"tool": tool_name,
"argument": tool_arg,
})
fn = AGENT_TOOLS.get(tool_name)
if fn is None:
tool_result = (f"Error: unknown tool '{tool_name}'. "
f"Available: {list(AGENT_TOOLS.keys())}")
else:
try:
tool_result = fn(tool_arg)
except Exception as e:
tool_result = f"Error: {e}"
trace.append({
"step": i + 1,
"type": "tool_result",
"tool": tool_name,
"output": tool_result,
})
history.append({"role": "assistant", "content": answer_stripped})
history.append({
"role": "user",
"content": (f"TOOL OUTPUT: {tool_result}\nNow either call another "
f"tool or finish with FINAL: <your answer>."),
})
return {
"final": "I ran out of steps before producing a final answer.",
"trace": trace,
}
# ββ Gradio Server App βββββββββββββββββββββββββββββββββββββββββββββββββββ
app = Server(title="ChatUI Studio", version="2.0.0")
@app.get("/api/models")
async def api_list_models():
return JSONResponse({
"status": "success",
"default": DEFAULT_MODEL,
"models": [
{"id": mid, **meta}
for mid, meta in MODEL_REGISTRY.items()
],
"cached": list(_MODEL_CACHE.keys()),
})
@app.get("/api/tasks")
async def api_list_tasks():
return JSONResponse({
"status": "success",
"tasks": [
{"id": tid, **meta}
for tid, meta in TASK_REGISTRY.items()
],
})
@app.post("/api/load_model")
async def api_load_model(req: Request):
body = await req.json()
model_id = body.get("model_id") or DEFAULT_MODEL
try:
if model_id not in MODEL_REGISTRY:
return JSONResponse({"status": "error", "message": "Unknown model."})
get_model(model_id)
return JSONResponse({
"status": "success",
"model": model_id,
"device": _MODEL_CACHE[model_id]["device"],
})
except Exception as e:
traceback.print_exc()
return JSONResponse({"status": "error", "message": str(e)})
@app.post("/api/run")
async def api_run_inference(req: Request):
try:
body = await req.json()
messages: List[Dict[str, Any]] = body.get("messages", [])
model_id = body.get("model_id") or DEFAULT_MODEL
task = body.get("task") or "chat"
max_tokens_i = int(body.get("max_tokens", 512))
temperature_f = float(body.get("temperature", 0.7))
system_prompt = body.get("system_prompt", "") or ""
if model_id not in MODEL_REGISTRY:
return JSONResponse({"status": "error", "message": f"Unknown model: {model_id}"})
task_meta = TASK_REGISTRY.get(task, TASK_REGISTRY["chat"])
sys_prompt = (system_prompt.strip() or task_meta["system_prompt"])
if task == "agent":
user_text = _flatten_content(messages[-1].get("content", "")) if messages else ""
result = run_agent(
model_id=model_id,
user_text=user_text,
system_prompt=sys_prompt,
max_tokens=max_tokens_i,
temperature=temperature_f,
)
return JSONResponse({
"status": "success",
"task": "agent",
"model": model_id,
"content": result["final"],
"trace": result["trace"],
"reasoning_content": "",
})
content, reasoning = generate(
model_id=model_id,
messages=messages,
system_prompt=sys_prompt,
max_tokens=max_tokens_i,
temperature=temperature_f,
)
return JSONResponse({
"status": "success",
"task": task,
"model": model_id,
"content": content,
"reasoning_content": reasoning or "",
})
except Exception as e:
traceback.print_exc()
return JSONResponse({"status": "error", "message": str(e)})
@app.get("/")
async def homepage():
html_path = STATIC_DIR / "index.html"
if html_path.exists():
with open(html_path, "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read(), status_code=200)
return HTMLResponse(
content="<h1>Frontend is building. Please refresh in a few seconds...</h1>",
status_code=200,
)
@app.get("/healthz")
async def healthz():
return JSONResponse({
"status": "ok",
"zerogpu": IS_ZEROGPU,
"device": _device(),
"models_cached": list(_MODEL_CACHE.keys()),
"models_registered": list(MODEL_REGISTRY.keys()),
})
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
if __name__ == "__main__":
import argparse
import inspect
parser = argparse.ArgumentParser(
description="ChatUI Studio β local AI chat server",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--host", "--server-name", dest="server_name", default="0.0.0.0",
help="Host/IP to bind to.",
)
parser.add_argument(
"--port", "--server-port", dest="server_port", type=int, default=7860,
help="Port to listen on.",
)
parser.add_argument(
"--share", dest="share", action="store_true",
help="Create a public Gradio share link.",
)
parser.add_argument(
"--no-share", dest="share", action="store_false",
help="Disable public share link.",
)
parser.set_defaults(share=False)
parser.add_argument(
"--show-error", dest="show_error", action="store_true",
help="Show full error tracebacks in the UI.",
)
parser.add_argument(
"--no-show-error", dest="show_error", action="store_false",
help="Hide error tracebacks in the UI.",
)
parser.set_defaults(show_error=True)
parser.add_argument(
"--quiet", action="store_true",
help="Suppress Gradio startup messages.",
)
parser.add_argument(
"--auth", nargs=2, metavar=("USER", "PASS"), default=None,
help="Enable basic auth with USER and PASS.",
)
parser.add_argument(
"--ssl-keyfile", default=None,
help="Path to SSL key file (enables HTTPS).",
)
parser.add_argument(
"--ssl-certfile", default=None,
help="Path to SSL certificate file.",
)
parser.add_argument(
"--ssl-keyfile-password", default=None,
help="Password for the SSL key file, if any.",
)
parser.add_argument(
"--favicon-path", default=None,
help="Path to a custom favicon.",
)
parser.add_argument(
"--allowed-paths", nargs="*", default=None,
help="Extra paths Gradio is allowed to serve from.",
)
parser.add_argument(
"--blocked-paths", nargs="*", default=None,
help="Paths Gradio must never serve.",
)
parser.add_argument(
"--max-file-size", default=None,
help="Max upload size (e.g. '10mb', '1gb').",
)
args = parser.parse_args()
launch_kwargs = {
"server_name": args.server_name,
"server_port": args.server_port,
"share": args.share,
"show_error": args.show_error,
"quiet": args.quiet,
"auth": tuple(args.auth) if args.auth else None,
}
if args.ssl_keyfile:
launch_kwargs["ssl_keyfile"] = args.ssl_keyfile
if args.ssl_certfile:
launch_kwargs["ssl_certfile"] = args.ssl_certfile
if args.ssl_keyfile_password:
launch_kwargs["ssl_keyfile_password"] = args.ssl_keyfile_password
if args.favicon_path:
launch_kwargs["favicon_path"] = args.favicon_path
if args.allowed_paths:
launch_kwargs["allowed_paths"] = args.allowed_paths
if args.blocked_paths:
launch_kwargs["blocked_paths"] = args.blocked_paths
if args.max_file_size:
launch_kwargs["max_file_size"] = args.max_file_size
# Drop kwargs that aren't supported by this Gradio version.
try:
supported = set(inspect.signature(app.launch).parameters.keys())
if "kwargs" not in supported:
launch_kwargs = {k: v for k, v in launch_kwargs.items() if k in supported}
except (TypeError, ValueError):
pass
app.launch(**launch_kwargs) |