Spaces:
Running on Zero
Running on Zero
File size: 4,062 Bytes
a45589e d944f50 650bbe0 d9bb3b3 a45589e cc7fd33 d944f50 b2ce466 a81da75 650bbe0 d944f50 650bbe0 cc7fd33 650bbe0 d944f50 d9bb3b3 650bbe0 d944f50 650bbe0 d944f50 757414d d944f50 4366340 d944f50 d9bb3b3 a81da75 d944f50 d9bb3b3 a81da75 a45589e a81da75 d9bb3b3 d944f50 a81da75 d944f50 a81da75 d944f50 a45589e d9bb3b3 d944f50 a81da75 b722630 a81da75 d944f50 a45589e d9bb3b3 | 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 | import os
import sys
import subprocess
import site
import threading
# 1. BIND TO THE PERSISTENT COMPILATION REGISTRY
PERSISTENT_PACKAGES = "/data/compiled_cache"
os.environ["PYTHONUSERBASE"] = PERSISTENT_PACKAGES
os.environ["HF_HOME"] = "/data/.huggingface"
os.environ["XDG_CACHE_HOME"] = "/data/.cache"
TARGET_SITE_PATH = f"{PERSISTENT_PACKAGES}/lib/python3.12/site-packages"
if TARGET_SITE_PATH not in sys.path:
sys.path.insert(0, TARGET_SITE_PATH)
site.addsitedir(TARGET_SITE_PATH)
try:
from llama_cpp import Llama
except ModuleNotFoundError:
print("⏳ First boot detected. Compiling hardware engine into permanent storage /data...")
os.makedirs(TARGET_SITE_PATH, exist_ok=True)
subprocess.check_call([
sys.executable, "-m", "pip", "install", "--user",
"llama-cpp-python",
"--extra-index-url", "https://github.io"
])
site.addsitedir(TARGET_SITE_PATH)
from llama_cpp import Llama
import gradio as gr
from huggingface_hub import hf_hub_download
import spaces
# 2. MODEL WEIGHT CONFIGURATIONS
print("Checking persistent storage for AI model weights...")
path_27b = hf_hub_download(repo_id="prism-ml/Bonsai-27B-gguf", filename="Bonsai-27B-Q1_0.gguf", local_dir="/data")
path_moe = hf_hub_download(repo_id="LiquidAI/LFM2-8B-A1B-GGUF", filename="LFM2-8B-A1B-Q4_K_M.gguf", local_dir="/data")
print("Initializing Liquid 8B MoE on active CPU thread...")
llm_cpu = Llama(model_path=path_moe, n_ctx=4096, n_gpu_layers=0, verbose=False)
# 3. NATIVE GRADIO WORKFLOWS (Ensures perfect ZeroGPU startup scans)
@spaces.GPU(duration=60)
def generate_27b(prompt):
clean_prompt = str(prompt).strip()
if not clean_prompt: return "Empty prompt."
llm_gpu = Llama(model_path=path_27b, n_ctx=32768, n_gpu_layers=-1, n_batch=512, ctk_quant=2, ctv_quant=2, flash_attn=True, verbose=False)
formatted = f"<|user|>\n{clean_prompt}<|endoftext|>\n<|assistant|>"
response = llm_gpu(formatted, max_tokens=512, temperature=0.7)
try: return response["choices"]["text"]
except: return str(response)
def generate_moe_cpu(prompt):
clean_prompt = str(prompt).strip()
if not clean_prompt: return "Empty prompt."
system_tool_prompt = "You are an advanced AI agent with Tool Calling capabilities."
formatted = f"<|im_start|>system\n{system_tool_prompt}<|im_end|>\n<|im_start|>user\n{clean_prompt}<|im_end|>\n<|im_start|>assistant\n"
response = llm_cpu(formatted, max_tokens=512, temperature=0.1)
try: return response["choices"]["text"]
except: return str(response)
# 4. START THE OPENAI COMPATIBILITY API AS A COMPANION PROCESS
def run_openai_api():
try:
import uvicorn
# This triggers our proxy script securely in the background without locking ZeroGPU scans
uvicorn.run("openai_api:api", host="0.0.0.0", port=8000, log_level="warning")
except Exception as e:
print(f"API Startup Error: {e}")
# Fire up the translator server on background thread port 8000
threading.Thread(target=run_openai_api, daemon=True).start()
# 5. GRADIO DASHBOARD
with gr.Blocks(title="Resilient AI Hub") as demo:
gr.Markdown("# 🌳 Unstoppable Split-Brain AI Hub")
with gr.Tab("🚀 Bonsai 27B (GPU Endpoint)"):
input_27b = gr.Textbox(label="Enter prompt for 27B model (Uses Quota)", lines=6)
output_27b = gr.Textbox(label="GPU Response Output")
btn_27b = gr.Button("Submit to GPU")
btn_27b.click(fn=generate_27b, inputs=input_27b, outputs=output_27b, api_name="chat")
with gr.Tab("🪵 Liquid 8B MoE (CPU Endpoint / Native Tool Calling)"):
input_moe = gr.Textbox(label="Enter prompt for MoE model (100% Free / Anti-Limit Backup)", lines=6)
output_moe = gr.Textbox(label="MoE CPU Output")
btn_moe = gr.Button("Submit to MoE Engine")
btn_moe.click(fn=generate_moe_cpu, inputs=input_moe, outputs=output_moe, api_name="chat_backup")
if __name__ == "__main__":
# Mount everything onto the default web configuration port
demo.launch(server_name="0.0.0.0", server_port=7860)
|