Spaces:
Sleeping
Sleeping
ui_components
#1
by FrostIce - opened
Files changed (6)
- .gitattributes+35-1
- README.md+6-6
- app.py+628-116
- config.pyAdded+230
- gitattributesDeleted-36
- requirements.txt+2-10
1assets/qwen.png filter=lfs diff=lfs merge=lfs -text1234567891011121314151617181920212223242526272829303132333435*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text
123456789101112---title: Close-SFGemoji: 📊colorFrom: redcolorTo: graysdk: gradiosdk_version: 5.27.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112---title: Close SFGemoji: 🏃colorFrom: greencolorTo: redsdk: gradiosdk_version: 5.39.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124# server.pyimport torchimport threadingimport timeimport numpy as npimport reimport jsonfrom transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamerimport gradio as gr# === Модель ===model_name = "Qwen/Qwen2.5-0.5B-Instruct"tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_name,device_map="cpu",torch_dtype=torch.float16,load_in_4bit=True,bnb_4bit_quant_type="nf4",bnb_4bit_compute_dtype=torch.float16,)try:model = torch.compile(model, mode="reduce-overhead", fullgraph=True)print("✅ torch.compile активирован")except:pass# === Tools ===tools = [{"name": "get_weather","parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}]def execute_tool_call(call):city = call.get("arguments", {}).get("city", "неизвестен")return f"🌤️ Погода в {city}: 22°C, солнечно. (симуляция)"# === NumPy-парсер ===def find_and_replace_tool_calls_numpy(buffer):chars = np.array(list(buffer), dtype='U1')indices = np.where(chars == '<tool_call>')[0]if len(indices) < 2 or len(indices) % 2 != 0:return buffer, Falsenew_buffer = bufferreplaced = Falsefor i in range(0, len(indices) - 1, 2):start, end = indices[i], indices[i + 1] + 2if end > len(buffer): continueblock = buffer[start:end]content = buffer[start+2:end-2].strip()try:json_match = re.search(r'\{.*\}', content, re.DOTALL)if json_match:data = json.loads(json_match.group())result = execute_tool_call(data)new_buffer = new_buffer.replace(block, f"\n\n✅ {result}\n\n")replaced = Trueexcept:passreturn new_buffer, replaced# === Генерация с "GPU-эффектом" ===def generate_stream(prompt, max_new_tokens=128, temperature=0.7, top_p=0.9):messages = [{"role": "user", "content": prompt}]inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(model.device)streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)thread = threading.Thread(target=model.generate, kwargs={"input_ids": inputs,"max_new_tokens": max_new_tokens,"temperature": temperature,"top_p": top_p,"do_sample": True,"pad_token_id": tokenizer.pad_token_id,"eos_token_id": tokenizer.eos_token_id,"streamer": streamer,"use_cache": True})thread.start()buffer = ""full_text = ""last_yield = time.time()for token in streamer:buffer += tokenfull_text += token# NumPy обработкаif "<tool_call>" in buffer:processed, changed = find_and_replace_tool_calls_numpy(full_text)if changed:full_text = processedbuffer = ""yield full_textcontinuenow = time.time()if (len(buffer) >= 30 orany(p in buffer for p in ".!?;\n") ornow - last_yield > 0.7):yield full_textbuffer = ""last_yield = nowif full_text:yield full_text# === Gradio ===with gr.Blocks() as demo:prompt = gr.Textbox(label="Ввод", placeholder="Спроси что-нибудь...")max_t = gr.Slider(64, 256, 128, step=32, label="Max Tokens")temp = gr.Slider(0.1, 1.5, 0.7, step=0.1, label="Temperature")top_p = gr.Slider(0.5, 1.0, 0.9, step=0.05, label="Top-p")btn = gr.Button("🚀 GPU-режим")output = gr.Textbox(label="Ответ")btn.click(generate_stream, [prompt, max_t, temp, top_p], output)if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636import uuidimport timeimport jsonimport gradio as grimport modelscope_studio.components.antd as antdimport modelscope_studio.components.antdx as antdximport modelscope_studio.components.base as msimport modelscope_studio.components.pro as proimport dashscopefrom config import DEFAULT_LOCALE, DEFAULT_SETTINGS, DEFAULT_THEME, DEFAULT_SUGGESTIONS, save_history, get_text, user_config, bot_config, welcome_config, api_key, MODEL_OPTIONS_MAPfrom ui_components.logo import Logofrom ui_components.settings_header import SettingsHeaderfrom ui_components.thinking_button import ThinkingButtonfrom dashscope import Generationdashscope.api_key = api_keydef format_history(history, sys_prompt):# messages = [{# "role": "system",# "content": sys_prompt,# }]messages = []for item in history:if item["role"] == "user":messages.append({"role": "user", "content": item["content"]})elif item["role"] == "assistant":contents = [{"type": "text","text": content["content"]} for content in item["content"] if content["type"] == "text"]messages.append({"role":"assistant","content":contents[0]["text"] if len(contents) > 0 else ""})return messagesclass Gradio_Events:@staticmethoddef submit(state_value):history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]settings = state_value["conversation_contexts"][state_value["conversation_id"]]["settings"]enable_thinking = state_value["conversation_contexts"][state_value["conversation_id"]]["enable_thinking"]model = settings.get("model")messages = format_history(history,sys_prompt=settings.get("sys_prompt", ""))history.append({"role":"assistant","content": [],"key":str(uuid.uuid4()),"header":MODEL_OPTIONS_MAP.get(model, {}).get("label", None),"loading":True,"status":"pending"})yield {chatbot: gr.update(value=history),state: gr.update(value=state_value),}try:response = Generation.call(model=model,messages=messages,stream=True,result_format='message',incremental_output=True,enable_thinking=enable_thinking,thinking_budget=settings.get("thinking_budget", 1) * 1024)start_time = time.time()reasoning_content = ""answer_content = ""is_thinking = Falseis_answering = Falsecontents = [None, None]for chunk in response:if (not chunk.output.choices[0].message.get("content")and not chunk.output.choices[0].message.get("reasoning_content")):passelse:delta = chunk.output.choices[0].messageif hasattr(delta,'reasoning_content') and delta.reasoning_content:if not is_thinking:contents[0] = {"type": "tool","content": "","options": {"title": get_text("Thinking...", "思考中..."),"status": "pending"},"copyable": False,"editable": False}is_thinking = Truereasoning_content += delta.reasoning_contentif hasattr(delta, 'content') and delta.content:if not is_answering:thought_cost_time = "{:.2f}".format(time.time() -start_time)if contents[0]:contents[0]["options"]["title"] = get_text(f"End of Thought ({thought_cost_time}s)",f"已深度思考 (用时{thought_cost_time}s)")contents[0]["options"]["status"] = "done"contents[1] = {"type": "text","content": "",}is_answering = Trueanswer_content += delta.contentif contents[0]:contents[0]["content"] = reasoning_contentif contents[1]:contents[1]["content"] = answer_contenthistory[-1]["content"] = [content for content in contents if content]history[-1]["loading"] = Falseyield {chatbot: gr.update(value=history),state: gr.update(value=state_value)}print("model: ", model, "-", "reasoning_content: ",reasoning_content, "\n", "content: ", answer_content)history[-1]["status"] = "done"cost_time = "{:.2f}".format(time.time() - start_time)history[-1]["footer"] = get_text(f"{cost_time}s",f"用时{cost_time}s")yield {chatbot: gr.update(value=history),state: gr.update(value=state_value),}except Exception as e:print("model: ", model, "-", "Error: ", e)history[-1]["loading"] = Falsehistory[-1]["status"] = "done"history[-1]["content"] += [{"type":"text","content":f'<span style="color: var(--color-red-500)">{str(e)}</span>'}]yield {chatbot: gr.update(value=history),state: gr.update(value=state_value)}raise e@staticmethoddef add_message(input_value, settings_form_value, thinking_btn_state_value,state_value):if not state_value["conversation_id"]:random_id = str(uuid.uuid4())history = []state_value["conversation_id"] = random_idstate_value["conversation_contexts"][state_value["conversation_id"]] = {"history": history}state_value["conversations"].append({"label": input_value,"key": random_id})history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]state_value["conversation_contexts"][state_value["conversation_id"]] = {"history": history,"settings": settings_form_value,"enable_thinking": thinking_btn_state_value["enable_thinking"]}history.append({"role": "user","content": input_value,"key": str(uuid.uuid4())})yield Gradio_Events.preprocess_submit(clear_input=True)(state_value)try:for chunk in Gradio_Events.submit(state_value):yield chunkexcept Exception as e:raise efinally:yield Gradio_Events.postprocess_submit(state_value)@staticmethoddef preprocess_submit(clear_input=True):def preprocess_submit_handler(state_value):history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]return {**({input:gr.update(value=None, loading=True) if clear_input else gr.update(loading=True),} if clear_input else {}),conversations:gr.update(active_key=state_value["conversation_id"],items=list(map(lambda item: {**item,"disabled":True if item["key"] != state_value["conversation_id"] else False,}, state_value["conversations"]))),add_conversation_btn:gr.update(disabled=True),clear_btn:gr.update(disabled=True),conversation_delete_menu_item:gr.update(disabled=True),chatbot:gr.update(value=history,bot_config=bot_config(disabled_actions=['edit', 'retry', 'delete']),user_config=user_config(disabled_actions=['edit', 'delete'])),state:gr.update(value=state_value),}return preprocess_submit_handler@staticmethoddef postprocess_submit(state_value):history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]return {input:gr.update(loading=False),conversation_delete_menu_item:gr.update(disabled=False),clear_btn:gr.update(disabled=False),conversations:gr.update(items=state_value["conversations"]),add_conversation_btn:gr.update(disabled=False),chatbot:gr.update(value=history,bot_config=bot_config(),user_config=user_config()),state:gr.update(value=state_value),}@staticmethoddef cancel(state_value):history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]history[-1]["loading"] = Falsehistory[-1]["status"] = "done"history[-1]["footer"] = get_text("Chat completion paused", "对话已暂停")return Gradio_Events.postprocess_submit(state_value)@staticmethoddef delete_message(state_value, e: gr.EventData):index = e._data["payload"][0]["index"]history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]history = history[:index] + history[index + 1:]state_value["conversation_contexts"][state_value["conversation_id"]]["history"] = historyreturn gr.update(value=state_value)@staticmethoddef edit_message(state_value, chatbot_value, e: gr.EventData):index = e._data["payload"][0]["index"]history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]history[index]["content"] = chatbot_value[index]["content"]return gr.update(value=state_value)@staticmethoddef regenerate_message(settings_form_value, thinking_btn_state_value,state_value, e: gr.EventData):index = e._data["payload"][0]["index"]history = state_value["conversation_contexts"][state_value["conversation_id"]]["history"]history = history[:index]state_value["conversation_contexts"][state_value["conversation_id"]] = {"history": history,"settings": settings_form_value,"enable_thinking": thinking_btn_state_value["enable_thinking"]}yield Gradio_Events.preprocess_submit()(state_value)try:for chunk in Gradio_Events.submit(state_value):yield chunkexcept Exception as e:raise efinally:yield Gradio_Events.postprocess_submit(state_value)@staticmethoddef select_suggestion(input_value, e: gr.EventData):input_value = input_value[:-1] + e._data["payload"][0]return gr.update(value=input_value)@staticmethoddef apply_prompt(e: gr.EventData):return gr.update(value=e._data["payload"][0]["value"]["description"])@staticmethoddef new_chat(thinking_btn_state, state_value):if not state_value["conversation_id"]:return gr.skip()state_value["conversation_id"] = ""thinking_btn_state["enable_thinking"] = Truereturn gr.update(active_key=state_value["conversation_id"]), gr.update(value=None), gr.update(value=DEFAULT_SETTINGS), gr.update(value=thinking_btn_state), gr.update(value=state_value)@staticmethoddef select_conversation(thinking_btn_state_value, state_value,e: gr.EventData):active_key = e._data["payload"][0]if state_value["conversation_id"] == active_key or (active_key not in state_value["conversation_contexts"]):return gr.skip()state_value["conversation_id"] = active_keythinking_btn_state_value["enable_thinking"] = state_value["conversation_contexts"][active_key]["enable_thinking"]return gr.update(active_key=active_key), gr.update(value=state_value["conversation_contexts"][active_key]["history"]), gr.update(value=state_value["conversation_contexts"][active_key]["settings"]), gr.update(value=thinking_btn_state_value), gr.update(value=state_value)@staticmethoddef click_conversation_menu(state_value, e: gr.EventData):conversation_id = e._data["payload"][0]["key"]operation = e._data["payload"][1]["key"]if operation == "delete":del state_value["conversation_contexts"][conversation_id]state_value["conversations"] = [item for item in state_value["conversations"]if item["key"] != conversation_id]if state_value["conversation_id"] == conversation_id:state_value["conversation_id"] = ""return gr.update(items=state_value["conversations"],active_key=state_value["conversation_id"]), gr.update(value=None), gr.update(value=state_value)else:return gr.update(items=state_value["conversations"]), gr.skip(), gr.update(value=state_value)return gr.skip()@staticmethoddef toggle_settings_header(settings_header_state_value):settings_header_state_value["open"] = not settings_header_state_value["open"]return gr.update(value=settings_header_state_value)@staticmethoddef clear_conversation_history(state_value):if not state_value["conversation_id"]:return gr.skip()state_value["conversation_contexts"][state_value["conversation_id"]]["history"] = []return gr.update(value=None), gr.update(value=state_value)@staticmethoddef update_browser_state(state_value):return gr.update(value=dict(conversations=state_value["conversations"],conversation_contexts=state_value["conversation_contexts"]))@staticmethoddef apply_browser_state(browser_state_value, state_value):state_value["conversations"] = browser_state_value["conversations"]state_value["conversation_contexts"] = browser_state_value["conversation_contexts"]return gr.update(items=browser_state_value["conversations"]), gr.update(value=state_value)css = """.gradio-container {padding: 0 !important;}.gradio-container > main.fillable {padding: 0 !important;}#chatbot {height: calc(100vh - 21px - 16px);max-height: 1500px;}#chatbot .chatbot-conversations {height: 100vh;background-color: var(--ms-gr-ant-color-bg-layout);padding-left: 4px;padding-right: 4px;}#chatbot .chatbot-conversations .chatbot-conversations-list {padding-left: 0;padding-right: 0;}#chatbot .chatbot-chat {padding: 32px;padding-bottom: 0;height: 100%;}@media (max-width: 768px) {#chatbot .chatbot-chat {padding: 0;}}#chatbot .chatbot-chat .chatbot-chat-messages {flex: 1;}#chatbot .setting-form-thinking-budget .ms-gr-ant-form-item-control-input-content {display: flex;flex-wrap: wrap;}"""model_options_map_json = json.dumps(MODEL_OPTIONS_MAP)js = "function init() { window.MODEL_OPTIONS_MAP=" + model_options_map_json + "}"with gr.Blocks(css=css, js=js, fill_width=True) as demo:state = gr.State({"conversation_contexts": {},"conversations": [],"conversation_id": "",})with ms.Application(), antdx.XProvider(theme=DEFAULT_THEME, locale=DEFAULT_LOCALE), ms.AutoLoading():with antd.Row(gutter=[20, 20], wrap=False, elem_id="chatbot"):# Left Columnwith antd.Col(md=dict(flex="0 0 260px", span=24, order=0),span=0,elem_style=dict(width=0),order=1):with ms.Div(elem_classes="chatbot-conversations"):with antd.Flex(vertical=True,gap="small",elem_style=dict(height="100%")):# LogoLogo()# New Conversation Buttonwith antd.Button(value=None,color="primary",variant="filled",block=True) as add_conversation_btn:ms.Text(get_text("New Conversation", "新建对话"))with ms.Slot("icon"):antd.Icon("PlusOutlined")# Conversations Listwith antdx.Conversations(elem_classes="chatbot-conversations-list",) as conversations:with ms.Slot('menu.items'):with antd.Menu.Item(label="Delete", key="delete",danger=True) as conversation_delete_menu_item:with ms.Slot("icon"):antd.Icon("DeleteOutlined")# Right Columnwith antd.Col(flex=1, elem_style=dict(height="100%")):with antd.Flex(vertical=True,gap="small",elem_classes="chatbot-chat"):# Chatbotchatbot = pro.Chatbot(elem_classes="chatbot-chat-messages",height=0,welcome_config=welcome_config(),user_config=user_config(),bot_config=bot_config())# Inputwith antdx.Suggestion(items=DEFAULT_SUGGESTIONS,# onKeyDown Handler in Javascriptshould_trigger="""(e, { onTrigger, onKeyDown }) => {switch(e.key) {case '/':onTrigger()breakcase 'ArrowRight':case 'ArrowLeft':case 'ArrowUp':case 'ArrowDown':break;default:onTrigger(false)}onKeyDown(e)}""") as suggestion:with ms.Slot("children"):with antdx.Sender(placeholder=get_text("Enter \"/\" to get suggestions","输入 \"/\" 获取提示"), ) as input:with ms.Slot("header"):settings_header_state, settings_form = SettingsHeader()with ms.Slot("prefix"):with antd.Flex(gap=4,wrap=True,elem_style=dict(maxWidth='40vw')):with antd.Button(value=None,type="text") as setting_btn:with ms.Slot("icon"):antd.Icon("SettingOutlined")with antd.Button(value=None,type="text") as clear_btn:with ms.Slot("icon"):antd.Icon("ClearOutlined")thinking_btn_state = ThinkingButton()# Events Handler# Browser State Handlerif save_history:browser_state = gr.BrowserState({"conversation_contexts": {},"conversations": [],},storage_key="qwen3_chat_demo_storage")state.change(fn=Gradio_Events.update_browser_state,inputs=[state],outputs=[browser_state])demo.load(fn=Gradio_Events.apply_browser_state,inputs=[browser_state, state],outputs=[conversations, state])# Conversations Handleradd_conversation_btn.click(fn=Gradio_Events.new_chat,inputs=[thinking_btn_state, state],outputs=[conversations, chatbot, settings_form,thinking_btn_state, state])conversations.active_change(fn=Gradio_Events.select_conversation,inputs=[thinking_btn_state, state],outputs=[conversations, chatbot, settings_form,thinking_btn_state, state])conversations.menu_click(fn=Gradio_Events.click_conversation_menu,inputs=[state],outputs=[conversations, chatbot, state])# Chatbot Handlerchatbot.welcome_prompt_select(fn=Gradio_Events.apply_prompt,outputs=[input])chatbot.delete(fn=Gradio_Events.delete_message,inputs=[state],outputs=[state])chatbot.edit(fn=Gradio_Events.edit_message,inputs=[state, chatbot],outputs=[state])regenerating_event = chatbot.retry(fn=Gradio_Events.regenerate_message,inputs=[settings_form, thinking_btn_state, state],outputs=[input, clear_btn, conversation_delete_menu_item,add_conversation_btn, conversations, chatbot, state])# Input Handlersubmit_event = input.submit(fn=Gradio_Events.add_message,inputs=[input, settings_form, thinking_btn_state, state],outputs=[input, clear_btn, conversation_delete_menu_item,add_conversation_btn, conversations, chatbot, state])input.cancel(fn=Gradio_Events.cancel,inputs=[state],outputs=[input, conversation_delete_menu_item, clear_btn,conversations, add_conversation_btn, chatbot, state],cancels=[submit_event, regenerating_event],queue=False)# Input Actions Handlersetting_btn.click(fn=Gradio_Events.toggle_settings_header,inputs=[settings_header_state],outputs=[settings_header_state])clear_btn.click(fn=Gradio_Events.clear_conversation_history,inputs=[state],outputs=[chatbot, state])suggestion.select(fn=Gradio_Events.select_suggestion,inputs=[input],outputs=[input])if __name__ == "__main__":demo.queue(default_concurrency_limit=100,max_size=100).launch(ssr_mode=False, max_threads=100)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230import osfrom modelscope_studio.components.pro.chatbot import ChatbotActionConfig, ChatbotBotConfig, ChatbotUserConfig, ChatbotWelcomeConfig
# Envis_cn = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'api_key = os.getenv('API_KEY')
def get_text(text: str, cn_text: str): if is_cn: return cn_text return text
# Save history in browsersave_history = True
# Chatbot Configdef user_config(disabled_actions=None): return ChatbotUserConfig( class_names=dict(content="user-message-content"), actions=[ "copy", "edit", ChatbotActionConfig( action="delete", popconfirm=dict(title=get_text("Delete the message", "删除消息"), description=get_text( "Are you sure to delete this message?", "确认删除该消息?"), okButtonProps=dict(danger=True))) ], disabled_actions=disabled_actions)
def bot_config(disabled_actions=None): return ChatbotBotConfig(actions=[ "copy", "edit", ChatbotActionConfig( action="retry", popconfirm=dict( title=get_text("Regenerate the message", "重新生成消息"), description=get_text( "Regenerate the message will also delete all subsequent messages.", "重新生成消息会删除所有后续消息。"), okButtonProps=dict(danger=True))), ChatbotActionConfig(action="delete", popconfirm=dict( title=get_text("Delete the message", "删除消息"), description=get_text( "Are you sure to delete this message?", "确认删除该消息?"), okButtonProps=dict(danger=True))) ], avatar="./assets/qwen.png", disabled_actions=disabled_actions)
def welcome_config(): return ChatbotWelcomeConfig( variant="borderless", icon="./assets/qwen.png", title=get_text("Hello, I'm Qwen3", "你好,我是 Qwen3"), description=get_text("Select a model and enter text to get started.", "选择模型并输入文本,开始对话吧。"), prompts=dict( title=get_text("How can I help you today?", "有什么我能帮助你的吗?"), styles={ "list": { "width": '100%', }, "item": { "flex": 1, }, }, items=[{ "label": get_text("📅 Make a plan", "📅 制定计划"), "children": [{ "description": get_text("Help me with a plan to start a business", "帮助我制定一个创业计划") }, { "description": get_text("Help me with a plan to achieve my goals", "帮助我制定一个实现目标的计划") }, { "description": get_text("Help me with a plan for a successful interview", "帮助我制定一个成功的面试计划") }] }, { "label": get_text("🖋 Help me write", "🖋 帮我写"), "children": [{ "description": get_text("Help me write a story with a twist ending", "帮助我写一个带有意外结局的故事") }, { "description": get_text("Help me write a blog post on mental health", "帮助我写一篇关于心理健康的博客文章") }, { "description": get_text("Help me write a letter to my future self", "帮助我写一封给未来自己的信") }] }]), )
DEFAULT_SUGGESTIONS = [{ "label": get_text('Make a plan', '制定计划'), "value": get_text('Make a plan', '制定计划'), "children": [{ "label": get_text("Start a business", "开始创业"), "value": get_text("Help me with a plan to start a business", "帮助我制定一个创业计划") }, { "label": get_text("Achieve my goals", "实现我的目标"), "value": get_text("Help me with a plan to achieve my goals", "帮助我制定一个实现目标的计划") }, { "label": get_text("Successful interview", "成功的面试"), "value": get_text("Help me with a plan for a successful interview", "帮助我制定一个成功的面试计划") }]}, { "label": get_text('Help me write', '帮我写'), "value": get_text("Help me write", '帮我写'), "children": [{ "label": get_text("Story with a twist ending", "带有意外结局的故事"), "value": get_text("Help me write a story with a twist ending", "帮助我写一个带有意外结局的故事") }, { "label": get_text("Blog post on mental health", "关于心理健康的博客文章"), "value": get_text("Help me write a blog post on mental health", "帮助我写一篇关于心理健康的博客文章") }, { "label": get_text("Letter to my future self", "给未来自己的信"), "value": get_text("Help me write a letter to my future self", "帮助我写一封给未来自己的信") }]}]
DEFAULT_SYS_PROMPT = "You are a helpful and harmless assistant."
MIN_THINKING_BUDGET = 1
MAX_THINKING_BUDGET = 38
DEFAULT_THINKING_BUDGET = 38
DEFAULT_MODEL = "qwen3-235b-a22b"
MODEL_OPTIONS = [ { "label": get_text("Qwen3-235B-A22B", "通义千问3-235B-A22B"), "modelId": "Qwen/Qwen3-235B-A22B", "value": "qwen3-235b-a22b" }, { "label": get_text("Qwen3-32B", "通义千问3-32B"), "modelId": "Qwen/Qwen3-32B", "value": "qwen3-32b" }, { "label": get_text("Qwen3-30B-A3B", "通义千问3-30B-A3B"), "modelId": "Qwen/Qwen3-30B-A3B", "value": "qwen3-30b-a3b" }, { "label": get_text("Qwen3-14B", "通义千问3-14B"), "modelId": "Qwen/Qwen3-14B", "value": "qwen3-14b" }, { "label": get_text("Qwen3-8B", "通义千问3-8B"), "modelId": "Qwen/Qwen3-8B", "value": "qwen3-8b" }, { "label": get_text("Qwen3-4B", "通义千问3-4B"), "modelId": "Qwen/Qwen3-4B", "value": "qwen3-4b" }, { "label": get_text("Qwen3-1.7B", "通义千问3-1.7B"), "modelId": "Qwen/Qwen3-1.7B", "value": "qwen3-1.7b" }, { "label": get_text("Qwen3-0.6B", "通义千问3-0.6B"), "modelId": "Qwen/Qwen3-0.6B", "value": "qwen3-0.6b" },]
for model in MODEL_OPTIONS: model[ "link"] = is_cn and f"https://modelscope.cn/models/{model['modelId']}" or f"https://huggingface.co/{model['modelId']}"
MODEL_OPTIONS_MAP = {model["value"]: model for model in MODEL_OPTIONS}
DEFAULT_LOCALE = 'zh_CN' if is_cn else 'en_US'
DEFAULT_THEME = { "token": { "colorPrimary": "#6A57FF", }}
DEFAULT_SETTINGS = { "model": DEFAULT_MODEL, "sys_prompt": DEFAULT_SYS_PROMPT, "thinking_budget": DEFAULT_THINKING_BUDGET}123456789101112131415161718192021222324252627282930313233343536*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textassets/qwen.png filter=lfs diff=lfs merge=lfs -text1234567891011vllmacceleratebitsandbytespefttransformerssentencepiecetorchgradiopillowmatplotlibtransformers-stream-generator123gradiomodelscope_studiodashscope
.gitattributes+35-1
README.md+6-6
app.py+628-116
config.pyAdded+230
gitattributesDeleted-36
requirements.txt+2-10