Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from itertools import islice
|
|
| 6 |
from datetime import datetime
|
| 7 |
import re # for parsing <think> blocks
|
| 8 |
import gradio as gr
|
| 9 |
-
from typing import Dict, Union
|
| 10 |
import torch
|
| 11 |
from transformers import pipeline, TextIteratorStreamer
|
| 12 |
from transformers import AutoTokenizer
|
|
@@ -249,172 +248,31 @@ def update_default_prompt(enable_search):
|
|
| 249 |
# ------------------------------
|
| 250 |
# Gradio UI
|
| 251 |
# ------------------------------
|
| 252 |
-
|
| 253 |
-
#
|
| 254 |
-
|
| 255 |
-
"en": {
|
| 256 |
-
"title": "Yee-R1 Demo",
|
| 257 |
-
"description": "Yee AI Data Security Expert",
|
| 258 |
-
"select_model": "Select Model",
|
| 259 |
-
"enable_search": "Enable Web Search",
|
| 260 |
-
"system_prompt": "System Prompt",
|
| 261 |
-
"generation_parameters": "Generation Parameters",
|
| 262 |
-
"max_tokens": "Max Tokens",
|
| 263 |
-
"temperature": "Temperature",
|
| 264 |
-
"top_k": "Top-K",
|
| 265 |
-
"top_p": "Top-P",
|
| 266 |
-
"repeat_penalty": "Repetition Penalty",
|
| 267 |
-
"web_search_settings": "Web Search Settings",
|
| 268 |
-
"max_results": "Max Results",
|
| 269 |
-
"max_chars_result": "Max Chars/Result",
|
| 270 |
-
"search_timeout": "Search Timeout (s)",
|
| 271 |
-
"clear_chat": "Clear Chat",
|
| 272 |
-
"cancel_generation": "Cancel Generation",
|
| 273 |
-
"chat_placeholder": "Type your message and press Enter...",
|
| 274 |
-
"theme_label": "Select Theme",
|
| 275 |
-
"language_label": "Select Language",
|
| 276 |
-
"theme_light": "Light",
|
| 277 |
-
"theme_dark": "Dark",
|
| 278 |
-
"language_en": "English",
|
| 279 |
-
"language_zh": "Chinese",
|
| 280 |
-
},
|
| 281 |
-
"zh": {
|
| 282 |
-
"title": "小熠演示",
|
| 283 |
-
"description": "小熠AI数据安全专家",
|
| 284 |
-
"select_model": "选择模型",
|
| 285 |
-
"enable_search": "启用网络搜索",
|
| 286 |
-
"system_prompt": "系统提示词",
|
| 287 |
-
"generation_parameters": "生成参数",
|
| 288 |
-
"max_tokens": "最大生成长度",
|
| 289 |
-
"temperature": "温度",
|
| 290 |
-
"top_k": "Top-K",
|
| 291 |
-
"top_p": "Top-P",
|
| 292 |
-
"repeat_penalty": "重复惩罚",
|
| 293 |
-
"web_search_settings": "网络搜索设置",
|
| 294 |
-
"max_results": "最大结果数",
|
| 295 |
-
"max_chars_result": "每个结果最大字符数",
|
| 296 |
-
"search_timeout": "搜索超时 (秒)",
|
| 297 |
-
"clear_chat": "清空聊天",
|
| 298 |
-
"cancel_generation": "取消生成",
|
| 299 |
-
"chat_placeholder": "请输入消息,按回车发送...",
|
| 300 |
-
"theme_label": "选择主题",
|
| 301 |
-
"language_label": "选择语言",
|
| 302 |
-
"theme_light": "明亮模式",
|
| 303 |
-
"theme_dark": "暗黑模式",
|
| 304 |
-
"language_en": "English",
|
| 305 |
-
"language_zh": "中文",
|
| 306 |
-
}
|
| 307 |
-
}
|
| 308 |
-
|
| 309 |
-
def get_ui_text(language, key):
|
| 310 |
-
return UI_TEXTS.get(language, UI_TEXTS["en"]).get(key, "")
|
| 311 |
-
|
| 312 |
-
with gr.Blocks(title=get_ui_text("en", "title")) as demo:
|
| 313 |
-
theme_dropdown = gr.Dropdown(label=get_ui_text("en", "theme_label"),
|
| 314 |
-
choices=[get_ui_text("en", "theme_light"), get_ui_text("en", "theme_dark")],
|
| 315 |
-
value=get_ui_text("en", "theme_light"))
|
| 316 |
-
language_dropdown = gr.Dropdown(label=get_ui_text("en", "language_label"),
|
| 317 |
-
choices=[get_ui_text("en", "language_en"), get_ui_text("en", "language_zh")],
|
| 318 |
-
value=get_ui_text("en", "language_en"))
|
| 319 |
-
|
| 320 |
-
title_md = gr.Markdown(value=get_ui_text("en", "title"))
|
| 321 |
-
description_md = gr.Markdown(value=get_ui_text("en", "description"))
|
| 322 |
-
|
| 323 |
with gr.Row():
|
| 324 |
with gr.Column(scale=3):
|
| 325 |
-
model_dd = gr.Dropdown(label=
|
| 326 |
-
search_chk = gr.Checkbox(label=
|
| 327 |
-
sys_prompt = gr.Textbox(label=
|
| 328 |
-
gr.Markdown(
|
| 329 |
-
max_tok = gr.Slider(64, 16384, value=4096, step=32, label=
|
| 330 |
-
temp = gr.Slider(0.1, 2.0, value=0.6, step=0.1, label=
|
| 331 |
k = gr.Slider(1, 100, value=40, step=1, label="Top-K")
|
| 332 |
p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-P")
|
| 333 |
-
rp = gr.Slider(1.0, 2.0, value=1.2, step=0.1, label=
|
| 334 |
-
gr.Markdown(
|
| 335 |
-
mr = gr.Number(value=6, precision=0, label=
|
| 336 |
-
mc = gr.Number(value=600, precision=0, label=
|
| 337 |
-
st = gr.Slider(minimum=0.0, maximum=30.0, step=0.5, value=5.0, label=
|
| 338 |
-
clr = gr.Button(
|
| 339 |
-
cnl = gr.Button(
|
| 340 |
with gr.Column(scale=7):
|
| 341 |
chat = gr.Chatbot(type="messages", show_copy_all_button=True)
|
| 342 |
-
txt = gr.Textbox(placeholder=
|
| 343 |
dbg = gr.Markdown()
|
| 344 |
|
| 345 |
-
# Function to update UI texts when language or theme changes
|
| 346 |
-
def update_ui(language, theme):
|
| 347 |
-
texts = UI_TEXTS.get("en") if language == UI_TEXTS["en"]["language_en"] else UI_TEXTS.get("zh")
|
| 348 |
-
# Map passed language (English/Chinese) to keys
|
| 349 |
-
lang_key = "en" if language == UI_TEXTS["en"]["language_en"] else "zh"
|
| 350 |
-
# Update labels
|
| 351 |
-
return (texts["title"], texts["description"],
|
| 352 |
-
texts["select_model"], texts["enable_search"], texts["system_prompt"],
|
| 353 |
-
texts["generation_parameters"], texts["max_tokens"], texts["temperature"], texts["repeat_penalty"],
|
| 354 |
-
texts["web_search_settings"], texts["max_results"], texts["max_chars_result"], texts["search_timeout"],
|
| 355 |
-
texts["clear_chat"], texts["cancel_generation"], texts["chat_placeholder"],
|
| 356 |
-
gr.themes.Dark() if theme == texts["theme_dark"] else gr.themes.Default())
|
| 357 |
-
|
| 358 |
-
def toggle_language_and_theme(language, theme):
|
| 359 |
-
# Return updated texts and theme to update all UI elements
|
| 360 |
-
lang_key = "en" if language == UI_TEXTS["en"]["language_en"] else "zh"
|
| 361 |
-
texts = UI_TEXTS[lang_key]
|
| 362 |
-
return {
|
| 363 |
-
"title_md": texts["title"],
|
| 364 |
-
"description_md": texts["description"],
|
| 365 |
-
"model_dd_label": texts["select_model"],
|
| 366 |
-
"search_chk_label": texts["enable_search"],
|
| 367 |
-
"sys_prompt_label": texts["system_prompt"],
|
| 368 |
-
"max_tok_label": texts["max_tokens"],
|
| 369 |
-
"temp_label": texts["temperature"],
|
| 370 |
-
"rp_label": texts["repeat_penalty"],
|
| 371 |
-
"mr_label": texts["max_results"],
|
| 372 |
-
"mc_label": texts["max_chars_result"],
|
| 373 |
-
"st_label": texts["search_timeout"],
|
| 374 |
-
"clr_label": texts["clear_chat"],
|
| 375 |
-
"cnl_label": texts["cancel_generation"],
|
| 376 |
-
"txt_placeholder": texts["chat_placeholder"],
|
| 377 |
-
"theme_obj": gr.themes.Dark() if theme == texts["theme_dark"] else gr.themes.Default()
|
| 378 |
-
}
|
| 379 |
-
|
| 380 |
-
# Update UI text labels on language or theme change
|
| 381 |
-
language_dropdown.change(
|
| 382 |
-
fn=toggle_language_and_theme,
|
| 383 |
-
inputs=[language_dropdown, theme_dropdown],
|
| 384 |
-
outputs=[
|
| 385 |
-
title_md, description_md, model_dd, search_chk, sys_prompt,
|
| 386 |
-
max_tok, temp, rp, mr, mc, st, clr, cnl, txt
|
| 387 |
-
],
|
| 388 |
-
_js="""
|
| 389 |
-
function updateLabels(resp) {
|
| 390 |
-
title_md.textContent = resp.title_md;
|
| 391 |
-
description_md.textContent = resp.description_md;
|
| 392 |
-
model_dd.label = resp.model_dd_label;
|
| 393 |
-
search_chk.label = resp.search_chk_label;
|
| 394 |
-
sys_prompt.label = resp.sys_prompt_label;
|
| 395 |
-
max_tok.label = resp.max_tok_label;
|
| 396 |
-
temp.label = resp.temp_label;
|
| 397 |
-
rp.label = resp.rp_label;
|
| 398 |
-
mr.label = resp.mr_label;
|
| 399 |
-
mc.label = resp.mc_label;
|
| 400 |
-
st.label = resp.st_label;
|
| 401 |
-
clr.textContent = resp.clr_label;
|
| 402 |
-
cnl.textContent = resp.cnl_label;
|
| 403 |
-
txt.placeholder = resp.txt_placeholder;
|
| 404 |
-
return;
|
| 405 |
-
}
|
| 406 |
-
"""
|
| 407 |
-
)
|
| 408 |
-
|
| 409 |
-
theme_dropdown.change(
|
| 410 |
-
fn=toggle_language_and_theme,
|
| 411 |
-
inputs=[language_dropdown, theme_dropdown],
|
| 412 |
-
outputs=[
|
| 413 |
-
title_md, description_md, model_dd, search_chk, sys_prompt,
|
| 414 |
-
max_tok, temp, rp, mr, mc, st, clr, cnl, txt
|
| 415 |
-
]
|
| 416 |
-
)
|
| 417 |
-
|
| 418 |
search_chk.change(fn=update_default_prompt, inputs=search_chk, outputs=sys_prompt)
|
| 419 |
clr.click(fn=lambda: ([], "", ""), outputs=[chat, txt, dbg])
|
| 420 |
cnl.click(fn=cancel_generation, outputs=dbg)
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
import re # for parsing <think> blocks
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
import torch
|
| 10 |
from transformers import pipeline, TextIteratorStreamer
|
| 11 |
from transformers import AutoTokenizer
|
|
|
|
| 248 |
# ------------------------------
|
| 249 |
# Gradio UI
|
| 250 |
# ------------------------------
|
| 251 |
+
with gr.Blocks(title="Yee-R1-Demo") as demo:
|
| 252 |
+
gr.Markdown("## Yee-R1 Demo")
|
| 253 |
+
gr.Markdown("小熠(Yee)AI 数据安全专家")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
with gr.Row():
|
| 255 |
with gr.Column(scale=3):
|
| 256 |
+
model_dd = gr.Dropdown(label="Select Model", choices=list(MODELS.keys()), value=list(MODELS.keys())[0])
|
| 257 |
+
search_chk = gr.Checkbox(label="Enable Web Search", value=True)
|
| 258 |
+
sys_prompt = gr.Textbox(label="System Prompt", lines=3, value=update_default_prompt(search_chk.value))
|
| 259 |
+
gr.Markdown("### Generation Parameters")
|
| 260 |
+
max_tok = gr.Slider(64, 16384, value=4096, step=32, label="Max Tokens")
|
| 261 |
+
temp = gr.Slider(0.1, 2.0, value=0.6, step=0.1, label="Temperature")
|
| 262 |
k = gr.Slider(1, 100, value=40, step=1, label="Top-K")
|
| 263 |
p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-P")
|
| 264 |
+
rp = gr.Slider(1.0, 2.0, value=1.2, step=0.1, label="Repetition Penalty")
|
| 265 |
+
gr.Markdown("### Web Search Settings")
|
| 266 |
+
mr = gr.Number(value=6, precision=0, label="Max Results")
|
| 267 |
+
mc = gr.Number(value=600, precision=0, label="Max Chars/Result")
|
| 268 |
+
st = gr.Slider(minimum=0.0, maximum=30.0, step=0.5, value=5.0, label="Search Timeout (s)")
|
| 269 |
+
clr = gr.Button("Clear Chat")
|
| 270 |
+
cnl = gr.Button("Cancel Generation")
|
| 271 |
with gr.Column(scale=7):
|
| 272 |
chat = gr.Chatbot(type="messages", show_copy_all_button=True)
|
| 273 |
+
txt = gr.Textbox(placeholder="Type your message and press Enter...")
|
| 274 |
dbg = gr.Markdown()
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
search_chk.change(fn=update_default_prompt, inputs=search_chk, outputs=sys_prompt)
|
| 277 |
clr.click(fn=lambda: ([], "", ""), outputs=[chat, txt, dbg])
|
| 278 |
cnl.click(fn=cancel_generation, outputs=dbg)
|