update daddy-like bf in readme with gradio web demo (#240)
This commit is contained in:
		
						commit
						e4c1e5e049
					
				| @ -98,6 +98,8 @@ | |||||||
| </table> | </table> | ||||||
| 
 | 
 | ||||||
| ## 🎇最近更新 | ## 🎇最近更新 | ||||||
|  | 
 | ||||||
|  | - 【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.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.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)开源 | - 【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)开源 | ||||||
|  | |||||||
| @ -101,7 +101,9 @@ The Model aims to fully understand and promote the mental health of individuals, | |||||||
| </table> | </table> | ||||||
| 
 | 
 | ||||||
| ## Recent Updates | ## 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.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 | - [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) | - [2023.04.14] Added [Quick Start](docs/quick_start_EN.md) and Nanny level tutorial [BabyEmoLLM](Baby_EmoLLM.ipynb) | ||||||
|  | |||||||
							
								
								
									
										177
									
								
								app_Llama3_Gradio.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										177
									
								
								app_Llama3_Gradio.py
									
									
									
									
									
										Normal file
									
								
							| @ -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 = ''' | ||||||
|  | <div> | ||||||
|  | <h1 style="text-align: center;">EmoLLM Llama3 心理咨询室 V4.0</h1> | ||||||
|  | 
 | ||||||
|  | <p align="center"> | ||||||
|  |   <a href="https://github.com/SmartFlowAI/EmoLLM/"> | ||||||
|  |     <img src="https://st-app-center-006861-9746-jlroxvg.openxlab.space/media/cda6c1a05dc8ba5b19ad3e7a24920fdf3750c917751202385a6dbc51.png" alt="Logo" width="20%"> | ||||||
|  |   </a> | ||||||
|  | </p> | ||||||
|  | 
 | ||||||
|  | <div align="center"> | ||||||
|  | 
 | ||||||
|  | <!-- PROJECT SHIELDS --> | ||||||
|  | [![OpenXLab_Model][OpenXLab_Model-image]][OpenXLab_Model-url]  | ||||||
|  | 
 | ||||||
|  | <h2 style="text-align: center;"> EmoLLM是一系列能够支持 理解用户-支持用户-帮助用户 心理健康辅导链路的 心理健康大模型 ,欢迎大家star~⭐⭐</h2> | ||||||
|  | <p>https://github.com/SmartFlowAI/EmoLLM</p> | ||||||
|  | </div> | ||||||
|  | 
 | ||||||
|  | </div> | ||||||
|  | 
 | ||||||
|  | [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 = """ | ||||||
|  | <p align="center"> Built with Meta Llama 3 </> | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | PLACEHOLDER = """ | ||||||
|  | <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;"> | ||||||
|  | 
 | ||||||
|  | </div> | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | css = """ | ||||||
|  | h1 { | ||||||
|  |   text-align: center; | ||||||
|  |   display: block; | ||||||
|  | } | ||||||
|  | <!-- | ||||||
|  | #duplicate-button { | ||||||
|  |   margin: auto; | ||||||
|  |   color: white; | ||||||
|  |   background: #1565c0; | ||||||
|  |   border-radius: 100vh; | ||||||
|  | } | ||||||
|  | --> | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | # 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() | ||||||
|  |      | ||||||
|  |      | ||||||
| @ -207,7 +207,7 @@ cur_query_prompt = '<|start_header_id|>user<|end_header_id|>\n\n{user}<|eot_id|> | |||||||
| def combine_history(prompt): | def combine_history(prompt): | ||||||
|     messages = st.session_state.messages |     messages = st.session_state.messages | ||||||
|     meta_instruction = ( |     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" |     total_prompt =f"<|start_header_id|>system<|end_header_id|>\n\n{meta_instruction}<|eot_id|>\n\n" | ||||||
|     for message in messages: |     for message in messages: | ||||||
|  | |||||||
| @ -205,7 +205,7 @@ cur_query_prompt = '<|start_header_id|>user<|end_header_id|>\n\n{user}<|eot_id|> | |||||||
| def combine_history(prompt): | def combine_history(prompt): | ||||||
|     messages = st.session_state.messages |     messages = st.session_state.messages | ||||||
|     meta_instruction = ( |     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" |     total_prompt =f"<|start_header_id|>system<|end_header_id|>\n\n{meta_instruction}<|eot_id|>\n\n" | ||||||
|     for message in messages: |     for message in messages: | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 HongCheng
						HongCheng