Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
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 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 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
|
| 330 |
with gr.Column(scale=0, min_width=280):
|
| 331 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|