Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
"""
|
2 |
-
|
3 |
-
|
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 (
|
@@ -17,11 +15,8 @@ from transformers import (
|
|
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 |
|
@@ -30,55 +25,57 @@ TEMPERATURE = 0.2
|
|
30 |
TOP_P = 0.9
|
31 |
|
32 |
# ------------------------------------------------------------------
|
33 |
-
#
|
34 |
# ------------------------------------------------------------------
|
35 |
-
def
|
36 |
"""
|
37 |
-
|
38 |
-
|
|
|
39 |
"""
|
40 |
-
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 |
-
|
48 |
os.environ["HF_TOKEN"] = token
|
49 |
login(token)
|
50 |
-
return
|
51 |
|
52 |
# ------------------------------------------------------------------
|
53 |
-
#
|
54 |
# ------------------------------------------------------------------
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
)
|
69 |
-
if tokenizer.pad_token is None:
|
70 |
-
|
71 |
-
|
72 |
-
model = AutoModelForCausalLM.from_pretrained(
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
)
|
|
|
79 |
|
80 |
# ------------------------------------------------------------------
|
81 |
-
#
|
82 |
# ------------------------------------------------------------------
|
83 |
def build_prompt(history, user_input):
|
84 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
@@ -121,39 +118,57 @@ def bot_turn(history):
|
|
121 |
yield history
|
122 |
|
123 |
# ------------------------------------------------------------------
|
124 |
-
#
|
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 |
-
*
|
130 |
-
|
131 |
""")
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
+
CodeNyx β zero-config Gradio chatbot
|
3 |
+
Auto-handles Hugging Face tokens without stdin prompts.
|
|
|
4 |
"""
|
5 |
|
6 |
import os
|
|
|
7 |
import gradio as gr
|
8 |
import torch
|
9 |
from transformers import (
|
|
|
15 |
from huggingface_hub import login
|
16 |
from threading import Thread
|
17 |
|
|
|
|
|
|
|
|
|
18 |
MODEL_ID = "bigcode/starcoder2-3b-instruct"
|
19 |
+
BOT_NAME = "CodeNyx"
|
20 |
SYSTEM_PROMPT = (f"You are {BOT_NAME}, an expert open-source coding assistant. "
|
21 |
"Always provide concise, runnable code snippets with short explanations.")
|
22 |
|
|
|
25 |
TOP_P = 0.9
|
26 |
|
27 |
# ------------------------------------------------------------------
|
28 |
+
# 1. Token helper (Gradio popup instead of stdin)
|
29 |
# ------------------------------------------------------------------
|
30 |
+
def ensure_token(token_value):
|
31 |
"""
|
32 |
+
token_value comes from the Gradio UI the first time.
|
33 |
+
We cache it in environment variable HF_TOKEN and login once.
|
34 |
+
Returns True on success.
|
35 |
"""
|
36 |
+
token = token_value.strip()
|
|
|
|
|
|
|
|
|
|
|
37 |
if not token:
|
38 |
+
return False
|
39 |
os.environ["HF_TOKEN"] = token
|
40 |
login(token)
|
41 |
+
return True
|
42 |
|
43 |
# ------------------------------------------------------------------
|
44 |
+
# 2. Lazy model loader (once token is ready)
|
45 |
# ------------------------------------------------------------------
|
46 |
+
model, tokenizer = None, None
|
47 |
+
|
48 |
+
def load_model():
|
49 |
+
global model, tokenizer
|
50 |
+
if model is not None:
|
51 |
+
return True # already loaded
|
52 |
+
|
53 |
+
bnb_config = BitsAndBytesConfig(
|
54 |
+
load_in_4bit=True,
|
55 |
+
bnb_4bit_compute_dtype=torch.float16,
|
56 |
+
bnb_4bit_quant_type="nf4",
|
57 |
+
bnb_4bit_use_double_quant=True,
|
58 |
+
)
|
59 |
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
61 |
+
MODEL_ID,
|
62 |
+
use_auth_token=os.getenv("HF_TOKEN"),
|
63 |
+
trust_remote_code=True,
|
64 |
+
)
|
65 |
+
if tokenizer.pad_token is None:
|
66 |
+
tokenizer.pad_token = tokenizer.eos_token
|
67 |
+
|
68 |
+
model = AutoModelForCausalLM.from_pretrained(
|
69 |
+
MODEL_ID,
|
70 |
+
quantization_config=bnb_config,
|
71 |
+
device_map="auto",
|
72 |
+
use_auth_token=os.getenv("HF_TOKEN"),
|
73 |
+
trust_remote_code=True,
|
74 |
+
)
|
75 |
+
return True
|
76 |
|
77 |
# ------------------------------------------------------------------
|
78 |
+
# 3. Chat logic
|
79 |
# ------------------------------------------------------------------
|
80 |
def build_prompt(history, user_input):
|
81 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
118 |
yield history
|
119 |
|
120 |
# ------------------------------------------------------------------
|
121 |
+
# 4. Gradio UI flow
|
122 |
# ------------------------------------------------------------------
|
123 |
with gr.Blocks(title=f"{BOT_NAME} β AI Pair-Programmer") as demo:
|
124 |
gr.Markdown(f"""
|
125 |
+
# π€ {BOT_NAME} β AI Pair-Programmer
|
126 |
+
*3 B params, 100 % free-tier friendly.*
|
127 |
+
Paste your Hugging Face token **once** if asked, then chat away.
|
128 |
""")
|
129 |
|
130 |
+
token_box = gr.Textbox(label="π€ Hugging Face Token (only first time)", type="password")
|
131 |
+
load_btn = gr.Button("Authorize")
|
132 |
+
status_lbl = gr.Label(value="Waiting for token β¦")
|
133 |
+
|
134 |
+
# --- main chat controls (hidden until token OK) ---
|
135 |
+
with gr.Column(visible=False) as chat_col:
|
136 |
+
chatbot = gr.Chatbot(height=450)
|
137 |
+
with gr.Row():
|
138 |
+
msg = gr.Textbox(
|
139 |
+
placeholder="Ask me to write / debug / explain code β¦",
|
140 |
+
lines=2,
|
141 |
+
scale=8,
|
142 |
+
show_label=False,
|
143 |
+
container=False,
|
144 |
+
)
|
145 |
+
send_btn = gr.Button("Send", scale=1, variant="primary")
|
146 |
+
clear_btn = gr.Button("ποΈ Clear")
|
147 |
+
|
148 |
+
# ------------------------------------------------------------------
|
149 |
+
# 5. Event wiring
|
150 |
+
# ------------------------------------------------------------------
|
151 |
+
def _auth(token):
|
152 |
+
ok = ensure_token(token)
|
153 |
+
if ok:
|
154 |
+
try:
|
155 |
+
load_model()
|
156 |
+
return gr.update(visible=False), gr.update(visible=True), "β
Ready! Start coding."
|
157 |
+
except Exception as e:
|
158 |
+
return gr.update(visible=True), gr.update(visible=False), f"β Error: {e}"
|
159 |
+
else:
|
160 |
+
return gr.update(visible=True), gr.update(visible=False), "β Invalid token."
|
161 |
+
|
162 |
+
load_btn.click(_auth, token_box, [token_box, chat_col, status_lbl])
|
163 |
+
|
164 |
+
def _send(user_msg, hist):
|
165 |
+
return user_turn(user_msg, hist)
|
166 |
+
|
167 |
+
def _bot(hist):
|
168 |
+
yield from bot_turn(hist)
|
169 |
+
|
170 |
+
msg.submit(_send, [msg, chatbot], [msg, chatbot]).then(_bot, chatbot, chatbot)
|
171 |
+
send_btn.click(_send, [msg, chatbot], [msg, chatbot]).then(_bot, chatbot, chatbot)
|
172 |
+
clear_btn.click(lambda: None, None, chatbot)
|
173 |
+
|
174 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True)
|