"""
来自 https://github.com/OpenLMLab/MOSS/blob/main/moss_web_demo_gradio.py
# 难点
## TODO
-[x] 代码和表格的预览
-[x] markdown解析:mdtex2html
-[ ] 可编辑chatbot:https://github.com/gradio-app/gradio/issues/4444
-[ ] 乱码问题
## Reference
- https://github.com/GaiZhenbiao/ChuanhuChatGPT/
"""
import config
from app_util import *
system_list = [
"You are a helpful assistant.",
"你是一个导游。",
"你是一个英语老师。",
"你是一个程序员。",
"你是一个心理咨询师。",
]
"""
TODO: 使用说明
"""
with gr.Blocks() as demo:
# Knowledge Distillation through Self Chatting
gr.HTML("""
Distilling the Knowledge through Self Chatting
""")
with gr.Row():
system = gr.Dropdown(
choices=system_list,
value=system_list[0],
allow_custom_value=True,
interactive=True,
label="System message",
scale=4,
)
gr.Dropdown(
["Qwen2-0.5B-Instruct", "llama3.1", "gemini"],
value="Qwen2-0.5B-Instruct",
label="model",
interactive=True,
scale=1,
)
chatbot = gr.Chatbot(avatar_images=("assets/man.png", "assets/bot.png"))
with gr.Row():
with gr.Column(scale=4):
generated_text = gr.Textbox(show_label=False, placeholder="...", lines=10, visible=False)
with gr.Row():
generate_btn = gr.Button("🤔️ Generate")
retry_btn = gr.Button("🔄 Regenerate")
undo_btn = gr.Button("↩️ Undo")
clear_btn = gr.Button("🗑️ Clear") # 🧹 Clear History (清除历史)
stop_btn = gr.Button("停止生成", variant="primary")
slider_max_tokens = gr.Slider(minimum=1, maximum=config.MAX_SEQUENCE_LENGTH,
value=config.DEFAULT_MAX_TOKENS, step=1, label="Max tokens")
slider_temperature = gr.Slider(minimum=0.1, maximum=10.0,
value=config.DEFAULT_TEMPERATURE, step=0.1, label="Temperature",
info="Larger temperature increase the randomness")
slider_top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=config.DEFAULT_TOP_P,
step=0.05,
label="Top-p (nucleus sampling)",
)
########
history = gr.State([{"role": "system", "content": system_list[0]}])
system.change(reset_state, inputs=[system], outputs=[chatbot, history])
clear_btn.click(reset_state, inputs=[system], outputs=[chatbot, history])
generate_btn.click(generate, [chatbot, history], outputs=[generated_text, chatbot, history],
show_progress="full")
retry_btn.click(undo_generate, [chatbot, history], outputs=[generated_text, chatbot, history]) \
.then(generate, [chatbot, history], outputs=[generated_text, chatbot, history],
show_progress="full")
undo_btn.click(undo_generate, [chatbot, history], outputs=[generated_text, chatbot, history])
slider_max_tokens.change(set_max_tokens, inputs=[slider_max_tokens])
slider_top_p.change(set_top_p, inputs=[slider_top_p])
slider_temperature.change(set_temperature, inputs=[slider_temperature])
# demo.queue().launch(share=False, server_name="0.0.0.0")
# demo.queue().launch(concurrency_count=1, max_size=5)
demo.queue().launch()