Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
CodeNyx β HF-Space
|
| 3 |
-
|
| 4 |
-
- 4-bit quant β < 8 GB VRAM
|
| 5 |
-
- Accepts HF token only for gated models (none required here)
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
@@ -17,26 +15,25 @@ from transformers import (
|
|
| 17 |
from huggingface_hub import login
|
| 18 |
from threading import Thread
|
| 19 |
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
SYSTEM_PROMPT = (
|
| 26 |
f"You are {BOT_NAME}, an expert open-source coding assistant. "
|
| 27 |
"Always provide concise, runnable code snippets with short explanations."
|
| 28 |
)
|
| 29 |
-
MAX_NEW_TOK = 1024
|
| 30 |
-
TEMPERATURE = 0.2
|
| 31 |
-
TOP_P = 0.9
|
| 32 |
|
| 33 |
# ------------------------------------------------------------------
|
| 34 |
-
#
|
| 35 |
# ------------------------------------------------------------------
|
| 36 |
-
def load_model(token: str
|
| 37 |
-
"""Return (
|
|
|
|
| 38 |
if token:
|
| 39 |
-
login(token)
|
| 40 |
|
| 41 |
bnb_config = BitsAndBytesConfig(
|
| 42 |
load_in_4bit=True,
|
|
@@ -45,35 +42,35 @@ def load_model(token: str = None):
|
|
| 45 |
bnb_4bit_use_double_quant=True,
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
MODEL_ID,
|
| 50 |
-
use_auth_token=token
|
| 51 |
trust_remote_code=True,
|
| 52 |
)
|
| 53 |
-
if
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
MODEL_ID,
|
| 58 |
quantization_config=bnb_config,
|
| 59 |
device_map="auto",
|
| 60 |
-
use_auth_token=token
|
| 61 |
trust_remote_code=True,
|
| 62 |
)
|
| 63 |
-
return
|
| 64 |
|
| 65 |
# ------------------------------------------------------------------
|
| 66 |
-
#
|
| 67 |
# ------------------------------------------------------------------
|
|
|
|
|
|
|
| 68 |
def build_prompt(history, user_input):
|
| 69 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 70 |
for human, ai in history:
|
| 71 |
-
messages
|
| 72 |
-
|
| 73 |
messages.append({"role": "user", "content": user_input})
|
| 74 |
-
return tokenizer.apply_chat_template(
|
| 75 |
-
messages, tokenize=False, add_generation_prompt=True
|
| 76 |
-
)
|
| 77 |
|
| 78 |
def user_turn(user_message, history):
|
| 79 |
return "", history + [[user_message, ""]]
|
|
@@ -82,12 +79,7 @@ def bot_turn(history):
|
|
| 82 |
prompt = build_prompt(history[:-1], history[-1][0])
|
| 83 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 84 |
|
| 85 |
-
streamer = TextIteratorStreamer(
|
| 86 |
-
tokenizer,
|
| 87 |
-
skip_prompt=True,
|
| 88 |
-
skip_special_tokens=True,
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
gen_kwargs = dict(
|
| 92 |
**inputs,
|
| 93 |
max_new_tokens=MAX_NEW_TOK,
|
|
@@ -106,27 +98,16 @@ def bot_turn(history):
|
|
| 106 |
yield history
|
| 107 |
|
| 108 |
# ------------------------------------------------------------------
|
| 109 |
-
#
|
| 110 |
# ------------------------------------------------------------------
|
| 111 |
-
tokenizer, model = None, None # lazy load
|
| 112 |
-
|
| 113 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
| 114 |
-
gr.Markdown(f""
|
| 115 |
-
# π€ {BOT_NAME} β AI Pair-Programmer
|
| 116 |
-
*Public model β no token needed.*
|
| 117 |
-
Ask any coding question and get **runnable code + short explanations**.
|
| 118 |
-
""")
|
| 119 |
-
|
| 120 |
-
# Optional token box (for future gated models)
|
| 121 |
-
token_box = gr.Textbox(label="π€ HF Token (optional)", type="password", visible=False)
|
| 122 |
-
load_btn = gr.Button("Load / Reload", visible=False)
|
| 123 |
-
status_lbl = gr.Label(value="Loading model β¦")
|
| 124 |
|
| 125 |
-
|
| 126 |
-
chatbot = gr.Chatbot(height=
|
| 127 |
with gr.Row():
|
| 128 |
msg = gr.Textbox(
|
| 129 |
-
placeholder="Ask me to write
|
| 130 |
lines=2,
|
| 131 |
scale=8,
|
| 132 |
show_label=False,
|
|
@@ -135,21 +116,15 @@ with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
|
| 135 |
send_btn = gr.Button("Send", scale=1, variant="primary")
|
| 136 |
clear_btn = gr.Button("ποΈ Clear")
|
| 137 |
|
| 138 |
-
|
| 139 |
-
# 5. Events
|
| 140 |
-
# ------------------------------------------------------------------
|
| 141 |
-
def _load(token):
|
| 142 |
global tokenizer, model
|
| 143 |
try:
|
| 144 |
-
tokenizer, model = load_model(
|
| 145 |
return "β
Model loaded!"
|
| 146 |
except Exception as e:
|
| 147 |
return f"β {e}"
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
# auto-load once on start
|
| 152 |
-
demo.load(_load, token_box, status_lbl)
|
| 153 |
|
| 154 |
def _send(user_msg, hist):
|
| 155 |
return user_turn(user_msg, hist)
|
|
@@ -162,7 +137,7 @@ with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
|
| 162 |
clear_btn.click(lambda: None, None, chatbot)
|
| 163 |
|
| 164 |
# ------------------------------------------------------------------
|
| 165 |
-
#
|
| 166 |
# ------------------------------------------------------------------
|
| 167 |
if __name__ == "__main__":
|
| 168 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
"""
|
| 2 |
+
CodeNyx β bullet-proof HF-Space chatbot
|
| 3 |
+
Public model β no token required by default
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 15 |
from huggingface_hub import login
|
| 16 |
from threading import Thread
|
| 17 |
|
| 18 |
+
BOT_NAME = "CodeNyx"
|
| 19 |
+
MODEL_ID = "bigcode/starcoder2-3b" # PUBLIC
|
| 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 provide concise, runnable code snippets with short explanations."
|
| 27 |
)
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# ------------------------------------------------------------------
|
| 30 |
+
# 1. Model loader with None-safe token
|
| 31 |
# ------------------------------------------------------------------
|
| 32 |
+
def load_model(token: str | None):
|
| 33 |
+
"""Return (tok, model) or raise."""
|
| 34 |
+
token = (token or "").strip() or None # None-safe
|
| 35 |
if token:
|
| 36 |
+
login(token)
|
| 37 |
|
| 38 |
bnb_config = BitsAndBytesConfig(
|
| 39 |
load_in_4bit=True,
|
|
|
|
| 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 |
+
mdl = AutoModelForCausalLM.from_pretrained(
|
| 54 |
MODEL_ID,
|
| 55 |
quantization_config=bnb_config,
|
| 56 |
device_map="auto",
|
| 57 |
+
use_auth_token=token,
|
| 58 |
trust_remote_code=True,
|
| 59 |
)
|
| 60 |
+
return tok, mdl
|
| 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},
|
| 71 |
+
{"role": "assistant", "content": ai}]
|
| 72 |
messages.append({"role": "user", "content": user_input})
|
| 73 |
+
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def user_turn(user_message, history):
|
| 76 |
return "", history + [[user_message, ""]]
|
|
|
|
| 79 |
prompt = build_prompt(history[:-1], history[-1][0])
|
| 80 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 81 |
|
| 82 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
gen_kwargs = dict(
|
| 84 |
**inputs,
|
| 85 |
max_new_tokens=MAX_NEW_TOK,
|
|
|
|
| 98 |
yield history
|
| 99 |
|
| 100 |
# ------------------------------------------------------------------
|
| 101 |
+
# 3. Gradio UI
|
| 102 |
# ------------------------------------------------------------------
|
|
|
|
|
|
|
| 103 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
| 104 |
+
gr.Markdown(f"# π€ {BOT_NAME} β AI Pair-Programmer\n*3 B params, public model, zero-config*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
status_lbl = gr.Label(value="Loading model β¦")
|
| 107 |
+
chatbot = gr.Chatbot(height=500)
|
| 108 |
with gr.Row():
|
| 109 |
msg = gr.Textbox(
|
| 110 |
+
placeholder="Ask me to write / debug / explain code β¦",
|
| 111 |
lines=2,
|
| 112 |
scale=8,
|
| 113 |
show_label=False,
|
|
|
|
| 116 |
send_btn = gr.Button("Send", scale=1, variant="primary")
|
| 117 |
clear_btn = gr.Button("ποΈ Clear")
|
| 118 |
|
| 119 |
+
def _load():
|
|
|
|
|
|
|
|
|
|
| 120 |
global tokenizer, model
|
| 121 |
try:
|
| 122 |
+
tokenizer, model = load_model(None) # None = no token
|
| 123 |
return "β
Model loaded!"
|
| 124 |
except Exception as e:
|
| 125 |
return f"β {e}"
|
| 126 |
|
| 127 |
+
demo.load(_load, None, status_lbl)
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
def _send(user_msg, hist):
|
| 130 |
return user_turn(user_msg, hist)
|
|
|
|
| 137 |
clear_btn.click(lambda: None, None, chatbot)
|
| 138 |
|
| 139 |
# ------------------------------------------------------------------
|
| 140 |
+
# 4. Launch
|
| 141 |
# ------------------------------------------------------------------
|
| 142 |
if __name__ == "__main__":
|
| 143 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|