insert2-cogs/transcription.py

57 lines
2.6 KiB
Python
Raw Permalink Normal View History

2024-08-07 18:44:50 +00:00
import nextcord
from nextcord.ext import commands
2024-08-05 19:12:11 +00:00
import os
from os import path
import asyncio
from pydub import AudioSegment
import numpy as np
import speech_recognition as sr
def prepaudio(audiofile):
st = sr.Recognizer()
oggfile = AudioSegment.from_ogg(audiofile)
oggfile.export("audio.wav", format="wav")
convertemessage = sr.AudioFile("audio.wav")
with convertemessage as sounds:
transcribeaudo = st.record(sounds)
return transcribeaudo
class transcription(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.st = sr.Recognizer()
@nextcord.message_command(name="Transcribe Using Sphinx")
async def transcribesphinx(self, interaction: nextcord.Interaction, message: nextcord.Message):
try:
await interaction.response.defer(ephemeral='true')
await message.attachments[0].save("audio.ogg")
embed=nextcord.Embed(title="Audio Transcription",description=self.st.recognize_sphinx(prepaudio("audio.ogg")), color=0x3584e4)
embed.set_author(name=message.author.display_name, url=message.jump_url, icon_url=message.author.display_avatar)
embed.set_footer(text="Accuracy not guaranteed")
await interaction.edit_original_message(embed=embed)
os.remove("audio.ogg")
os.remove("audio.wav")
except Exception as e:
await interaction.edit_original_message(content=f'an error appears to have occoured please report it to the developer: {e}')
2024-08-05 19:12:11 +00:00
2024-08-07 18:44:50 +00:00
@nextcord.message_command(name="Transcribe Using Google")
async def transcribegoogle(self, interaction: nextcord.Interaction, message: nextcord.Message):
2024-08-05 19:12:11 +00:00
try:
2024-08-07 18:44:50 +00:00
await interaction.response.defer(ephemeral='true')
2024-08-05 19:12:11 +00:00
await message.attachments[0].save("audio.ogg")
# WARNING Google is propritary, consider disabling however sphynix is currently not very good so this provides an option
2024-08-07 18:44:50 +00:00
embed=nextcord.Embed(title="Audio Transcription",description=self.st.recognize_google(prepaudio("audio.ogg")), color=0x3584e4)
2024-08-05 19:12:11 +00:00
embed.set_author(name=message.author.display_name, url=message.jump_url, icon_url=message.author.display_avatar)
embed.set_footer(text="Accuracy not guaranteed")
2024-08-07 18:44:50 +00:00
await interaction.edit_original_message(embed=embed)
2024-08-05 19:12:11 +00:00
await asyncio.sleep(3)
os.remove("audio.ogg")
os.remove("audio.wav")
except Exception as e:
2024-08-07 18:44:50 +00:00
await interaction.edit_original_message(content=f'an error appears to have occoured please report it to the developer: {e}')
2024-08-05 19:12:11 +00:00
def setup(bot: commands.Bot):
bot.add_cog(transcription(bot))