Kaveen Kumarasinghe 1 year ago
commit 6c4438de45

@ -1051,7 +1051,9 @@ class Commands(discord.Cog, name="Commands"):
@add_to_group("transcribe")
@discord.slash_command(
name="link", description="Transcribe a file link or youtube link", guild_ids=ALLOWED_GUILDS
name="link",
description="Transcribe a file link or youtube link",
guild_ids=ALLOWED_GUILDS,
)
@discord.guild_only()
@discord.option(
@ -1069,6 +1071,6 @@ class Commands(discord.Cog, name="Commands"):
min_value=0,
)
async def transcribe_link(
self, ctx: discord.ApplicationContext, link: str, temperature: float
self, ctx: discord.ApplicationContext, link: str, temperature: float
):
await self.transcribe_cog.transcribe_link_command(ctx, link, temperature)

@ -33,7 +33,10 @@ class TranscribeService(discord.Cog, name="TranscribeService"):
self.usage_service = usage_service
# Make the "audiotemp" folder if it doesn't exist, using pathlib
Path("audiotemp").mkdir(parents=True, exist_ok=True)
async def transcribe_link_command(self, ctx: discord.ApplicationContext, link:str, temperature: float):
async def transcribe_link_command(
self, ctx: discord.ApplicationContext, link: str, temperature: float
):
# Check if this discord file is an instance of mp3, mp4, mpeg, mpga, m4a, wav, or webm.
await ctx.defer()
@ -54,33 +57,53 @@ class TranscribeService(discord.Cog, name="TranscribeService"):
Path("audiotemp/{}temp.mp3".format(str(ctx.user.id))).unlink()
print("before call")
try:
file_path = await asyncio.get_running_loop().run_in_executor(None, partial(yt.streams.filter().first().download, output_path="audiotemp", filename="{}temp".format(str(ctx.user.id))))
file_path = await asyncio.get_running_loop().run_in_executor(
None,
partial(
yt.streams.filter().first().download,
output_path="audiotemp",
filename="{}temp".format(str(ctx.user.id)),
),
)
except Exception as e:
traceback.print_exc()
await ctx.respond("Failed to download youtube video. Please try again later. "+str(e))
await ctx.respond(
"Failed to download youtube video. Please try again later. "
+ str(e)
)
return
print("after call the file path was" + file_path)
else:
await ctx.respond("Please upload a valid youtube link. Other links are not implemented yet")
await ctx.respond(
"Please upload a valid youtube link. Other links are not implemented yet"
)
return
# Load the file object from the file_path
file = discord.File(file_path)
response_message = await ctx.respond(embed=EmbedStatics.build_transcribe_progress_embed())
response_message = await ctx.respond(
embed=EmbedStatics.build_transcribe_progress_embed()
)
try:
response = await self.model.send_transcription_request(file, temperature, user_api_key)
response = await self.model.send_transcription_request(
file, temperature, user_api_key
)
print(response)
if len(response) > 4080:
# Chunk the response into 2048 character chunks, each an embed page
chunks = [response[i:i+2048] for i in range(0, len(response), 2048)]
chunks = [response[i : i + 2048] for i in range(0, len(response), 2048)]
embed_pages = []
for chunk in chunks:
embed_pages.append(discord.Embed(title="Transcription Page {}".format(len(embed_pages) + 1), description=chunk))
embed_pages.append(
discord.Embed(
title="Transcription Page {}".format(len(embed_pages) + 1),
description=chunk,
)
)
paginator = pages.Paginator(
pages=embed_pages,
@ -140,8 +163,12 @@ class TranscribeService(discord.Cog, name="TranscribeService"):
chunks = [response[i : i + 2048] for i in range(0, len(response), 2048)]
embed_pages = []
for chunk in chunks:
embed_pages.append(discord.Embed(title="Transcription Page {}".format(len(embed_pages) + 1), description=chunk))
embed_pages.append(
discord.Embed(
title="Transcription Page {}".format(len(embed_pages) + 1),
description=chunk,
)
)
paginator = pages.Paginator(
pages=embed_pages,

@ -923,14 +923,27 @@ class Model:
max_tries=4,
on_backoff=backoff_handler_request,
)
async def send_transcription_request(self, file: [discord.Attachment, discord.File], temperature_override=None, custom_api_key=None, ):
async def send_transcription_request(
self,
file: [discord.Attachment, discord.File],
temperature_override=None,
custom_api_key=None,
):
async with aiohttp.ClientSession(raise_for_status=True) as session:
data = aiohttp.FormData()
data.add_field("model", "whisper-1")
print("audio."+file.filename.split(".")[-1])
print("audio." + file.filename.split(".")[-1])
data.add_field(
"file", await file.read() if isinstance(file, discord.Attachment) else await file.fp.read(), filename="audio."+file.filename.split(".")[-1] if isinstance(file, discord.Attachment) else "audio.mp4", content_type=file.content_type if isinstance(file, discord.Attachment) else "video/mp4"
"file",
await file.read()
if isinstance(file, discord.Attachment)
else await file.fp.read(),
filename="audio." + file.filename.split(".")[-1]
if isinstance(file, discord.Attachment)
else "audio.mp4",
content_type=file.content_type
if isinstance(file, discord.Attachment)
else "video/mp4",
)
if temperature_override:

Loading…
Cancel
Save