{"id":19469,"date":"2025-01-27T10:48:44","date_gmt":"2025-01-27T02:48:44","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=19469"},"modified":"2025-07-21T03:21:33","modified_gmt":"2025-07-20T19:21:33","slug":"llasa","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/de\/llasa\/","title":{"rendered":"Llasa 1~8B\uff1a\u9ad8\u54c1\u8d28\u8bed\u97f3\u751f\u6210\u548c\u514b\u9686\u7684\u5f00\u6e90\u6587\u672c\u8f6c\u8bed\u97f3\u6a21\u578b"},"content":{"rendered":"<p>Llasa-3B\u662f\u7531\u9999\u6e2f\u79d1\u6280\u5927\u5b66\u97f3\u9891\u5b9e\u9a8c\u5ba4\uff08HKUST Audio\uff09\u5f00\u53d1\u7684\u4e00\u4e2a\u5f00\u6e90\u6587\u672c\u8f6c\u8bed\u97f3\uff08TTS\uff09\u6a21\u578b\u3002\u8be5\u6a21\u578b\u57fa\u4e8eLlama 3.2B\u67b6\u6784\uff0c\u7ecf\u8fc7\u7ec6\u81f4\u8c03\u4f18\uff0c\u63d0\u4f9b\u9ad8\u8d28\u91cf\u7684\u8bed\u97f3\u751f\u6210\uff0c\u4e0d\u4ec5\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u8fd8\u80fd\u5b9e\u73b0\u60c5\u611f\u8868\u8fbe\u548c\u4e2a\u6027\u5316\u8bed\u97f3\u514b\u9686\u3002Llasa-3B\u4ee5\u5176\u5728\u81ea\u7136\u8bed\u97f3\u5408\u6210\u65b9\u9762\u7684\u8868\u73b0\u529b\u548c\u7075\u6d3b\u6027\uff0c\u5438\u5f15\u4e86\u4f17\u591a\u7814\u7a76\u4eba\u5458\u548c\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u3002<\/p>\n<div id=\"attachment_19470\" style=\"width: 2688px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-19470\" class=\"wp-image-19470 size-full\" title=\"Llasa 1~8B\uff1a\u5b9e\u73b0\u9ad8\u8d28\u91cf\u8bed\u97f3\u751f\u6210\u548c\u514b\u9686\u7684\u5f00\u6e90\u6587\u672c\u8f6c\u8bed\u97f3\u6a21\u578b-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f.png\" alt=\"Llasa 1~8B\uff1a\u5b9e\u73b0\u9ad8\u8d28\u91cf\u8bed\u97f3\u751f\u6210\u548c\u514b\u9686\u7684\u5f00\u6e90\u6587\u672c\u8f6c\u8bed\u97f3\u6a21\u578b-1\" width=\"2678\" height=\"1314\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f.png 2678w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f-300x147.png 300w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f-1024x502.png 1024w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f-768x377.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f-1536x754.png 1536w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f-2048x1005.png 2048w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/01\/6e663c664cea81f-18x9.png 18w\" sizes=\"auto, (max-width: 2678px) 100vw, 2678px\" \/><p id=\"caption-attachment-19470\" class=\"wp-caption-text\">\u4f53\u9a8c\u5730\u5740\uff1ahttps:\/\/huggingface.co\/spaces\/srinivasbilla\/llasa-3b-tts<\/p><\/div>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li><strong>\u6587\u672c\u8f6c\u8bed\u97f3<\/strong>\uff1a\u5c06\u6587\u5b57\u8f6c\u6362\u6210\u81ea\u7136\u6d41\u7545\u7684\u58f0\u97f3\u3002<\/li>\n<li><strong>\u8bed\u97f3\u514b\u9686<\/strong>\uff1a\u4ec5\u970015\u79d2\u7684\u97f3\u9891\u7247\u6bb5\u5373\u53ef\u514b\u9686\u7279\u5b9a\u4eba\u58f0\uff0c\u5305\u62ec\u97f3\u8272\u548c\u60c5\u611f\u3002<\/li>\n<li><strong>\u591a\u8bed\u8a00\u652f\u6301<\/strong>\uff1a\u652f\u6301\u4e2d\u6587\u548c\u82f1\u8bed\uff0c\u65e8\u5728\u6269\u5c55\u5230\u66f4\u591a\u8bed\u8a00\u3002<\/li>\n<li><strong>\u60c5\u611f\u8868\u8fbe<\/strong>\uff1a\u80fd\u591f\u5728\u751f\u6210\u7684\u8bed\u97f3\u4e2d\u6ce8\u5165\u60c5\u611f\uff0c\u63d0\u5347\u8bed\u97f3\u7684\u771f\u5b9e\u6027\u3002<\/li>\n<li><strong>\u591a\u6a21\u578b\u652f\u6301<\/strong>\uff1a\u63d0\u4f9b 1B \u548c 3B \u53c2\u6570\u89c4\u6a21\u6a21\u578b\uff0c\u672a\u6765\u8fd8\u5c06\u63a8\u51fa 8B \u6a21\u578b<\/li>\n<li><strong>\u5f00\u653e\u6743\u91cd<\/strong>\uff1a\u6240\u6709\u6a21\u578b\u5747\u63d0\u4f9b\u5f00\u653e\u6743\u91cd\uff0c\u5f00\u53d1\u8005\u53ef\u76f4\u63a5\u4f7f\u7528\u6216\u4e8c\u6b21\u5fae\u8c03\uff0c\u652f\u6301 Transformers \u548c <a href=\"https:\/\/www.kdjingpai.com\/de\/vllm\/\">vLLM<\/a> \u6846\u67b6\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>\u5b89\u88c5\u4e0e\u73af\u5883\u51c6\u5907<\/h3>\n<p>\u8981\u4f7f\u7528Llasa-3B\u6a21\u578b\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u4ee5\u4e0b\u73af\u5883\uff1a<\/p>\n<p><strong>Python\u73af\u5883<\/strong>\uff1a\u5efa\u8bae\u4f7f\u7528Python 3.9\u6216\u4ee5\u4e0a\u7248\u672c\u3002<br \/>\n<strong>\u76f8\u5173\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5<code>torch<\/code>,\u00a0<code>transformers<\/code>,\u00a0<code>xcodec2<\/code>\u7b49\u5e93\u3002<\/p>\n<pre><code>conda create -n xcodec2 python=3.9\r\nconda activate xcodec2\r\npip install transformers torch xcodec2==0.1.3\r\n<\/code><\/pre>\n<p><strong>\u6a21\u578b\u4e0b\u8f7d\u4e0e\u52a0\u8f7d<\/strong><\/p>\n<p>\u8bbf\u95eeHugging Face\u4e0a\u7684<a href=\"https:\/\/huggingface.co\/HKUSTAudio\/Llasa-3B\">Llasa-3B\u9875\u9762<\/a>\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Hugging Face\u7684\u6a21\u578b\u4e0b\u8f7d\u529f\u80fd\uff1a<\/p>\n<pre><code>from transformers import AutoTokenizer, AutoModelForCausalLM\r\nimport torch\r\nimport soundfile as sf\r\nllasa_3b = 'HKUST-Audio\/Llasa-3B'\r\ntokenizer = AutoTokenizer.from_pretrained(llasa_3b)\r\nmodel = AutoModelForCausalLM.from_pretrained(llasa_3b)\r\nmodel.eval()\r\nmodel.to('cuda')  # \u5982\u679c\u6709GPU\r\n<\/code><\/pre>\n<p><strong>\u6587\u672c\u8f6c\u8bed\u97f3\u8fc7\u7a0b<\/strong><\/p>\n<ol>\n<li><strong>\u51c6\u5907\u6587\u672c<\/strong>\uff1a\n<ul>\n<li>\u8f93\u5165\u4f60\u5e0c\u671b\u8f6c\u6362\u6210\u8bed\u97f3\u7684\u6587\u672c\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u6587\u672c\u9884\u5904\u7406<\/strong>\uff1a\n<ul>\n<li>\u4f7f\u7528\u7279\u5b9a\u7684\u683c\u5f0f\u5316\u6587\u672c\u6765\u6307\u5bfc\u6a21\u578b\u8fdb\u884c\u8bed\u97f3\u751f\u6210\uff0c\u5982\uff1a\n<pre><code>input_text = \"\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u6587\u672c\uff0c\u8bf7\u8f6c\u6210\u8bed\u97f3\u3002\"\r\nformatted_text = f\"&lt;|TEXT_UNDERSTANDING_START|&gt;{input_text}&lt;|TEXT_UNDERSTANDING_END|&gt;\"\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u751f\u6210\u8bed\u97f3<\/strong>\uff1a\n<ul>\n<li>\u5c06\u6587\u672c\u8f6c\u6362\u6210\u6a21\u578b\u53ef\u4ee5\u7406\u89e3\u7684token\uff1a\n<pre><code>chat = [\r\n{\"role\": \"user\", \"content\": \"Convert the text to speech:\" + formatted_text},\r\n{\"role\": \"assistant\", \"content\": \"&lt;|SPEECH_GENERATION_START|&gt;\"}\r\n]\r\ninput_ids = tokenizer.apply_chat_template(chat, tokenize=True, return_tensors='pt', continue_final_message=True)\r\ninput_ids = input_ids.to('cuda')\r\n<\/code><\/pre>\n<\/li>\n<li>\u751f\u6210\u8bed\u97f3token\uff1a\n<pre><code>speech_end_id = tokenizer.convert_tokens_to_ids('&lt;|SPEECH_GENERATION_END|&gt;')\r\noutputs = model.generate(input_ids, max_length=2048, eos_token_id=speech_end_id, do_sample=True, top_p=1, temperature=0.8)\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u8bed\u97f3\u89e3\u7801<\/strong>\uff1a\n<ul>\n<li>\u5c06\u751f\u6210\u7684token\u8f6c\u6362\u56de\u97f3\u9891\uff1a\n<pre><code>from xcodec2.modeling_xcodec2 import XCodec2Model\r\nmodel_path = \"HKUST-Audio\/xcodec2\"\r\nCodec_model = XCodec2Model.from_pretrained(model_path).eval().cuda()\r\ngenerated_ids = outputs[0][input_ids.shape[1]:-1]\r\nspeech_tokens = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)\r\nspeech_ids = [int(token[4:-2]) for <a href=\"https:\/\/www.kdjingpai.com\/de\/tokenization\/\">token<\/a> in speech_tokens if token.startswith('&lt;|s_') and token.endswith('|&gt;')]\r\nspeech_tokens_tensor = torch.tensor(speech_ids).cuda().unsqueeze(0).unsqueeze(0)\r\ngen_wav = Codec_model.decode_code(speech_tokens_tensor)\r\nsf.write(\"output.wav\", gen_wav[0, 0, :].cpu().numpy(), 16000)\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u8bed\u97f3\u514b\u9686<\/strong><\/p>\n<ul>\n<li><strong>\u5f55\u5236\u6216\u51c6\u590715\u79d2\u5de6\u53f3\u7684\u539f\u58f0\u97f3\u9891<\/strong>\uff1a\n<ul>\n<li>\u4f7f\u7528\u5f55\u97f3\u8bbe\u5907\u6216\u63d0\u4f9b\u73b0\u6709\u7684\u97f3\u9891\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u8bed\u97f3\u514b\u9686\u8fc7\u7a0b<\/strong>\uff1a\n<ul>\n<li>\u5c06\u539f\u58f0\u97f3\u9891\u7f16\u7801\u6210\u6a21\u578b\u53ef\u4ee5\u4f7f\u7528\u7684\u4ee3\u7801\u4e66\uff1a\n<pre><code>prompt_wav = sf.read(\"your_source_audio.wav\")[0]  # \u5fc5\u987b\u662f16kHz\u91c7\u6837\u7387\r\nvq_code_prompt = Codec_model.encode_code(torch.from_numpy(prompt_wav).unsqueeze(0).unsqueeze(0).cuda())\r\n<\/code><\/pre>\n<\/li>\n<li>\u5728\u6587\u672c\u751f\u6210\u8fc7\u7a0b\u4e2d\u52a0\u5165\u97f3\u9891\u63d0\u793a\uff1a\n<pre><code>speech_ids_prefix = [f\"&lt;|s_{id}|&gt;\"foridin vq_code_prompt[0, 0, :].tolist()]\r\nchat = [\r\n{\"role\": \"user\", \"content\": \"Convert the text to speech:\" + formatted_text},\r\n{\"role\": \"assistant\", \"content\": \"&lt;|SPEECH_GENERATION_START|&gt;\" + ''.join(speech_ids_prefix)}\r\n]\r\n# \u540e\u7eed\u6b65\u9aa4\u4e0e\u6587\u672c\u8f6c\u8bed\u97f3\u76f8\u540c\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u97f3\u9891\u8f93\u5165\u683c\u5f0f\u6b63\u786e\uff0cLlasa-3B\u4ec5\u652f\u630116kHz\u7684\u97f3\u9891\u3002<\/li>\n<li>\u6a21\u578b\u7684\u8868\u73b0\u53d7\u8f93\u5165\u6587\u672c\u548c\u97f3\u9891\u8d28\u91cf\u7684\u76f4\u63a5\u5f71\u54cd\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u8d28\u91cf\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Llasa-3B\u662f\u7531\u9999\u6e2f\u79d1\u6280\u5927\u5b66\u97f3\u9891\u5b9e\u9a8c\u5ba4\uff08HKUST Audio\uff09\u5f00\u53d1\u7684\u4e00\u4e2a\u5f00\u6e90\u6587\u672c\u8f6c\u8bed\u97f3\uff08TTS\uff09\u6a21\u578b\u3002\u8be5\u6a21\u578b\u57fa\u4e8eLlama 3.2B\u67b6\u6784\uff0c\u7ecf\u8fc7\u7ec6\u81f4\u8c03\u4f18\uff0c\u63d0\u4f9b\u9ad8\u8d28\u91cf\u7684\u8bed\u97f3\u751f\u6210\uff0c\u4e0d\u4ec5\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u8fd8\u80fd\u5b9e\u73b0\u60c5\u611f\u8868\u8fbe\u548c\u4e2a\u6027\u5316\u8bed\u97f3\u514b\u9686\u3002Llas&#8230;<\/p>\n","protected":false},"author":1,"featured_media":31260,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,392,400],"tags":[230,215,237],"class_list":["post-19469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tool","category-models","category-speech-model","tag-aikaiyuanxiangmu","tag-aiwenbenzhuanyuyin","tag-aiyuyinkelong"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/19469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/comments?post=19469"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/19469\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media\/31260"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media?parent=19469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/categories?post=19469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/tags?post=19469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}