transcribe-bot/transcribebot.py

80 lines
3.7 KiB
Python
Raw Normal View History

2023-04-15 13:29:24 +00:00
import disnake
from disnake.ext import commands
from dotenv import load_dotenv
import os
2023-04-15 13:58:19 +00:00
from os import path
2023-04-16 17:30:12 +00:00
import asyncio
2023-04-15 13:58:19 +00:00
from pydub import AudioSegment
2023-06-19 17:10:17 +00:00
import deepspeech
import numpy as np
2023-04-15 13:58:19 +00:00
import speech_recognition as sr
2023-04-15 13:29:24 +00:00
2023-04-15 13:58:19 +00:00
st = sr.Recognizer()
2023-04-15 13:29:24 +00:00
load_dotenv()
bot = commands.Bot(command_prefix='.')
2023-04-16 00:00:56 +00:00
def prepaudio(audiofile):
oggfile = AudioSegment.from_ogg(audiofile)
2023-04-16 00:05:19 +00:00
oggfile.export("audio.wav", format="wav")
2023-04-16 00:00:56 +00:00
convertemessage = sr.AudioFile("audio.wav")
with convertemessage as sounds:
transcribeaudo = st.record(sounds)
return transcribeaudo
2023-04-16 16:34:58 +00:00
@bot.message_command(name="Transcribe Using Sphinx")
2023-04-16 00:00:56 +00:00
async def transcribesphinx(inter: disnake.ApplicationCommandInteraction, message: disnake.Message):
try:
await inter.response.defer(ephemeral='true')
await message.attachments[0].save("audio.ogg")
2023-06-19 17:10:17 +00:00
embed=disnake.Embed(title="Audio Transcription",description=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")
2023-04-16 17:21:06 +00:00
await inter.edit_original_message(embed=embed)
2023-04-16 00:00:56 +00:00
os.remove("audio.ogg")
os.remove("audio.wav")
except Exception as e:
await inter.edit_original_message(content=f'an error appears to have occoured please report it to the developer: {e}')
2023-04-16 16:34:58 +00:00
@bot.message_command(name="Transcribe Using Google")
2023-04-16 00:00:56 +00:00
async def transcribesphinx(inter: disnake.ApplicationCommandInteraction, message: disnake.Message):
2023-04-15 14:45:39 +00:00
try:
await inter.response.defer(ephemeral='true')
await message.attachments[0].save("audio.ogg")
2023-04-16 00:10:35 +00:00
# WARNING Google is propritary, consider disabling however sphynix is currently not very good so this provides an option
2023-06-19 17:10:17 +00:00
embed=disnake.Embed(title="Audio Transcription",description=st.recognize_google(prepaudio("audio.ogg")), color=0x3584e4)
2023-04-16 17:22:35 +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")
await inter.edit_original_message(embed=embed)
2023-04-16 17:30:12 +00:00
await asyncio.sleep(3)
2023-04-15 14:45:39 +00:00
os.remove("audio.ogg")
os.remove("audio.wav")
except Exception as e:
await inter.edit_original_message(content=f'an error appears to have occoured please report it to the developer: {e}')
2023-06-19 17:10:17 +00:00
@bot.message_command(name="Transcribe Using DeepSpeech")
2023-06-21 20:44:34 +00:00
async def transcribedeepspeech(inter: disnake.ApplicationCommandInteraction, message: disnake.Message):
2023-06-19 17:10:17 +00:00
try:
await inter.response.defer(ephemeral='true')
await message.attachments[0].save("audio.ogg")
audio = AudioSegment.from_file("audio.ogg", format="ogg")
audio.export("audio.wav", format="wav")
model = deepspeech.Model('deepspeech-0.9.3-models.pbmm')
model.enableExternalScorer('deepspeech-0.9.3-models.scorer')
model.setBeamWidth(500)
model.setScorerAlphaBeta(0.75, 1.85)
with open("audio.wav", 'rb') as f:
audio = np.frombuffer(f.read(), np.int16)
embed=disnake.Embed(title="Audio Transcription",description=model.stt(audio), 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 inter.edit_original_message(embed=embed)
await asyncio.sleep(3)
os.remove("audio.ogg")
os.remove("audio.wav")
except Exception as e:
await inter.edit_original_message(content=f'an error appears to have occoured please report it to the developer: {e}')
2023-04-15 13:29:24 +00:00
2023-06-21 20:44:34 +00:00
bot.run(os.getenv("TOKEN"))