whisper-bot/whisper.py

45 lines
1.6 KiB
Python
Raw Normal View History

2024-08-14 09:13:44 +02:00
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(
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)
2024-08-14 09:31:02 +02:00
# Watch out! If you join an old room you'll see lots of old messages
2024-08-14 09:13:44 +02:00
await client.sync_forever(timeout=30000) # milliseconds
asyncio.run(main())