Spaces:
Sleeping
Sleeping
File size: 11,127 Bytes
d237d26 6b4d4a0 d237d26 6b4d4a0 d237d26 6b4d4a0 d237d26 3f07cf9 022bd2f d237d26 022bd2f d237d26 6b4d4a0 d237d26 022bd2f d237d26 3437b02 d237d26 6b4d4a0 d237d26 3437b02 d237d26 6b4d4a0 d237d26 6b4d4a0 d237d26 6b4d4a0 d237d26 6b4d4a0 d237d26 6b4d4a0 d237d26 | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | """
SAM-AI Cloud Interface (ZeroGPU Space Edition)
==============================================
Dual-mode Gradio application offering SAM-AI Chat and SAM-AI Code, powered
exclusively by sovereign open-weight foundation model DeepSeek-R1-Distill-Qwen-7B.
"""
import os
import sys
import time
import gradio as gr
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent.resolve()))
from model_loader import engine, ModelLifecycleStatus, SAM_AI_SYSTEM_PROMPT
# ZeroGPU requires model loading at module level
print("[*] Starting SAM-AI ZeroGPU Space...")
try:
engine.load()
except Exception as exc:
print(f"[!] Warning: Initial model loading encountered exception: {exc}")
SYSTEM_PROMPT_CHAT = SAM_AI_SYSTEM_PROMPT
SYSTEM_PROMPT_CODE = (
"You are SAM-AI Code, a specialized sovereign software engineering assistant developed "
"by Parallax Intelligence Lab. You assist with coding, debugging, refactoring, and code review. "
"When suggesting changes, present code clearly and use standard unified diff format where appropriate. "
"Never invent fake library APIs or pretend unexecuted commands ran successfully."
)
try:
import spaces
except ImportError:
class _MockSpaces:
def GPU(self, duration=60):
def decorator(fn):
return fn
return decorator
spaces = _MockSpaces()
@spaces.GPU(duration=20)
def chat_respond(user_message, chat_history, temperature, top_p, max_tokens):
"""Handles multi-turn conversation streaming in SAM-AI Chat."""
if not user_message or not user_message.strip():
yield chat_history, ""
return
# Build history in OpenAI-style format
messages = []
for turn in (chat_history or []):
if turn[0]:
messages.append({"role": "user", "content": turn[0]})
if turn[1]:
messages.append({"role": "assistant", "content": turn[1]})
messages.append({"role": "user", "content": user_message})
# Append pending message to history (with deduplication guard)
history_list = [list(turn) for turn in (chat_history or [])]
if history_list and history_list[-1][0] == user_message and not history_list[-1][1]:
new_history = history_list
else:
new_history = history_list + [[user_message, ""]]
for partial_response in engine.generate_stream(
messages=messages,
temperature=temperature,
top_p=top_p,
max_new_tokens=int(max_tokens),
system_prompt=SYSTEM_PROMPT_CHAT
):
new_history[-1][1] = partial_response
yield new_history, ""
@spaces.GPU(duration=20)
def code_respond(user_prompt, code_context, task_type, temperature, max_tokens):
"""Handles coding tasks, explanations, and diff generation in SAM-AI Code."""
if not user_prompt or not user_prompt.strip():
yield "Please provide a coding prompt or question."
return
full_content = f"### Task: {task_type}\n\n"
if code_context and code_context.strip():
full_content += f"```\n{code_context.strip()}\n```\n\n"
full_content += f"**User Request:**\n{user_prompt.strip()}"
messages = [{"role": "user", "content": full_content}]
for partial_response in engine.generate_stream(
messages=messages,
temperature=temperature,
top_p=0.95,
max_new_tokens=int(max_tokens),
system_prompt=SYSTEM_PROMPT_CODE
):
yield partial_response
def get_status_markdown():
"""Generates real-time status banner for header."""
diag = engine.get_diagnostics()
status = diag["status"]
is_loaded = diag["is_loaded"]
device = diag["device"].upper()
adapter_name = Path(diag["adapter_dir"]).name if diag.get("adapter_dir") else "None (Pure Base)"
adapter_label = f" + <code>{adapter_name}</code>" if diag.get("adapter_dir") else " <em>(Pure Foundation Model)</em>"
if is_loaded:
badge = "🟢 **READY (SOVEREIGN ZERO-GPU)**"
color = "green"
elif status == ModelLifecycleStatus.LOADING_BASE_MODEL or status == ModelLifecycleStatus.ATTACHING_ADAPTER:
badge = "🟡 **LOADING WEIGHTS...**"
color = "orange"
else:
badge = f"🔴 **NOT READY: {status}**"
color = "red"
return (
f"<div style='border: 1px solid #E8E2DA; border-radius: 8px; padding: 12px 16px; background-color: #FDFCFA; color: #28231F; margin-bottom: 12px; font-family: sans-serif;'>"
f"<div style='display: flex; justify-content: space-between; align-items: center;'>"
f"<div><strong>Model:</strong> <code>{diag['base_model']}</code>{adapter_label}</div>"
f"<div><strong>Status:</strong> {badge} | <strong>Device:</strong> <code>{device}</code></div>"
f"</div>"
f"<div style='font-size: 0.85em; color: #6B5E55; margin-top: 4px;'>{diag['status_detail']}</div>"
f"</div>"
)
# Build Gradio UI with matching warm light theme (Ivory / Terracotta / Charcoal)
custom_theme = gr.themes.Soft(
primary_hue="orange",
secondary_hue="stone",
neutral_hue="stone",
).set(
body_background_fill="#FAF8F5",
body_text_color="#28231F",
block_background_fill="#FFFFFF",
block_border_color="#E8E2DA",
button_primary_background_fill="#D97745",
button_primary_background_fill_hover="#C86332",
button_primary_text_color="#FFFFFF",
)
with gr.Blocks(theme=custom_theme, title="SAM-AI Sovereign Assistant") as demo:
gr.Markdown(
"""
# 🧠 SAM-AI Sovereign Assistant
**Self-Hosted Open-Weight Intelligence | DeepSeek-R1-Distill-Qwen-7B (Pure Sovereign Foundation Model)**\
*Running on Hugging Face Spaces ZeroGPU (NVIDIA RTX Pro 6000 Blackwell)*
"""
)
status_header = gr.HTML(value=get_status_markdown)
with gr.Tabs() as tabs:
# =====================================================================
# Tab 1: SAM-AI Chat
# =====================================================================
with gr.Tab("💬 SAM-AI Chat", id="tab_chat"):
chatbot = gr.Chatbot(
height=520,
show_copy_button=True,
bubble_full_width=False,
render_markdown=True,
)
with gr.Row():
chat_input = gr.Textbox(
placeholder="Ask a question, mathematical problem, or reasoning challenge...",
show_label=False,
scale=8,
lines=2,
)
with gr.Column(scale=2, min_width=120):
send_btn = gr.Button("Send", variant="primary")
clear_btn = gr.Button("Clear Chat")
with gr.Accordion("⚙️ Inference Parameters", open=False):
with gr.Row():
chat_temp = gr.Slider(
minimum=0.0, maximum=1.0, value=0.6, step=0.05, label="Temperature (Creativity / Randomness)"
)
chat_top_p = gr.Slider(
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P (Nucleus Sampling)"
)
chat_max_tokens = gr.Slider(
minimum=256, maximum=4096, value=2048, step=128, label="Max New Tokens"
)
# Event wiring
chat_msg_event = chat_input.submit(
chat_respond,
inputs=[chat_input, chatbot, chat_temp, chat_top_p, chat_max_tokens],
outputs=[chatbot, chat_input],
api_name="chat_respond",
)
send_btn.click(
chat_respond,
inputs=[chat_input, chatbot, chat_temp, chat_top_p, chat_max_tokens],
outputs=[chatbot, chat_input],
api_name="chat_respond",
)
clear_btn.click(lambda: None, None, chatbot, queue=False)
# =====================================================================
# Tab 2: SAM-AI Code
# =====================================================================
with gr.Tab("💻 SAM-AI Code", id="tab_code"):
with gr.Row():
with gr.Column(scale=5):
code_task = gr.Radio(
choices=["Generate Code", "Debug & Fix", "Refactor & Optimize", "Generate Unified Diff"],
value="Generate Code",
label="Coding Task Type"
)
code_prompt = gr.Textbox(
label="Coding Instructions / Problem Description",
placeholder="e.g. Implement a thread-safe LRU cache with eviction telemetry in Python...",
lines=4,
)
code_context = gr.Code(
label="Existing Code / Context (Optional)",
language="python",
lines=12,
)
run_code_btn = gr.Button("Generate Solution", variant="primary")
with gr.Column(scale=5):
code_output = gr.Markdown(
label="SAM-AI Code Response",
value="*Click 'Generate Solution' to run SAM-AI Code on the assigned ZeroGPU.*"
)
with gr.Accordion("⚙️ Code Generation Parameters", open=False):
with gr.Row():
code_temp = gr.Slider(
minimum=0.0, maximum=1.0, value=0.2, step=0.05, label="Temperature (Lower = More Deterministic)"
)
code_max_tokens = gr.Slider(
minimum=512, maximum=4096, value=2048, step=128, label="Max Tokens"
)
run_code_btn.click(
code_respond,
inputs=[code_prompt, code_context, code_task, code_temp, code_max_tokens],
outputs=[code_output],
api_name="code_respond",
)
# =====================================================================
# Tab 3: Diagnostics & Sovereign Architecture
# =====================================================================
with gr.Tab("🛡️ Sovereign Diagnostics", id="tab_diag"):
refresh_diag_btn = gr.Button("Refresh Telemetry")
diag_json = gr.JSON(value=engine.get_diagnostics, label="Engine Diagnostic State")
refresh_diag_btn.click(engine.get_diagnostics, outputs=[diag_json])
gr.Markdown(
"""
---
**Parallax Intelligence Lab | SAM-AI Project**
*Zero commercial third-party inference APIs. Pure open-weight execution.*
"""
)
# Configure Gradio queue for ZeroGPU concurrency management
demo.queue(
max_size=20,
default_concurrency_limit=1,
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
)
|