AryanRathod3097 commited on
Commit
9e2faf0
Β·
verified Β·
1 Parent(s): aacf87a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -24
app.py CHANGED
@@ -1,8 +1,11 @@
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 (
@@ -11,21 +14,46 @@ from transformers import (
11
  BitsAndBytesConfig,
12
  TextIteratorStreamer
13
  )
 
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,
@@ -33,21 +61,26 @@ bnb_config = BitsAndBytesConfig(
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})
@@ -58,11 +91,9 @@ def build_prompt(history, user_input):
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
 
@@ -74,14 +105,13 @@ def bot_turn(history):
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 = ""
@@ -91,12 +121,12 @@ def bot_turn(history):
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
 
@@ -104,7 +134,7 @@ with gr.Blocks(title=f"{BOT_NAME} – AI Pair-Programmer") as demo:
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,
@@ -114,7 +144,6 @@ with gr.Blocks(title=f"{BOT_NAME} – AI Pair-Programmer") as demo:
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
  )
@@ -124,7 +153,7 @@ with gr.Blocks(title=f"{BOT_NAME} – AI Pair-Programmer") as demo:
124
  clear_btn.click(lambda: None, None, chatbot, queue=False)
125
 
126
  # ------------------------------------------------------------------
127
- # 5. Launch
128
  # ------------------------------------------------------------------
129
  if __name__ == "__main__":
130
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
 
1
  """
2
  codenyx.py – CodeNyx AI pair-programmer
3
+ One-file, zero-config. If the model is gated, the UI will
4
+ prompt for your HF token once and remember it.
5
  """
6
 
7
+ import os
8
+ import sys
9
  import gradio as gr
10
  import torch
11
  from transformers import (
 
14
  BitsAndBytesConfig,
15
  TextIteratorStreamer
16
  )
17
+ from huggingface_hub import login
18
  from threading import Thread
19
 
20
  # ------------------------------------------------------------------
21
+ # 1. Branding & constants
22
  # ------------------------------------------------------------------
23
+ BOT_NAME = "CodeNyx"
24
+ MODEL_ID = "bigcode/starcoder2-3b-instruct"
25
+ SYSTEM_PROMPT = (f"You are {BOT_NAME}, an expert open-source coding assistant. "
26
+ "Always provide concise, runnable code snippets with short explanations.")
27
+
28
+ MAX_NEW_TOK = 1024
29
+ TEMPERATURE = 0.2
30
+ TOP_P = 0.9
31
+
32
+ # ------------------------------------------------------------------
33
+ # 2. Token helper
34
+ # ------------------------------------------------------------------
35
+ def get_hf_token():
36
+ """
37
+ Ask the user interactively once, store in HF_TOKEN env var.
38
+ If the var already exists, use it silently.
39
+ """
40
+ token = os.getenv("HF_TOKEN")
41
+ if token:
42
+ return token
43
+
44
+ print("πŸ€— Hugging Face token required for gated/private models.")
45
+ token = input("Paste your HF token (will be cached in HF_TOKEN): ").strip()
46
+ if not token:
47
+ sys.exit("No token provided – aborting.")
48
+ os.environ["HF_TOKEN"] = token
49
+ login(token)
50
+ return token
51
 
52
  # ------------------------------------------------------------------
53
+ # 3. Load model & tokenizer (with fallback)
54
  # ------------------------------------------------------------------
55
+ get_hf_token() # ensures HF_TOKEN is set if needed
56
+
57
  bnb_config = BitsAndBytesConfig(
58
  load_in_4bit=True,
59
  bnb_4bit_compute_dtype=torch.float16,
 
61
  bnb_4bit_use_double_quant=True,
62
  )
63
 
64
+ tokenizer = AutoTokenizer.from_pretrained(
65
+ MODEL_ID,
66
+ use_auth_token=os.getenv("HF_TOKEN"),
67
+ trust_remote_code=True,
68
+ )
69
+ if tokenizer.pad_token is None:
70
+ tokenizer.pad_token = tokenizer.eos_token
71
 
72
  model = AutoModelForCausalLM.from_pretrained(
73
  MODEL_ID,
74
  quantization_config=bnb_config,
75
  device_map="auto",
76
+ use_auth_token=os.getenv("HF_TOKEN"),
77
  trust_remote_code=True,
78
  )
79
 
80
  # ------------------------------------------------------------------
81
+ # 4. Chat logic
82
  # ------------------------------------------------------------------
83
  def build_prompt(history, user_input):
 
84
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
85
  for human, ai in history:
86
  messages.append({"role": "user", "content": human})
 
91
  )
92
 
93
  def user_turn(user_message, history):
 
94
  return "", history + [[user_message, ""]]
95
 
96
  def bot_turn(history):
 
97
  prompt = build_prompt(history[:-1], history[-1][0])
98
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
99
 
 
105
 
106
  gen_kwargs = dict(
107
  **inputs,
108
+ max_new_tokens=MAX_NEW_TOK,
109
+ temperature=TEMPERATURE,
110
+ top_p=TOP_P,
111
  do_sample=True,
112
  pad_token_id=tokenizer.eos_token_id,
113
  streamer=streamer,
114
  )
 
115
  Thread(target=model.generate, kwargs=gen_kwargs).start()
116
 
117
  partial = ""
 
121
  yield history
122
 
123
  # ------------------------------------------------------------------
124
+ # 5. Gradio UI
125
  # ------------------------------------------------------------------
126
  with gr.Blocks(title=f"{BOT_NAME} – AI Pair-Programmer") as demo:
127
  gr.Markdown(f"""
128
  # πŸ€– {BOT_NAME} AI Pair-Programmer
129
+ *Runs on CPU or GPU; 3 B parameters, free-tier friendly.*
130
  Ask any coding question and get **runnable code + short explanations**.
131
  """)
132
 
 
134
 
135
  with gr.Row():
136
  msg = gr.Textbox(
137
+ placeholder="Type your programming question here …",
138
  lines=2,
139
  scale=8,
140
  show_label=False,
 
144
 
145
  clear_btn = gr.Button("πŸ—‘οΈ Clear")
146
 
 
147
  msg.submit(user_turn, [msg, chatbot], [msg, chatbot], queue=False).then(
148
  bot_turn, chatbot, chatbot
149
  )
 
153
  clear_btn.click(lambda: None, None, chatbot, queue=False)
154
 
155
  # ------------------------------------------------------------------
156
+ # 6. Launch
157
  # ------------------------------------------------------------------
158
  if __name__ == "__main__":
159
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)