{"id":1517,"date":"2024-11-03T22:20:16","date_gmt":"2024-11-03T14:20:16","guid":{"rendered":"https:\/\/xujiwei.com\/blog\/?p=1517"},"modified":"2024-11-03T22:20:16","modified_gmt":"2024-11-03T14:20:16","slug":"diy-chatgpt-based-voice-assistant","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/","title":{"rendered":"\u505a\u4e00\u4e2a\u57fa\u4e8e ChatGPT \u7684\u8bed\u97f3\u52a9\u624b\u5427\uff5e"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\">\u6587\u7ae0\u76ee\u5f55<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69d75a5b2bb22\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69d75a5b2bb22\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E6%95%B4%E4%BD%93%E4%BB%8B%E7%BB%8D\" title=\"\u6574\u4f53\u4ecb\u7ecd\">\u6574\u4f53\u4ecb\u7ecd<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#snowboy\" title=\"snowboy\">snowboy<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%94%A4%E9%86%92%E8%AF%8D\" title=\"\u81ea\u5b9a\u4e49\u5524\u9192\u8bcd\">\u81ea\u5b9a\u4e49\u5524\u9192\u8bcd<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E4%BD%BF%E7%94%A8_fast-whisper_%E8%AF%AD%E9%9F%B3%E8%BD%AC%E6%96%87%E6%9C%AC\" title=\"\u4f7f\u7528 fast-whisper \u8bed\u97f3\u8f6c\u6587\u672c\">\u4f7f\u7528 fast-whisper \u8bed\u97f3\u8f6c\u6587\u672c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E8%B0%83%E7%94%A8_OpenAI_API\" title=\"\u8c03\u7528 OpenAI API\">\u8c03\u7528 OpenAI API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E6%96%87%E6%9C%AC%E8%BD%AC%E8%AF%AD%E9%9F%B3\" title=\"\u6587\u672c\u8f6c\u8bed\u97f3\">\u6587\u672c\u8f6c\u8bed\u97f3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E8%BF%9E%E6%8E%A5_snowboy%E3%80%81fast-whisper%E3%80%81OpenAI_%E5%92%8C_EmoitVoice\" title=\"\u8fde\u63a5 snowboy\u3001fast-whisper\u3001OpenAI \u548c EmoitVoice\">\u8fde\u63a5 snowboy\u3001fast-whisper\u3001OpenAI \u548c EmoitVoice<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E4%B8%80%E9%94%AE%E8%BF%90%E8%A1%8C%E8%84%9A%E6%9C%AC\" title=\"\u4e00\u952e\u8fd0\u884c\u811a\u672c\">\u4e00\u952e\u8fd0\u884c\u811a\u672c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98\" title=\"\u5df2\u77e5\u95ee\u9898\">\u5df2\u77e5\u95ee\u9898<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/#%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE\" title=\"\u5f00\u6e90\u9879\u76ee\">\u5f00\u6e90\u9879\u76ee<\/a><\/li><\/ul><\/nav><\/div>\n<p>\u867d\u7136\u6709\u70b9\u665a\u4e86\uff0c\u4f46\u662f\u8fd8\u662f\u5206\u4eab\u4e00\u4e0b\u4e4b\u524d\u6298\u817e\u7684\u672c\u5730\u8fd0\u884c\u4e00\u4e2a\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u8bed\u97f3\u52a9\u624b\u7684\u8fc7\u7a0b\uff5e<\/p>\n<p>\u505a\u8fd9\u4e2a\u6765\u6e90\u4e8e\u5728\u63a8\u4e0a\u770b\u5230\u6709\u4eba\u5206\u4eab\u4e86\u4f7f\u7528\u5927\u8bed\u8a00\u6a21\u578b\u642d\u5efa\u7684\u8bed\u97f3\u52a9\u624b\uff0c\u521a\u597d\u4e5f\u60f3\u8bd5\u8bd5\u5927\u8bed\u8a00\u6a21\u578b\u76f8\u5173\u7684\u5e93\u548c\u4ee3\u7801\u4ec0\u4e48\u7684\u3002<\/p>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2024\/11\/diy-chatgpt-based-voice-assistant.jpg\" alt=\"diy-chatgpt-based-voice-assistant\" \/><\/p>\n<p>\u7136\u540e\u6211\u53c2\u7167\u63a8\u4e3b\u7684\u4ecb\u7ecd\uff0c\u4e5f\u4f7f\u7528\u76f8\u5173\u7684\u6846\u67b6\u642d\u5efa\u4e86\u4e00\u4e2a\u672c\u5730\u7684\u8bed\u97f3\u52a9\u624b\uff0c\u4e0d\u8fc7\u56e0\u4e3a\u6ca1\u6709 GPU \u4ee5\u53ca\u6d4b\u8bd5\u7684\u65f6\u5019\u5c0f\u53c2\u6570\u91cf\u7684\u5927\u8bed\u8a00\u6a21\u578b\u4e5f\u4e0d\u662f\u5f88\u597d\u7528\uff0c\u56e0\u6b64 LLM \u8fd9\u5757\u662f\u4f7f\u7528\u7684 OpenAI \u7684 API\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E6%95%B4%E4%BD%93%E4%BB%8B%E7%BB%8D\"><\/span>\u6574\u4f53\u4ecb\u7ecd<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8fd9\u4e2a\u8bed\u97f3\u52a9\u624b\u4e3b\u8981\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6846\u67b6\u548c\u670d\u52a1\uff1a<\/p>\n<ul>\n<li>snowboy\uff1a\u7528\u4e8e\u58f0\u97f3\u68c0\u6d4b\u3001\u58f0\u97f3\u5f55\u5236\u7b49\u529f\u80fd\uff0c\u5e76\u4e14\u8fd8\u652f\u6301\u9759\u97f3\u68c0\u6d4b\uff08VAD\uff09<\/li>\n<li>faster-whisper\uff1a\u7528\u4e8e\u8bed\u97f3\u8f6c\u6587\u672c\uff0c\u8fd9\u4e2a\u662f\u4f7f\u7528\u4e86 OpenAI \u7684 whisper \u6a21\u578b\uff0c\u5e76\u4e14\u91cd\u65b0\u5b9e\u73b0\u7684\u5e93\uff0c\u901f\u5ea6\u6bd4\u5b98\u65b9\u7684\u5feb\u5f88\u591a<\/li>\n<li>SpeechRecognition\uff1a\u7528\u6765\u8fdb\u884c\u5f55\u97f3\uff0c\u5728\u4f7f\u7528 snowboy \u8bc6\u522b\u5230\u5524\u9192\u8bcd\u540e\uff0c\u5c31\u7528\u8fd9\u4e2a\u5e93\u6765\u5c06\u540e\u7eed\u7684\u5bf9\u8bdd\u8fdb\u884c\u5f55\u5236\uff0c\u4ea4\u7ed9 whisper \u8bed\u97f3\u8f6c\u6587\u672c<\/li>\n<li>EmotiVoice\uff1a\u6587\u672c\u8f6c\u8bed\u97f3\uff0c\u5c06\u7528\u6237\u7684\u5bf9\u8bdd\u5185\u5bb9\u901a\u8fc7 API \u8be2\u95ee GPT \u4e4b\u540e\uff0c\u518d\u5c06\u8fd4\u56de\u7684\u6587\u672c\u5185\u5bb9\u751f\u6210\u8bed\u97f3\u5e76\u64ad\u653e<\/li>\n<li>OpenAI\uff1a\u7528\u6765\u5206\u6790\u7528\u6237\u7684\u5bf9\u8bdd\u5e76\u7ed9\u51fa\u5bf9\u5e94\u7684\u56de\u7b54<\/li>\n<\/ul>\n<p>\u5176\u4e2d fast-whisper \u5982\u679c\u60f3\u8981\u901f\u5ea6\u5feb\u7684\u8bdd\uff0c\u6700\u597d\u8fd8\u662f\u8981\u6709 Nvdia \u7684 GPU\uff0c\u4f7f\u7528 CPU \u7684\u8bdd\u901f\u5ea6\u4f1a\u6162\u4e0d\u5c11\uff0c\u4e0d\u8fc7\u6211\u5728 M1 MacBook \u4e0a\u6d4b\u8bd5\u52c9\u5f3a\u80fd\u7528\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"snowboy\"><\/span>snowboy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u9879\u76ee\u4e3b\u9875\uff1a<a href=\"https:\/\/github.com\/Kitt-AI\/snowboy\">https:\/\/github.com\/Kitt-AI\/snowboy<\/a><\/p>\n<blockquote>\n<p>Snowboy is a customizable hotword detection engine for you to create your own hotword like &quot;OK Google&quot; or &quot;Alexa&quot;.<\/p>\n<\/blockquote>\n<p>snowboy \u662f\u4e00\u4e2a\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5524\u9192\u8bcd\u7684\u8bed\u97f3\u68c0\u6d4b\u5f15\u64ce\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4f8b\u5982\u50cf\u5c0f\u7c73\u97f3\u7bb1\u90a3\u6837\u901a\u8fc7\u201c\u5c0f\u7231\u540c\u5b66\u201d\u6765\u5524\u9192\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u8fd9\u4e2a\u5524\u9192\u8bcd\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u6bd4\u5982\u81ea\u5b9a\u4e49\u4e00\u4e2a\u201cJARVIS\u201d\u4ec0\u4e48\u7684\uff0c\u7387\u5148\u5b9e\u73b0\u94a2\u94c1\u4fa0\u7684\u667a\u80fd\u52a9\u624b \ud83d\ude03\u3002<\/p>\n<p>\u5b89\u88c5 snowboy \u6709\u4e00\u4e9b\u6b65\u9aa4\uff0c\u5c06\u9879\u76ee\u7684\u4ed3\u5e93\u514b\u9686\u4e0b\u6765\u4e4b\u540e\uff0c\u9700\u8981\u5148\u5b89\u88c5 portaudio\u3001pyaudio \u548c\u7f16\u8bd1 swig \u6a21\u5757\uff0c\u4ee5\u4e0b\u662f\u5728 macOS \u5e73\u53f0\u4e0a\u7684\u5b89\u88c5\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 brew \u5b89\u88c5 portaudio\uff1a <code>brew install portaudio<\/code><\/li>\n<li>\u4f7f\u7528 pip \u5b89\u88c5 pyaudio\uff1a <code>pip install pyaudio<\/code><\/li>\n<li>\u5728 <code>swig\/Python3<\/code> \u76ee\u5f55\u4e2d\u8fd0\u884c <code>make<\/code> \u5c31\u53ef\u4ee5\u76f4\u63a5\u5b8c\u6210\u7f16\u8bd1\u3002<\/li>\n<\/ol>\n<p>\u7136\u540e\u5728 <code>examples\/Python3<\/code> \u76ee\u5f55\u4e2d\u5df2\u7ecf\u6709\u4e0d\u5c11\u793a\u4f8b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff0c\u4f8b\u5982\u4f7f\u7528\u81ea\u5e26\u7684 snowboy \u5524\u9192\u8bcd\uff1a<\/p>\n<p><code>python demo.py resources\/models\/snowboy.umdl<\/code><\/p>\n<p>\u8fd9\u6837\u8dd1\u8d77\u6765\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5bf9\u7740\u9ea6\u514b\u98ce\u8bf4 snowboy \u6765\u8fdb\u5165\u540e\u7eed\u5f55\u97f3\u6d41\u7a0b\u4e86\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%87%AA%E5%AE%9A%E4%B9%89%E5%94%A4%E9%86%92%E8%AF%8D\"><\/span>\u81ea\u5b9a\u4e49\u5524\u9192\u8bcd<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Snowboy Personal Wake Word Recorder\uff1a <a href=\"https:\/\/snowboy.hahack.com\/\">https:\/\/snowboy.hahack.com\/<\/a><\/p>\n<p>\u5df2\u7ecf\u6709\u70ed\u5fc3\u7f51\u53cb\u76f4\u63a5\u505a\u4e86\u4e00\u4e2a\u7f51\u9875\u6765\u751f\u6210 snowboy \u5524\u9192\u8bcd\u7684\u6a21\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0a\u9762\u7684\u7f51\u5740\u8bbf\u95ee\uff0c\u5f55\u5236\u4e09\u4e2a\u97f3\u9891\u540e\uff0c\u5c31\u53ef\u4ee5\u751f\u6210 snowboy \u6240\u9700\u8981\u7684 umdl \u6a21\u578b\u4e86\uff0c\u8bd5\u8bd5\u505a\u4e00\u4e2a JARVIS \u5427\uff5e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8_fast-whisper_%E8%AF%AD%E9%9F%B3%E8%BD%AC%E6%96%87%E6%9C%AC\"><\/span>\u4f7f\u7528 fast-whisper \u8bed\u97f3\u8f6c\u6587\u672c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u9879\u76ee\u4e3b\u9875\uff1a <a href=\"https:\/\/github.com\/SYSTRAN\/faster-whisper\">https:\/\/github.com\/SYSTRAN\/faster-whisper<\/a><\/p>\n<blockquote>\n<p>faster-whisper is a reimplementation of OpenAI's Whisper model using CTranslate2, which is a fast inference engine for Transformer models.<\/p>\n<p>This implementation is up to 4 times faster than openai\/whisper for the same accuracy while using less memory. The efficiency can be further improved with 8-bit quantization on both CPU and GPU.<\/p>\n<\/blockquote>\n<p>\u5728\u4f7f\u7528 snowboy \u8bc6\u522b\u5230\u5524\u9192\u8bcd\u5e76\u5b8c\u6210\u5f55\u97f3\u540e\uff0c\u5c31\u9700\u8981\u5c06\u7528\u6237\u7684\u8bed\u97f3\u8f6c\u6362\u4e3a\u6587\u672c\uff0c\u518d\u53d1\u7ed9 ChatGPT API \u8fdb\u884c\u95ee\u7b54\u4e86\u3002<\/p>\n<p>\u5b89\u88c5 fast-whipser \u76f4\u63a5\u4f7f\u7528 pip \u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre><code class=\"language-bash\">pip install faster-whisper<\/code><\/pre>\n<p>\u8981\u5c06\u8bed\u97f3\u8f6c\u6362\u4e3a\u6587\u672c\uff0c\u76f4\u63a5\u4f7f\u7528\u4ed3\u5e93\u4e2d\u7684\u793a\u4f8b\u4ee3\u7801\u5c31\u53ef\u4ee5\uff1a<\/p>\n<pre><code class=\"language-python\">from faster_whisper import WhisperModel\n\nmodel_size = &quot;small&quot;\n\n# Run on GPU with FP16\n# model = WhisperModel(model_size, device=&quot;cuda&quot;, compute_type=&quot;float16&quot;)\n\n# or run on GPU with INT8\n# model = WhisperModel(model_size, device=&quot;cuda&quot;, compute_type=&quot;int8_float16&quot;)\n# or run on CPU with INT8\nmodel = WhisperModel(model_size, device=&quot;cpu&quot;, compute_type=&quot;int8&quot;)\n\nsegments, info = model.transcribe(&quot;audio.mp3&quot;, beam_size=5)\n\nprint(&quot;Detected language &#039;%s&#039; with probability %f&quot; % (info.language, info.language_probability))\n\nfor segment in segments:\n    print(&quot;[%.2fs -&gt; %.2fs] %s&quot; % (segment.start, segment.end, segment.text))<\/code><\/pre>\n<p>\u8fd9\u91cc\u662f\u4f7f\u7528\u4e86 CPU + int8 \u6765\u8fd0\u884c\u8fd9\u4e2a\u6a21\u578b\uff0c\u5982\u679c\u6709 Nvdia \u663e\u5361\u7684\u8bdd\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 cuda \u52a0\u901f\u3002<\/p>\n<p>\u5f97\u5230\u6587\u672c\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5c06\u6587\u672c\u53d1\u9001\u7ed9 ChatGPT API \u6765\u5f97\u5230\u7528\u6237\u95ee\u9898\u7684\u7b54\u6848\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%B0%83%E7%94%A8_OpenAI_API\"><\/span>\u8c03\u7528 OpenAI API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8fd9\u91cc\u5c31\u662f\u7b80\u5355\u5730\u5c06\u7528\u6237\u7684\u95ee\u9898\u53d1\u9001\u7ed9 Open API\uff0c\u62ff\u5230\u54cd\u5e94\u540e\u76f4\u63a5\u8f93\u51fa\u7ed9\u7528\u6237\uff0c\u4ee3\u7801\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<pre><code class=\"language-python\">from openai import OpenAI\nclient = OpenAI()\n\ndef openai_api(message):\n    completion = client.chat.completions.create(\n    model=&quot;gpt-3.5-turbo&quot;,\n    messages=[\n        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a voice assistant; please use concise language to answer questions.&quot;},\n        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: message}\n    ]\n    )\n\n    resp_msg = completion.choices[0].message.content\n    print(resp_msg)\n    return resp_msg\n<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E6%96%87%E6%9C%AC%E8%BD%AC%E8%AF%AD%E9%9F%B3\"><\/span>\u6587\u672c\u8f6c\u8bed\u97f3<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u62ff\u5230 ChatGPT \u8fd4\u56de\u7684\u6587\u672c\u4e4b\u540e\uff0c\u8fd8\u9700\u8981\u8f6c\u6362\u4e3a\u8bed\u97f3\u64ad\u653e\u51fa\u6765\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u7f51\u6613\u5f00\u6e90\u7684 EmoitVoice \u5e93\u3002<\/p>\n<p>\u9879\u76ee\u5730\u5740\uff1a <a href=\"https:\/\/github.com\/netease-youdao\/EmotiVoice\">https:\/\/github.com\/netease-youdao\/EmotiVoice<\/a><\/p>\n<p>\u6839\u636e\u5b98\u65b9\u6559\u7a0b\u5b89\u88c5\u5b8c EmoitVoice \u4e4b\u540e\uff0c\u4e3a\u4e86\u8ba9\u5b83\u53d8\u6210\u4e00\u4e2a API Server \u8dd1\u8d77\u6765\uff0c\u518d\u5b89\u88c5\u4e00\u4e9b\u5176\u4ed6\u7684\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u5b89\u88c5 ffmpeg \u7528\u6765\u8f6c mp3\nbrew install ffmpeg\n# \u5b89\u88c5\u4f9d\u8d56\npip install fastapi pydub uvicorn\n# \u8fd0\u884c API\npython -m uvicorn openaiapi:app --reload<\/code><\/pre>\n<p>\u7136\u540e\u5c01\u88c5\u4e00\u4e2a API \u8bf7\u6c42\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\nimport subprocess\n\n# \u8bbe\u7f6eAPI\u7aef\u70b9\napi_endpoint = &quot;http:\/\/127.0.0.1:8000\/v1\/audio\/speech&quot;\n\n# \u8bbe\u7f6e\u8bf7\u6c42\u6807\u5934\nheaders = {\n    &quot;Content-Type&quot;: &quot;application\/json&quot;\n}\n\n# \u8981\u8f6c\u6362\u7684\u6587\u672c\u5185\u5bb9\ntext_to_convert = &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;\n\ndef emotivoice_api(text_to_convert):\n    print(&quot;call emotivoice api&quot;)\n    # \u6784\u5efaJSON\u8bf7\u6c42\u4f53\n    request_body = {\n        &quot;input&quot;: text_to_convert,\n        &quot;voice&quot;: &quot;8051&quot;,\n        &quot;prompt&quot;: &quot;&quot;, # \u5f00\u5fc3 \/ \u60b2\u4f24\n        &quot;model&quot;: &quot;emoti-voice&quot;,\n        &quot;response_format&quot;: &quot;mp3&quot;,\n        &quot;language&quot;: &quot;zh_us&quot;\n    }\n\n    # \u53d1\u9001POST\u8bf7\u6c42\n    response = requests.post(api_endpoint, json=request_body, headers=headers)\n\n    # \u89e3\u6790API\u54cd\u5e94\n    if response.status_code == 200:\n        # \u4ece\u54cd\u5e94\u4e2d\u83b7\u53d6\u8bed\u97f3\u6587\u4ef6\n        audio_data = response.content\n        # \u5904\u7406\u8bed\u97f3\u6570\u636e\uff0c\u4f8b\u5982\u4fdd\u5b58\u4e3a\u6587\u4ef6\u6216\u8fdb\u884c\u5176\u4ed6\u5904\u7406\n        # \u5c06\u8bed\u97f3\u6570\u636e\u4fdd\u5b58\u4e3a\u672c\u5730\u6587\u4ef6\n        with open(&quot;output_audio.mp3&quot;, &quot;wb&quot;) as audio_file:\n            audio_file.write(audio_data)\n        print(&quot;\u8bed\u97f3\u6587\u4ef6\u5df2\u4fdd\u5b58\u4e3aoutput_audio.mp3&quot;)\n        subprocess.run([&#039;afplay&#039;, &#039;output_audio.mp3&#039;])\n    else:\n        print(&quot;API\u8bf7\u6c42\u5931\u8d25\uff1a&quot;, response.status_code, response.text)<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E8%BF%9E%E6%8E%A5_snowboy%E3%80%81fast-whisper%E3%80%81OpenAI_%E5%92%8C_EmoitVoice\"><\/span>\u8fde\u63a5 snowboy\u3001fast-whisper\u3001OpenAI \u548c EmoitVoice<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u914d\u7f6e\u5b8c\u6bcf\u4e2a\u7ec4\u4ef6\u4e4b\u540e\uff0c\u5c31\u662f\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\uff0c\u6838\u5fc3\u8fd8\u662f\u4f7f\u7528\u7684 snowboy \u793a\u4f8b\u4ee3\u7801\uff0c\u6700\u91cd\u8981\u7684\u662f\u5176\u4e2d\u7684 <code>audioRecorderCallback<\/code>\uff0c\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u5b9e\u73b0\u4e86\u6574\u4e2a\u8bed\u97f3\u5bf9\u8bdd\u6d41\u7a0b\uff1a<\/p>\n<pre><code class=\"language-python\">def audioRecorderCallback(fname):\n    print(&quot;converting audio to text&quot;)\n    r = sr.Recognizer()\n    with sr.AudioFile(fname) as source:\n        audio = r.record(source)  # read the entire audio file\n    # recognize speech using Whisper\n\n    start_time = time.time()\n    segments, info = whisper_model.transcribe(fname, beam_size=5)\n\n    print(&quot;Detected language &#039;%s&#039; with probability %f&quot; % (info.language, info.language_probability))\n\n    fulltext = &quot;&quot;\n\n    for segment in segments:\n        fulltext += segment.text\n        fulltext += &quot;\\n&quot;\n        print(&quot;[%.2fs -&gt; %.2fs] %s&quot; % (segment.start, segment.end, segment.text))\n    print(&quot;time cost&quot;, time.time() - start_time)\n\n    ai_msg = openai_api(fulltext)\n\n    emotivoice_api(ai_msg)\n\n    os.remove(fname)<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5927\u8bed\u8a00\u6a21\u578b\u8bed\u97f3\u5bf9\u8bdd\u52a9\u624b\uff5e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%80%E9%94%AE%E8%BF%90%E8%A1%8C%E8%84%9A%E6%9C%AC\"><\/span>\u4e00\u952e\u8fd0\u884c\u811a\u672c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e3a\u4e86\u80fd\u5feb\u901f\u5730\u628a\u8fd9\u4e9b\u7ec4\u4ef6\u90fd\u8fd0\u884c\u8d77\u6765\uff0c\u6211\u628a\u4e0a\u9762\u8fd9\u4e9b\u5b89\u88c5\u6b65\u9aa4\u90fd\u5199\u6210\u4e86\u4e00\u4e2a\u4e00\u952e\u8fd0\u884c\u811a\u672c\uff0c\u53ea\u8981\u73af\u5883\u53d8\u91cf\u4e2d\u6709 <code>python3.10<\/code> \u5c31\u53ef\u4ee5\u5feb\u901f\u8dd1\u8d77\u6765\uff0c\u8fd9\u4e2a\u811a\u672c\u4e3b\u8981\u4f1a\u5b8c\u6210\u4ee5\u4e0b\u5de5\u4f5c\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 brew \u5b89\u88c5\u76f8\u5173\u4f9d\u8d56<\/li>\n<li>\u4f7f\u7528 pip \u5b89\u88c5\u6240\u6709 python \u4f9d\u8d56\u5305<\/li>\n<li>\u7f16\u8bd1 snowboy Python wrapper<\/li>\n<li>\u5b89\u88c5\u548c\u914d\u7f6e EmoitVoice<\/li>\n<\/ul>\n<p>\u5177\u4f53\u811a\u672c\u5185\u5bb9\u53ef\u4ee5\u5f00\u6e90\u9879\u76ee\u4ed3\u5e93\u7684\u6587\u4ef6\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98\"><\/span>\u5df2\u77e5\u95ee\u9898<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>snowboy \u9879\u76ee\u53ea\u5728 macOS \u5e73\u53f0\u53ea\u63d0\u4f9b\u4e86 x86_64 \u67b6\u6784\u7684\u9759\u6001\u5e93\uff0c\u56e0\u6b64\u5728 M \u7cfb\u5217 CPU \u7684 Mac \u4e0a\u65e0\u6cd5\u7f16\u8bd1\uff0c\u9700\u8981\u4f7f\u7528 Rosetta \u65b9\u5f0f\u8fd0\u884c\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE\"><\/span>\u5f00\u6e90\u9879\u76ee<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6211\u628a\u8fd0\u884c\u8fd9\u4e2a\u52a9\u624b\u6240\u6709\u76f8\u5173\u7684\u4ee3\u7801\u90fd\u6574\u7406\u597d\u653e\u5728 GitHub \u4ed3\u5e93\u4e2d\uff0c\u5982\u4f55\u5feb\u901f\u90e8\u7f72\u53ef\u4ee5\u770b\u9879\u76ee\u4e2d\u7684 README\u3002<\/p>\n<p>\u9879\u76ee\u5730\u5740\uff1a <a href=\"https:\/\/github.com\/ohdarling\/GPTVoiceChat\">https:\/\/github.com\/ohdarling\/GPTVoiceChat<\/a><\/p>\n<p>\u53e6\u5916\u9879\u76ee\u662f\u5728 macOS 14 + Python 3.10 \u73af\u5883\u4e0b\u6d4b\u8bd5\u7684\uff0c\u5982\u679c\u73af\u5883\u4e0d\u592a\u76f8\u540c\uff0c\u5728\u5b89\u88c5\u548c\u8fd0\u884c\u65f6\u53ef\u80fd\u4f1a\u78b0\u5230\u95ee\u9898\uff0c\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u81ea\u5df1\u89e3\u51b3\u4e00\u4e0b\u5566\uff5e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u867d\u7136\u6709\u70b9\u665a\u4e86\uff0c\u4f46\u662f\u8fd8\u662f\u5206\u4eab\u4e00\u4e0b\u4e4b\u524d\u6298\u817e\u7684\u672c\u5730\u8fd0\u884c\u4e00\u4e2a\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u8bed\u97f3\u52a9\u624b &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2024\/11\/diy-chatgpt-based-voice-assistant\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":1518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[430,432,435,431,433,434],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1517"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=1517"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media\/1518"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}