Building a Speech Transcription App Using Flask and OpenAI

Pretty Printed
20 Jun 202421:57

Summary

TLDRDieses Video zeigt, wie man mit Flask und Open AI eine einfache App baut, die es Benutzern ermöglicht, ihre Stimme aufzunehmen, in Text umzuwandeln und die Transkription im Browser anzuzeigen. Der Prozess umfasst die Verwendung des Browser-Audio-Recording-APIs, das Senden des Audios an Open AI für die Transkription und die Anzeige des Ergebnisses. Zusätzlich wird ein Coaching-Programm erwähnt, das bei Bedarf für individuelle Projekthilfe angeboten wird.

Takeaways

  • 😀 Das Video zeigt, wie man mit Flask und Open AI eine App baut, die die Aufnahme der Benutzerstimme und ihre Transkription ermöglicht.
  • 🎙️ Die App veranschaulicht, wie man mit dem 'Record'-Button die Sprache des Benutzers aufzeichnet und an Open AI sendet.
  • 📝 Open AI wandelt die aufgezeichnete Sprache in Text um, der dann im Browser angezeigt wird.
  • 🛠️ Der Entwickler bietet auch persönliche Coaching-Programme für Python-, Flask- oder Django-Projekte an.
  • 🔗 Interessenten können unter 'prettyprint.com coaching' nach weiteren Informationen suchen und sich für das Coaching anmelden.
  • 📱 Der Browser-API 'getUserMedia' wird verwendet, um die Aufnahme der Sprache direkt aus dem Browser zu ermöglichen.
  • 👍 Es wird eine Überprüfung implementiert, ob die 'getUserMedia'-Funktion im Browser verfügbar ist, um eine Fehlermeldung anzuzeigen, falls nicht.
  • 🔴 Der 'Record'-Button wechselt seine Farbe, um anzuzeigen, ob die Aufnahme aktiv ist oder nicht.
  • 🔁 Der aufgezeichnete Audio-Stream wird in 'Chunks' unterteilt, die später zu einem Blob zusammengefasst werden, bevor sie an Open AI gesendet werden.
  • 📤 Die App verwendet JavaScripts 'fetch'-Methode, um die Audio-Daten an die Flask-App zu senden, die dann an Open AI weiterleitet.
  • 📥 Open AIs API wandelt die empfangenen Audio-Daten in eine Transkription um, die von der Flask-App zurück an den Browser gesendet wird.
  • 🖥️ Der Flask-Server ist für die Verarbeitung der Anfrage verantwortlich, einschließlich der Verwendung des Open AI-Modells 'whisper-1' für die Transkription.
  • 📝 Die finale Transkription wird im Browser angezeigt, wobei der Benutzer die Möglichkeit hat, die Aufnahme zu wiederholen und die Transkription zu aktualisieren.

Q & A

  • Was zeigt der Autor in dem Video?

    -Der Autor zeigt, wie man mit Flask und Open AI eine einfache App baut, die es Benutzern erlaubt, ihre Stimme aufzunehmen, diese in Text umzuwandeln und den Text im Browser anzuzeigen.

  • Was ist das Ziel des Projekts, das im Video vorgestellt wird?

    -Das Ziel des Projekts ist es, die Fähigkeiten der Open AI API zu demonstrieren, insbesondere die Funktion zur Spracherkennung und -umwandlung in Text.

  • Welche Technologien werden im Video verwendet?

    -Im Video werden Flask, Open AI, JavaScript und Browser-APIs verwendet, um die Spracherfassung und -umwandlung zu ermöglichen.

  • Was ist der Zweck von 'getUserMedia' in diesem Kontext?

    -'getUserMedia' ist eine Browser-API, die verwendet wird, um den Benutzer aufzufordern, die Erlaubnis zur Aufnahme von Audio zu erteilen und einen Aufnahmegerät wie ein Mikrofon auszuwählen.

  • Wie wird die Aufnahme gestoppt und an die Open AI API gesendet?

    -Die Aufnahme wird gestoppt, indem der 'stop'-Button betätigt wird. Die aufgezeichneten Audio-Chunks werden zu einem Blob zusammengefasst und dann an die Open AI API gesendet, um die Spracherkennung durchzuführen.

  • Welche Rolle spielt JavaScript im Projekt?

    -JavaScript wird verwendet, um die Benutzeroberfläche zu steuern, die Aufnahme zu verwalten, die Audio-Chunks zu sammeln und sie an die Flask-App zu senden.

  • Was ist der Zweck der 'mediaRecorder' Variable im Code?

    -Die 'mediaRecorder' Variable ist ein Medienrekorder, der verwendet wird, um die Benutzerstimme aufzunehmen und die aufgezeichneten Daten in Form von Audio-Chunks zu speichern.

  • Wie wird die Transkription der Sprache aus dem Audio erreicht?

    -Die Transkription wird durch den Aufruf der Open AI API mit dem Modell 'whisper-1' erreicht, das die aufgezeichneten Audio-Daten empfängt und sie in Text umwandelt.

  • Was passiert, wenn der Benutzer die Erlaubnis zur Mikrofonnutzung verweigert?

    -Wenn der Benutzer die Erlaubnis verweigert, kann die App das Mikrofon nicht nutzen, um die Sprache aufzunehmen, und der Benutzer erhält eine Fehlermeldung.

  • Wie kann man die Transkription im Browser anzeigen?

    -Die Transkription kann im Browser angezeigt werden, indem sie in das HTML-Dokument eingebettet wird, sobald sie von der Open AI API zurückgegeben wird.

  • Was ist der Zweck des 'coaching program', das im Video erwähnt wird?

    -Das 'coaching program' ist ein Programm, bei dem der Autor bei Python-, Flask- oder Django-Projekten Einzelpersonen berät und unterstützt, indem er gemeinsam mit ihnen an ihrem Code arbeitet und Probleme löst.

Outlines

00:00

😀 Einführung in die App-Entwicklung mit Flask und Open AI

Der erste Absatz stellt das Videothema vor: die Entwicklung einer einfachen App mit Flask und Open AI, die es ermöglicht, die Stimme eines Benutzers aufzunehmen, zu transkribieren und den Text im Browser anzuzeigen. Der Ersteller demonstriert die Funktionsweise und bietet an, bei Projekten mit Python, Flask oder Django persönliche Unterstützung durch ein Coaching-Programm anzubieten. Zudem wird eine einfache Flask-App mit einer Route, einem Aufnahme-Button und einem Bereich für die Transkriptionsausgabe vorgestellt.

05:02

🔧 Aufbau der Aufnahmefunktion mit JavaScript und Browser-API

Der zweite Absatz beschreibt den Prozess der Implementierung der Aufnahmefunktion in JavaScript. Dazu gehört das Sicherstellen der Verfügbarkeit der 'getUserMedia'-Funktion im Browser, das Festlegen von Callback-Funktionen für Erfolgs- und Fehlerfälle, das Anfordern der Aufnahmeberechtigung durch den Benutzer und das Erstellen eines 'MediaRecorder'-Objekts. Zudem wird erläutert, wie man die Farbe des Aufnahme-Buttons mit Bootstrap anpasst, je nachdem ob die Aufnahme gestartet oder gestoppt wurde.

10:03

🎙️ Verwaltung der Audio-Aufnahme und -Transkription

Der dritte Absatz konzentriert sich auf die Verwaltung der Audio-Aufnahme. Es wird erklärt, wie man die aufgenommenen Audio-Daten in 'Chunks' verarbeitet, diese in ein Array speichert und nach dem Stoppen der Aufnahme zu einem 'Blob' zusammenfasst. Anschließend wird die Verwendung von 'FormData' beschrieben, um die Audio-Daten an die Flask-App zu senden, und wie man mit JavaScripts 'fetch'-Funktion eine POST-Anfrage an die Server-Route 'transcribe' richtet.

15:05

📡 Integration der Open AI API zur Spracherkennung

Der vierte Absatz behandelt die Integration der Open AI API in die Flask-App zur Umwandlung des Audio-Blobs in eine Text-Transkription. Dazu gehört das Einrichten der 'requests'-Bibliothek, das Konvertieren des Audio-Blobs in ein 'BytesIO'-Objekt, das Senden des Audios an die Open AI API und das Empfangen der Transkriptions-Antwort. Es wird auch auf die Notwendigkeit hingewiesen, den Open AI Client mit einem API-Schlüssel zu konfigurieren.

20:06

🖥️ Anzeige der Transkription im Browser und Projektabschluss

Der fünfte und letzte Absatz beschreibt, wie die erhaltene Transkription in der Webanwendung angezeigt wird. Der Ersteller zeigt, wie man die Transkription direkt im Browser darstellt, indem man den Inhalt in ein HTML-Element einfügt. Schließlich werden die Ergebnisse des Projekts demonstriert, wobei die Transkription des aufgezeichneten Textes angezeigt wird, sobald die Aufnahme gestoppt wird. Der Ersteller beendet das Video mit einem Aufruf an die Zuschauer, Fragen zu stellen, das Video zu liken und das Kanal abonnieren.

Mindmap

Keywords

💡Flask

Flask ist ein leichtgewichtiger Web-Framework für Python, das es ermöglicht, Webanwendungen und APIs schnell und einfach zu erstellen. Im Video wird Flask verwendet, um eine Anwendung zu erstellen, die die Aufnahme und Transkription von Sprache verarbeitet. Das Hauptthema des Videos ist die Integration von Flask mit OpenAI, um eine interaktive Spracherkennung zu demonstrieren.

💡OpenAI

OpenAI ist eine künstliche Intelligenz-Forschungsorganisation, die APIs und Modelle zur Verfügung stellt, die für Aufgaben wie Textgenerierung, Spracherkennung und mehr verwendet werden können. Im Kontext des Videos wird OpenAI für die Transkription des aufgezeichneten Sprachinhalts verwendet, um zu demonstrieren, wie die OpenAI-API in Kombination mit Flask funktioniert.

💡Voice Recording

Die Sprachaufnahme bezieht sich auf das Aufnehmen von Sprache mithilfe von Mikrofonen oder anderen Audio-Aufnahmegeräten. Im Video wird gezeigt, wie man mit JavaScript und Browser-APIs Sprache aufnimmt, die dann an OpenAI gesendet wird, um eine Transkription zu erhalten.

💡Transcription

Transkription ist der Prozess, bei dem gesprochene Sprache in geschriebenen Text umgewandelt wird. Im Video wird die Transkription verwendet, um die Aufnahmen des Benutzers in Text zu verwandeln, der dann in der Webbrowser-Oberfläche angezeigt wird, um die Funktionalität der OpenAI-API zu veranschaulichen.

💡getUserMedia

getUserMedia ist eine Browser-API, die es Webentwicklern ermöglicht, auf die Medienstreams des Benutzers, wie Audio oder Video, zuzugreifen. Im Video wird diese API verwendet, um die Mikrofonfunktion des Benutzers zu aktivieren und die Sprache aufzunehmen, bevor sie an OpenAI gesendet wird.

💡Media Recorder API

Die Media Recorder API ist ein Teil der Web-APIs, die es Entwicklern ermöglicht, Medienstreams wie Audio aufzuzeichnen. Im Video wird diese API genutzt, um die Sprache des Benutzers zu erfassen und in ein digitales Format zu konvertieren, das für die Transkription an OpenAI gesendet werden kann.

💡JavaScript

JavaScript ist eine Programmiersprache, die häufig für die interaktive Verarbeitung in Webbrowsern verwendet wird. Im Video wird JavaScript für die Erstellung der Funktionalität zur Spracherfassung und -aufnahme sowie für die Interaktion mit der OpenAI-API verwendet.

💡Fetch API

Die Fetch API ist eine moderne Methode in JavaScript, um Netzwerk-Anfragen zu senden und zu empfangen. Im Video wird Fetch verwendet, um die aufgezeichnete Audio-Datei an die Flask-App zu senden, die dann an OpenAI weitergeleitet wird, um die Transkription zu erhalten.

💡WebM

WebM ist ein quelloffenes Medienformat, das für die Kodierung von Audio und Video verwendet wird. Im Video wird WebM als Dateiformat für die Audio-Aufnahmen verwendet, bevor sie an OpenAI gesendet werden, um die Transkription zu ermöglichen.

💡API Key

Ein API Key ist ein spezifischer Code, der für die Authentifizierung und den Zugriff auf APIs verwendet wird. Im Video wird ein API Key für OpenAI verwendet, um eine Verbindung zur OpenAI-API herzustellen und die Transkription der Sprache durchzuführen.

💡Whisper

Whisper ist ein Modell von OpenAI, das für die Spracherkennung und -transkription verwendet wird. Im Video wird das Whisper-Modell für die Konvertierung des aufgezeichneten Audios in Text genutzt, um zu demonstrieren, wie Sprache in geschriebene Informationen umgewandelt werden kann.

Highlights

Heute zeigen wir, wie man mit Flask und Open AI eine App baut, die es ermöglicht, die Stimme eines Benutzers aufzunehmen, zu transkribieren und den Text anzuzeigen.

Die App nutzt den Browser-API 'getUserMedia', um die Stimme des Benutzers aufzuzeichnen.

Es wird eine JavaScript-Datei 'app.js' erstellt, um die Aufnahme-Funktionalität zu implementieren.

Es wird eine Überprüfung implementiert, ob die 'getUserMedia'-Funktion im Browser verfügbar ist.

Die App fragt den Benutzer um Erlaubnis für die Mikrofonnutzung und die Auswahl des Aufnahmegeräts.

Die Medienaufnahme wird gestartet und gestoppt, indem der 'Record'-Button gedrückt wird.

Die aufgenommenen Audio-Daten werden in 'Chunks' gespeichert und später zu einem 'Blob' zusammengefasst.

Die App verwendet 'fetch', um die Audio-Daten an die Flask-App zu senden.

In der Flask-App wird eine Route 'transcribe' erstellt, um die Audio-Daten zu empfangen.

Die Audio-Daten werden in ein 'BytesIO'-Objekt umgewandelt, um sie an die Open AI API zu senden.

Die Open AI API wird verwendet, um die Audio-Daten in Text zu transkribieren.

Die Transkriptions-Ausgabe wird aus der Open AI API extrahiert und in der App angezeigt.

Die App ermöglicht es dem Benutzer, die Transkription sofort nach dem Stoppen der Aufnahme zu sehen.

Die App kann mehrere Aufnahmen durchführen und die Transkriptionen werden im Browser aktualisiert.

Es wird eine Anleitung gegeben, wie man die Open AI API installiert und konfiguriert.

Das Projekt demonstriert die Verwendung der Open AI API für die Spracherkennung und -transkription.

Es wird ein Coaching-Programm angeboten, um bei Python-, Flask- oder Django-Projekten Unterstützung zu bieten.

Die finale App zeigt, wie einfach es ist, die Open AI API in eine Webanwendung zu integrieren.

Transcripts

play00:00

hey everyone in today's video I'll show

play00:02

you how to build a simple app with flask

play00:04

and open AI that allows you to record a

play00:06

user's voice and transcribe that voice

play00:09

and then display the text so as an

play00:11

example I'll hit record here and I'll

play00:13

just start talking so everything that

play00:15

I'm saying here as the record button is

play00:17

read will be sent over to open Ai and it

play00:20

will determine what I said converted to

play00:22

text and then I will display it here in

play00:24

the browser so let me hit the button

play00:26

again and wait just a second and we see

play00:29

here

play00:30

I have the transcription of everything

play00:33

that I just said so I've transcribed the

play00:37

uh audio that I just sent and we have it

play00:39

displayed here so that's what I'm going

play00:41

to demonstrate in this video it's a a

play00:44

simple little project with flask and

play00:45

openi just to show you what you can do

play00:47

with the open AI API uh but before we

play00:50

get into that I just want to say that if

play00:51

you need one-on-one help with any of

play00:53

your projects so your python projects

play00:55

flask Jango whatever I do have something

play00:58

called the coaching program available

play00:59

where I work with people oneon-one we

play01:01

have zoom calls where I can look at your

play01:03

code and work through any problems that

play01:05

you have or help you just write code for

play01:07

your app so if you're interested in that

play01:08

just go to pretty print.com coaching and

play01:12

I will uh be available to help you with

play01:14

that just read the instructions on how

play01:15

to fill out the form and I'll will get

play01:17

in contact with you so with that said

play01:19

let's get into building this project in

play01:21

flask and open the eye and and also

play01:23

there's a bit of JavaScript involved as

play01:25

well okay so to get started with this

play01:27

this is what I have to begin with it's

play01:29

just a very simple flas cap that has one

play01:32

route so far inside that one route it

play01:35

has a record button and an area where

play01:37

it's going to display the output of the

play01:40

transcription so this is what it looks

play01:42

like right now you just see the record

play01:44

button but when I press it it doesn't do

play01:46

anything I'm also adding this app.js

play01:50

file here so this is where I'll write

play01:52

all the JavaScript for this little

play01:54

project so I'll start writing it now

play01:56

because I want to set up the

play01:58

functionality where I can actually

play02:00

record my voice first and then once I

play02:02

can record my voice I'll then take the

play02:05

audio and pass it to open AI to get the

play02:08

transcription and then I can insert it

play02:10

into the page so here I have these two

play02:12

constants record and output uh those are

play02:15

just the elements on the page so I can

play02:18

uh know when the record button was

play02:20

pressed and I know where to put the uh

play02:24

output transcription so the first thing

play02:26

I need to do is I need to set up the

play02:28

ability to record so this is is going to

play02:30

be using a browser API so there's no

play02:33

custom code that needs to be written to

play02:35

record your voice you just need to use

play02:36

the API that comes with your browser one

play02:39

of the mini apis that comes with your

play02:41

browser so uh to do that uh the first

play02:44

thing I need to do is I need to make

play02:46

sure that the user actually has this

play02:47

available and pretty much everyone in

play02:50

2024 and going forward should have this

play02:52

on their browsers but just in case they

play02:54

don't uh you want to put a check in your

play02:56

code to make sure that they actually

play02:58

have the functionality so to do that uh

play03:01

what you do is you see if the function

play03:04

that you need to use first so this

play03:05

function is called get user media you

play03:07

see if it exists in the browser if it

play03:10

does then you can go forward with

play03:11

actually setting everything up if it

play03:13

doesn't exist then you can give them

play03:14

like some error message so the API is

play03:17

going to be under

play03:18

Navigator medad

play03:23

devices. user media right so if this

play03:27

exists if this function exists that

play03:29

means that uh you can record audio from

play03:32

their browser if it doesn't exist then

play03:36

you can't and you just have to give them

play03:37

an error message so uh what I'll do is

play03:39

I'll put a comment here and

play03:42

then after the brackets here I'll put an

play03:45

else and I'll just uh

play03:48

alert um you know get user media uh not

play03:53

supported in your browser okay so let's

play03:58

say it's not supported just like that so

play04:02

if this function doesn't exist like I

play04:03

said then we can't really do anything um

play04:06

with this particular project or you just

play04:08

need an alternate way of getting their

play04:10

voice but you can't use this but if they

play04:12

do then you can continue with the code

play04:15

here uh so the way that this works is

play04:19

once you call this function gate user

play04:21

media it's going to display the uh

play04:25

permissions popup in the browser asking

play04:27

the user to give permission uh to record

play04:31

audio and also they'll have to select a

play04:34

recording device as well so if they have

play04:36

like multiple microphones connected to

play04:38

their computer like I do right now then

play04:40

they'll have to select the one that they

play04:41

want to use to uh record so that's what

play04:44

happens when you call this function and

play04:45

when you call that function uh that

play04:47

would either be successful or a failure

play04:49

so if they give you permission and they

play04:51

select a device it's going to succeed

play04:54

and then uh you can get the sound from

play04:56

that device and if they say no or

play04:59

there's just some other error then um

play05:02

you know you can't do anything because

play05:03

they're not giving you permission to uh

play05:07

use their

play05:08

microphone so for that I need to Define

play05:11

these two callback functions so let me

play05:13

Define those here first uh I'm going to

play05:16

use the approach of defining functions

play05:19

like this um so it's going to be a

play05:22

callback function so it's going to start

play05:23

with on that's just like a little

play05:25

convention um but let's say on media

play05:30

setup or on media start how about that

play05:34

uh on media start I don't even like that

play05:36

name um on media setup success okay this

play05:40

is a good name so on media setup success

play05:42

I want to call this function when um the

play05:45

user like gives permission so um like I

play05:48

said I'm using this style of functions

play05:50

simply because um the API that I'm using

play05:54

uh I need to Define some functions for

play05:56

things that happen when like um you know

play05:59

you

play06:00

you stop the media recording or like

play06:02

sound comes in and this is the easiest

play06:04

syntax so I just want to keep it

play06:05

consistent so I'm going to use it for

play06:07

the callbacks as well so uh this is

play06:10

going to take in some stream this is the

play06:12

basically the microphone the source of

play06:14

the audio so I'll just put that there

play06:17

and you know what I'll put an alert so

play06:20

um you know everything is working just

play06:24

so we can

play06:26

see and then I'll call this on media

play06:30

setup failure right and this function

play06:34

doesn't have to take anything but you

play06:36

can have an error in here an error

play06:38

message I'll just put error like that

play06:40

and then I'll just alert whatever the

play06:42

error is okay so this should be enough

play06:46

for the Callback functions for now and

play06:48

then once I have those callback

play06:50

functions I want to actually use them um

play06:52

in G user media so I'm going to call

play06:56

Navigator media devices

play06:59

G user

play07:01

media and then I want to pass in a

play07:04

dictionary or since this is Javascript

play07:07

um an object that just tells the browser

play07:12

what kind of media I'm interested in

play07:14

getting so in this case I just want

play07:15

audio I don't want like video or

play07:17

anything so I just put audio true here

play07:20

and then after this I can just call then

play07:22

and I pass in the two callbacks so on

play07:24

media

play07:27

success and on media setup failure okay

play07:32

so it's on media success not on media

play07:37

setup success there we go so that should

play07:40

be enough so let me go back over to the

play07:42

browser and refresh and now we see as

play07:45

soon as I refresh like this popup comes

play07:47

up where I can select an audio source so

play07:50

I have two microphones set up and I can

play07:53

either select allow or block I'll select

play07:55

allow and we see everything is working

play07:57

and then we see the icon here that just

play08:00

shows that I've granted permission to

play08:01

this website and every time I refresh

play08:04

like uh it doesn't have to ask for

play08:06

permission because it still remembers

play08:09

that on this particular website I've

play08:11

given permission so uh if I close my

play08:14

browser reopen it and go back to the

play08:16

same page then I'll have to Grant

play08:19

permission again so okay so now I have

play08:22

this ready and what I want to do next is

play08:25

I want to set up the ability to actually

play08:28

take my voice record my voice and then

play08:30

send it somewhere so in the on media

play08:32

setup success function that's where I'll

play08:34

put like all the code for this uh so the

play08:36

first thing I need to do is I need to

play08:38

grab the actual um stream and create a

play08:43

media recorder out of it so I'll do cons

play08:45

media

play08:47

recorder and this is going to be equal

play08:49

to new media recorder so I'm

play08:53

instantiating the media recorder so this

play08:55

is from the JavaScript API the browser

play08:59

API

play09:00

and let me make that a capital

play09:02

R and this should be const okay so I'm

play09:05

instantiating this and then I'll use

play09:07

this media recorder for everything so

play09:09

now what I want to consider is what

play09:11

happens when I start recording so I have

play09:15

this um record element here this value

play09:20

and I'll just do unclick like I said I

play09:22

don't usually use this style of writing

play09:24

functions but because I'm using the

play09:26

media recorder I think it makes more

play09:27

sense so record. on click

play09:30

um we have the function here and when I

play09:34

do this I want to check the status of

play09:36

the media recorder so if the media

play09:38

recorder is currently recording I'm

play09:40

basically going to stop the media

play09:41

recorder and if it isn't recording then

play09:43

I want to start it so I can check the

play09:46

state of the media recorder by doing

play09:47

media recorder. State and this will be

play09:51

equal to recording so if it's recording

play09:54

then I'm going to stop it so I'll do

play09:57

media recorder. stop

play10:00

and then I have some bootstrap stuff um

play10:03

basically I want to change the color of

play10:05

the record button so I can do record.

play10:08

class list. remove I want to remove the

play10:12

button danger right so when I start the

play10:14

recording I want to turn the button to

play10:16

be red and when I stop I want to turn it

play10:19

back to the original blue that it is

play10:21

here so for that I have to remove the

play10:23

danger class danger is the red and I

play10:26

need to add primary so classless uh. add

play10:30

and then button primary and bootstrap

play10:32

and then this will be the opposite uh in

play10:34

this else here so this else represents

play10:38

um the state that is not recording

play10:40

meaning that you haven't started

play10:41

recording yet so here I'm going to start

play10:44

the media recorder so media recorder.

play10:46

start not starter but start and then I

play10:49

want to add and remove the correct

play10:52

classes so I'm going to remove button

play10:56

primary this time remove the blue and

play10:59

then I'm going to add the button danger

play11:03

so here when the user clicks on the

play11:06

record button if it's already recording

play11:09

it stops changes the colors if it isn't

play11:11

recording it starts the recording and it

play11:14

changes the colors as well okay so the

play11:17

idea is when you are recording audio

play11:20

from this um the user's Voice or

play11:23

whatever sound is coming is it's going

play11:25

to be coming in in chunks right so um

play11:29

it's not like going to be a continuous

play11:33

thing that has the audio instead it's

play11:35

going to be broken up into little pieces

play11:37

so you can take those little pieces and

play11:39

put them into an array and then you can

play11:42

take that entire array and take that as

play11:44

the recording once the user hits stop so

play11:46

the way you can do this is you can first

play11:48

uh initialize like a list so we'll say

play11:51

uh we'll call this chunks or initialize

play11:54

an array in JavaScript and then down

play11:56

here I have a media

play12:00

quarter and then I need to define the on

play12:03

data available function right so I'm

play12:05

adding a function

play12:07

here and E is going to be you know the

play12:12

stuff that is available and I simply

play12:14

want to push the data from E like the

play12:16

event of data being available into the

play12:19

chunk so I'll do chunks. push e

play12:24

data right so let's say like each chunk

play12:27

is 1 second so if I record for 10

play12:29

seconds then I'll have 10 items in this

play12:31

array when I'm done when I hit the the

play12:34

stop button or when I hit the record

play12:36

button again to stop it so once I have

play12:39

all the chunks after I stop it I need to

play12:41

do something with it so first I need to

play12:44

know when I've stopped so I can set up

play12:47

another function here so media recorder

play12:50

on

play12:51

stop so when I stop I can do something

play12:55

and what I want to do is I want to

play12:57

create a new object object that holds

play13:00

all of the information from the chunks

play13:02

so like I said I set up the chunks

play13:03

because the data is going to come into

play13:05

chunks but once I stop the recording I

play13:07

want to take all those chunks and create

play13:09

like one thing and in JavaScript that's

play13:11

called a

play13:12

blob so what I'll do is I'll say uh let

play13:15

blob so this will be a variable uh equal

play13:18

new blob so I'm instantiating a blob and

play13:21

I'm just going to pass the chunks so

play13:24

chunks here and I need to give it a file

play13:28

type right so the type type here is

play13:29

going to be

play13:31

audio audio webm which is the default

play13:36

type for audio when you're recording

play13:38

like this and it's completely fine for

play13:39

our purposes if you wanted to change

play13:41

this you could but audio webm is

play13:44

completely

play13:45

fine so once I do that I can like clear

play13:47

out the chunks so I'll just say chunks

play13:49

equals that so I can record multiple

play13:51

times and then once I have this blob I

play13:54

can go ahead and send it over to my

play13:56

flask app so flask is about to come into

play13:59

play again uh so what I want to do is I

play14:01

want to create some form data here and

play14:03

I'm going to add it to the form right so

play14:05

it's not like a regular HTML form but

play14:08

for me this is the most convenient way

play14:10

of getting the blob over to flask so I'm

play14:13

going to say form data append and I'll

play14:17

add a form element called audio and I'm

play14:21

passing the blob to that so now that I

play14:23

have this form data and I've attached

play14:24

The Blob to it I need to send that over

play14:26

to my flas app so I can use fetch for

play14:28

that

play14:30

and we'll create an inpoint called

play14:34

transcribe and then here we'll set up

play14:36

the things here so the method is going

play14:39

to be

play14:42

post and then the body is going to be

play14:45

the form

play14:48

data right so post requests using the

play14:51

form data and the form data only has the

play14:53

blob in it when this returns properly I

play14:58

want to call this then and then let's

play15:00

say

play15:02

response and I want to convert that to

play15:04

Json so I'm going to return my uh

play15:07

transcription in Json so I'm just

play15:10

converting it to Json and then I'll add

play15:11

another

play15:14

then and this will have the

play15:17

data that's actually in my Json and for

play15:20

now I'll just console log it so console

play15:23

log data or really I can just alert it I

play15:26

think that would be fine as well all

play15:28

right

play15:30

so just to recap um I set up the

play15:33

functionality where I can change the

play15:35

color of the button and stop or start

play15:37

the media recorder I have this function

play15:40

here that will be called every time

play15:42

there's data available from the stream

play15:44

so it just gets pushed onto this array

play15:47

called chunks and then when I stop the

play15:50

recording it will create a blob using

play15:52

the chunks it will add that blob to some

play15:55

form data and then I'll pass that form

play15:57

data to fetch so I can send it over to

play15:59

my flask app so now let me go over to my

play16:02

flas app and work on that so I

play16:06

have a new route here called transcribe

play16:09

that I want to build so app route

play16:12

transcribe I remember

play16:14

the methods will just be post for this

play16:18

and I'll call it

play16:21

transcribe and what I want to do is I

play16:23

want to import requests from flask

play16:25

because I need to get the data from the

play16:27

requests so I'll I'll just call this

play16:29

file and I'll say requests. files and

play16:32

then audio so audio is just the audio

play16:34

that I put here in form data next I need

play16:37

to convert that audio to something that

play16:39

I can send to the open AI API so what

play16:43

I'm going to do is I'm going to

play16:45

import import IO and I'm going to use

play16:49

that to create a bytes IO object and

play16:53

I'll just call this buffer so buffer

play16:55

equals io. bytes IO and I'll take the

play16:59

file and read it in right so it's going

play17:01

to read in the file data and create a

play17:03

new bytes iio object from that and I'm

play17:06

calling it buffer reason why I'm doing

play17:08

that is because I need something I can

play17:10

send to open a I can't send the file

play17:12

itself directly from the form I need to

play17:15

convert it to a form that uh I can send

play17:17

to open AI so that's why I'm doing it

play17:19

here and then I need to give it a name

play17:21

the name doesn't really matter but I

play17:23

noticed in testing that the open AI API

play17:26

doesn't like when you send a buffer With

play17:28

No Name so I'm just going to call this

play17:31

audio

play17:33

do

play17:35

webm right that should be

play17:38

fine so next I need to use the open AI

play17:41

API so first I can pip install it so pip

play17:45

install open

play17:47

Ai and then I can go ahead and import

play17:50

the main class so from open AI import

play17:53

open Ai and I can set up the client so

play17:57

client equals open Ai and I have my API

play18:02

key in my environment so it's there you

play18:04

don't see it but you have to set that up

play18:06

before so you can either put it here as

play18:08

like API key equals whatever or you can

play18:11

put it in the environment like me under

play18:13

an environment variable called open AI

play18:16

API key so once you have that client

play18:19

then you can use it for stuff so here um

play18:23

I'm going to create a variable called

play18:26

transcript so this is going to be the uh

play18:29

output from open Ai and I'm going to use

play18:31

the client that I just instantiated and

play18:34

then I'll use the API call for

play18:36

transcription so it's client.

play18:39

audio.

play18:40

transcriptions doc create and then you

play18:43

need to give it a model uh the model

play18:45

that I'll use is whisper one and then

play18:48

the file so the file is just going to be

play18:51

buffer right and from this I want to

play18:52

return adjacent object uh we'll just say

play18:56

output and I'll take the transcript and

play19:00

grab the text so let's see if this works

play19:02

let's go ahead and start the

play19:05

app and let's go over and check it out

play19:09

so I'll refresh the page and remember

play19:12

the permission is still there so I'll

play19:14

hit the record button and I'll just

play19:15

start talking so record and I'm still

play19:18

talking I'm using the microphone that

play19:19

I'm recording the video with so

play19:21

everything I'm saying now should be

play19:22

recorded we see that the button is red

play19:25

so now I'll stop it by clicking it again

play19:30

and we see this popup this alert it has

play19:34

object object the reason why it has

play19:35

object object is because I'm trying to

play19:37

console log or not console log but alert

play19:41

um an object here so instead of just

play19:43

data I'll do data. output because that's

play19:47

what I put it under and I'll try it

play19:49

again so let me just

play19:51

refresh and I'll record again so now I'm

play19:54

recording and now when I hit the stop

play19:56

button or the record button again it's

play19:58

going to alert the actual text so let's

play20:01

see if that

play20:02

happens so just wait a moment and we see

play20:06

here the text that I was just saying as

play20:08

I was reading so I'll record again and

play20:10

blah blah blah you just heard it okay so

play20:13

the last thing I want to do is I want to

play20:14

Simply put that here so the user can see

play20:17

it and that's very simple to do so

play20:19

instead of alerting data. output I'll

play20:22

just take the output element

play20:26

here so output

play20:29

enter HTML and I'll do data output just

play20:33

like that so now let's try one more time

play20:36

I'll refresh I'll hit the record button

play20:39

here and I'll just talk a little bit and

play20:41

we should see that when I hit the stop

play20:43

button all the text is going to appear

play20:46

here when everything is done so I'll hit

play20:49

stop and we see the text appear here

play20:52

this is everything that I just said and

play20:55

of course if I wanted to record again

play20:57

because I'm clearing out the chunks I

play20:59

can so now when I uh hit the record

play21:02

button again and it stops it should just

play21:05

uh replace everything here with

play21:07

everything that I said after I hit the

play21:08

button again so I'll stop

play21:11

it and we see that the text has just

play21:15

changed so that's it for this little

play21:16

project that I want to show you it's

play21:18

just a simple way of using the the open

play21:20

AI API but a lot of people are

play21:23

interested in this API nowadays so um I

play21:25

thought I'd make more videos this is a

play21:27

second video with open AI so I'll

play21:29

probably make more videos in the future

play21:31

but I just want to show you like um a

play21:33

little example of what you can do with

play21:35

the uh open AI API so that's it for this

play21:39

video if you have any questions about

play21:40

anything that I've done here feel free

play21:41

to leave a comment down below if you

play21:43

like this video please give me a thumbs

play21:44

up and if you haven't subscribed to my

play21:46

channel already please subscribe so

play21:47

thank you for watching and I will talk

play21:48

to you next time

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
FlaskOpen AISpracherkennungTranskriptionWebentwicklungApp-BauenJavaScriptBrowser-APIMediaRecorderAPI-Nutzung