File size: 10,003 Bytes
385c8f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Agent Zero β€” HF Spaces Native Version
Loads your actual ScottzillaSystems model weights directly via transformers.
No TGE endpoints, no LiteLLM proxy, no Docker Compose β€” works on any HF Space.
"""

import os
import re
import json
import asyncio
from pathlib import Path
from typing import List, Dict, Optional, Any
from threading import Thread

import gradio as gr
import spaces
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer


# ─── Configuration ───────────────────────────────────────────────────────────

AVAILABLE_MODELS = {
    "cydonia-24b": {
        "repo": "ScottzillaSystems/Cydonia-24B-v4.1",
        "description": "Cydonia 24B β€” Mistral-based general purpose",
        "tier": "T2",
        "device_map": "auto",
        "max_new_tokens": 2048,
    },
    "qwen3.5-27b": {
        "repo": "ScottzillaSystems/Qwen3.5-27B-Claude-4.6-Opus-Reasoning-Distilled",
        "description": "Qwen3.5 27B β€” Claude Opus distilled reasoning",
        "tier": "T3",
        "device_map": "auto",
        "max_new_tokens": 4096,
    },
    "qwen3.5-9b": {
        "repo": "ScottzillaSystems/Qwen3.5-9B-Chat",
        "description": "Qwen3.5 9B β€” Fast general purpose, daily driver",
        "tier": "T1",
        "device_map": "auto",
        "max_new_tokens": 2048,
    },
    "chatgpt5": {
        "repo": "ScottzillaSystems/ChatGPT-5-Chat",
        "description": "ChatGPT-5 494M β€” Ultra-fast router/classification",
        "tier": "T0",
        "device_map": "auto",
        "max_new_tokens": 1024,
    },
    "fallen-command": {
        "repo": "ScottzillaSystems/Fallen-Command-A-111B-Chat",
        "description": "Fallen Command 111B β€” Flagship reasoning",
        "tier": "T4",
        "device_map": "auto",
        "load_in_8bit": True,
        "max_new_tokens": 4096,
    },
}

DEFAULT_MODEL = "qwen3.5-9b"

_model_cache: Dict[str, Any] = {}
_tokenizer_cache: Dict[str, Any] = {}


# ─── Model Loading ───────────────────────────────────────────────────────────

def load_model(model_key: str):
    """Load model and tokenizer, caching in memory."""
    if model_key in _model_cache:
        return _model_cache[model_key], _tokenizer_cache[model_key]

    config = AVAILABLE_MODELS.get(model_key)
    if not config:
        raise ValueError(f"Unknown model: {model_key}")

    repo_id = config["repo"]
    print(f"[AgentZero] Loading {model_key} from {repo_id}...")

    tokenizer = AutoTokenizer.from_pretrained(
        repo_id, trust_remote_code=True, token=os.getenv("HF_TOKEN"),
    )
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token

    load_kwargs = {
        "pretrained_model_name_or_path": repo_id,
        "trust_remote_code": True,
        "token": os.getenv("HF_TOKEN"),
        "torch_dtype": torch.bfloat16,
        "device_map": config.get("device_map", "auto"),
    }
    if config.get("load_in_8bit"):
        load_kwargs["load_in_8bit"] = True

    model = AutoModelForCausalLM.from_pretrained(**load_kwargs)

    _model_cache[model_key] = model
    _tokenizer_cache[model_key] = tokenizer

    print(f"[AgentZero] {model_key} loaded")
    return model, tokenizer


def unload_model(model_key: str):
    if model_key in _model_cache:
        del _model_cache[model_key]
        del _tokenizer_cache[model_key]
        torch.cuda.empty_cache()
        return f"Unloaded {model_key}"
    return f"{model_key} not loaded"


def get_status():
    loaded = list(_model_cache.keys())
    mem = torch.cuda.memory_allocated() // 1024**3 if torch.cuda.is_available() else 0
    return f"Loaded: {', '.join(loaded) if loaded else 'none'} | GPU: {mem}GB"


# ─── Inference ───────────────────────────────────────────────────────────────

@spaces.GPU(duration=120)
def generate_stream(model_key, messages, max_new_tokens=None, temperature=0.7):
    model, tokenizer = load_model(model_key)
    config = AVAILABLE_MODELS[model_key]
    if max_new_tokens is None:
        max_new_tokens = config.get("max_new_tokens", 2048)

    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(prompt, return_tensors="pt", padding=True)
    inputs = {k: v.to(model.device) for k, v in inputs.items()}

    streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

    gen_kwargs = dict(
        inputs, streamer=streamer, max_new_tokens=max_new_tokens,
        do_sample=True, temperature=temperature, top_p=0.9,
        pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.eos_token_id,
    )

    Thread(target=model.generate, kwargs=gen_kwargs).start()
    for text in streamer:
        yield text


# ─── Gradio UI ───────────────────────────────────────────────────────────────

CSS = """
.az-header { text-align: center; padding: 20px; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); border-radius: 12px; margin-bottom: 16px; }
.az-header h1 { color: #e94560; margin: 0; font-size: 2em; }
.az-header p { color: #a0a0b0; margin: 4px 0 0 0; }
.model-card { background: #0f0f23; padding: 12px; border-radius: 8px; border-left: 4px solid #e94560; }
.tier-T0 { background: #00d4aa; color: #000; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; }
.tier-T1 { background: #00a8e8; color: #000; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; }
.tier-T2 { background: #f7b731; color: #000; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; }
.tier-T3 { background: #e94560; color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; }
.tier-T4 { background: #9b59b6; color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; }
"""


def create_ui():
    with gr.Blocks(css=CSS, title="Agent Zero v2") as demo:
        with gr.Column(elem_classes="az-header"):
            gr.HTML("<h1>πŸ€– Agent Zero v2</h1><p>Loading YOUR model weights β€” no proxies, no TGI, no lies</p>")

        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### Model")
                model_dd = gr.Dropdown(choices=list(AVAILABLE_MODELS.keys()), value=DEFAULT_MODEL, label="Active Model")
                model_info = gr.Markdown("Select a model")

                with gr.Accordion("Catalog", open=False):
                    rows = ""
                    for k, v in AVAILABLE_MODELS.items():
                        rows += f"<tr><td><b>{k}</b></td><td><span class='tier-{v['tier']}'>{v['tier']}</span></td><td>{v['description']}</td></tr>"
                    gr.HTML(f"<table width='100%'>{rows}</table>")

                with gr.Accordion("Settings", open=False):
                    max_tok = gr.Slider(128, 4096, value=2048, step=128, label="Max New Tokens")
                    temp = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")

                status = gr.Textbox(value="Ready", label="Status", interactive=False)

            with gr.Column(scale=3):
                chatbot = gr.Chatbot(type="messages", height=550, label="Agent Zero v2")
                with gr.Row():
                    msg = gr.Textbox(placeholder="Ask anything... model loads on first send", show_label=False, scale=8)
                    send = gr.Button("Send", scale=1, variant="primary")
                with gr.Row():
                    clear = gr.Button("πŸ—‘ Clear")
                    unload = gr.Button("πŸ”„ Unload")
                    statbtn = gr.Button("πŸ“Š Status")

        def update_info(k):
            c = AVAILABLE_MODELS.get(k, {})
            tier = c.get("tier", "T0")
            return (
                f"<div class='model-card'><b>{c.get('description', '?')}</b><br>"
                f"<span class='tier-{tier}'>{tier}</span> | "
                f"{c.get('max_new_tokens', '?')} tokens<br>"
                f"<code>{c.get('repo', '?')}</code></div>"
            )

        model_dd.change(update_info, model_dd, model_info)

        async def chat_fn(message, history, mk, mtok, tmp):
            if not message.strip():
                yield history, "", ""
            history = history or []
            history.append({"role": "user", "content": message})
            yield history, "", f"Loading {mk}..."
            try:
                msgs = [{"role": h["role"], "content": h["content"]} for h in history]
                out = ""
                for chunk in generate_stream(mk, msgs, mtok, tmp):
                    out += chunk
                    if history and history[-1]["role"] == "assistant":
                        history[-1]["content"] = out
                    else:
                        history.append({"role": "assistant", "content": out})
                    yield history, "", get_status()
            except Exception as e:
                history.append({"role": "assistant", "content": f"❌ Error: {e}"})
                yield history, "", get_status()

        send.click(chat_fn, [msg, chatbot, model_dd, max_tok, temp], [chatbot, msg, status])
        msg.submit(chat_fn, [msg, chatbot, model_dd, max_tok, temp], [chatbot, msg, status])
        clear.click(lambda: ([], "", "Ready"), outputs=[chatbot, msg, status])
        unload.click(lambda m: (unload_model(m), get_status()), model_dd, [status, status])
        statbtn.click(get_status, outputs=status)

    return demo


if __name__ == "__main__":
    demo = create_ui()
    demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")), share=False)