{"id":463939,"date":"2024-09-28T03:26:27","date_gmt":"2024-09-28T03:26:27","guid":{"rendered":"https:\/\/Blockchain.News\/news\/integrating-speech-to-text-functionality-in-django-applications"},"modified":"2024-09-28T03:26:27","modified_gmt":"2024-09-28T03:26:27","slug":"integrating-speech-to-text-functionality-in-django-applications","status":"publish","type":"post","link":"https:\/\/e-bitco.in\/index.php\/2024\/09\/28\/integrating-speech-to-text-functionality-in-django-applications\/","title":{"rendered":"Integrating Speech-to-Text Functionality in Django Applications"},"content":{"rendered":"<figure class=\"figure mt-2\">\n<p> <a href=\"https:\/\/blockchain.news\/Profile\/Lawrence-Jengar\">Lawrence Jengar<\/a> <span class=\"publication-date ml-2\"> Sep 28, 2024 03:26<\/span> <\/p>\n<p class=\"lead\">Learn how to integrate Speech-to-Text into Django apps using AssemblyAI API. Build an app to transcribe audio files and display the transcriptions.<\/p>\n<p> <a href=\"https:\/\/image.blockchain.news:443\/features\/8A6D364E10667B70266C559AAAD3793038EA7B225A572DDB5616E316563F53D8.jpg\"> <img decoding=\"async\" class=\"rounded\" src=\"https:\/\/image.blockchain.news:443\/features\/8A6D364E10667B70266C559AAAD3793038EA7B225A572DDB5616E316563F53D8.jpg\" alt=\"Integrating Speech-to-Text Functionality in Django Applications\"> <\/a> <\/figure>\n<p>Integrating Speech-to-Text functionality into Django applications can significantly enhance user experience by allowing audio transcription directly within the app. According to AssemblyAI, developers can leverage their API to implement this feature seamlessly.<\/p>\n<h2>Setting Up the Project<\/h2>\n<p>To get started, create a new project folder and establish a virtual environment:<\/p>\n<pre><code># Mac\/Linux\npython3 -m venv venv\n. venv\/bin\/activate # Windows\npython -m venv venv\n.\\venv\\Scripts\\activate.bat<\/code><\/pre>\n<p>Next, install the necessary packages including Django, AssemblyAI Python SDK, and python-dotenv:<\/p>\n<pre><code>pip install Django assemblyai python-dotenv<\/code><\/pre>\n<h2>Creating the Django Project<\/h2>\n<p>Create a new Django project named &#8216;stt_project&#8217; and a new app within it called &#8216;transcriptions&#8217;:<\/p>\n<pre><code>django-admin startproject stt_project\ncd stt_project\npython manage.py startapp transcriptions<\/code><\/pre>\n<h2>Building the View<\/h2>\n<p>In the &#8216;transcriptions&#8217; app, create a view to handle file uploads and transcriptions. Open <code>transcriptions\/views.py<\/code> and add the following code:<\/p>\n<pre><code>from django.shortcuts import render\nfrom django import forms\nimport assemblyai as aai class UploadFileForm(forms.Form): audio_file = forms.FileField() def index(request): context = None if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['audio_file'] transcriber = aai.Transcriber() transcript = transcriber.transcribe(file.file) file.close() context = {'transcript': transcript.text} if not transcript.error else {'error': transcript.error} return render(request, 'transcriptions\/index.html', context)<\/code><\/pre>\n<h2>Defining URL Configuration<\/h2>\n<p>Map the view to a URL by creating <code>transcriptions\/urls.py<\/code>:<\/p>\n<pre><code>from django.urls import path\nfrom . import views urlpatterns = [ path('', views.index, name='index'),\n]<\/code><\/pre>\n<p>Include this app URL pattern in the global project URL configuration in <code>stt_project\/urls.py<\/code>:<\/p>\n<pre><code>from django.contrib import admin\nfrom django.urls import include, path urlpatterns = [ path('', include('transcriptions.urls')), path('admin\/', admin.site.urls),\n]<\/code><\/pre>\n<h2>Creating the HTML Template<\/h2>\n<p>Inside the &#8216;transcriptions\/templates&#8217; directory, create an <code>index.html<\/code> file with the following content:<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt; &lt;meta charset=\"UTF-8\"&gt; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt; &lt;title&gt;AssemblyAI Django App&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;h1&gt;Transcription App with AssemblyAI&lt;\/h1&gt; &lt;form method=\"post\" enctype=\"multipart\/form-data\"&gt; {% csrf_token %} &lt;input type=\"file\" accept=\"audio\/*\" name=\"audio_file\"&gt; &lt;button type=\"submit\"&gt;Upload&lt;\/button&gt; &lt;\/form&gt; &lt;h2&gt;Transcript:&lt;\/h2&gt; {% if error %} &lt;p style=\"color: red\"&gt;{{ error }}&lt;\/p&gt; {% endif %} &lt;p&gt;{{ transcript }}&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Setting the API Key<\/h2>\n<p>Store the AssemblyAI API key in a <code>.env<\/code> file in the root directory:<\/p>\n<pre><code>ASSEMBLYAI_API_KEY=your_api_key_here<\/code><\/pre>\n<p>Load this environment variable in <code>stt_project\/settings.py<\/code>:<\/p>\n<pre><code>from dotenv import load_dotenv\nload_dotenv()<\/code><\/pre>\n<h2>Running the Django App<\/h2>\n<p>Start the server using the following command:<\/p>\n<pre><code>python manage.py runserver<\/code><\/pre>\n<p>Visit the app in your browser, upload an audio file, and see the transcribed text appear.<\/p>\n<h2>Non-blocking Implementations<\/h2>\n<p>To avoid blocking operations, consider using webhooks or async functions. Webhooks notify you when the transcription is ready, while async calls allow the app to continue running during the transcription process.<\/p>\n<h3>Using Webhooks<\/h3>\n<p>Set a webhook URL in the transcription config and handle the webhook delivery in a separate view function:<\/p>\n<pre><code>webhook_url = f'{request.get_host()}\/webhook'\nconfig = aai.TranscriptionConfig().set_webhook(webhook_url)\ntranscriber.submit(file.file, config)<\/code><\/pre>\n<p>Define the webhook receiver:<\/p>\n<pre><code>def webhook(request): if request.method == 'POST': data = json.loads(request.body) transcript_id = data['transcript_id'] transcript = aai.Transcript.get_by_id(transcript_id)<\/code><\/pre>\n<p>Map this view to a URL:<\/p>\n<pre><code>urlpatterns = [ path('', views.index, name='index'), path('webhook\/', views.webhook, name='webhook'),\n]<\/code><\/pre>\n<h3>Using Async Functions<\/h3>\n<p>Utilize async views in Django for non-blocking transcription:<\/p>\n<pre><code>transcript_future = transcriber.transcribe_async(file.file)\nif transcript_future.done(): transcript = transcript_future.result()<\/code><\/pre>\n<h2>Speech-to-Text Options for Django Apps<\/h2>\n<p>When implementing Speech-to-Text, consider cloud-based APIs like AssemblyAI or Google Cloud Speech-to-Text for high accuracy and <a rel=\"nofollow noopener\" href=\"https:\/\/blockchain.news\/tag\/scalability\" target=\"_blank\"><u><b><i><dfn data-info=\"A change in the size or scale to handle the network\u2019s demands. This word is used to refer to a blockchain project\u2019s ability to handle networ...\">scalability<\/dfn><\/i><\/b><\/u><\/a>, or open-source libraries like SpeechRecognition and Whisper for greater control and privacy.<\/p>\n<h2>Conclusion<\/h2>\n<p>This guide shows how to integrate Speech-to-Text into Django apps using the AssemblyAI API. Developers can choose between blocking and non-blocking implementations and select the best Speech-to-Text solution based on their needs.<\/p>\n<p>For more details, visit the <a rel=\"nofollow\" href=\"https:\/\/www.assemblyai.com\/blog\/speech-to-text-with-django\/\">AssemblyAI blog<\/a>.<\/p>\n<p><span><i>Image source: Shutterstock<\/i><\/span> <!-- Divider --> <!-- Author info END --> <!-- Divider --> <a href=\"https:\/\/blockchain.news\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lawrence Jengar Sep 28, 2024 03:26 Learn how to integrate Speech-to-Text into Django apps using AssemblyAI API. Build an app to transcribe audio files and display the transcriptions. Integrating Speech-to-Text functionality into Django applications can significantly enhance user experience by allowing audio transcription directly within the app. According to AssemblyAI, developers can leverage their API [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":463940,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[19529,21653,25,19528],"class_list":{"0":"post-463939","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-blockchain","8":"tag-assemblyai","9":"tag-django","10":"tag-news","11":"tag-speech-to-text"},"_links":{"self":[{"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/posts\/463939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/comments?post=463939"}],"version-history":[{"count":0,"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/posts\/463939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/media\/463940"}],"wp:attachment":[{"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/media?parent=463939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/categories?post=463939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/e-bitco.in\/index.php\/wp-json\/wp\/v2\/tags?post=463939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}