Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
"""
|
| 2 |
-
codenyx.py β CodeNyx
|
| 3 |
-
|
| 4 |
"""
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
| 7 |
from transformers import (
|
|
@@ -13,110 +14,114 @@ from transformers import (
|
|
| 13 |
from threading import Thread
|
| 14 |
|
| 15 |
# ------------------------------------------------------------------
|
| 16 |
-
# 1.
|
| 17 |
# ------------------------------------------------------------------
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
TEMPERATURE = 0.2
|
| 25 |
-
TOP_P = 0.9
|
| 26 |
|
| 27 |
# ------------------------------------------------------------------
|
| 28 |
-
# 2.
|
| 29 |
# ------------------------------------------------------------------
|
| 30 |
bnb_config = BitsAndBytesConfig(
|
| 31 |
load_in_4bit=True,
|
| 32 |
bnb_4bit_compute_dtype=torch.float16,
|
| 33 |
bnb_4bit_quant_type="nf4",
|
| 34 |
-
bnb_4bit_use_double_quant=True
|
| 35 |
)
|
| 36 |
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 38 |
-
|
| 39 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 40 |
|
| 41 |
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
MODEL_ID,
|
| 43 |
quantization_config=bnb_config,
|
| 44 |
device_map="auto",
|
| 45 |
-
trust_remote_code=True
|
| 46 |
)
|
| 47 |
|
| 48 |
# ------------------------------------------------------------------
|
| 49 |
-
# 3. Chat
|
| 50 |
# ------------------------------------------------------------------
|
| 51 |
def build_prompt(history, user_input):
|
| 52 |
-
"""
|
| 53 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 54 |
-
for
|
| 55 |
-
messages.append({"role": "user", "content":
|
| 56 |
-
messages.append({"role": "assistant", "content":
|
| 57 |
messages.append({"role": "user", "content": user_input})
|
| 58 |
return tokenizer.apply_chat_template(
|
| 59 |
messages, tokenize=False, add_generation_prompt=True
|
| 60 |
)
|
| 61 |
|
| 62 |
-
def
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 65 |
|
| 66 |
streamer = TextIteratorStreamer(
|
| 67 |
tokenizer,
|
| 68 |
skip_prompt=True,
|
| 69 |
-
skip_special_tokens=True
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
**inputs,
|
| 74 |
-
max_new_tokens=
|
| 75 |
-
temperature=
|
| 76 |
-
top_p=
|
| 77 |
do_sample=True,
|
| 78 |
pad_token_id=tokenizer.eos_token_id,
|
| 79 |
-
streamer=streamer
|
| 80 |
)
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
partial = ""
|
| 84 |
for new_text in streamer:
|
| 85 |
partial += new_text
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
# ------------------------------------------------------------------
|
| 89 |
# 4. Gradio UI
|
| 90 |
# ------------------------------------------------------------------
|
| 91 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
| 92 |
gr.Markdown(f"""
|
| 93 |
-
# π€ {BOT_NAME}
|
| 94 |
-
*
|
| 95 |
-
|
| 96 |
""")
|
| 97 |
|
| 98 |
-
chatbot = gr.Chatbot(
|
| 99 |
-
label=f"{BOT_NAME} Chat",
|
| 100 |
-
height=600,
|
| 101 |
-
avatar_images=(None, "https://huggingface.co/spaces/bigcode/starcoder-logo/resolve/main/logo.png")
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
msg = gr.Textbox(
|
| 105 |
-
placeholder="Ask me to write, debug, or explain code...",
|
| 106 |
-
lines=2,
|
| 107 |
-
show_label=False,
|
| 108 |
-
container=False
|
| 109 |
-
)
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
return "", history + [[user_message, ""]]
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
)
|
| 119 |
-
|
| 120 |
|
| 121 |
# ------------------------------------------------------------------
|
| 122 |
# 5. Launch
|
|
|
|
| 1 |
"""
|
| 2 |
+
codenyx.py β CodeNyx AI pair-programmer
|
| 3 |
+
Runs great on HF Free tier (16 GB RAM) or any laptop with 8 GB+ VRAM.
|
| 4 |
"""
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
from transformers import (
|
|
|
|
| 14 |
from threading import Thread
|
| 15 |
|
| 16 |
# ------------------------------------------------------------------
|
| 17 |
+
# 1. Model & branding
|
| 18 |
# ------------------------------------------------------------------
|
| 19 |
+
MODEL_ID = "bigcode/starcoder2-3b-instruct-v0.1" # 3 B params, fits 16 GB
|
| 20 |
+
BOT_NAME = "CodeNyx"
|
| 21 |
+
SYSTEM_PROMPT = (
|
| 22 |
+
f"You are {BOT_NAME}, an expert open-source coding assistant. "
|
| 23 |
+
"Always provide concise, runnable code snippets with short explanations."
|
| 24 |
+
)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# ------------------------------------------------------------------
|
| 27 |
+
# 2. 4-bit quantization so CPU-only laptops can run too
|
| 28 |
# ------------------------------------------------------------------
|
| 29 |
bnb_config = BitsAndBytesConfig(
|
| 30 |
load_in_4bit=True,
|
| 31 |
bnb_4bit_compute_dtype=torch.float16,
|
| 32 |
bnb_4bit_quant_type="nf4",
|
| 33 |
+
bnb_4bit_use_double_quant=True,
|
| 34 |
)
|
| 35 |
|
| 36 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 37 |
+
tokenizer.pad_token = tokenizer.eos_token # silence warning
|
|
|
|
| 38 |
|
| 39 |
model = AutoModelForCausalLM.from_pretrained(
|
| 40 |
MODEL_ID,
|
| 41 |
quantization_config=bnb_config,
|
| 42 |
device_map="auto",
|
| 43 |
+
trust_remote_code=True,
|
| 44 |
)
|
| 45 |
|
| 46 |
# ------------------------------------------------------------------
|
| 47 |
+
# 3. Chat logic
|
| 48 |
# ------------------------------------------------------------------
|
| 49 |
def build_prompt(history, user_input):
|
| 50 |
+
"""Convert Gradio history into model chat template."""
|
| 51 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 52 |
+
for human, ai in history:
|
| 53 |
+
messages.append({"role": "user", "content": human})
|
| 54 |
+
messages.append({"role": "assistant", "content": ai})
|
| 55 |
messages.append({"role": "user", "content": user_input})
|
| 56 |
return tokenizer.apply_chat_template(
|
| 57 |
messages, tokenize=False, add_generation_prompt=True
|
| 58 |
)
|
| 59 |
|
| 60 |
+
def user_turn(user_message, history):
|
| 61 |
+
"""Add user message to history."""
|
| 62 |
+
return "", history + [[user_message, ""]]
|
| 63 |
+
|
| 64 |
+
def bot_turn(history):
|
| 65 |
+
"""Generate assistant reply in streaming mode."""
|
| 66 |
+
prompt = build_prompt(history[:-1], history[-1][0])
|
| 67 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 68 |
|
| 69 |
streamer = TextIteratorStreamer(
|
| 70 |
tokenizer,
|
| 71 |
skip_prompt=True,
|
| 72 |
+
skip_special_tokens=True,
|
| 73 |
)
|
| 74 |
|
| 75 |
+
gen_kwargs = dict(
|
| 76 |
**inputs,
|
| 77 |
+
max_new_tokens=1024,
|
| 78 |
+
temperature=0.2,
|
| 79 |
+
top_p=0.9,
|
| 80 |
do_sample=True,
|
| 81 |
pad_token_id=tokenizer.eos_token_id,
|
| 82 |
+
streamer=streamer,
|
| 83 |
)
|
| 84 |
+
|
| 85 |
+
Thread(target=model.generate, kwargs=gen_kwargs).start()
|
| 86 |
|
| 87 |
partial = ""
|
| 88 |
for new_text in streamer:
|
| 89 |
partial += new_text
|
| 90 |
+
history[-1][1] = partial
|
| 91 |
+
yield history
|
| 92 |
|
| 93 |
# ------------------------------------------------------------------
|
| 94 |
# 4. Gradio UI
|
| 95 |
# ------------------------------------------------------------------
|
| 96 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
| 97 |
gr.Markdown(f"""
|
| 98 |
+
# π€ {BOT_NAME} AI Pair-Programmer
|
| 99 |
+
*Lightweight 3 B model, 100 % free tier friendly.*
|
| 100 |
+
Ask any coding question and get **runnable code + short explanations**.
|
| 101 |
""")
|
| 102 |
|
| 103 |
+
chatbot = gr.Chatbot(height=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
with gr.Row():
|
| 106 |
+
msg = gr.Textbox(
|
| 107 |
+
placeholder="Type your programming question here ...",
|
| 108 |
+
lines=2,
|
| 109 |
+
scale=8,
|
| 110 |
+
show_label=False,
|
| 111 |
+
container=False,
|
| 112 |
+
)
|
| 113 |
+
submit_btn = gr.Button("Send", scale=1, variant="primary")
|
| 114 |
|
| 115 |
+
clear_btn = gr.Button("ποΈ Clear")
|
|
|
|
| 116 |
|
| 117 |
+
# Event wiring
|
| 118 |
+
msg.submit(user_turn, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 119 |
+
bot_turn, chatbot, chatbot
|
| 120 |
+
)
|
| 121 |
+
submit_btn.click(user_turn, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 122 |
+
bot_turn, chatbot, chatbot
|
| 123 |
)
|
| 124 |
+
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 125 |
|
| 126 |
# ------------------------------------------------------------------
|
| 127 |
# 5. Launch
|