SeaWolf-AI commited on
Commit
bef73b8
Β·
verified Β·
1 Parent(s): 7719836

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -225,9 +225,16 @@ def generate_reply(
225
  max_new_tokens: int,
226
  temperature: float,
227
  top_p: float,
 
228
  ) -> Generator[str, None, None]:
229
  """Main generation function."""
230
 
 
 
 
 
 
 
231
  use_think = "Thinking" in thinking_mode
232
  max_new_tokens = min(int(max_new_tokens), 8192)
233
 
@@ -308,27 +315,33 @@ footer { display: none !important; }
308
  .model-box .st { font-size: 10px; color: #78716c; margin-top: 4px; }
309
  """
310
 
311
- _mcfg = MODELS[DEFAULT_MODEL]
312
- MODEL_INFO_HTML = (
313
- f'<div class="model-box">'
314
- f'<b>{"⚑" if _mcfg["arch"]=="MoE" else "πŸ†"} {DEFAULT_MODEL}</b> '
315
- f'<span style="font-size:9px;padding:2px 6px;border-radius:6px;background:rgba(109,40,217,.08);color:#6d28d9;font-weight:700">{_mcfg["arch"]}</span><br>'
316
- f'<div class="st">{_mcfg["active"]} active / {_mcfg["total"]} total Β· πŸ‘οΈ Vision Β· {_mcfg["ctx"]} context</div>'
317
- f'<div class="st">{_mcfg["desc"]}</div>'
318
- f'<div class="st" style="margin-top:6px">'
319
- f'<a href="https://huggingface.co/{MODELS[DEFAULT_MODEL]["id"]}" target="_blank" style="color:#6d28d9;font-weight:700;text-decoration:none">πŸ€— Model Card β†—</a> Β· '
320
- f'<a href="https://deepmind.google/models/gemma/gemma-4/" target="_blank" style="color:#059669;font-weight:700;text-decoration:none">πŸ”¬ DeepMind β†—</a>'
321
- f'</div></div>'
322
- )
 
 
323
 
324
  with gr.Blocks(title="Gemma 4 Playground") as demo:
325
 
326
  gr.Markdown("## πŸ’Ž Gemma 4 Playground\nGoogle DeepMind Β· Apache 2.0 Β· Vision Β· Thinking")
327
 
328
  with gr.Row():
329
- # ── Sidebar (compact) ──
330
  with gr.Column(scale=0, min_width=280):
331
- gr.HTML(MODEL_INFO_HTML)
 
 
 
 
332
  image_input = gr.Image(label="πŸ‘οΈ Image (Vision)", type="filepath", height=140)
333
  thinking_radio = gr.Radio(["⚑ Fast", "🧠 Thinking"], value="⚑ Fast", label="Mode")
334
  with gr.Accordion("βš™οΈ Settings", open=False):
@@ -350,21 +363,22 @@ with gr.Blocks(title="Gemma 4 Playground") as demo:
350
  send_btn = gr.Button("↑", variant="primary", scale=0, min_width=48, elem_id="send-btn")
351
 
352
  # ── Events ──
 
353
  preset_dd.change(fn=lambda k: PRESETS.get(k, PRESETS["general"]), inputs=[preset_dd], outputs=[sys_prompt])
354
 
355
  def user_msg(msg, hist):
356
  if not msg.strip(): return "", hist
357
  return "", hist + [{"role": "user", "content": msg}]
358
 
359
- def bot_reply(hist, think, img, sysp, maxt, tmp, tp):
360
  if not hist or hist[-1]["role"] != "user": return hist
361
  txt, past = hist[-1]["content"], hist[:-1]
362
  hist = hist + [{"role": "assistant", "content": ""}]
363
- for chunk in generate_reply(txt, past, think, img, sysp, maxt, tmp, tp):
364
  hist[-1]["content"] = chunk
365
  yield hist
366
 
367
- ins = [chatbot, thinking_radio, image_input, sys_prompt, max_tok, temp, topp]
368
  send_btn.click(user_msg, [chat_input, chatbot], [chat_input, chatbot], queue=False).then(bot_reply, ins, chatbot)
369
  chat_input.submit(user_msg, [chat_input, chatbot], [chat_input, chatbot], queue=False).then(bot_reply, ins, chatbot)
370
  clear_btn.click(lambda: [], None, chatbot, queue=False)
 
225
  max_new_tokens: int,
226
  temperature: float,
227
  top_p: float,
228
+ model_choice: str = "",
229
  ) -> Generator[str, None, None]:
230
  """Main generation function."""
231
 
232
+ # Model switching (may take 1-2 min on first switch)
233
+ target = model_choice if model_choice in MODELS else DEFAULT_MODEL
234
+ if target != _loaded_model_name:
235
+ yield f"⏳ Loading **{target}**... (졜초 μ „ν™˜ μ‹œ 1-2λΆ„ μ†Œμš”)"
236
+ _load_model(target)
237
+
238
  use_think = "Thinking" in thinking_mode
239
  max_new_tokens = min(int(max_new_tokens), 8192)
240
 
 
315
  .model-box .st { font-size: 10px; color: #78716c; margin-top: 4px; }
316
  """
317
 
318
+ def _model_info_html(name):
319
+ m = MODELS.get(name, MODELS[DEFAULT_MODEL])
320
+ icon = "⚑" if m["arch"] == "MoE" else "πŸ†"
321
+ return (
322
+ f'<div class="model-box">'
323
+ f'<b>{icon} {name}</b> '
324
+ f'<span style="font-size:9px;padding:2px 6px;border-radius:6px;background:rgba(109,40,217,.08);color:#6d28d9;font-weight:700">{m["arch"]}</span><br>'
325
+ f'<div class="st">{m["active"]} active / {m["total"]} total Β· πŸ‘οΈ Vision Β· {m["ctx"]} context</div>'
326
+ f'<div class="st">{m["desc"]}</div>'
327
+ f'<div class="st" style="margin-top:6px">'
328
+ f'<a href="https://huggingface.co/{m["id"]}" target="_blank" style="color:#6d28d9;font-weight:700;text-decoration:none">πŸ€— Model Card β†—</a> Β· '
329
+ f'<a href="https://deepmind.google/models/gemma/gemma-4/" target="_blank" style="color:#059669;font-weight:700;text-decoration:none">πŸ”¬ DeepMind β†—</a>'
330
+ f'</div></div>'
331
+ )
332
 
333
  with gr.Blocks(title="Gemma 4 Playground") as demo:
334
 
335
  gr.Markdown("## πŸ’Ž Gemma 4 Playground\nGoogle DeepMind Β· Apache 2.0 Β· Vision Β· Thinking")
336
 
337
  with gr.Row():
338
+ # ── Sidebar ──
339
  with gr.Column(scale=0, min_width=280):
340
+ model_dd = gr.Dropdown(
341
+ choices=list(MODELS.keys()), value=DEFAULT_MODEL, label="Model",
342
+ info="⚑MoE=Fast | πŸ†Dense=Best quality (μ „ν™˜ μ‹œ 1-2λΆ„)",
343
+ )
344
+ model_info = gr.HTML(value=_model_info_html(DEFAULT_MODEL))
345
  image_input = gr.Image(label="πŸ‘οΈ Image (Vision)", type="filepath", height=140)
346
  thinking_radio = gr.Radio(["⚑ Fast", "🧠 Thinking"], value="⚑ Fast", label="Mode")
347
  with gr.Accordion("βš™οΈ Settings", open=False):
 
363
  send_btn = gr.Button("↑", variant="primary", scale=0, min_width=48, elem_id="send-btn")
364
 
365
  # ── Events ──
366
+ model_dd.change(fn=_model_info_html, inputs=[model_dd], outputs=[model_info])
367
  preset_dd.change(fn=lambda k: PRESETS.get(k, PRESETS["general"]), inputs=[preset_dd], outputs=[sys_prompt])
368
 
369
  def user_msg(msg, hist):
370
  if not msg.strip(): return "", hist
371
  return "", hist + [{"role": "user", "content": msg}]
372
 
373
+ def bot_reply(hist, think, img, sysp, maxt, tmp, tp, model):
374
  if not hist or hist[-1]["role"] != "user": return hist
375
  txt, past = hist[-1]["content"], hist[:-1]
376
  hist = hist + [{"role": "assistant", "content": ""}]
377
+ for chunk in generate_reply(txt, past, think, img, sysp, maxt, tmp, tp, model):
378
  hist[-1]["content"] = chunk
379
  yield hist
380
 
381
+ ins = [chatbot, thinking_radio, image_input, sys_prompt, max_tok, temp, topp, model_dd]
382
  send_btn.click(user_msg, [chat_input, chatbot], [chat_input, chatbot], queue=False).then(bot_reply, ins, chatbot)
383
  chat_input.submit(user_msg, [chat_input, chatbot], [chat_input, chatbot], queue=False).then(bot_reply, ins, chatbot)
384
  clear_btn.click(lambda: [], None, chatbot, queue=False)