From 6b61a3e16ee107361ad08f1766e2655ec9ef94b4 Mon Sep 17 00:00:00 2001 From: "yuze.zyz" Date: Fri, 19 Jan 2024 00:27:12 +0800 Subject: [PATCH 1/2] fix ui bugs --- swift/ui/app.py | 6 +++- swift/ui/llm_infer/llm_infer.py | 28 +++++++++++---- swift/ui/llm_train/runtime.py | 64 +++++++++++++++------------------ 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/swift/ui/app.py b/swift/ui/app.py index 90fc3730d2..e21b78bd44 100644 --- a/swift/ui/app.py +++ b/swift/ui/app.py @@ -34,5 +34,9 @@ def run_ui(): LLMTrain.build_ui(LLMTrain) LLMInfer.build_ui(LLMInfer) + port = os.environ.get('WEBUI_PORT', None) app.queue().launch( - height=800, share=bool(int(os.environ.get('WEBUI_SHARE', '0')))) + server_name=os.environ.get('WEBUI_SERVER', None), + server_port=port if port is None else int(port), + height=800, + share=bool(int(os.environ.get('WEBUI_SHARE', '0')))) diff --git a/swift/ui/llm_infer/llm_infer.py b/swift/ui/llm_infer/llm_infer.py index 5cb2d079b7..7b9ac4c3ec 100644 --- a/swift/ui/llm_infer/llm_infer.py +++ b/swift/ui/llm_infer/llm_infer.py @@ -41,6 +41,12 @@ class LLMInfer(BaseUI): 'en': 'Start to load model, please wait' } }, + 'loaded_alert': { + 'value': { + 'zh': '模型加载完成', + 'en': 'Model loaded' + } + }, 'chatbot': { 'value': { 'zh': '对话框', @@ -117,19 +123,27 @@ def do_build_ui(cls, base_tab: Type['BaseUI']): clear_history.click( fn=cls.clear_session, inputs=[], outputs=[prompt, chatbot]) cls.element('load_checkpoint').click( - cls.reset_memory, [], [model_and_template], - show_progress=False).then( + cls.reset_memory, [], [model_and_template])\ + .then(cls.reset_loading_button, [], [cls.element('load_checkpoint')]).then( cls.prepare_checkpoint, [ value for value in cls.elements().values() if not isinstance(value, (Tab, Accordion)) - ], [model_and_template], - show_progress=True).then(cls.change_interactive, [], - [prompt]) - cls.element('load_checkpoint').click( + ], [model_and_template]).then(cls.change_interactive, [], + [prompt]).then( # noqa cls.clear_session, inputs=[], outputs=[prompt, chatbot], - queue=True) + queue=True).then(cls.reset_load_button, [], [cls.element('load_checkpoint')]) + + @classmethod + def reset_load_button(cls): + gr.Info(cls.locale('loaded_alert', cls.lang)['value']) + return gr.update( + value=cls.locale('load_checkpoint', cls.lang)['value']) + + @classmethod + def reset_loading_button(cls): + return gr.update(value=cls.locale('load_alert', cls.lang)['value']) @classmethod def reset_memory(cls): diff --git a/swift/ui/llm_train/runtime.py b/swift/ui/llm_train/runtime.py index 773616a544..da43961c2f 100644 --- a/swift/ui/llm_train/runtime.py +++ b/swift/ui/llm_train/runtime.py @@ -120,9 +120,7 @@ def do_build_ui(cls, base_tab: Type['BaseUI']): base_tab.element('show_log').click( Runtime.update_log, [], [cls.element('log')]).then( Runtime.wait, [base_tab.element('logging_dir')], - [cls.element('log')], - show_progress=True, - queue=True) + [cls.element('log')]) base_tab.element('start_tb').click( Runtime.start_tb, @@ -147,40 +145,34 @@ def wait(cls, logging_dir): latest_data = '' lines = collections.deque( maxlen=int(os.environ.get('MAX_LOG_LINES', 50))) - while True: - try: - with open(log_file) as input: - input.seek(offset) - fail_cnt = 0 - while True: + try: + with open(log_file, 'r') as input: + input.seek(offset) + fail_cnt = 0 + while True: + try: latest_data += input.read() - offset = input.tell() - if not latest_data: - time.sleep(0.5) - fail_cnt += 1 - if fail_cnt > 5: - break - - if '\n' not in latest_data: - continue - latest_lines = latest_data.split('\n') - if latest_data[-1] != '\n': - latest_data = latest_lines[-1] - latest_lines = latest_lines[:-1] - else: - latest_data = '' - lines.extend(latest_lines) - yield '\n'.join(lines) - except IOError: - pass - - process_name = 'swift' - process_find = False - for proc in psutil.process_iter(): - if proc.name() == process_name: - process_find = proc.pid - if not process_find: - break + except UnicodeDecodeError: + continue + offset = input.tell() + if not latest_data: + time.sleep(0.5) + fail_cnt += 1 + if fail_cnt > 20: + break + + if '\n' not in latest_data: + continue + latest_lines = latest_data.split('\n') + if latest_data[-1] != '\n': + latest_data = latest_lines[-1] + latest_lines = latest_lines[:-1] + else: + latest_data = '' + lines.extend(latest_lines) + yield '\n'.join(lines) + except IOError: + pass @classmethod def show_log(cls, logging_dir): From dabac37809a4e3bff79e0e40410e883aff1a0836 Mon Sep 17 00:00:00 2001 From: "yuze.zyz" Date: Fri, 19 Jan 2024 00:33:19 +0800 Subject: [PATCH 2/2] add docs --- ...\235\242\350\256\255\347\273\203\346\216\250\347\220\206.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/docs/source/GetStarted/\347\225\214\351\235\242\350\256\255\347\273\203\346\216\250\347\220\206.md" "b/docs/source/GetStarted/\347\225\214\351\235\242\350\256\255\347\273\203\346\216\250\347\220\206.md" index 8ce33f7713..1705ab3878 100644 --- "a/docs/source/GetStarted/\347\225\214\351\235\242\350\256\255\347\273\203\346\216\250\347\220\206.md" +++ "b/docs/source/GetStarted/\347\225\214\351\235\242\350\256\255\347\273\203\346\216\250\347\220\206.md" @@ -10,3 +10,5 @@ web-ui没有传入参数,所有可控部分都在界面中。但是有几个 > WEBUI_SHARE=1 控制gradio是否是share状态 > SWIFT_UI_LANG=en/zh 控制web-ui界面语言 +> WEBUI_SERVER server_name参数, web-ui host ip,0.0.0.0代表所有ip均可访问,127.0.0.1代表只允许本机访问 +> WEBUI_PORT web-ui的端口号