Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/whisper/transcription_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
import pyaudio
import numpy as np
import wave
import useful_transformers as rkut
import requests



CHANNELS = 1
FORMAT = pyaudio.paInt16
RATE = 16000
RECORD_SECONDS = 5
# CHUNK_SIZE = RATE * RECORD_SECONDS
CHUNK_SIZE = 1024

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK_SIZE)
print("recording...")
frames = []
fulldata = np.empty(RATE*RECORD_SECONDS*CHANNELS)

for i in range(0, int(RATE / CHUNK_SIZE * RECORD_SECONDS)):
data = stream.read(CHUNK_SIZE)
numpydata = np.frombuffer(data, dtype='int16')
frames.append(numpydata)
print("finished recording")

combin_array = np.concatenate(frames).tobytes()
url = "http://127.0.0.1:5000/api/transcription"
response = requests.post(url, data=combin_array)

# Check the response
if response.status_code == 200:
result = response.json()
print(result)
else:
print('Error:', response.status_code)

stream.stop_stream()
stream.close()
p.terminate()

23 changes: 23 additions & 0 deletions examples/whisper/transcription_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import Flask, request, jsonify
import numpy as np
from .whisper import decode_pcm

app = Flask(__name__)

@app.route('/api/transcription', methods=['POST'])
def consume_buffer():
# Receive the audio buffer as NumPy array from the request
array_data = np.frombuffer(request.data, dtype='int16')

# Decode the buffer
result = buffer_decode(array_data)

# Return the result as a JSON response
return jsonify(result=result)

def buffer_decode(array):
text = decode_pcm(array, "tiny.en")
return {'message': text}

if __name__ == '__main__':
app.run()