Handle caching

This commit is contained in:
Nexus 2023-12-03 22:49:34 +00:00
parent 222f75db3b
commit 248d300c47
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -13,6 +13,7 @@ import os
import random
import re
import shutil
import hashlib
import subprocess
import sys
import tempfile
@ -2457,6 +2458,12 @@ class OtherCog(commands.Cog):
return await ctx.respond("No voice messages.")
if getattr(config, "OPENAI_KEY", None) is None:
return await ctx.respond("Service unavailable.")
file_hash = hashlib.sha1(usedforsecurity=False)
file_hash.update(await attachment.read())
file_hash = file_hash.hexdigest()
cache = Path.home() / ".cache" / "lcc-bot" / ("%s-transcript.txt" % file_hash)
if not cache.exists():
client = openai.OpenAI(api_key=config.OPENAI_KEY)
with tempfile.NamedTemporaryFile("wb+", suffix=".mp4") as f:
with tempfile.NamedTemporaryFile("wb+", suffix="-" + attachment.filename) as f2:
@ -2474,8 +2481,13 @@ class OtherCog(commands.Cog):
file=pathlib.Path(f.name),
model="whisper-1"
)
text = transcript.text
cache.write_text(text)
else:
text = cache.read_text()
paginator = commands.Paginator("", "", 4096)
for line in transcript.text.splitlines():
for line in text.splitlines():
paginator.add_line(textwrap.shorten(line, 4096))
embeds = list(map(lambda p: discord.Embed(description=p), paginator.pages))
return await ctx.respond(embeds=embeds or [discord.Embed(description="No text found.")])