AxionLab-official commited on
Commit
de13e30
Β·
verified Β·
1 Parent(s): 560025e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -51
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[dict], system: str) -> str:
31
- """Convert chat history into the Alpaca instruct format the model expects."""
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, content = msg["role"], msg["content"]
 
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
- # ── Generation ────────────────────────────────────────────────────────────────
56
 
57
- def generate_response(
58
  message: str,
59
- history: list[dict],
60
  system_prompt: str,
61
  max_new_tokens: int,
62
  temperature: float,
63
  top_p: float,
64
  repetition_penalty: float,
65
  ) -> str:
66
- history = history + [{"role": "user", "content": message}]
67
- prompt = build_prompt(history, system_prompt)
68
 
69
- inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
 
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 if temperature > 0 else 1.0,
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
- DESCRIPTION = """
91
- <div style="text-align:center; padding: 8px 0 4px;">
92
- <h1 style="font-size:2rem; margin:0;">πŸ¦… Supra-50M Instruct</h1>
93
- <p style="color:#888; margin:4px 0 0;">50M-parameter chat model by <a href="https://huggingface.co/SupraLabs" target="_blank">SupraLabs</a> β€” running on CPU</p>
94
- </div>
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
- label="Chat",
104
- type="messages",
105
- height=480,
106
- show_copy_button=True,
 
 
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
- chat_history = gr.State([])
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 = generate_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, chat_history, system_prompt, max_new_tokens, temperature, top_p, repetition_penalty],
160
- outputs=[chatbot, chat_history, msg_box],
161
  )
162
 
163
  clear_btn.click(
164
  fn=lambda: ([], [], ""),
165
- outputs=[chatbot, chat_history, msg_box],
166
  )
167
 
168
  gr.Markdown(
169
- "<p style='text-align:center; color:#aaa; font-size:0.8rem; margin-top:12px;'>"
170
- "Model: <a href='https://huggingface.co/SupraLabs/Supra-50M-Instruct' target='_blank'>SupraLabs/Supra-50M-Instruct</a> β€” "
171
- "Apache 2.0 License β€” Β© SupraLabs 2026</p>"
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