diff --git a/examples/whisper/transcription_client.py b/examples/whisper/transcription_client.py new file mode 100644 index 0000000..4cbe2c5 --- /dev/null +++ b/examples/whisper/transcription_client.py @@ -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() + diff --git a/examples/whisper/transcription_server.py b/examples/whisper/transcription_server.py new file mode 100644 index 0000000..f17d2f0 --- /dev/null +++ b/examples/whisper/transcription_server.py @@ -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()