{"id":3645,"date":"2024-11-15T08:49:46","date_gmt":"2024-11-15T16:49:46","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=3645"},"modified":"2024-11-15T09:18:32","modified_gmt":"2024-11-15T17:18:32","slug":"working-with-audio-in-semantic-kernel-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/working-with-audio-in-semantic-kernel-python\/","title":{"rendered":"Working with Audio in Semantic Kernel Python"},"content":{"rendered":"<p><span style=\"font-family: arial, helvetica, sans-serif;\">We are pleased to announce the arrival of audio support in Semantic Kernel Python. This new audio functionality will enable you to create more interactive and accessible user experiences. In this blog post, I will detail the new interface, the existing connectors, and provide samples. Please continue reading for more information.<\/span><\/p>\n<h3><span style=\"font-family: arial, helvetica, sans-serif;\">Audio-to-Text<\/span><\/h3>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">The first feature we are introducing is the ability to transcribe audio into text. The foundational class for this feature is\u00a0<strong><code>AudioToTextClientBase<\/code><\/strong>, which includes two public methods:\u00a0<strong><code>get_text_contents<\/code>\u00a0<\/strong>and\u00a0<strong><code>get_text_content<\/code><\/strong>. The former returns a list of possible transcriptions based on the number requested, while the latter returns a single transcription. The transcriptions are returned as <strong><code>TextContent<\/code> <\/strong>objects. Notably, <strong><code>get_text_content<\/code>\u00a0<\/strong>internally calls\u00a0<strong><code>get_text_contents<\/code>\u00a0<\/strong>and simply returns the first transcription from the list.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">As of the publication of this blog post, the available services include\u00a0<strong><code>OpenAIAudioToText<\/code>\u00a0<\/strong>and\u00a0<strong><code>AzureAudioToText<\/code><\/strong>, allowing you to utilize either your OpenAI endpoints or Azure deployments.<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\">AudioContent<\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">With the introduction of audio support, we are also introducing a new content type known as\u00a0<strong><code>AudioContent<\/code><\/strong>. Instances of the\u00a0<strong><code>AudioContent<\/code>\u00a0<\/strong>class should encapsulate either the binary data or the URI pointing to the location of the audio data. Additionally, this class offers a convenient method that allows you to create an\u00a0<strong><code>AudioContent<\/code> <\/strong>object directly from a file:<\/span><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">AudioContent.from_audio_file(path=PATH_TO_AUDIO_FILE)<\/code><\/pre>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\">Audio-to-Text Example Using Azure OpenAI<\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Please ensure that Semantic Kernel is updated to the latest version. To process audio input, the following components are required:<\/span><\/p>\n<ol>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">A speech-to-text model, such as whisper-1<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">An audio input device<\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">To begin, create the service with the following code:<\/span><\/p>\n<div>\n<div>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from semantic_kernel.connectors.ai.open_ai.services.azure_audio_to_text import AzureAudioToText\r\n\r\n\r\naudio_to_text_service = AzureAudioToText(api_key=\"...\", deployment_name=\"...\", endpoint=\"...\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Next, you will need to create the audio content that will be transcribed into text:<\/span><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from semantic_kernel.contents.audio_content import AudioContent\r\n\r\n\r\naudio_content = AudioContent.from_audio_file(path=\"...\")<\/code><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Finally, you can invoke the service to get the transcription:<\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">user_input = await audio_to_text_service.get_text_content(audio_content)\r\nprint(user_input)<\/code><\/pre>\n<p>To further create an interactive chat app that takes audio as input, please read this <a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/allow-users-to-talk-and-listen-to-your-chatbot-using-semantic-kernel-python\/\">blog post<\/a> or see the <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/python\/samples\/concepts\/audio\/01-chat_with_audio_input.py\">sample app<\/a> in our GitHub repository.<\/p>\n<h3><span style=\"font-family: arial, helvetica, sans-serif;\">Text-To-Audio<\/span><\/h3>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">The second feature we are introducing is the ability to create audio from text. The foundational class for this feature is <strong><code>TextToAudioClientBase<\/code><\/strong>, which includes two public methods:\u00a0<strong><code>get_audio_contents<\/code>\u00a0<\/strong>and\u00a0<strong><code>get_audio_content<\/code><\/strong>. Similar to <strong><code>AudioToTextCientBase<\/code>,t<\/strong>he former returns a list of possible audio generations based on the number requested, while the latter returns a single audio generation. The audio generations are returned as <strong><code>AudioContent<\/code> <\/strong>objects that contain the audio data. To listen to the audio data, <strong><code>AudioContent<\/code>\u00a0<\/strong>provide another convenient method to save the data to an audio file:<\/span><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">audio_content = ...\r\naudio_content.write_to_file(path=PATH_TO_FILE)<\/code><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">As of the publication of this blog post, the available services include\u00a0<strong><code>OpenAIAudioToText<\/code>\u00a0<\/strong>and\u00a0<strong><code>AzureAudioToText<\/code><\/strong>, allowing you to utilize either your OpenAI endpoints or Azure deployments.<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\">Text-to-Audio Example Using Azure OpenAI<\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Please ensure that Semantic Kernel is updated to the latest version. To process audio output, the following components are required:<\/span><\/p>\n<ol>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">A text-to-speech model, such as tts<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">An audio output device<\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">To begin, create the service with the following code:<\/span><\/p>\n<div>\n<div>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from semantic_kernel.connectors.ai.open_ai.services.azure_text_to_audio import AzureTextToAudio\r\n\r\n\r\ntext_to_audio_service = AzureTextToAudio(api_key=\"...\", deployment_name=\"...\", endpoint=\"...\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Next<\/span><span style=\"font-family: arial, helvetica, sans-serif;\">, you can invoke the service to get an audio generation:<\/span><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">audio_content = await text_to_audio_service.get_audio_content(\"Hello World!\")<\/code><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Finally, save the audio content so that you can listen to it with your favorite player:<\/span><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">audio_content.write_to_file(path=\"...\")<\/code><\/pre>\n<p>To further create an interactive chat app that output audio, please read more in this <a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/allow-users-to-talk-and-listen-to-your-chatbot-using-semantic-kernel-python\/\">blog post<\/a> or see the <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/python\/samples\/concepts\/audio\/02-chat_with_audio_output.py\">sample app<\/a> in our GitHub repository.<\/p>\n<h3 id=\"conclusion\">Conclusion<button class=\"linkicon\" aria-label=\"Copy Post URL\"><\/button><\/h3>\n<p>To learn more about Semantic Kernel\u00a0visit our\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/concepts\/ai-services\/\" target=\"_blank\" rel=\"noopener\">learn site<\/a>\u00a0as well as our\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\" target=\"_blank\" rel=\"noopener\">GitHub repository<\/a>. Please reach out if you have any questions or feedback through our\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/discussions\/categories\/general\" target=\"_blank\" rel=\"noopener\">Semantic Kernel GitHub Discussion Channel<\/a>. We look forward to hearing from you!\u00a0We would also love your support, if you\u2019ve enjoyed using Semantic Kernel, give us a star on\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\" target=\"_blank\" rel=\"noopener\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are pleased to announce the arrival of audio support in Semantic Kernel Python. This new audio functionality will enable you to create more interactive and accessible user experiences. In this blog post, I will detail the new interface, the existing connectors, and provide samples. Please continue reading for more information. Audio-to-Text The first feature [&hellip;]<\/p>\n","protected":false},"author":165150,"featured_media":2364,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,63,53,9],"class_list":["post-3645","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-semantic-kernel","tag-ai","tag-microsoft-semantic-kernel","tag-python","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>We are pleased to announce the arrival of audio support in Semantic Kernel Python. This new audio functionality will enable you to create more interactive and accessible user experiences. In this blog post, I will detail the new interface, the existing connectors, and provide samples. Please continue reading for more information. Audio-to-Text The first feature [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/3645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/165150"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=3645"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/3645\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/2364"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=3645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=3645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=3645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}