You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.3 KiB

import os
DEBUG = os.environ.get('DEBUG')
import logging
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s: - %(message)s',
level=logging.DEBUG if DEBUG else logging.INFO)
logging.getLogger('aiohttp').setLevel(logging.DEBUG if DEBUG else logging.WARNING)
import ffmpeg
import whisper
import time
import asyncio
from aiohttp import web, ClientSession, ClientError
import numpy as np
app = web.Application()
PORT = 3002
SAMPLE_RATE = 16000
logging.info('Loading whisper model...')
model = whisper.load_model('medium')
logging.info('Done.')
#start = time.time()
#result = model.transcribe('whisper-test.ogg')
#print('finished in', time.time() - start, 's')
#
#print(result['text'])
def load_audio(binary_file, sr = SAMPLE_RATE):
# stolen from https://github.com/ckaytev/tgisper/blob/main/tgisper/tgisperbot.py
try:
# This launches a subprocess to decode audio while down-mixing and
# resampling as necessary.
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
out, _ = (
ffmpeg.input("pipe:", threads=0)
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=sr)
.run(cmd="ffmpeg", capture_stdout=True, capture_stderr=True, input=binary_file)
)
except ffmpeg.Error as e:
raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0
async def index(request):
return web.Response(text='hello world', content_type='text/html')
async def post_whisper(request):
data = await request.post()
audio = load_audio(data['audio'].file.read())
logging.info('Starting audio transcription...')
result = model.transcribe(audio)
logging.info('Done.')
return web.json_response(result)
async def run_webserver():
logging.info('Starting webserver on port: %s', PORT)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '0.0.0.0', PORT)
await site.start()
while True:
await asyncio.sleep(10)
if __name__ == '__main__':
app.router.add_get('/', index)
app.router.add_post('/whisper', post_whisper)
loop = asyncio.get_event_loop()
a = loop.create_task(run_webserver())
loop.run_forever()