Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
"""
|
| 2 |
-
CodeNyx β
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
@@ -12,59 +14,51 @@ from transformers import (
|
|
| 12 |
BitsAndBytesConfig,
|
| 13 |
TextIteratorStreamer
|
| 14 |
)
|
| 15 |
-
from huggingface_hub import login
|
| 16 |
from threading import Thread
|
| 17 |
|
| 18 |
BOT_NAME = "CodeNyx"
|
| 19 |
-
MODEL_ID = "bigcode/starcoder2-3b"
|
|
|
|
| 20 |
MAX_NEW_TOK = 1024
|
| 21 |
TEMPERATURE = 0.2
|
| 22 |
TOP_P = 0.9
|
| 23 |
|
| 24 |
SYSTEM_PROMPT = (
|
| 25 |
-
f"You are {BOT_NAME}, an expert open-source coding assistant
|
| 26 |
-
"Always
|
| 27 |
)
|
| 28 |
|
| 29 |
# ------------------------------------------------------------------
|
| 30 |
-
# 1.
|
| 31 |
# ------------------------------------------------------------------
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
load_in_4bit=True,
|
| 40 |
-
bnb_4bit_compute_dtype=torch.float16,
|
| 41 |
-
bnb_4bit_quant_type="nf4",
|
| 42 |
-
bnb_4bit_use_double_quant=True,
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
tok = AutoTokenizer.from_pretrained(
|
| 46 |
-
MODEL_ID,
|
| 47 |
-
use_auth_token=token,
|
| 48 |
-
trust_remote_code=True,
|
| 49 |
-
)
|
| 50 |
-
if tok.pad_token is None:
|
| 51 |
-
tok.pad_token = tok.eos_token
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# ------------------------------------------------------------------
|
| 63 |
# 2. Chat helpers
|
| 64 |
# ------------------------------------------------------------------
|
| 65 |
-
tokenizer, model = None, None
|
| 66 |
-
|
| 67 |
def build_prompt(history, user_input):
|
|
|
|
| 68 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 69 |
for human, ai in history:
|
| 70 |
messages += [{"role": "user", "content": human},
|
|
@@ -101,13 +95,15 @@ def bot_turn(history):
|
|
| 101 |
# 3. Gradio UI
|
| 102 |
# ------------------------------------------------------------------
|
| 103 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
| 104 |
-
gr.Markdown(f"
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
with gr.Row():
|
| 109 |
msg = gr.Textbox(
|
| 110 |
-
placeholder="
|
| 111 |
lines=2,
|
| 112 |
scale=8,
|
| 113 |
show_label=False,
|
|
@@ -116,25 +112,13 @@ with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
|
| 116 |
send_btn = gr.Button("Send", scale=1, variant="primary")
|
| 117 |
clear_btn = gr.Button("ποΈ Clear")
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
demo.load(_load, None, status_lbl)
|
| 128 |
-
|
| 129 |
-
def _send(user_msg, hist):
|
| 130 |
-
return user_turn(user_msg, hist)
|
| 131 |
-
|
| 132 |
-
def _bot(hist):
|
| 133 |
-
yield from bot_turn(hist)
|
| 134 |
-
|
| 135 |
-
msg.submit(_send, [msg, chatbot], [msg, chatbot]).then(_bot, chatbot, chatbot)
|
| 136 |
-
send_btn.click(_send, [msg, chatbot], [msg, chatbot]).then(_bot, chatbot, chatbot)
|
| 137 |
-
clear_btn.click(lambda: None, None, chatbot)
|
| 138 |
|
| 139 |
# ------------------------------------------------------------------
|
| 140 |
# 4. Launch
|
|
|
|
| 1 |
"""
|
| 2 |
+
CodeNyx β 100 % ready-to-run Space
|
| 3 |
+
- Model loads at startup, no button needed
|
| 4 |
+
- 3 B StarCoder2 4-bit β < 8 GB RAM
|
| 5 |
+
- Answers generated directly from The Stack v2 (code-only corpus)
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
|
|
| 14 |
BitsAndBytesConfig,
|
| 15 |
TextIteratorStreamer
|
| 16 |
)
|
|
|
|
| 17 |
from threading import Thread
|
| 18 |
|
| 19 |
BOT_NAME = "CodeNyx"
|
| 20 |
+
MODEL_ID = "bigcode/starcoder2-3b"
|
| 21 |
+
|
| 22 |
MAX_NEW_TOK = 1024
|
| 23 |
TEMPERATURE = 0.2
|
| 24 |
TOP_P = 0.9
|
| 25 |
|
| 26 |
SYSTEM_PROMPT = (
|
| 27 |
+
f"You are {BOT_NAME}, an expert open-source coding assistant trained on "
|
| 28 |
+
"The Stack v2. Always return concise, runnable code snippets with brief explanations."
|
| 29 |
)
|
| 30 |
|
| 31 |
# ------------------------------------------------------------------
|
| 32 |
+
# 1. Startup model loader (runs once)
|
| 33 |
# ------------------------------------------------------------------
|
| 34 |
+
print("π CodeNyx β loading model β¦")
|
| 35 |
+
bnb_config = BitsAndBytesConfig(
|
| 36 |
+
load_in_4bit=True,
|
| 37 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 38 |
+
bnb_4bit_quant_type="nf4",
|
| 39 |
+
bnb_4bit_use_double_quant=True,
|
| 40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 43 |
+
MODEL_ID,
|
| 44 |
+
trust_remote_code=True,
|
| 45 |
+
)
|
| 46 |
+
if tokenizer.pad_token is None:
|
| 47 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 48 |
+
|
| 49 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
+
MODEL_ID,
|
| 51 |
+
quantization_config=bnb_config,
|
| 52 |
+
device_map="auto",
|
| 53 |
+
trust_remote_code=True,
|
| 54 |
+
)
|
| 55 |
+
print("β
CodeNyx model and tokenizer loaded.")
|
| 56 |
|
| 57 |
# ------------------------------------------------------------------
|
| 58 |
# 2. Chat helpers
|
| 59 |
# ------------------------------------------------------------------
|
|
|
|
|
|
|
| 60 |
def build_prompt(history, user_input):
|
| 61 |
+
"""Turn chat history into the modelβs prompt format."""
|
| 62 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 63 |
for human, ai in history:
|
| 64 |
messages += [{"role": "user", "content": human},
|
|
|
|
| 95 |
# 3. Gradio UI
|
| 96 |
# ------------------------------------------------------------------
|
| 97 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
| 98 |
+
gr.Markdown(f"""
|
| 99 |
+
# π€ {BOT_NAME} β AI Pair-Programmer
|
| 100 |
+
Trained on **The Stack v2** (4 T tokens, permissive licences).
|
| 101 |
+
Ask any coding question and receive **runnable code + short explanations**.
|
| 102 |
+
""")
|
| 103 |
+
chatbot = gr.Chatbot(height=500, label=f"{BOT_NAME} Chat")
|
| 104 |
with gr.Row():
|
| 105 |
msg = gr.Textbox(
|
| 106 |
+
placeholder="Type your programming question here β¦",
|
| 107 |
lines=2,
|
| 108 |
scale=8,
|
| 109 |
show_label=False,
|
|
|
|
| 112 |
send_btn = gr.Button("Send", scale=1, variant="primary")
|
| 113 |
clear_btn = gr.Button("ποΈ Clear")
|
| 114 |
|
| 115 |
+
msg.submit(user_turn, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 116 |
+
bot_turn, chatbot, chatbot
|
| 117 |
+
)
|
| 118 |
+
send_btn.click(user_turn, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 119 |
+
bot_turn, chatbot, chatbot
|
| 120 |
+
)
|
| 121 |
+
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# ------------------------------------------------------------------
|
| 124 |
# 4. Launch
|