From 64ec45a9ced2a78f048867a897a900fc7c759c04 Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 14 Aug 2024 09:13:44 +0200 Subject: [PATCH] initial commit --- .env.example | 3 +++ .gitignore | 2 ++ README.md | 5 +++++ requirements.txt | 34 ++++++++++++++++++++++++++++++++++ whisper.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 requirements.txt create mode 100644 whisper.py diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..fe47ab6 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +HOMESERVER=https://matrix.example.com +USER=@whisper_bot:matrix.example.com +PASSWORD=super-secret-password diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d19362 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +.env diff --git a/README.md b/README.md new file mode 100644 index 0000000..dfdf64f --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +``` +python3 -m venv .venv +source .venv/bin/activate +pip3 install -r requirements.txt +``` diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8890ad9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,34 @@ +aiofiles==23.2.1 +aiohappyeyeballs==2.3.5 +aiohttp==3.10.3 +aiohttp-socks==0.8.4 +aiosignal==1.3.1 +async-timeout==4.0.3 +atomicwrites==1.4.1 +attrs==24.2.0 +cachetools==4.2.4 +certifi==2024.7.4 +cffi==1.17.0 +charset-normalizer==3.3.2 +frozenlist==1.4.1 +h11==0.14.0 +h2==4.1.0 +hpack==4.0.0 +hyperframe==6.0.1 +idna==3.7 +jsonschema==4.23.0 +jsonschema-specifications==2023.12.1 +matrix-nio==0.24.0 +multidict==6.0.5 +peewee==3.17.6 +pycparser==2.22 +pycryptodome==3.20.0 +python-dotenv==1.0.1 +python-olm==3.2.16 +python-socks==2.5.0 +referencing==0.35.1 +requests==2.32.3 +rpds-py==0.20.0 +unpaddedbase64==2.1.0 +urllib3==2.2.2 +yarl==1.9.4 diff --git a/whisper.py b/whisper.py new file mode 100644 index 0000000..3aef652 --- /dev/null +++ b/whisper.py @@ -0,0 +1,44 @@ +from nio import AsyncClient, MatrixRoom, RoomMessageAudio +from dotenv import load_dotenv +import asyncio +import requests +import os + +def audio_callback_closure(client: AsyncClient): + async def audio_callback(room: MatrixRoom, event: RoomMessageAudio) -> None: + print( + f"Audio received in room {room.display_name}\n" + f"{room.user_name(event.sender)} | {event.body} | {event.url}" + ) + audio = await client.download(event.url) + files = {'audio_file': audio.body} + #with open("/tmp/audiofile",'bw') as file: + # file.write(audio.body) + response = requests.post('http://127.0.0.1:9000/asr?encode=true&task=transcribe&word_timestamps=false&output=txt', files=files) + room_id = room.room_id + await client.room_send( + # Watch out! If you join an old room you'll see lots of old messages + room_id=room_id, + message_type="m.room.message", + content={"msgtype": "m.text", "body": response.content.decode()}, + ) + return audio_callback + + +async def main() -> None: + load_dotenv() + homeserver = str(os.getenv("HOMESERVER")) + user = str(os.getenv("USER")) + password = str(os.getenv("PASSWORD")) + client = AsyncClient(homeserver, user) + client.add_event_callback(audio_callback_closure(client), RoomMessageAudio) + + print(await client.login(password)) + # "Logged in as @alice:example.org device id: RANDOMDID" + + await client.sync() + for room in client.invited_rooms: + await client.join(room) + await client.sync_forever(timeout=30000) # milliseconds + +asyncio.run(main())