This commit is contained in:
2024-09-18 00:24:53 +02:00
parent acfda1e29e
commit 7f57da86f0
+57 -28
View File
@@ -1,33 +1,54 @@
import logging
import wave
import assemblyai as aai import assemblyai as aai
import discord import discord
from discord.ext import commands, voice_recv from discord.ext import commands, voice_recv
import logging
from discord.opus import Decoder as OpusDecoder from discord.opus import Decoder as OpusDecoder
import wave
# Replace with your API key # Replace with your API key
aai.settings.api_key = "aa9962f0088a449a9c4ab2361e96cc08" aai.settings.api_key = "aa9962f0088a449a9c4ab2361e96cc08"
discord.opus._load_default() discord.opus._load_default()
# VoiceState
# self_mute=False
# self_deaf=False
# self_stream=False
# suppress=False
# requested_to_speak_at=None
# channel=<VoiceChannel id=1285599336318632036
# name='testing'
# rtc_region=None
# position=10
# bitrate=64000
# video_quality_mode=<VideoQualityMode.auto: 1>
# user_limit=0
# category_id=1084451744496496753
# URL of the file to transcribe # URL of the file to transcribe
# You can also transcribe a local file by passing in a file path # You can also transcribe a local file by passing in a file path
# FILE_URL = './path/to/file.mWp3 # FILE_URL = './path/to/file.mWp3
logger = logging.getLogger("discord") logger = logging.getLogger("discord")
location = "/home/pi/Conjurer/" location = "/home/pi/Conjurer/"
class Events(commands.Cog): class Events(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
async def on_voice_state_update(self, before, after, third): async def on_voice_state_update(self, user, before, after):
logger.info("self %s", self) if before.channel is None:
logger.info("before %s", before) logger.info("User %s connected to channel %s", user, after.channel.name)
logger.info("after %s", after) elif after.channel is None:
logger.info("third %s", third) logger.info("User %s disconnected from channel %s", user, before.channel.name)
else:
logger.info("User VC status changed %s", user)
logger.info("Before %s", before)
logger.info("After %s", after)
class UserTranscript(): class UserTranscript:
def __init__(self, user_id): def __init__(self, user_id):
self.username = str(user_id) self.username = str(user_id)
@@ -35,24 +56,34 @@ class UserTranscript():
self.file_name_past = None self.file_name_past = None
self.file_name_present = location + self.username + str(self.present_file_id) self.file_name_present = location + self.username + str(self.present_file_id)
self.transcript_file_present = wave.Wave_write = wave.open(self.file_name_present, 'wb') self.transcript_file_present = wave.Wave_write = wave.open(
self.file_name_present, "wb"
)
self.file_name_future = location + self.username + str(self.present_file_id + 1)
self.transcript_file_future = wave.Wave_write = wave.open(
self.file_name_future, "wb"
)
self.file_name_future = location + self.username + str(self.present_file_id+1)
self.transcript_file_future = wave.Wave_write = wave.open(self.file_name_future, 'wb')
def rotate(self): def rotate(self):
self.transcript_file_present.close() self.transcript_file_present.close()
self.name_file_past = self.file_name_present self.name_file_past = self.file_name_present
self.present_file_id += 1 self.present_file_id += 1
self.file_name_present = self.file_name_future self.file_name_present = self.file_name_future
self.transcript_file_present = self.transcript_file_future self.transcript_file_present = self.transcript_file_future
self.transcript_file_future = location + self.username + str(self.present_file_id+1) self.transcript_file_future = (
self.transcript_file_future = wave.Wave_write = wave.open(self.file_name_future, 'wb') location + self.username + str(self.present_file_id + 1)
#send past to transcription )
self.transcript_file_future = wave.Wave_write = wave.open(
self.file_name_future, "wb"
)
# send past to transcription
def cleanup(self): def cleanup(self):
self.transcript_file_present.close() self.transcript_file_present.close()
self.transcript_file_future.close() self.transcript_file_future.close()
#send present to transcription # send present to transcription
class SRBuffer(voice_recv.AudioSink): class SRBuffer(voice_recv.AudioSink):
"""Endpoint AudioSink that generates a wav file. """Endpoint AudioSink that generates a wav file.
@@ -63,14 +94,13 @@ class SRBuffer(voice_recv.AudioSink):
SAMPLE_WIDTH = OpusDecoder.SAMPLE_SIZE // OpusDecoder.CHANNELS SAMPLE_WIDTH = OpusDecoder.SAMPLE_SIZE // OpusDecoder.CHANNELS
SAMPLING_RATE = OpusDecoder.SAMPLING_RATE SAMPLING_RATE = OpusDecoder.SAMPLING_RATE
#on member join dodajemytypa do listy # on member join dodajemytypa do listy
#on member disconnect - dropujemy go # on member disconnect - dropujemy go
def __init__(self, destination): def __init__(self, destination):
super().__init__() super().__init__()
self._file: wave.Wave_write = wave.open(destination, 'wb') self._file: wave.Wave_write = wave.open(destination, "wb")
self._file.setnchannels(self.CHANNELS) self._file.setnchannels(self.CHANNELS)
self._file.setsampwidth(self.SAMPLE_WIDTH) self._file.setsampwidth(self.SAMPLE_WIDTH)
self._file.setframerate(self.SAMPLING_RATE) self._file.setframerate(self.SAMPLING_RATE)
@@ -101,12 +131,10 @@ class Transcriber(commands.Cog):
config = aai.TranscriptionConfig(speaker_labels=True, language_code="pl") config = aai.TranscriptionConfig(speaker_labels=True, language_code="pl")
transcriber = aai.Transcriber() transcriber = aai.Transcriber()
transcript = transcriber.transcribe( transcript = transcriber.transcribe(file_url, config=config)
file_url,
config=config
)
for utterance in transcript.utterances: for utterance in transcript.utterances:
print(f"Speaker {utterance.speaker}: {utterance.text}") print(f"Speaker {utterance.speaker}: {utterance.text}")
@commands.hybrid_command(name="transcribe") @commands.hybrid_command(name="transcribe")
async def test(self, ctx): async def test(self, ctx):
logger.info("Attempt transcribe") logger.info("Attempt transcribe")
@@ -114,14 +142,15 @@ class Transcriber(commands.Cog):
logger.info("Connected") logger.info("Connected")
vc.listen(self.wsink) vc.listen(self.wsink)
@commands.command(name="stop_transcribe") @commands.command(name="stop_transcribe")
async def stop(self, ctx): async def stop(self, ctx):
await ctx.voice_client.disconnect() await ctx.voice_client.disconnect()
async def setup(bot): async def setup(bot):
await bot.add_cog(Transcriber(bot)) await bot.add_cog(Transcriber(bot))
await bot.add_cog(Events(bot)) await bot.add_cog(Events(bot))
#1. zapisuj kwestie człowieka do pliku w którym będzie można stwierdzić kto co powiedział (callback z basicaudio + write z wavesinka). Zamykaj plik i wysyłaj do transkrypcji w momencie ciszy dłuższej niż 0.5s. Jeśli człowiek nadaje cały czas dawaj sygnał że nie można go transkrybować - i wyłaczaj zapis.
#2. Transkrypcja to abstrakt - w zależności od tego która metoda jest włączona wysyła do odpowiedniego silnika, silniki offline odpalam na activcomie. Zaczynamy od assemblyai bo najlepiej wspiera polski. Potem zobaczymy co dalej. # 1. zapisuj kwestie człowieka do pliku w którym będzie można stwierdzić kto co powiedział (callback z basicaudio + write z wavesinka). Zamykaj plik i wysyłaj do transkrypcji w momencie ciszy dłuższej niż 0.5s. Jeśli człowiek nadaje cały czas dawaj sygnał że nie można go transkrybować - i wyłaczaj zapis.
# 2. Transkrypcja to abstrakt - w zależności od tego która metoda jest włączona wysyła do odpowiedniego silnika, silniki offline odpalam na activcomie. Zaczynamy od assemblyai bo najlepiej wspiera polski. Potem zobaczymy co dalej.