initial commit

This commit is contained in:
Moritz 2024-08-14 09:13:44 +02:00
commit 64ec45a9ce
5 changed files with 88 additions and 0 deletions

3
.env.example Normal file
View File

@ -0,0 +1,3 @@
HOMESERVER=https://matrix.example.com
USER=@whisper_bot:matrix.example.com
PASSWORD=super-secret-password

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.venv
.env

5
README.md Normal file
View File

@ -0,0 +1,5 @@
```
python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
```

34
requirements.txt Normal file
View File

@ -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

44
whisper.py Normal file
View File

@ -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())