From 416fd977eabd0bb0e3b63b851facddd39bafc331 Mon Sep 17 00:00:00 2001 From: HongCheng Date: Sat, 11 May 2024 18:37:13 +0900 Subject: [PATCH 1/4] update gradio web_demo --- app_Llama3_Gradio.py | 177 +++++++++++++++++++++++++++++++++++++++++ app_web_demo-Llama3.py | 2 +- web_demo-Llama3.py | 2 +- 3 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 app_Llama3_Gradio.py diff --git a/app_Llama3_Gradio.py b/app_Llama3_Gradio.py new file mode 100644 index 0000000..8cb71ed --- /dev/null +++ b/app_Llama3_Gradio.py @@ -0,0 +1,177 @@ +import gradio as gr +import os +import torch +from transformers import GemmaTokenizer, AutoModelForCausalLM +from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer +from threading import Thread + + +DESCRIPTION = ''' +
+

EmoLLM Llama3 心理咨询室 V4.0

+ +

+ + Logo + +

+ +
+ + +[![OpenXLab_Model][OpenXLab_Model-image]][OpenXLab_Model-url] + +

EmoLLM是一系列能够支持 理解用户-支持用户-帮助用户 心理健康辅导链路的 心理健康大模型 ,欢迎大家star~⭐⭐

+

https://github.com/SmartFlowAI/EmoLLM

+
+ +
+ +[OpenXLab_Model-image]: https://cdn-static.openxlab.org.cn/header/openxlab_models.svg +[OpenXLab_Model-url]: https://openxlab.org.cn/models/detail/chg0901/EmoLLM-Llama3-8B-Instruct3.0 + +''' + +LICENSE = """ +

Built with Meta Llama 3 +""" + +PLACEHOLDER = """ +

+ +
+""" + + +css = """ +h1 { + text-align: center; + display: block; +} + +""" + +# download internlm2 to the base_path directory using git tool +base_path = './EmoLLM-Llama3-8B-Instruct3.0' +os.system(f'git clone https://code.openxlab.org.cn/chg0901/EmoLLM-Llama3-8B-Instruct3.0.git {base_path}') +os.system(f'cd {base_path} && git lfs pull') + + +# Load the tokenizer and model +tokenizer = AutoTokenizer.from_pretrained(base_path,trust_remote_code=True) +model = AutoModelForCausalLM.from_pretrained(base_path,trust_remote_code=True, device_map="auto", torch_dtype=torch.float16).eval() # to("cuda:0") +terminators = [ + tokenizer.eos_token_id, + tokenizer.convert_tokens_to_ids("<|eot_id|>") +] + +def chat_llama3_8b(message: str, + history: list, + temperature: float, + max_new_tokens: int, + top_p: float + ) -> str: + """ + Generate a streaming response using the llama3-8b model. + Args: + message (str): The input message. + history (list): The conversation history used by ChatInterface. + temperature (float): The temperature for generating the response. + max_new_tokens (int): The maximum number of new tokens to generate. + Returns: + str: The generated response. + """ + conversation = [] + + for user, assistant in history: + conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}]) + conversation.append({"role": "user", "content": message}) + + input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device) + + streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True) + + generate_kwargs = dict( + input_ids= input_ids, + streamer=streamer, + max_new_tokens=max_new_tokens, + do_sample=True, + temperature=temperature, + top_p = top_p, + eos_token_id=terminators, + ) + # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash. + if temperature == 0: + generate_kwargs['do_sample'] = False + + t = Thread(target=model.generate, kwargs=generate_kwargs) + t.start() + + outputs = [] + for text in streamer: + outputs.append(text) + yield "".join(outputs) + + + +# Gradio block +chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='EmoLLM Chat') + +with gr.Blocks(fill_height=True, css=css) as demo: + + gr.Markdown(DESCRIPTION) + # gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button") + gr.ChatInterface( + fn=chat_llama3_8b, + chatbot=chatbot, + fill_height=True, + additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False), + additional_inputs=[ + gr.Slider(minimum=0, + maximum=1, + step=0.1, + value=0.95, + label="Temperature", + render=False), + gr.Slider(minimum=128, + maximum=4096, + step=1, + value=4096, + label="Max new tokens", + render=False ), + gr.Slider(minimum=0.0, + maximum=1, + step=0.01, + value=0.8, + label="Top P", + render=False ), + # gr.Slider(minimum=128, + # maximum=4096, + # step=1, + # value=512, + # label="Max new tokens", + # render=False ), + ], + examples=[ + ['请介绍你自己。'], + ['我觉得我在学校的学习压力好大啊,虽然我真的很喜欢我的专业,但最近总是担心自己无法达到自己的期望,这让我有点焦虑。'], + ['我最近总觉得自己在感情上陷入了困境,我喜欢上了我的朋友,但又害怕表达出来会破坏我们现在的关系...'], + ['我感觉自己像是被困在一个无尽的循环中。每天醒来都感到身体沉重,对日常活动提不起兴趣,工作、锻炼甚至是我曾经喜欢的事物都让我觉得厌倦'], + ['最近工作压力特别大,还有一些家庭矛盾'] + ], + cache_examples=False, + ) + + gr.Markdown(LICENSE) + +if __name__ == "__main__": + demo.launch() + + \ No newline at end of file diff --git a/app_web_demo-Llama3.py b/app_web_demo-Llama3.py index bd5a6a8..2f4f678 100644 --- a/app_web_demo-Llama3.py +++ b/app_web_demo-Llama3.py @@ -207,7 +207,7 @@ cur_query_prompt = '<|start_header_id|>user<|end_header_id|>\n\n{user}<|eot_id|> def combine_history(prompt): messages = st.session_state.messages meta_instruction = ( - "你是心理健康助手EmoLLM, 由EmoLLM团队打造, 是一个研究过无数具有心理健康问题的病人与心理健康医生对话的心理专家, 在心理方面拥有广博的知识储备和丰富的研究咨询经验。你旨在通过专业心理咨询, 协助来访者完成心理诊断。请充分利用专业心理学知识与咨询技术, 一步步帮助来访者解决心理问题。\n\n" + "你是心理健康助手EmoLLM, 由EmoLLM团队打造。\n\n" ) total_prompt =f"<|start_header_id|>system<|end_header_id|>\n\n{meta_instruction}<|eot_id|>\n\n" for message in messages: diff --git a/web_demo-Llama3.py b/web_demo-Llama3.py index 391b0b4..6cf2eee 100644 --- a/web_demo-Llama3.py +++ b/web_demo-Llama3.py @@ -205,7 +205,7 @@ cur_query_prompt = '<|start_header_id|>user<|end_header_id|>\n\n{user}<|eot_id|> def combine_history(prompt): messages = st.session_state.messages meta_instruction = ( - "你是心理健康助手EmoLLM, 由EmoLLM团队打造, 是一个研究过无数具有心理健康问题的病人与心理健康医生对话的心理专家, 在心理方面拥有广博的知识储备和丰富的研究咨询经验。你旨在通过专业心理咨询, 协助来访者完成心理诊断。请充分利用专业心理学知识与咨询技术, 一步步帮助来访者解决心理问题。\n\n" + "你是心理健康助手EmoLLM, 由EmoLLM团队打造\n\n" ) total_prompt =f"<|start_header_id|>system<|end_header_id|>\n\n{meta_instruction}<|eot_id|>\n\n" for message in messages: From 03c0668abb97440b38a3a069dc7e90e3609d5c36 Mon Sep 17 00:00:00 2001 From: HongCheng Date: Sat, 11 May 2024 18:46:40 +0900 Subject: [PATCH 2/4] update daddy-like bf in readme --- README.md | 2 ++ README_EN.md | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 813d44e..fae331a 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,8 @@ ## 🎇最近更新 + +- 【2024.05.08】EmoLLM**爹系男友阅览体验版**上线 [1. **百度AppBuilder**](https://appbuilder.baidu.com/s/4cLyw) [2. **OpenXLab**](https://openxlab.org.cn/apps/detail/chg0901/EmoLLM3.0_Gradio_Llama3-8B-Instruct3.0), 欢迎点赞收藏 - 【2024.05.07】[增量预训练指南](xtuner_config/pt/README.md) - 【2024.05.04】基于LLaMA3_8b_instruct的[EmoLLM3.0 OpenXLab Demo](https://st-app-center-006861-9746-jlroxvg.openxlab.space/)上线([重启链接](https://openxlab.org.cn/apps/detail/chg0901/EmoLLM-Llama3-8B-Instruct3.0)), [**LLAMA3微调指南**](xtuner_config/README_llama3_8b_instruct_qlora_alpaca_e3_M.md)**更新**,在[**OpenXLab**](https://openxlab.org.cn/models/detail/chg0901/EmoLLM-Llama3-8B-Instruct3.0)和[**ModelScope**](https://modelscope.cn/models/chg0901/EmoLLM-Llama3-8B-Instruct3.0/summary)平台发布**LLaMA3_8b_instruct-8B QLoRA微调模型 EmoLLM3.0权重** - 【2024.04.20】[LLAMA3微调指南](xtuner_config/README_llama3_8b_instruct_qlora_alpaca_e3_M.md)及基于[LLaMA3_8b_instruct的艾薇](https://openxlab.org.cn/models/detail/ajupyter/EmoLLM-LLaMA3_8b_instruct_aiwei)开源 diff --git a/README_EN.md b/README_EN.md index 0954449..72bd8d9 100644 --- a/README_EN.md +++ b/README_EN.md @@ -101,7 +101,9 @@ The Model aims to fully understand and promote the mental health of individuals, ## Recent Updates -- [2024.05.07][Incremental Pre-training Guide](xtuner_config/pt/README.md) + +- [2024.05.08] EmoLLM**Daddy-like BF V0.1** is public now in [1. **Baidu AppBuilder**](https://appbuilder.baidu.com/s/4cLyw) and [2. **OpenXLab**](https://openxlab.org.cn/apps/detail/chg0901/EmoLLM3.0_Gradio_Llama3-8B-Instruct3.0), welcome to like and add it to your collections! +- [2024.05.07] [Incremental Pre-training Guide](xtuner_config/pt/README.md) - [2024.05.04] [EmoLLM3.0 OpenXLab Demo](https://st-app-center-006861-9746-jlroxvg.openxlab.space/) based on LLaMA3_8b_instruct is available now ([restart link]((https://openxlab.org.cn/apps/detail/chg0901/EmoLLM-Llama3-8B-Instruct3.0))), [LLAMA3 fine-tuning guide](xtuner_config/README_llama3_8b_instruct_qlora_alpaca_e3_M.md) is updated, LLaMA3_8b_instruct-8B QLoRA fine-tuning model EmoLLM3.0 weights are released on [**OpenXLab**](https://openxlab.org.cn/models/detail/chg0901/EmoLLM-Llama3-8B-Instruct3.0) and [**ModelScope**](https://modelscope.cn/models/chg0901/EmoLLM-Llama3-8B-Instruct3.0/summary) platforms - [2024.04.20] [LLAMA3 fine-tuning guide](xtuner_config/README_llama3_8b_instruct_qlora_alpaca_e3_M.md) and based on [LLaMA3_8b_instruct's aiwei](https://openxlab.org.cn/models/detail/ajupyter/EmoLLM-LLaMA3_8b_instruct_aiwei) open source - [2023.04.14] Added [Quick Start](docs/quick_start_EN.md) and Nanny level tutorial [BabyEmoLLM](Baby_EmoLLM.ipynb) From b38db7b323fe77cb36b23e2a332dfb3111a3d9db Mon Sep 17 00:00:00 2001 From: MING_X Date: Sat, 11 May 2024 23:42:38 +0800 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- README_EN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 813d44e..4edfd6a 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ git clone https://github.com/SmartFlowAI/EmoLLM.git - [数据构建](#数据构建) - [微调指南](#微调指南) - [部署指南](#部署指南) - - [RAG](#rag检索增强生成pipeline) + - [RAG](#rag检索增强生成) - 查看更多详情 diff --git a/README_EN.md b/README_EN.md index 0954449..76b1e2b 100644 --- a/README_EN.md +++ b/README_EN.md @@ -206,7 +206,7 @@ git clone https://github.com/SmartFlowAI/EmoLLM.git - [Data Construction](#data-construction) - [Fine-tuning Guide](#fine-tuning-guide) - [Deployment Guide](#deployment-guide) - - [RAG](#rag-retrieval-augmented-generation-pipeline) + - [RAG](#rag-retrieval-augmented-generation) - View More Details From 9f1aa3d2291b1f8b398e21a2bb39e9d72df62665 Mon Sep 17 00:00:00 2001 From: MING_X Date: Sat, 11 May 2024 23:54:23 +0800 Subject: [PATCH 4/4] Update README.md --- README.md | 6 ++++++ README_EN.md | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4edfd6a..a68897f 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,7 @@ - [🎨微调指南](#微调指南) - [🔧部署指南](#部署指南) - [⚙RAG(检索增强生成)](#rag检索增强生成) + - [🎓评测指南](#评测指南) - [使用到的框架](#使用到的框架) - [如何参与本项目](#如何参与本项目) - [作者(排名不分先后)](#作者排名不分先后) @@ -206,6 +207,7 @@ git clone https://github.com/SmartFlowAI/EmoLLM.git - [微调指南](#微调指南) - [部署指南](#部署指南) - [RAG](#rag检索增强生成) + - [评测指南](#评测指南) - 查看更多详情 @@ -234,6 +236,10 @@ git clone https://github.com/SmartFlowAI/EmoLLM.git - 详见[RAG](rag/README.md) +### 🎓评测指南 + +- 本模型评测分为通用评测和专业评测,请阅读[评测指南](evaluate/README.md)查阅 +
更多详情 diff --git a/README_EN.md b/README_EN.md index 76b1e2b..1343ccc 100644 --- a/README_EN.md +++ b/README_EN.md @@ -179,6 +179,7 @@ The Model aims to fully understand and promote the mental health of individuals, - [🎨Fine-tuning Guide](#fine-tuning-guide) - [🔧Deployment Guide](#deployment-guide) - [⚙RAG (Retrieval Augmented Generation)](#rag-retrieval-augmented-generation) + - [🎓Evaluation Guide](#evaluation-guide) - [Frameworks Used](#frameworks-used) - [How to participate in this project](#how-to-participate-in-this-project) - [Version control](#version-control) @@ -207,6 +208,7 @@ git clone https://github.com/SmartFlowAI/EmoLLM.git - [Fine-tuning Guide](#fine-tuning-guide) - [Deployment Guide](#deployment-guide) - [RAG](#rag-retrieval-augmented-generation) + - [Evaluation Guide](#evaluation-guide) - View More Details @@ -216,7 +218,7 @@ git clone https://github.com/SmartFlowAI/EmoLLM.git ### 📌Data Construction -- Please read the [Data Construction Guide ](generate_data/tutorial_EN.md)for reference. +- Please read the [Data Construction Guide ](generate_data/tutorial_EN.md) for reference. - The dataset used for this fine-tuning can be found at [datasets](datasets/data.json) @@ -233,6 +235,10 @@ For details, see the [fine-tuning guide](xtuner_config/README_EN.md) - See [RAG](rag/README_EN.md) +### 🎓Evaluation Guide + +- The model evaluation is divided into **General Metrics Evaluation** and **Professional Metrics Evaluation**,Please read the [evaluation guide](evaluate/README.md) for reference. +
Additional Details