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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -61
app.py CHANGED
@@ -1,6 +1,8 @@
1
  """
2
- CodeNyx – bullet-proof HF-Space chatbot
3
- Public model β†’ no token required by default
 
 
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" # 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,
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
- 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},
@@ -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"# πŸ€– {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,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
- 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)
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