Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,127 +1,85 @@
|
|
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
|
9 |
import gradio as gr
|
10 |
import torch
|
11 |
-
from transformers import
|
12 |
-
AutoTokenizer,
|
13 |
-
AutoModelForCausalLM,
|
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.
|
33 |
# ------------------------------------------------------------------
|
34 |
-
|
35 |
-
|
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 |
-
|
44 |
-
trust_remote_code=True,
|
45 |
-
)
|
46 |
-
if tokenizer.pad_token is None:
|
47 |
-
tokenizer.pad_token = tokenizer.eos_token
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
54 |
)
|
55 |
-
print("β
CodeNyx model and tokenizer loaded.")
|
56 |
|
57 |
# ------------------------------------------------------------------
|
58 |
-
#
|
59 |
# ------------------------------------------------------------------
|
60 |
-
def
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
return
|
71 |
-
|
72 |
-
def bot_turn(history):
|
73 |
-
prompt = build_prompt(history[:-1], history[-1][0])
|
74 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
do_sample=True,
|
83 |
-
pad_token_id=tokenizer.eos_token_id,
|
84 |
-
streamer=streamer,
|
85 |
)
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
93 |
|
94 |
# ------------------------------------------------------------------
|
95 |
-
#
|
96 |
# ------------------------------------------------------------------
|
97 |
-
with gr.Blocks(title=f"{BOT_NAME} β
|
98 |
gr.Markdown(f"""
|
99 |
-
# π€ {BOT_NAME} β
|
100 |
-
|
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,
|
110 |
-
container=False,
|
111 |
-
)
|
112 |
-
send_btn = gr.Button("Send", scale=1, variant="primary")
|
113 |
-
clear_btn = gr.Button("ποΈ Clear")
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
)
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
# ------------------------------------------------------------------
|
124 |
-
#
|
125 |
# ------------------------------------------------------------------
|
126 |
if __name__ == "__main__":
|
127 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
1 |
+
# app.py β CodeNyx (StarCoderBase-1B) β full generation & FIM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
|
|
|
|
|
|
|
|
|
|
5 |
from threading import Thread
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# ------------------------------------------------------------------
|
8 |
+
# 1. 1 B model β identical to official snippet
|
9 |
# ------------------------------------------------------------------
|
10 |
+
CHECKPOINT = "bigcode/starcoderbase-1b"
|
11 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(CHECKPOINT).to(DEVICE)
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# ------------------------------------------------------------------
|
17 |
+
# 2. Branding
|
18 |
+
# ------------------------------------------------------------------
|
19 |
+
BOT_NAME = "CodeNyx"
|
20 |
+
SYSTEM = (
|
21 |
+
f"You are {BOT_NAME}, an expert coding assistant trained on The Stack v1.2. "
|
22 |
+
"Return only complete, runnable code with a short comment."
|
23 |
)
|
|
|
24 |
|
25 |
# ------------------------------------------------------------------
|
26 |
+
# 3. Helper: full generation
|
27 |
# ------------------------------------------------------------------
|
28 |
+
def full_generation(prompt: str):
|
29 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt").to(DEVICE)
|
30 |
+
with torch.no_grad():
|
31 |
+
outputs = model.generate(
|
32 |
+
inputs,
|
33 |
+
max_new_tokens=512,
|
34 |
+
temperature=0.2,
|
35 |
+
do_sample=True,
|
36 |
+
pad_token_id=tokenizer.eos_token_id,
|
37 |
+
)
|
38 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# ------------------------------------------------------------------
|
41 |
+
# 4. Helper: fill-in-the-middle (FIM)
|
42 |
+
# ------------------------------------------------------------------
|
43 |
+
def fim_generation(prefix: str, suffix: str):
|
44 |
+
fim_text = (
|
45 |
+
f"<fim_prefix>{prefix}<fim_suffix>{suffix}<fim_middle>"
|
|
|
|
|
|
|
46 |
)
|
47 |
+
inputs = tokenizer.encode(fim_text, return_tensors="pt").to(DEVICE)
|
48 |
+
with torch.no_grad():
|
49 |
+
outputs = model.generate(
|
50 |
+
inputs,
|
51 |
+
max_new_tokens=256,
|
52 |
+
temperature=0.2,
|
53 |
+
do_sample=True,
|
54 |
+
pad_token_id=tokenizer.eos_token_id,
|
55 |
+
)
|
56 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
57 |
|
58 |
# ------------------------------------------------------------------
|
59 |
+
# 5. Gradio interface
|
60 |
# ------------------------------------------------------------------
|
61 |
+
with gr.Blocks(title=f"{BOT_NAME} β StarCoderBase-1B") as demo:
|
62 |
gr.Markdown(f"""
|
63 |
+
# π€ {BOT_NAME} β powered by StarCoderBase-1B (The Stack v1.2)
|
64 |
+
*Ask for full code or let the model **fill-in-the-middle** of any snippet.*
|
|
|
65 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
with gr.Tab("Full Generation"):
|
68 |
+
prompt_in = gr.Textbox(label="Prompt", lines=3, placeholder="def fibonacci(n):")
|
69 |
+
full_out = gr.Code(label="Generated Code", language="python")
|
70 |
+
gen_btn = gr.Button("Generate")
|
71 |
+
gen_btn.click(full_generation, prompt_in, full_out)
|
72 |
+
|
73 |
+
with gr.Tab("Fill-in-the-Middle"):
|
74 |
+
with gr.Row():
|
75 |
+
prefix_in = gr.Textbox(label="Prefix", lines=3, placeholder="def fibonacci(n):\n ")
|
76 |
+
suffix_in = gr.Textbox(label="Suffix", lines=3, placeholder="\n return result")
|
77 |
+
fim_out = gr.Code(label="Completed Code", language="python")
|
78 |
+
fim_btn = gr.Button("Complete")
|
79 |
+
fim_btn.click(fim_generation, [prefix_in, suffix_in], fim_out)
|
80 |
|
81 |
# ------------------------------------------------------------------
|
82 |
+
# 6. Launch
|
83 |
# ------------------------------------------------------------------
|
84 |
if __name__ == "__main__":
|
85 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|