Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# CONFIGURATION
|
| 6 |
+
MODEL_PATH = "Bonsai-1.7B-v1.0.gguf"
|
| 7 |
+
SYSTEM_PROMPT = "You are AGA-Bonsai, a 1-bit native intelligence for Synaptic DNA and NorthWatch. Be concise, street-smart, and local-aware."
|
| 8 |
+
|
| 9 |
+
def generate_bonsai(message, history, temperature, top_p, max_tokens):
|
| 10 |
+
# Format the prompt with history and system instructions
|
| 11 |
+
full_prompt = f"System: {SYSTEM_PROMPT}\n"
|
| 12 |
+
for human, assistant in history:
|
| 13 |
+
full_prompt += f"User: {human}\nAssistant: {assistant}\n"
|
| 14 |
+
full_prompt += f"User: {message}\nAssistant:"
|
| 15 |
+
|
| 16 |
+
# Call the pre-built binary (ensure llama-cli is in your path)
|
| 17 |
+
cmd = [
|
| 18 |
+
"./llama-cli",
|
| 19 |
+
"-m", MODEL_PATH,
|
| 20 |
+
"-p", full_prompt,
|
| 21 |
+
"-n", str(max_tokens),
|
| 22 |
+
"--temp", str(temperature),
|
| 23 |
+
"--top-p", str(top_p),
|
| 24 |
+
"--threads", "2", # Perfect for your Celeron N2830
|
| 25 |
+
"--repeat_penalty", "1.1",
|
| 26 |
+
"--no-display-prompt"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 31 |
+
response = ""
|
| 32 |
+
for line in process.stdout:
|
| 33 |
+
response += line
|
| 34 |
+
yield response
|
| 35 |
+
except Exception as e:
|
| 36 |
+
yield f"Error: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# UI DESIGN
|
| 39 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 40 |
+
gr.Markdown("# 🌿 Bonsai 1-bit Sandbox")
|
| 41 |
+
gr.Markdown("### AGA Global Tech: Testing Environment for Synaptic DNA & NorthWatch")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column(scale=4):
|
| 45 |
+
chatbot = gr.ChatInterface(
|
| 46 |
+
fn=generate_bonsai,
|
| 47 |
+
additional_inputs=[
|
| 48 |
+
gr.Slider(0.1, 1.0, value=0.5, label="Temperature (Logic vs Creativity)"),
|
| 49 |
+
gr.Slider(0.1, 1.0, value=0.9, label="Top-P"),
|
| 50 |
+
gr.Slider(64, 2048, value=512, label="Max Tokens"),
|
| 51 |
+
],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
with gr.Column(scale=1):
|
| 55 |
+
gr.Markdown("## Test Battery")
|
| 56 |
+
test_sec = gr.Button("🚨 NorthWatch Test")
|
| 57 |
+
test_code = gr.Button("💻 Synaptic DNA Test")
|
| 58 |
+
|
| 59 |
+
# Pre-defined test triggers
|
| 60 |
+
def sec_test(): return "Analyze this report: 'Movement of 20 unidentified motorcycles near Birnin Gwari forest, heading South.' Extract JSON with location and threat level."
|
| 61 |
+
def code_test(): return "Show me a C struct for a BitLinear layer optimized for 4GB RAM."
|
| 62 |
+
|
| 63 |
+
# These would set the chat input (simplified for this design)
|
| 64 |
+
test_sec.click(fn=lambda: "🚨 Security Test triggered. Copy/Paste: " + sec_test(), outputs=None)
|
| 65 |
+
test_code.click(fn=lambda: "💻 Coding Test triggered. Copy/Paste: " + code_test(), outputs=None)
|
| 66 |
+
|
| 67 |
+
demo.queue().launch()
|