Skip to content

Commit 6bf895b

Browse files
unamedkrclaude
andcommitted
fix(python): add model aliases to API + graceful port conflict handling
1. Model aliases in Python API (#74): - `Model.from_pretrained("llama3.2:1b")` now works (was: only full names) - Added `_MODEL_ALIASES` dict and `_resolve_model_name()` in __init__.py - Same aliases as CLI: smollm2, llama3.2:1b, phi3.5:mini, etc. 2. Port conflict handling (#75): - `quantcpp serve` now checks if port is in use before launching - Shows: "error: port 8080 is already in use. Try --port 8081" - Previously: SIGABRT crash with no error message Fixes #74, #75 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 34e5539 commit 6bf895b

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

bindings/python/quantcpp/__init__.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,36 @@ def _download_with_progress(url: str, dest: Path, desc: str) -> None:
155155
tmp.rename(dest)
156156

157157

158+
_MODEL_ALIASES = {
159+
"smollm2": "SmolLM2-1.7B",
160+
"smollm2:1.7b": "SmolLM2-1.7B",
161+
"smollm2:135m": "SmolLM2-135M",
162+
"qwen3.5": "Qwen3.5-0.8B",
163+
"qwen3.5:0.8b": "Qwen3.5-0.8B",
164+
"llama3.2": "Llama-3.2-1B",
165+
"llama3.2:1b": "Llama-3.2-1B",
166+
"phi3.5": "Phi-3.5-mini",
167+
"phi3.5:mini": "Phi-3.5-mini",
168+
"phi-3.5": "Phi-3.5-mini",
169+
"phi-3.5-mini": "Phi-3.5-mini",
170+
}
171+
172+
173+
def _resolve_model_name(name: str) -> str:
174+
"""Resolve alias or case-insensitive name to canonical registry key."""
175+
if name in _MODEL_REGISTRY:
176+
return name
177+
return _MODEL_ALIASES.get(name.lower(), name)
178+
179+
158180
def download(name: str) -> str:
159181
"""Download a model from HuggingFace Hub and return its local path.
160182
161183
Parameters
162184
----------
163185
name : str
164-
Model name from the registry. Currently available:
165-
``"SmolLM2-135M"`` (~135 MB, good for testing).
186+
Model name or alias. Examples: ``"Phi-3.5-mini"``, ``"phi3.5:mini"``,
187+
``"smollm2"``, ``"llama3.2:1b"``.
166188
167189
Returns
168190
-------
@@ -171,9 +193,10 @@ def download(name: str) -> str:
171193
172194
Examples
173195
--------
174-
>>> path = quantcpp.download("SmolLM2-135M")
196+
>>> path = quantcpp.download("phi3.5:mini")
175197
>>> m = quantcpp.Model(path)
176198
"""
199+
name = _resolve_model_name(name)
177200
if name not in _MODEL_REGISTRY:
178201
avail = ", ".join(sorted(_MODEL_REGISTRY))
179202
raise ValueError(

bindings/python/quantcpp/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ def cmd_serve(args):
250250
print(" Or install via your package manager.", file=sys.stderr)
251251
return 2
252252

253+
# Check if port is available before launching server
254+
import socket
255+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
256+
if sock.connect_ex(("127.0.0.1", args.port)) == 0:
257+
print(f"error: port {args.port} is already in use.", file=sys.stderr)
258+
print(f" Try a different port: quantcpp serve {args.model} --port {args.port + 1}",
259+
file=sys.stderr)
260+
return 1
261+
253262
cmd = [binary, model_path, "-p", str(args.port), "-j", str(args.threads)]
254263
print(f"quantcpp serve {os.path.basename(model_path)} on :{args.port}", file=sys.stderr)
255264
print("", file=sys.stderr)

0 commit comments

Comments
 (0)