Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,135 @@
|
|
| 1 |
|
| 2 |
-
"""
|
| 3 |
-
|
| 4 |
-
v1 (GPU Space): DEFAULT_BACKEND=hf_local_gguf β runs DavidAU GGUF on GPU
|
| 5 |
-
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
| 9 |
-
import traceback
|
| 10 |
import subprocess
|
| 11 |
|
| 12 |
-
# ββ Runtime install: llama-cpp-python (
|
| 13 |
try:
|
| 14 |
import llama_cpp
|
| 15 |
except ImportError:
|
| 16 |
-
print("[RUNTIME] Installing llama-cpp-python
|
| 17 |
subprocess.check_call([
|
| 18 |
sys.executable, "-m", "pip", "install", "--no-cache-dir",
|
| 19 |
"--extra-index-url", "https://abetlen.github.io/llama-cpp-python/whl/cpu",
|
| 20 |
"llama-cpp-python"
|
| 21 |
])
|
| 22 |
import llama_cpp
|
| 23 |
-
print("[RUNTIME] llama-cpp-python installed
|
| 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 |
-
def _get_chairman_space_id() -> str:
|
| 70 |
-
return os.environ.get("CHAIRMAN_SPACE_ID", "kirikir13/mr-SmithDavinci_Full-Harness_v1")
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
try:
|
| 74 |
-
print("Importing smith_davinci...", file=sys.stderr)
|
| 75 |
-
from smith_davinci.engine import SmithEngine
|
| 76 |
-
from ui.app import create_ui
|
| 77 |
-
|
| 78 |
-
print("Creating engine...", file=sys.stderr)
|
| 79 |
-
engine = SmithEngine()
|
| 80 |
-
|
| 81 |
-
from smith_davinci.backends.hf_local_gguf import HFLocalGGUFBackend
|
| 82 |
-
engine.registry._backends["hf_local_gguf"] = HFLocalGGUFBackend
|
| 83 |
-
print("[REGISTRY] Force-registered hf_local_gguf backend", file=sys.stderr)
|
| 84 |
-
|
| 85 |
-
engine.default_backend = _get_default_backend()
|
| 86 |
-
engine.chairman_space_id = _get_chairman_space_id()
|
| 87 |
-
engine.is_hf_spaces = _is_on_hf_spaces()
|
| 88 |
-
|
| 89 |
-
print(
|
| 90 |
-
f"[ENV] Backend: {engine.default_backend} | "
|
| 91 |
-
f"Chairman Space: {engine.chairman_space_id} | "
|
| 92 |
-
f"On HF: {engine.is_hf_spaces}",
|
| 93 |
-
file=sys.stderr,
|
| 94 |
)
|
| 95 |
|
| 96 |
-
print("Creating UI...", file=sys.stderr)
|
| 97 |
-
demo = create_ui(engine)
|
| 98 |
-
|
| 99 |
-
print("DEMO CREATED β handing off to runtime", file=sys.stderr)
|
| 100 |
-
except Exception as e:
|
| 101 |
-
print("FATAL STARTUP ERROR:", repr(e), file=sys.stderr)
|
| 102 |
-
traceback.print_exc(file=sys.stderr)
|
| 103 |
-
raise
|
| 104 |
-
|
| 105 |
if __name__ == "__main__":
|
| 106 |
-
demo.launch(
|
| 107 |
-
debug=True,
|
| 108 |
-
share=False,
|
| 109 |
-
server_name="0.0.0.0",
|
| 110 |
-
server_port=7860,
|
| 111 |
-
)
|
|
|
|
| 1 |
|
| 2 |
+
"""codeMax β DavidAU coder/chat with file drop-in."""
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import os
|
| 5 |
import sys
|
|
|
|
| 6 |
import subprocess
|
| 7 |
|
| 8 |
+
# ββ Runtime install: llama-cpp-python (CPU) ββ
|
| 9 |
try:
|
| 10 |
import llama_cpp
|
| 11 |
except ImportError:
|
| 12 |
+
print("[RUNTIME] Installing llama-cpp-python (CPU)...", flush=True)
|
| 13 |
subprocess.check_call([
|
| 14 |
sys.executable, "-m", "pip", "install", "--no-cache-dir",
|
| 15 |
"--extra-index-url", "https://abetlen.github.io/llama-cpp-python/whl/cpu",
|
| 16 |
"llama-cpp-python"
|
| 17 |
])
|
| 18 |
import llama_cpp
|
| 19 |
+
print("[RUNTIME] llama-cpp-python installed", flush=True)
|
| 20 |
+
|
| 21 |
+
import gradio as gr
|
| 22 |
+
from huggingface_hub import hf_hub_download
|
| 23 |
+
from llama_cpp import Llama
|
| 24 |
+
|
| 25 |
+
# ββ Load DavidAU ββ
|
| 26 |
+
REPO_ID = "DavidAU/Qwen3.6-27B-Heretic-Uncensored-FINETUNE-NEO-CODE-Di-IMatrix-MAX-GGUF"
|
| 27 |
+
FILENAME = "Qwen3.6-27B-NEO-CODE-HERE-2T-OT-Q4_K_M.gguf"
|
| 28 |
+
|
| 29 |
+
print(f"[MODEL] Downloading {FILENAME}...", flush=True)
|
| 30 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 31 |
+
print(f"[MODEL] Loading...", flush=True)
|
| 32 |
+
|
| 33 |
+
llm = Llama(
|
| 34 |
+
model_path=model_path,
|
| 35 |
+
n_ctx=8192,
|
| 36 |
+
n_threads=8,
|
| 37 |
+
verbose=False,
|
| 38 |
+
)
|
| 39 |
+
print("[MODEL] Ready.", flush=True)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def build_prompt(message, history, system_prompt, file_content=None):
|
| 43 |
+
"""Build ChatML prompt with optional file content."""
|
| 44 |
+
parts = []
|
| 45 |
+
if system_prompt:
|
| 46 |
+
parts.append(f"<|system|>\n{system_prompt}</s>")
|
| 47 |
+
if history:
|
| 48 |
+
for msg in history:
|
| 49 |
+
role = msg.get("role", "user")
|
| 50 |
+
content = msg.get("content", "")
|
| 51 |
+
parts.append(f"<|{role}|>\n{content}</s>")
|
| 52 |
+
user_msg = message
|
| 53 |
+
if file_content:
|
| 54 |
+
user_msg = f"File contents:\n```\n{file_content}\n```\n\nUser: {message}"
|
| 55 |
+
parts.append(f"<|user|>\n{user_msg}</s>")
|
| 56 |
+
parts.append("<|assistant|>\n")
|
| 57 |
+
return "\n".join(parts)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def respond(message, history, system_prompt, file_upload):
|
| 61 |
+
"""Generate response from DavidAU."""
|
| 62 |
+
if not message.strip():
|
| 63 |
+
return ""
|
| 64 |
+
|
| 65 |
+
file_content = None
|
| 66 |
+
if file_upload is not None:
|
| 67 |
+
try:
|
| 68 |
+
if isinstance(file_upload, dict):
|
| 69 |
+
path = file_upload.get("name", file_upload.get("path", ""))
|
| 70 |
+
else:
|
| 71 |
+
path = str(file_upload)
|
| 72 |
+
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
| 73 |
+
file_content = f.read()
|
| 74 |
+
except Exception:
|
| 75 |
+
pass
|
| 76 |
+
|
| 77 |
+
# Convert history to dict format
|
| 78 |
+
history_dicts = []
|
| 79 |
+
if history:
|
| 80 |
+
for h in history:
|
| 81 |
+
if isinstance(h, (list, tuple)) and len(h) == 2:
|
| 82 |
+
history_dicts.append({"role": "user", "content": h[0]})
|
| 83 |
+
history_dicts.append({"role": "assistant", "content": h[1]})
|
| 84 |
+
|
| 85 |
+
prompt = build_prompt(message, history_dicts, system_prompt, file_content)
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
stream = llm(
|
| 89 |
+
prompt,
|
| 90 |
+
max_tokens=2048,
|
| 91 |
+
stop=["</s>", "<|user|>"],
|
| 92 |
+
stream=True,
|
| 93 |
+
)
|
| 94 |
+
response = ""
|
| 95 |
+
for output in stream:
|
| 96 |
+
token = output["choices"][0].get("text", "")
|
| 97 |
+
response += token
|
| 98 |
+
yield response
|
| 99 |
+
except Exception as e:
|
| 100 |
+
yield f"[Error: {e}]"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ββ UI ββ
|
| 104 |
+
with gr.Blocks(title="codeMax β DavidAU Coder", css="""
|
| 105 |
+
:root { --bg: #0d1117; --fg: #c9d1d9; --accent: #58a6ff; }
|
| 106 |
+
body, .gradio-container { background: var(--bg) !important; color: var(--fg) !important; }
|
| 107 |
+
.gr-button-primary { background: var(--accent) !important; }
|
| 108 |
+
""") as demo:
|
| 109 |
+
gr.Markdown("# codeMax\n### DavidAU Qwen3.6-27B β Coder + File Drop")
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column(scale=1):
|
| 112 |
+
system_box = gr.Textbox(
|
| 113 |
+
label="System Prompt",
|
| 114 |
+
value="You are an expert software engineer. Be concise, write clean code.",
|
| 115 |
+
lines=3,
|
| 116 |
+
)
|
| 117 |
+
file_input = gr.File(label="Drop code file here", file_types=[".py", ".js", ".ts", ".tsx", ".json", ".txt", ".md", ".html", ".css"])
|
| 118 |
+
with gr.Column(scale=3):
|
| 119 |
+
chatbot = gr.Chatbot(label="Chat", height=480, type="messages")
|
| 120 |
+
msg_box = gr.Textbox(label="Message", placeholder="Ask me to code something...", lines=2)
|
| 121 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 122 |
+
|
| 123 |
+
send_btn.click(
|
| 124 |
+
respond,
|
| 125 |
+
inputs=[msg_box, chatbot, system_box, file_input],
|
| 126 |
+
outputs=[chatbot],
|
| 127 |
)
|
| 128 |
+
msg_box.submit(
|
| 129 |
+
respond,
|
| 130 |
+
inputs=[msg_box, chatbot, system_box, file_input],
|
| 131 |
+
outputs=[chatbot],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|