import os import re import io import json import base64 import time import uuid import datetime import contextlib import urllib.request import html as _html from pathlib import Path # --- Preload CUDA runtime libs before importing llama_cpp --- # The cu124 llama-cpp-python wheel's libllama.so needs libcudart.so.12 / # libcublas at import time. On ZeroGPU those aren't on the default loader # path, so we dlopen the pip-provided nvidia libs (cudart first) globally. import ctypes import glob import site def _preload_cuda(): bases = set(site.getsitepackages()) try: bases.add(site.getusersitepackages()) except Exception: pass libs = [] for base in bases: libs += glob.glob(os.path.join(base, "nvidia", "*", "lib", "*.so*")) priority = {"cuda_runtime": 0, "cublas": 1} def _key(p): for name, rank in priority.items(): if name in p: return rank return 2 for so in sorted(set(libs), key=_key): try: ctypes.CDLL(so, mode=ctypes.RTLD_GLOBAL) except OSError: pass _preload_cuda() import gradio as gr import spaces # Gradio 5+ sanitizes gr.HTML by default, stripping ' '' '' ) # --------------------------------------------------------------------------- # Skills: reusable instruction snippets injected into the system prompt. # --------------------------------------------------------------------------- SKILLS = { "Single-file HTML artifact": ( "When the user wants a web app, page, game, or visual UI, WRITE ONE complete, " "self-contained HTML file (inline CSS and JS) to disk with a ```file:index.html block " "— never paste the file in the chat. No external files. Avoid CDNs unless asked." ), "Tailwind via Play CDN": ( "Style with Tailwind using the Play CDN (