This commit is contained in:
insert 2024-08-12 08:41:53 -04:00
parent 114fddadf1
commit 5feb9e0adc
Signed by: insert
GPG key ID: A70775C389ACF105

View file

@ -6,6 +6,7 @@ import asyncio
from pydub import AudioSegment from pydub import AudioSegment
import numpy as np import numpy as np
import speech_recognition as sr import speech_recognition as sr
import whisper
def prepaudio(audiofile): def prepaudio(audiofile):
st = sr.Recognizer() st = sr.Recognizer()
@ -21,21 +22,21 @@ class transcription(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
self.st = sr.Recognizer() self.st = sr.Recognizer()
self.model = whisper.load_model("base")
@nextcord.message_command(name="Transcribe Using Sphinx") @nextcord.message_command(name="Transcribe Using Whisper")
async def transcribesphinx(self, interaction: nextcord.Interaction, message: nextcord.Message): async def transcribewhisper(self, interaction: nextcord.Interaction, message: nextcord.Message):
try: await interaction.response.defer(ephemeral='true')
await interaction.response.defer(ephemeral='true') await message.attachments[0].save("audio.ogg")
await message.attachments[0].save("audio.ogg") oggfile = AudioSegment.from_ogg("audio.ogg")
embed=nextcord.Embed(title="Audio Transcription",description=self.st.recognize_sphinx(prepaudio("audio.ogg")), color=0x3584e4) oggfile.export("audio.mp3", format="mp3")
embed.set_author(name=message.author.display_name, url=message.jump_url, icon_url=message.author.display_avatar) embed=nextcord.Embed(title="Audio Transcription",description=self.model.transcribe("audio.mp3")["text"], color=0x3584e4)
embed.set_footer(text="Accuracy not guaranteed") embed.set_author(name=message.author.display_name, url=message.jump_url, icon_url=message.author.display_avatar)
await interaction.edit_original_message(embed=embed) embed.set_footer(text="Accuracy not guaranteed")
os.remove("audio.ogg") await interaction.edit_original_message(embed=embed)
os.remove("audio.wav") os.remove("audio.ogg")
except Exception as e: os.remove("audio.mp3")
await interaction.edit_original_message(content=f'an error appears to have occoured please report it to the developer: {e}')
@nextcord.message_command(name="Transcribe Using Google") @nextcord.message_command(name="Transcribe Using Google")
async def transcribegoogle(self, interaction: nextcord.Interaction, message: nextcord.Message): async def transcribegoogle(self, interaction: nextcord.Interaction, message: nextcord.Message):