Skip to content

Commit

Permalink
bugfix google stt and docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
dkammer committed Aug 12, 2024
1 parent b5cccb5 commit 9744ac3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM python:3
WORKDIR /app

RUN apt update -y && apt install ffmpeg -y
RUN apt-get install flac
RUN pip install numba
RUN pip install tiktoken
RUN pip install git+https://github.com/openai/whisper.git
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ npm start

Build and deploy the Python web server version with the provided Docker file using the following commands:

```bash
docker build . -t visualengineers/whisper-service
docker run --name=whisper-server -d -p=5002:80 --restart always visualengineers/whisper-service
```
$ docker build . -t visualengineers/whisper-service
$ docker run --name=whisper-server -d -p=5002:80 --restart always visualengineers/whisper-service

Alternatively use:

```bash
docker compose up --build
```

# Usage
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
flask:
container_name: whisperservice
# docker files cannot reference elements in parent directory, therefore context specifies the working directory
build: .
restart: always
ports:
- '5002:80'
26 changes: 16 additions & 10 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

whisperCommand = os.getenv('WHISPER_PATH')

def cleanUp():
# https://pynative.com/python-delete-files-and-directories/#h-delete-all-files-from-a-directory
for file_name in os.listdir(TMP_PATH):
# construct full file path
file = os.path.join(TMP_PATH, file_name)
if os.path.isfile(file) and file_name != '.gitkeep':
os.remove(file)

def saveFile(f):
letters = string.ascii_lowercase
filename = ''.join(random.choice(letters) for i in range(32))
Expand Down Expand Up @@ -51,6 +59,8 @@ def google():
return "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
return "Could not request results from Google Speech Recognition service; {0}".format(e)
finally:
cleanUp()
else:
return render_template('google.html')

Expand All @@ -70,11 +80,12 @@ def whisper():
lang = request.form['language'].strip()
if lang == '':
lang = 'de'
if not('audio' in f.mimetype):
return "Error Filetype"

file = saveFile(request.files['uploaded_file'])
resultFile = file + '.txt'
f = request.files['uploaded_file']
if not('audio' in f.mimetype):
return "Error Filetype"
file = saveFile(f)
resultFile = file.split('.')[0] + '.txt'

stream = os.popen(whisperCommand + ' ' + file + ' --model ' + model + ' --output_dir ' + TMP_PATH + ' --language ' + lang + ' --output_format txt' + ' --fp16 False')
output = stream.read()
Expand All @@ -87,12 +98,7 @@ def whisper():
print("An exception occured")
contents = 'Server Error'
finally:
# https://pynative.com/python-delete-files-and-directories/#h-delete-all-files-from-a-directory
for file_name in os.listdir(TMP_PATH):
# construct full file path
file = os.path.join(TMP_PATH, file_name)
if os.path.isfile(file) and file_name != '.gitkeep':
os.remove(file)
cleanUp()

return contents
else:
Expand Down

0 comments on commit 9744ac3

Please sign in to comment.