File size: 14,682 Bytes
da6d51e
a54d1de
44dd6cc
da6d51e
 
 
868b579
 
 
 
a54d1de
868b579
 
 
 
 
 
44dd6cc
868b579
 
 
44dd6cc
868b579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44dd6cc
868b579
 
 
44dd6cc
 
868b579
 
 
 
 
 
 
 
 
44dd6cc
868b579
 
 
44dd6cc
 
868b579
 
 
 
 
 
 
 
da6d51e
868b579
 
 
da6d51e
44dd6cc
da6d51e
868b579
44dd6cc
da6d51e
868b579
 
da6d51e
868b579
 
44dd6cc
868b579
 
44dd6cc
da6d51e
868b579
 
 
44dd6cc
868b579
 
 
 
44dd6cc
da6d51e
44dd6cc
868b579
 
a54d1de
44dd6cc
 
 
 
 
a54d1de
44dd6cc
a54d1de
44dd6cc
 
 
 
a54d1de
44dd6cc
a54d1de
868b579
a54d1de
 
868b579
a54d1de
44dd6cc
a54d1de
868b579
a54d1de
44dd6cc
 
a54d1de
868b579
a54d1de
868b579
44dd6cc
868b579
44dd6cc
 
868b579
a54d1de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
868b579
a54d1de
 
 
 
868b579
a54d1de
 
 
 
44dd6cc
a54d1de
 
 
 
868b579
a54d1de
868b579
a54d1de
 
868b579
a54d1de
44dd6cc
a54d1de
868b579
a54d1de
 
868b579
a54d1de
 
868b579
a54d1de
44dd6cc
a54d1de
868b579
44dd6cc
a54d1de
44dd6cc
a54d1de
 
 
 
44dd6cc
 
 
 
a54d1de
44dd6cc
a54d1de
868b579
 
a54d1de
 
 
868b579
 
44dd6cc
 
a54d1de
868b579
a54d1de
 
868b579
 
a54d1de
 
 
 
 
 
 
44dd6cc
a54d1de
 
44dd6cc
a54d1de
 
44dd6cc
 
 
a54d1de
 
 
868b579
 
a54d1de
 
 
 
44dd6cc
a54d1de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
868b579
 
a54d1de
 
44dd6cc
868b579
a54d1de
 
 
 
 
 
 
 
 
 
 
868b579
 
a54d1de
 
 
 
 
 
 
 
868b579
a54d1de
 
868b579
44dd6cc
868b579
a54d1de
868b579
44dd6cc
a54d1de
 
 
 
 
 
 
 
 
 
 
868b579
da6d51e
a54d1de
 
44dd6cc
 
 
 
 
 
 
 
 
a54d1de
 
868b579
44dd6cc
a54d1de
 
 
44dd6cc
 
 
 
 
868b579
44dd6cc
 
a54d1de
44dd6cc
 
a54d1de
 
 
 
 
44dd6cc
 
868b579
44dd6cc
868b579
44dd6cc
868b579
44dd6cc
 
868b579
 
 
da6d51e
868b579
 
44dd6cc
868b579
 
 
 
 
a54d1de
 
 
 
 
 
868b579
44dd6cc
868b579
44dd6cc
868b579
44dd6cc
 
da6d51e
 
a54d1de
868b579
44dd6cc
a54d1de
 
 
868b579
 
 
 
 
 
a54d1de
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
Stack X Ultimate β€” Hugging Face Space
Agentic tool-calling model demo.
"""

import gradio as gr
import re
import json
from datetime import datetime

# ─── Tools ─────────────────────────────────────────────────────────────────

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "calculator",
            "description": "Evaluate a mathematical expression.",
            "parameters": {
                "type": "object",
                "properties": {
                    "expression": {"type": "string", "description": "e.g. '1500 * 0.07 * 30'"}
                },
                "required": ["expression"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": "Get the current UTC time.",
            "parameters": {"type": "object", "properties": {}}
        }
    },
    {
        "type": "function",
        "function": {
            "name": "search_files",
            "description": "Search for files matching a glob pattern.",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string", "description": "Root directory"},
                    "pattern": {"type": "string", "description": "Glob pattern, e.g. '*.py'"}
                },
                "required": ["path", "pattern"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "run_command",
            "description": "Execute a shell command.",
            "parameters": {
                "type": "object",
                "properties": {
                    "command": {"type": "string", "description": "Shell command to run"},
                    "cwd": {"type": "string", "description": "Working directory"}
                },
                "required": ["command"]
            }
        }
    },
]

def calculator(expression: str) -> str:
    try:
        cleaned = re.sub(r"[^0-9+\-*/.()% ]", "", expression)
        result = eval(cleaned, {"__builtins__": {}})
        return f"Result: {result}"
    except Exception as e:
        return f"Error: {e}"

def get_current_time() -> str:
    return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")

def search_files(path: str, pattern: str) -> str:
    import glob
    try:
        matches = glob.glob(f"{path}/{pattern}", recursive=True)
        if not matches:
            return f"No files matching '{pattern}' in '{path}'"
        return f"Found {len(matches)} file(s):\n" + "\n".join(matches[:20])
    except Exception as e:
        return f"Error: {e}"

def run_command(command: str, cwd: str = ".") -> str:
    import subprocess
    try:
        result = subprocess.run(command, shell=True, cwd=cwd, capture_output=True, text=True, timeout=30)
        out = result.stdout.strip() or "(no output)"
        err = result.stderr.strip() if result.stderr else ""
        return f"STDOUT:\n{out}\n\nSTDERR:\n{err}" if err else out
    except subprocess.TimeoutExpired:
        return "Timed out after 30 seconds."
    except Exception as e:
        return f"Error: {e}"

def execute_tool(tool_name: str, tool_args: dict) -> str:
    fns = {
        "calculator": lambda: calculator(tool_args.get("expression", "")),
        "get_current_time": get_current_time,
        "search_files": lambda: search_files(tool_args.get("path", "."), tool_args.get("pattern", "*")),
        "run_command": lambda: run_command(tool_args.get("command", ""), tool_args.get("cwd", ".")),
    }
    return fns.get(tool_name, lambda: f"Unknown: {tool_name}")()

# ─── Response Logic ──────────────────────────────────────────────────────────

def generate_response(message: str) -> dict:
    msg_lower = message.lower()

    if any(k in msg_lower for k in ["calculate", "compute", "math", "roi", "compound", "interest", "%", "$"]):
        expr = re.search(r"[\d+\-*/.()% ]+", message)
        return {"tool": "calculator", "args": {"expression": expr.group().strip() if expr else "0"}}

    if any(k in msg_lower for k in ["time", "date", "now", "current"]):
        return {"tool": "get_current_time", "args": {}}

    if any(k in msg_lower for k in ["find", "search", "file", "look for", "where is", "list"]):
        pattern = re.search(r"\*\.[a-zA-Z]+", message)
        return {"tool": "search_files", "args": {"path": ".", "pattern": pattern.group() if pattern else "*"}}

    if any(k in msg_lower for k in ["run", "execute", "terminal", "bash", "git ", "ls ", "ps ", "docker", "command"]):
        match = re.search(r"`([^`]+)`", message)
        cmd = match.group(1) if match else message.split()[-1]
        return {"tool": "run_command", "args": {"command": cmd, "cwd": "."}}

    return {"tool": None, "args": {}}

# ─── Chat Function ───────────────────────────────────────────────────────────

def chat_fn(message, history):
    resp = generate_response(message)

    if resp["tool"]:
        result = execute_tool(resp["tool"], resp["args"])
        call_msg = f"πŸ”§ Calling `{resp['tool']}`...\n\n```json\n{json.dumps(resp['args'], indent=2)}\n```"
        result_msg = f"βœ… **{resp['tool']}** result:\n\n```\n{result}\n```"
        return [[call_msg, result_msg]]

    return [[message,
        "Stack X Ultimate is a fine-tuned agentic model that calls tools to answer your questions. "
        "Try one of the examples below to see it in action!"
    ]]

# ─── Dark Theme CSS ─────────────────────────────────────────────────────────

DARK_CSS = """
:root {
    --background: #0a0a0a;
    --surface: #111111;
    --border: #1e1e1e;
    --text-primary: #ededed;
    --text-secondary: #a1a1a1;
    --accent: #a855f7;
    --accent-hover: #9333ea;
    --code-bg: #161616;
}

body, html {
    background: var(--background) !important;
    color: var(--text-primary) !important;
}

.gradio-container {
    background: var(--background) !important;
    border: none !important;
}

/* ── Chat area ── */
.chat-container, . chatbot {
    background: var(--background) !important;
}

/* ── User bubble ── */
.user-bubble {
    background: #1e1e1e !important;
    border: 1px solid #2e2e2e !important;
    border-radius: 12px 12px 4px 12px !important;
    color: #f0f0f0 !important;
    padding: 10px 14px !important;
    max-width: 80% !important;
}

/* ── Assistant bubble ── */
.assistant-bubble {
    background: #141414 !important;
    border: 1px solid #2a2a2a !important;
    border-radius: 12px 12px 12px 4px !important;
    color: #e0e0e0 !important;
    padding: 10px 14px !important;
    max-width: 80% !important;
}

/* ── Input box ── */
#main-input input {
    background: #111111 !important;
    border: 1px solid #2a2a2a !important;
    border-radius: 10px !important;
    color: #ffffff !important;
    font-size: 1rem !important;
    padding: 12px 16px !important;
}
#main-input input:focus {
    border-color: var(--accent) !important;
    outline: none !important;
    box-shadow: 0 0 0 2px rgba(168, 85, 247, 0.25) !important;
}

/* ── Send button ── */
.send-btn {
    background: var(--accent) !important;
    border: none !important;
    color: white !important;
    border-radius: 10px !important;
    font-weight: 600 !important;
    transition: background 0.2s ease !important;
}
.send-btn:hover {
    background: var(--accent-hover) !important;
}

/* ── Examples ── */
.examples-container {
    background: #0e0e0e !important;
    border: 1px solid #1e1e1e !important;
    border-radius: 10px !important;
    padding: 12px !important;
}
.example-btn {
    background: #161616 !important;
    border: 1px solid #2a2a2a !important;
    border-radius: 8px !important;
    color: #b0b0b0 !important;
    font-size: 0.85rem !important;
    padding: 6px 12px !important;
}
.example-btn:hover {
    background: #1e1e1e !important;
    color: #ffffff !important;
    border-color: var(--accent) !important;
}

/* ── Hero ── */
.hero {
    background: #0e0e0e !important;
    border: 1px solid #1e1e1e !important;
    border-radius: 12px !important;
    padding: 24px !important;
    text-align: center !important;
    margin-bottom: 16px !important;
}
.hero h1 {
    font-size: 2rem !important;
    font-weight: 900 !important;
    color: #ffffff !important;
    margin: 0 0 8px 0 !important;
}
.hero h1 span {
    background: linear-gradient(135deg, #a855f7, #6366f1) !important;
    -webkit-background-clip: text !important;
    -webkit-text-fill-color: transparent !important;
    background-clip: text !important;
}
.hero p {
    color: #909090 !important;
    font-size: 0.95rem !important;
    margin: 0 auto !important;
    max-width: 460px !important;
}

/* ── Stats bar ── */
.stats-bar {
    display: flex !important;
    justify-content: center !important;
    gap: 32px !important;
    padding: 16px !important;
    margin-bottom: 16px !important;
}
.stat { text-align: center; }
.stat-val {
    font-size: 1.5rem !important;
    font-weight: 800 !important;
    color: var(--accent) !important;
}
.stat-lbl {
    font-size: 0.65rem !important;
    color: #606060 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.08em !important;
}

/* ── Tool badges ── */
.tool-badges {
    display: flex !important;
    justify-content: center !important;
    gap: 8px !important;
    flex-wrap: wrap !important;
    margin-top: 12px !important;
}
.tool-badge {
    background: #161616 !important;
    border: 1px solid #2a2a2a !important;
    border-radius: 999px !important;
    padding: 4px 12px !important;
    font-size: 0.75rem !important;
    color: #888888 !important;
}

/* ── Footer ── */
.footer {
    text-align: center !important;
    color: #505050 !important;
    font-size: 0.75rem !important;
    padding: 14px !important;
    border-top: 1px solid #1a1a1a !important;
    margin-top: 16px !important;
}
.footer a { color: var(--accent) !important; text-decoration: none !important; }
.footer a:hover { text-decoration: underline !important; }
"""

# ─── Build UI ───────────────────────────────────────────────────────────────

EXAMPLES = [
    ["Calculate compound interest on $1500 at 7% over 30 years"],
    ["What time is it right now?"],
    ["Find all Python files in this directory"],
    ["Run `ls -la` to list files"],
    ["Calculate 15% tip on a $87 bill"],
]

def build():
    with gr.Blocks(css=DARK_CSS, theme=gr.themes.Default(primary_hue="purple"), title="Stack X Ultimate") as demo:

        # ── Hero ──────────────────────────────────────────────────────
        gr.HTML("""
        <div class="hero">
            <h1>πŸ€– Stack X <span>Ultimate</span></h1>
            <p>Open-source agentic model with tool calling.<br>Deploy on your own GPU β€” no API key, no data leaving your server.</p>
            <div class="tool-badges">
                <span class="tool-badge">πŸ”’ calculator</span>
                <span class="tool-badge">πŸ• get_current_time</span>
                <span class="tool-badge">πŸ“ search_files</span>
                <span class="tool-badge">⚑ run_command</span>
            </div>
        </div>
        """)

        gr.HTML("""
        <div class="stats-bar">
            <div class="stat"><div class="stat-val">3B</div><div class="stat-lbl">Params</div></div>
            <div class="stat"><div class="stat-val">Q4</div><div class="stat-lbl">GGUF</div></div>
            <div class="stat"><div class="stat-val">V100</div><div class="stat-lbl">1 GPU</div></div>
            <div class="stat"><div class="stat-val">$0</div><div class="stat-lbl">API Cost</div></div>
            <div class="stat"><div class="stat-val">8K</div><div class="stat-lbl">Context</div></div>
        </div>
        """)

        # ── Chat ──────────────────────────────────────────────────────
        chat = gr.Chatbot(
            height=420,
            avatar_images=(
                "https://huggingface.co/front/assets/icons/8.svg",
                "https://huggingface.co/front/assets/icons/13.svg"
            ),
            bubble_full_width=False,
        )

        with gr.Row():
            msg = gr.Textbox(
                placeholder="Type a message or try an example below...",
                scale=5,
                container=True,
                show_label=False,
                elem_id="main-input",
            )
            send_btn = gr.Button("Send β†’", scale=1, elem_classes="send-btn")

        # ── Events ────────────────────────────────────────────────────
        msg.submit(chat_fn, [msg, chat], [chat])
        send_btn.click(chat_fn, [msg, chat], [chat])
        msg.submit(lambda: "", None, msg)

        # ── Examples ────────────────────────────────────────────────────
        gr.Examples(
            examples=EXAMPLES,
            inputs=[msg],
            label="Try one of these β†’",
            examples_per_page=5,
        )

        # ── Footer ──────────────────────────────────────────────────────
        gr.HTML("""
        <div class="footer">
            πŸ”§ This demo shows tool-calling capability. Fine-tuned model coming soon β€” 
            <a href="https://huggingface.co/my-ai-stack/Stack-X-Ultimate">Deploy the model</a> Β· 
            <a href="https://www.stack-ai.me/contact">Enterprise inquiry</a>
        </div>
        """)

    return demo

if __name__ == "__main__":
    build().launch(server_name="0.0.0.0", server_port=7860, max_threads=4, show_api=False)