Spaces:
Running on Zero
Running on Zero
Download app.py from apathy-exe/Inkling-2-Mini: direct link, hf CLI and curl.
- Browser
- Download file 5.3 kB
-
https://huggingface.co/spaces/apathy-exe/Inkling-2-Mini/resolve/main/app.py
- Command line
-
hf download hf://spaces/apathy-exe/Inkling-2-Mini/app.py
-
curl -L -o app.py https://huggingface.co/spaces/apathy-exe/Inkling-2-Mini/resolve/main/app.py
5.3 kB
| from __future__ import annotations | |
| import json | |
| import os | |
| import subprocess | |
| import tarfile | |
| import time | |
| import urllib.request | |
| import zipfile | |
| from fastapi import Request | |
| from fastapi.responses import StreamingResponse | |
| from gradio import Server | |
| import httpx | |
| app = Server() | |
| try: | |
| import spaces | |
| HAS_SPACES = True | |
| except ImportError: | |
| HAS_SPACES = False | |
| def whygpu(): | |
| pass | |
| def build_latest_llama_server(): | |
| extract_dir = os.path.abspath("llama.cpp-prebuilt") | |
| binary_path = os.path.join(extract_dir, "llama-server") | |
| if os.path.exists(binary_path): | |
| return binary_path | |
| tag = "b12649-mix-c7059d6" | |
| api_url = f"https://api.github.com/repos/unslothai/llama.cpp/releases/tags/{tag}" | |
| req = urllib.request.Request(api_url, headers={"User-Agent": "python"}) | |
| with urllib.request.urlopen(req) as resp: | |
| release = json.loads(resp.read().decode()) | |
| assets = release.get("assets", []) | |
| # Pick CPU build if available (change to "cuda12-portable" / "cuda13-portable" if using GPU) | |
| target = next( | |
| (a for a in assets if "linux-x64" in a["name"] and "cpu" in a["name"] and a["name"].endswith(".tar.gz")), | |
| None, | |
| ) | |
| if target is None: | |
| names = [a["name"] for a in assets if "linux-x64" in a["name"]] | |
| raise RuntimeError(f"No linux-x64 CPU tarball found in {tag}. Available linux-x64 assets: {names}") | |
| tar_path = os.path.abspath("llama-prebuilt.tar.gz") | |
| print(f"Downloading {target['browser_download_url']}...") | |
| urllib.request.urlretrieve(target["browser_download_url"], tar_path) | |
| os.makedirs(extract_dir, exist_ok=True) | |
| with tarfile.open(tar_path, "r:gz") as tar: | |
| tar.extractall(extract_dir) | |
| os.remove(tar_path) | |
| # Some archives extract into a nested directory; locate llama-server | |
| if not os.path.exists(binary_path): | |
| for root, _, files in os.walk(extract_dir): | |
| if "llama-server" in files: | |
| binary_path = os.path.join(root, "llama-server") | |
| break | |
| if not os.path.exists(binary_path): | |
| raise RuntimeError("llama-server not found in extracted archive!") | |
| os.chmod(binary_path, 0o755) | |
| return binary_path | |
| def apathy_exe(): | |
| model_path = hf_hub_download( | |
| repo_id="thinkingmachines/Inkling-2-Mini", | |
| filename="q.gguf", | |
| ) | |
| def start_llama_server(): | |
| binary_path = build_latest_llama_server() | |
| from huggingface_hub import hf_hub_download | |
| model_path = hf_hub_download( | |
| repo_id="unsloth/Inkling-2-Mini-GGUF", | |
| filename="Inkling-2-Mini-UD-Q4_K_XL.gguf", | |
| ) | |
| mmproj_path = hf_hub_download( | |
| repo_id="unsloth/Inkling-2-Mini-GGUF", | |
| filename="mmproj-BF16.gguf", | |
| ) | |
| #draft_path = hf_hub_download( | |
| # repo_id="unsloth/Inkling-2-Mini-GGUF", | |
| # filename="dspark/dspark-Inkling-2-Mini-BF16.gguf", | |
| #) | |
| binary_dir = os.path.dirname(os.path.abspath(binary_path)) | |
| env = os.environ.copy() | |
| env["LD_LIBRARY_PATH"] = f"{binary_dir}:{env.get('LD_LIBRARY_PATH', '')}" | |
| cmd = [ | |
| binary_path, | |
| "-m", model_path, | |
| "-mm", mmproj_path, | |
| #"-md", draft_path, | |
| #"--spec-type", "draft-dspark", | |
| #"--spec-draft-n-max", "15", | |
| #"--spec-draft-p-min", "0.70", | |
| "--port", "8000", | |
| "--host", "127.0.0.1", | |
| "-t", "16", | |
| "-tb", "16", | |
| "-fa", "on", | |
| "--parallel", "1", | |
| "--load-mode", "mmap", | |
| "--no-warmup", | |
| "--no-repack", | |
| "-lzm", "on", | |
| "--cache-type-k", "q4_0", | |
| "--cache-type-v", "q4_0", | |
| "--jinja", | |
| "--temp", "1.0", | |
| "--top-p", "1.0", | |
| "--top-k", "0", | |
| "--min-p", "0.0", | |
| ] | |
| process = subprocess.Popen(cmd, env=env) | |
| time.sleep(5) | |
| return process | |
| async def proxy_middleware(request: Request, call_next): | |
| path = request.url.path | |
| if path.startswith("/gradio_api"): | |
| return await call_next(request) | |
| url = f"http://127.0.0.1:8000{path}" | |
| headers = { | |
| k: v for k, v in request.headers.items() | |
| if k.lower() not in ("host", "content-length", "accept-encoding") | |
| } | |
| body = await request.body() | |
| client = httpx.AsyncClient(timeout=httpx.Timeout(600.0, connect=120.0)) | |
| req = client.build_request( | |
| method=request.method, | |
| url=url, | |
| headers=headers, | |
| params=request.query_params, | |
| content=body, | |
| ) | |
| try: | |
| response = await client.send(req, stream=True) | |
| except Exception as e: | |
| await client.aclose() | |
| return StreamingResponse(iter([f"Proxy Error: {e}".encode()]), status_code=502) | |
| async def stream_and_close(): | |
| try: | |
| async for chunk in response.aiter_raw(): | |
| yield chunk | |
| finally: | |
| await response.aclose() | |
| await client.aclose() | |
| res_headers = { | |
| k: v for k, v in response.headers.items() | |
| if k.lower() not in ("content-length", "transfer-encoding", "connection") | |
| } | |
| return StreamingResponse( | |
| stream_and_close(), | |
| status_code=response.status_code, | |
| headers=res_headers | |
| ) | |
| if __name__ == "__main__": | |
| start_llama_server() | |
| app.launch(show_error=True) |