Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,11 @@ import site
|
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# 1. BIND TO THE PERSISTENT COMPILATION REGISTRY
|
| 10 |
PERSISTENT_PACKAGES = "/data/compiled_cache"
|
|
@@ -18,7 +23,6 @@ if TARGET_SITE_PATH not in sys.path:
|
|
| 18 |
sys.path.insert(0, TARGET_SITE_PATH)
|
| 19 |
site.addsitedir(TARGET_SITE_PATH)
|
| 20 |
|
| 21 |
-
# Pull the pre-compiled llama-cpp wheel built in your previous runtime pass
|
| 22 |
try:
|
| 23 |
from llama_cpp import Llama
|
| 24 |
print("π Perfect! Pre-compiled engine found in /data. Loading instantly...")
|
|
@@ -33,32 +37,28 @@ except ModuleNotFoundError:
|
|
| 33 |
site.addsitedir(TARGET_SITE_PATH)
|
| 34 |
from llama_cpp import Llama
|
| 35 |
|
| 36 |
-
import gradio as gr
|
| 37 |
from huggingface_hub import hf_hub_download
|
| 38 |
import spaces
|
| 39 |
|
| 40 |
-
# 2. MODEL
|
| 41 |
print("Checking persistent storage for AI model weights...")
|
| 42 |
|
| 43 |
-
# Model 1: The Main 27B Monster for GPU (3.9 GB)
|
| 44 |
path_27b = hf_hub_download(
|
| 45 |
repo_id="prism-ml/Bonsai-27B-gguf",
|
| 46 |
filename="Bonsai-27B-Q1_0.gguf",
|
| 47 |
local_dir="/data"
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# Model 2: Verified LiquidAI repo and file path
|
| 51 |
path_moe = hf_hub_download(
|
| 52 |
repo_id="LiquidAI/LFM2-8B-A1B-GGUF",
|
| 53 |
filename="LFM2-8B-A1B-Q4_K_M.gguf",
|
| 54 |
local_dir="/data"
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# Initialize the 8B MoE model to the unmetered CPU thread
|
| 58 |
print("Initializing Liquid 8B MoE on active CPU thread...")
|
| 59 |
llm_cpu = Llama(model_path=path_moe, n_ctx=4096, n_gpu_layers=0, verbose=False)
|
| 60 |
|
| 61 |
-
# 3. ENDPOINT
|
| 62 |
@spaces.GPU(duration=60)
|
| 63 |
def generate_27b(prompt):
|
| 64 |
clean_prompt = str(prompt).strip()
|
|
@@ -75,36 +75,27 @@ def generate_27b(prompt):
|
|
| 75 |
def generate_moe_cpu(prompt):
|
| 76 |
clean_prompt = str(prompt).strip()
|
| 77 |
if not clean_prompt: return "Empty prompt."
|
| 78 |
-
|
| 79 |
system_tool_prompt = "You are an advanced AI agent with Tool Calling capabilities."
|
| 80 |
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"
|
| 81 |
-
|
| 82 |
response = llm_cpu(formatted, max_tokens=512, temperature=0.1)
|
| 83 |
try: return response["choices"]["text"]
|
| 84 |
except: return str(response)
|
| 85 |
|
| 86 |
-
# 4. GRADIO
|
| 87 |
with gr.Blocks(title="Resilient AI Hub") as demo:
|
| 88 |
gr.Markdown("# π³ Unstoppable Split-Brain AI Hub")
|
| 89 |
-
|
| 90 |
with gr.Tab("π Bonsai 27B (GPU Endpoint)"):
|
| 91 |
input_27b = gr.Textbox(label="Enter prompt for 27B model (Uses Quota)", lines=6)
|
| 92 |
output_27b = gr.Textbox(label="GPU Response Output")
|
| 93 |
btn_27b = gr.Button("Submit to GPU")
|
| 94 |
btn_27b.click(fn=generate_27b, inputs=input_27b, outputs=output_27b, api_name="chat")
|
| 95 |
-
|
| 96 |
with gr.Tab("πͺ΅ Liquid 8B MoE (CPU Endpoint / Native Tool Calling)"):
|
| 97 |
input_moe = gr.Textbox(label="Enter prompt for MoE model (100% Free / Anti-Limit Backup)", lines=6)
|
| 98 |
output_moe = gr.Textbox(label="MoE CPU Output")
|
| 99 |
btn_moe = gr.Button("Submit to MoE Engine")
|
| 100 |
btn_moe.click(fn=generate_moe_cpu, inputs=input_moe, outputs=output_moe, api_name="chat_backup")
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
# 5. FASTAPI /V1 OPENAI COMPATIBILITY MOUNT
|
| 104 |
-
# ==========================================
|
| 105 |
-
# This acts as a background translator server for incoming OpenCode requests!
|
| 106 |
-
fastapi_app = FastAPI()
|
| 107 |
-
|
| 108 |
@fastapi_app.post("/v1/chat/completions")
|
| 109 |
async def openai_endpoints_router(request: Request):
|
| 110 |
try:
|
|
@@ -115,28 +106,23 @@ async def openai_endpoints_router(request: Request):
|
|
| 115 |
except Exception:
|
| 116 |
return JSONResponse({"error": "Invalid JSON context formatting payload"}, status_code=400)
|
| 117 |
|
| 118 |
-
# Route input context arrays directly to the right execution model function
|
| 119 |
if "liquid" in chosen_model or "cpu" in chosen_model or "backup" in chosen_model:
|
| 120 |
model_reply = generate_moe_cpu(user_prompt)
|
| 121 |
else:
|
| 122 |
model_reply = generate_27b(user_prompt)
|
| 123 |
|
| 124 |
-
# Standard OpenAI JSON dictionary schema response structure format
|
| 125 |
return JSONResponse({
|
| 126 |
"id": "hf-split-brain-chat",
|
| 127 |
"object": "chat.completion",
|
| 128 |
"model": chosen_model,
|
| 129 |
"choices": [{
|
| 130 |
"index": 0,
|
| 131 |
-
"message": {
|
| 132 |
-
"role": "assistant",
|
| 133 |
-
"content": model_reply
|
| 134 |
-
},
|
| 135 |
"finish_reason": "stop"
|
| 136 |
}]
|
| 137 |
})
|
| 138 |
|
| 139 |
-
#
|
| 140 |
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
import uvicorn
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Initialize FastAPI and Gradio interfaces at the absolute top layer
|
| 11 |
+
# This guarantees Hugging Face's orchestrator natively validates the environment routes on boot!
|
| 12 |
+
fastapi_app = FastAPI()
|
| 13 |
|
| 14 |
# 1. BIND TO THE PERSISTENT COMPILATION REGISTRY
|
| 15 |
PERSISTENT_PACKAGES = "/data/compiled_cache"
|
|
|
|
| 23 |
sys.path.insert(0, TARGET_SITE_PATH)
|
| 24 |
site.addsitedir(TARGET_SITE_PATH)
|
| 25 |
|
|
|
|
| 26 |
try:
|
| 27 |
from llama_cpp import Llama
|
| 28 |
print("π Perfect! Pre-compiled engine found in /data. Loading instantly...")
|
|
|
|
| 37 |
site.addsitedir(TARGET_SITE_PATH)
|
| 38 |
from llama_cpp import Llama
|
| 39 |
|
|
|
|
| 40 |
from huggingface_hub import hf_hub_download
|
| 41 |
import spaces
|
| 42 |
|
| 43 |
+
# 2. MODEL WEIGHT CONFIGURATIONS
|
| 44 |
print("Checking persistent storage for AI model weights...")
|
| 45 |
|
|
|
|
| 46 |
path_27b = hf_hub_download(
|
| 47 |
repo_id="prism-ml/Bonsai-27B-gguf",
|
| 48 |
filename="Bonsai-27B-Q1_0.gguf",
|
| 49 |
local_dir="/data"
|
| 50 |
)
|
| 51 |
|
|
|
|
| 52 |
path_moe = hf_hub_download(
|
| 53 |
repo_id="LiquidAI/LFM2-8B-A1B-GGUF",
|
| 54 |
filename="LFM2-8B-A1B-Q4_K_M.gguf",
|
| 55 |
local_dir="/data"
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
print("Initializing Liquid 8B MoE on active CPU thread...")
|
| 59 |
llm_cpu = Llama(model_path=path_moe, n_ctx=4096, n_gpu_layers=0, verbose=False)
|
| 60 |
|
| 61 |
+
# 3. ENDPOINT WORKFLOWS
|
| 62 |
@spaces.GPU(duration=60)
|
| 63 |
def generate_27b(prompt):
|
| 64 |
clean_prompt = str(prompt).strip()
|
|
|
|
| 75 |
def generate_moe_cpu(prompt):
|
| 76 |
clean_prompt = str(prompt).strip()
|
| 77 |
if not clean_prompt: return "Empty prompt."
|
|
|
|
| 78 |
system_tool_prompt = "You are an advanced AI agent with Tool Calling capabilities."
|
| 79 |
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"
|
|
|
|
| 80 |
response = llm_cpu(formatted, max_tokens=512, temperature=0.1)
|
| 81 |
try: return response["choices"]["text"]
|
| 82 |
except: return str(response)
|
| 83 |
|
| 84 |
+
# 4. GRADIO DUAL-TAB UI LAYOUT
|
| 85 |
with gr.Blocks(title="Resilient AI Hub") as demo:
|
| 86 |
gr.Markdown("# π³ Unstoppable Split-Brain AI Hub")
|
|
|
|
| 87 |
with gr.Tab("π Bonsai 27B (GPU Endpoint)"):
|
| 88 |
input_27b = gr.Textbox(label="Enter prompt for 27B model (Uses Quota)", lines=6)
|
| 89 |
output_27b = gr.Textbox(label="GPU Response Output")
|
| 90 |
btn_27b = gr.Button("Submit to GPU")
|
| 91 |
btn_27b.click(fn=generate_27b, inputs=input_27b, outputs=output_27b, api_name="chat")
|
|
|
|
| 92 |
with gr.Tab("πͺ΅ Liquid 8B MoE (CPU Endpoint / Native Tool Calling)"):
|
| 93 |
input_moe = gr.Textbox(label="Enter prompt for MoE model (100% Free / Anti-Limit Backup)", lines=6)
|
| 94 |
output_moe = gr.Textbox(label="MoE CPU Output")
|
| 95 |
btn_moe = gr.Button("Submit to MoE Engine")
|
| 96 |
btn_moe.click(fn=generate_moe_cpu, inputs=input_moe, outputs=output_moe, api_name="chat_backup")
|
| 97 |
|
| 98 |
+
# 5. OPENAI /V1 ROUTING HOOKS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
@fastapi_app.post("/v1/chat/completions")
|
| 100 |
async def openai_endpoints_router(request: Request):
|
| 101 |
try:
|
|
|
|
| 106 |
except Exception:
|
| 107 |
return JSONResponse({"error": "Invalid JSON context formatting payload"}, status_code=400)
|
| 108 |
|
|
|
|
| 109 |
if "liquid" in chosen_model or "cpu" in chosen_model or "backup" in chosen_model:
|
| 110 |
model_reply = generate_moe_cpu(user_prompt)
|
| 111 |
else:
|
| 112 |
model_reply = generate_27b(user_prompt)
|
| 113 |
|
|
|
|
| 114 |
return JSONResponse({
|
| 115 |
"id": "hf-split-brain-chat",
|
| 116 |
"object": "chat.completion",
|
| 117 |
"model": chosen_model,
|
| 118 |
"choices": [{
|
| 119 |
"index": 0,
|
| 120 |
+
"message": {"role": "assistant", "content": model_reply},
|
|
|
|
|
|
|
|
|
|
| 121 |
"finish_reason": "stop"
|
| 122 |
}]
|
| 123 |
})
|
| 124 |
|
| 125 |
+
# Mount Gradio over the core FastAPI application layers
|
| 126 |
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|