AryanRathod3097 commited on
Commit
0d74b25
Β·
verified Β·
1 Parent(s): 1638eb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -102
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. 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},
65
- {"role": "assistant", "content": ai}]
66
- messages.append({"role": "user", "content": user_input})
67
- return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
68
-
69
- def user_turn(user_message, history):
70
- return "", history + [[user_message, ""]]
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
- streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
77
- gen_kwargs = dict(
78
- **inputs,
79
- max_new_tokens=MAX_NEW_TOK,
80
- temperature=TEMPERATURE,
81
- top_p=TOP_P,
82
- do_sample=True,
83
- pad_token_id=tokenizer.eos_token_id,
84
- streamer=streamer,
85
  )
86
- Thread(target=model.generate, kwargs=gen_kwargs).start()
87
-
88
- partial = ""
89
- for new_text in streamer:
90
- partial += new_text
91
- history[-1][1] = partial
92
- yield history
 
 
 
93
 
94
  # ------------------------------------------------------------------
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,
110
- container=False,
111
- )
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
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)