"""
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" + {adapter_name}" if diag.get("adapter_dir") else " (Pure Foundation Model)"
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"
{diag['base_model']}{adapter_label}{device}