Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ hf_logging.set_verbosity_error()
|
|
| 13 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
|
| 15 |
MODEL_ID = "SupraLabs/Supra-50M-Instruct"
|
| 16 |
-
DEVICE = "cpu"
|
| 17 |
|
| 18 |
# ββ Load model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
|
|
@@ -27,56 +26,66 @@ print("[+] Model ready.")
|
|
| 27 |
|
| 28 |
# ββ Prompt builder (Alpaca format) ββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
|
| 30 |
-
def build_prompt(history: list
|
| 31 |
-
"""Convert chat history
|
| 32 |
parts = []
|
| 33 |
|
| 34 |
if system.strip():
|
| 35 |
parts.append(
|
| 36 |
"Below is an instruction that describes a task. "
|
| 37 |
"Write a response that appropriately completes the request.\n\n"
|
| 38 |
-
f"### Instruction:\n{system}\n\n### Response:\nUnderstood.\n"
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
for msg in history:
|
| 42 |
-
role
|
|
|
|
| 43 |
if role == "user":
|
| 44 |
parts.append(
|
| 45 |
"Below is an instruction that describes a task. "
|
| 46 |
"Write a response that appropriately completes the request.\n\n"
|
| 47 |
f"### Instruction:\n{content}\n\n### Response:\n"
|
| 48 |
)
|
| 49 |
-
elif role == "assistant":
|
| 50 |
-
parts.append(content + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
return "".join(parts)
|
| 53 |
|
| 54 |
|
| 55 |
-
# ββ
|
| 56 |
|
| 57 |
-
def
|
| 58 |
message: str,
|
| 59 |
-
history: list
|
| 60 |
system_prompt: str,
|
| 61 |
max_new_tokens: int,
|
| 62 |
temperature: float,
|
| 63 |
top_p: float,
|
| 64 |
repetition_penalty: float,
|
| 65 |
) -> str:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
with torch.no_grad():
|
| 72 |
output_ids = model.generate(
|
| 73 |
**inputs,
|
| 74 |
-
max_new_tokens=max_new_tokens,
|
| 75 |
-
do_sample=temperature > 0,
|
| 76 |
-
temperature=temperature
|
| 77 |
-
top_p=top_p,
|
| 78 |
top_k=50,
|
| 79 |
-
repetition_penalty=repetition_penalty,
|
| 80 |
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 81 |
eos_token_id=tokenizer.eos_token_id,
|
| 82 |
)
|
|
@@ -87,33 +96,22 @@ def generate_response(
|
|
| 87 |
|
| 88 |
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
-
with gr.Blocks(title="Supra-50M Instruct", theme=gr.themes.Soft()) as demo:
|
| 98 |
-
gr.HTML(DESCRIPTION)
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Column(scale=3):
|
| 102 |
-
chatbot = gr.Chatbot(
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
-
with gr.Row():
|
| 109 |
-
msg_box = gr.Textbox(
|
| 110 |
-
placeholder="Type your messageβ¦",
|
| 111 |
-
show_label=False,
|
| 112 |
-
scale=5,
|
| 113 |
-
lines=1,
|
| 114 |
-
max_lines=4,
|
| 115 |
-
submit_btn=True,
|
| 116 |
-
)
|
| 117 |
|
| 118 |
with gr.Column(scale=1, min_width=220):
|
| 119 |
gr.Markdown("### βοΈ Parameters")
|
|
@@ -138,15 +136,13 @@ with gr.Blocks(title="Supra-50M Instruct", theme=gr.themes.Soft()) as demo:
|
|
| 138 |
|
| 139 |
# ββ State & wiring ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
def on_submit(message, history, system, max_tok, temp, top_p_val, rep_pen):
|
| 144 |
if not message.strip():
|
| 145 |
return history, history, ""
|
| 146 |
|
| 147 |
-
response =
|
| 148 |
-
message, history, system, max_tok, temp, top_p_val, rep_pen
|
| 149 |
-
)
|
| 150 |
|
| 151 |
history = history + [
|
| 152 |
{"role": "user", "content": message},
|
|
@@ -156,19 +152,19 @@ with gr.Blocks(title="Supra-50M Instruct", theme=gr.themes.Soft()) as demo:
|
|
| 156 |
|
| 157 |
msg_box.submit(
|
| 158 |
fn=on_submit,
|
| 159 |
-
inputs=[msg_box,
|
| 160 |
-
outputs=[chatbot,
|
| 161 |
)
|
| 162 |
|
| 163 |
clear_btn.click(
|
| 164 |
fn=lambda: ([], [], ""),
|
| 165 |
-
outputs=[chatbot,
|
| 166 |
)
|
| 167 |
|
| 168 |
gr.Markdown(
|
| 169 |
-
"<p style='text-align:center; color:#aaa; font-size:0.8rem; margin-top:
|
| 170 |
-
"Model: <a href='https://huggingface.co/SupraLabs/Supra-50M-Instruct' target='_blank'>
|
| 171 |
-
"Apache 2.0
|
| 172 |
)
|
| 173 |
|
| 174 |
|
|
|
|
| 13 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
|
| 15 |
MODEL_ID = "SupraLabs/Supra-50M-Instruct"
|
|
|
|
| 16 |
|
| 17 |
# ββ Load model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 18 |
|
|
|
|
| 26 |
|
| 27 |
# ββ Prompt builder (Alpaca format) ββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
|
| 29 |
+
def build_prompt(history: list, system: str, new_message: str) -> str:
|
| 30 |
+
"""Convert chat history + new message into Alpaca instruct format."""
|
| 31 |
parts = []
|
| 32 |
|
| 33 |
if system.strip():
|
| 34 |
parts.append(
|
| 35 |
"Below is an instruction that describes a task. "
|
| 36 |
"Write a response that appropriately completes the request.\n\n"
|
| 37 |
+
f"### Instruction:\n{system}\n\n### Response:\nUnderstood.\n\n"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# history is list of {"role": ..., "content": ...} dicts (Gradio 6 format)
|
| 41 |
for msg in history:
|
| 42 |
+
role = msg["role"] if isinstance(msg, dict) else msg[0]
|
| 43 |
+
content = msg["content"] if isinstance(msg, dict) else msg[1]
|
| 44 |
if role == "user":
|
| 45 |
parts.append(
|
| 46 |
"Below is an instruction that describes a task. "
|
| 47 |
"Write a response that appropriately completes the request.\n\n"
|
| 48 |
f"### Instruction:\n{content}\n\n### Response:\n"
|
| 49 |
)
|
| 50 |
+
elif role == "assistant" and content:
|
| 51 |
+
parts.append(content + "\n\n")
|
| 52 |
+
|
| 53 |
+
# Add new user message
|
| 54 |
+
parts.append(
|
| 55 |
+
"Below is an instruction that describes a task. "
|
| 56 |
+
"Write a response that appropriately completes the request.\n\n"
|
| 57 |
+
f"### Instruction:\n{new_message}\n\n### Response:\n"
|
| 58 |
+
)
|
| 59 |
|
| 60 |
return "".join(parts)
|
| 61 |
|
| 62 |
|
| 63 |
+
# ββ Generate ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 64 |
|
| 65 |
+
def chat(
|
| 66 |
message: str,
|
| 67 |
+
history: list,
|
| 68 |
system_prompt: str,
|
| 69 |
max_new_tokens: int,
|
| 70 |
temperature: float,
|
| 71 |
top_p: float,
|
| 72 |
repetition_penalty: float,
|
| 73 |
) -> str:
|
| 74 |
+
if not message.strip():
|
| 75 |
+
return ""
|
| 76 |
|
| 77 |
+
prompt = build_prompt(history, system_prompt, message)
|
| 78 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 79 |
|
| 80 |
with torch.no_grad():
|
| 81 |
output_ids = model.generate(
|
| 82 |
**inputs,
|
| 83 |
+
max_new_tokens=int(max_new_tokens),
|
| 84 |
+
do_sample=temperature > 0.01,
|
| 85 |
+
temperature=float(temperature),
|
| 86 |
+
top_p=float(top_p),
|
| 87 |
top_k=50,
|
| 88 |
+
repetition_penalty=float(repetition_penalty),
|
| 89 |
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 90 |
eos_token_id=tokenizer.eos_token_id,
|
| 91 |
)
|
|
|
|
| 96 |
|
| 97 |
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 98 |
|
| 99 |
+
with gr.Blocks(title="Supra-50M Instruct") as demo:
|
| 100 |
+
gr.Markdown(
|
| 101 |
+
"# π¦
Supra-50M Instruct\n"
|
| 102 |
+
"50M-parameter chat model by [SupraLabs](https://huggingface.co/SupraLabs) β running on CPU"
|
| 103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column(scale=3):
|
| 107 |
+
chatbot = gr.Chatbot(label="Chat", height=480)
|
| 108 |
+
msg_box = gr.Textbox(
|
| 109 |
+
placeholder="Type your message and press Enterβ¦",
|
| 110 |
+
show_label=False,
|
| 111 |
+
lines=1,
|
| 112 |
+
max_lines=4,
|
| 113 |
+
submit_btn=True,
|
| 114 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
with gr.Column(scale=1, min_width=220):
|
| 117 |
gr.Markdown("### βοΈ Parameters")
|
|
|
|
| 136 |
|
| 137 |
# ββ State & wiring ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 138 |
|
| 139 |
+
chat_state = gr.State([])
|
| 140 |
|
| 141 |
def on_submit(message, history, system, max_tok, temp, top_p_val, rep_pen):
|
| 142 |
if not message.strip():
|
| 143 |
return history, history, ""
|
| 144 |
|
| 145 |
+
response = chat(message, history, system, max_tok, temp, top_p_val, rep_pen)
|
|
|
|
|
|
|
| 146 |
|
| 147 |
history = history + [
|
| 148 |
{"role": "user", "content": message},
|
|
|
|
| 152 |
|
| 153 |
msg_box.submit(
|
| 154 |
fn=on_submit,
|
| 155 |
+
inputs=[msg_box, chat_state, system_prompt, max_new_tokens, temperature, top_p, repetition_penalty],
|
| 156 |
+
outputs=[chatbot, chat_state, msg_box],
|
| 157 |
)
|
| 158 |
|
| 159 |
clear_btn.click(
|
| 160 |
fn=lambda: ([], [], ""),
|
| 161 |
+
outputs=[chatbot, chat_state, msg_box],
|
| 162 |
)
|
| 163 |
|
| 164 |
gr.Markdown(
|
| 165 |
+
"<p style='text-align:center; color:#aaa; font-size:0.8rem; margin-top:8px;'>"
|
| 166 |
+
"Model: <a href='https://huggingface.co/SupraLabs/Supra-50M-Instruct' target='_blank'>"
|
| 167 |
+
"SupraLabs/Supra-50M-Instruct</a> β Apache 2.0 β Β© SupraLabs 2026</p>"
|
| 168 |
)
|
| 169 |
|
| 170 |
|