Help understanding a Python web server app.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
(NOTE: You don't need to know much about the Kaldi ASR project for this thread. It's just a speech recognition engine that allows you to build your own language models.)

On my voice recognition project, I found an example of how to use Kaldi in a browser and it is written in python. I really like it, but I'm a .NET guy and the project I want to incorporate this in is ASP.NET Core.

I can mostly read Python syntax, but am utterly unfamiliar with the Python web server used in this project. Here is what I understand and what I don't: (Please correct any mistakes)

  1. The web server is started by running server.py with arguments of "--servers /server/servers.json".
  2. The server begins listening on port 8080, but I don't know why. (Servers.json specifies port 5050, but netstat never shows anything listening on port 5050, and the code never mentions 8080) Is 5050 used somewhere?
  3. Demo begins by calling HTTP://localhost:8080 which serves up Index.html and client.js
  4. The user clicks the Start button which starts an RTC connection via POST localhost:8080/offer and connects the microphone audio to the RTC connection.
  5. On the server the Offer method connects the RTC connection and initializes the kaldi object with KaldiSink() from kaldi.py
  6. The KaldiSink.start(self) method is called. I don't understand what the self object is. Is it just a reference to an instance of the class or something else?
  7. Once the two-way connection is created, somehow the server audio input gets fed into the Kaldi engine, and the text output of the Kaldi gets fed back to the client. How does the webserver get an instance of the Kaldi engine? Where is it coming from file-wise? How is this instance wired up to the RTC connection?

Can anyone help fill in the blanks for me? I think I can handle re-writing it in asp.NET if I get understand what Python is doing here.
 

Vince-RA

Ars Praefectus
5,519
Subscriptor++
It looks to me like the only way to set the port is with a --port argument, which defaults to 8080 here if unspecified: https://github.com/danijel3/KaldiWebrtc ... ver.py#L82

I don't see anywhere that the port will be read in from the servers.json file - the web.run call explicitly references args.port: https://github.com/danijel3/KaldiWebrtc ... er.py#L100

It does, however, look like KaldiServer class will end up using the port in server.json: https://github.com/danijel3/KaldiWebrtc ... ldi.py#L92 -

Looks like the javascript calls the offer method in server.py? https://github.com/danijel3/KaldiWebrtc ... ent.js#L52

That's going to get a Kaldi server from the list defined in servers.json (listening on port 5050)

This is not a style of Python that I am comfortable with - but I think this is the gist of it. Client -> port 8080 -> webserver -> port 5050 -> Kaldi
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
Client -> port 8080 -> webserver -> port 5050 -> Kaldi
I think you're right here. I don't know why netstat -a doesn't show 5050 as listening, but that might not be important.

You did an awesome job of filling in a lot of the little details for me. Many thanks!

Now, the big issue still remains: How/where is the web server creating instances of Kaldi, and which version (file-wise) of Kaldi is getting used?
(FWIW, Kaldi is a C++ .dll that does the actual speech recognition, and different compiled versions can use different language models. Since I'll be using a custom compiled version of Kaldi for my project, I need to know how to get my version called, not just a default version.)

My guess is that it is getting loaded as a reference .dll somewhere, but I'm lost as to where that happens. Is it possibly defined in the Docker image?
 

Vince-RA

Ars Praefectus
5,519
Subscriptor++
That looks to be all handled via docker (or if you ran it outside docker, it seems to assume that Kaldi is already running on port 5050):

https://github.com/danijel3/KaldiWebrtc ... ose.yml#L4

https://hub.docker.com/r/danijel3/kaldi-online-tcp

I don't know exactly how that image is built, but this seems the most likely culprit:

https://github.com/danijel3/KaldiWebrtc ... Dockerfile

So, it will git pull from master and build latest, basically. Seems like it would be easy enough for you to create a docker image with your custom version and just drop it in the docker-compose.yml in place of this image.
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
That looks to be all handled via docker (or if you ran it outside docker, it seems to assume that Kaldi is already running on port 5050):

. . .

So, it will git pull from master and build latest, basically. Seems like it would be easy enough for you to create a docker image with your custom version and just drop it in the docker-compose.yml in place of this image.

Nice catch! Much appreciated. That shows how the Kaldi executable is sourced, and thus how it can be changed.

Now that leads to the big question of how does the Docker image wire up and spin up this executable into something listening on port 5050?
 

Vince-RA

Ars Praefectus
5,519
Subscriptor++
I skipped right past the readme in the docker subdir, which I think gives you what you want in the Making your own... section: https://github.com/danijel3/KaldiWebrtc ... /README.md

The danijel3/kaldi-online-tcp:aspire image is actually built from this Dockerfile: https://github.com/danijel3/KaldiWebrtc ... Dockerfile

The upstream source of that is this one I mentioned previously: https://github.com/danijel3/KaldiWebrtc ... Dockerfile

I'm not at all clear on how port 5050 gets wired up here - I would expect to see an "EXPOSE 5050/tcp" directive in at least one of those Dockerfiles...

The ENTRYPOINT directive tells it to listen on port 5050: https://github.com/danijel3/KaldiWebrtc ... kerfile#L8

...but that's not the same as telling docker to allow connections to the running container. Surely I am missing something here, but I don't see it :confused: Have you tried running this docker-compose.yml? Does it work?