mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-19 18:16:34 +01:00
Handle caching
This commit is contained in:
parent
222f75db3b
commit
248d300c47
1 changed files with 33 additions and 21 deletions
|
@ -13,6 +13,7 @@ import os
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
|
import hashlib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -2457,6 +2458,12 @@ class OtherCog(commands.Cog):
|
||||||
return await ctx.respond("No voice messages.")
|
return await ctx.respond("No voice messages.")
|
||||||
if getattr(config, "OPENAI_KEY", None) is None:
|
if getattr(config, "OPENAI_KEY", None) is None:
|
||||||
return await ctx.respond("Service unavailable.")
|
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)
|
client = openai.OpenAI(api_key=config.OPENAI_KEY)
|
||||||
with tempfile.NamedTemporaryFile("wb+", suffix=".mp4") as f:
|
with tempfile.NamedTemporaryFile("wb+", suffix=".mp4") as f:
|
||||||
with tempfile.NamedTemporaryFile("wb+", suffix="-" + attachment.filename) as f2:
|
with tempfile.NamedTemporaryFile("wb+", suffix="-" + attachment.filename) as f2:
|
||||||
|
@ -2474,8 +2481,13 @@ class OtherCog(commands.Cog):
|
||||||
file=pathlib.Path(f.name),
|
file=pathlib.Path(f.name),
|
||||||
model="whisper-1"
|
model="whisper-1"
|
||||||
)
|
)
|
||||||
|
text = transcript.text
|
||||||
|
cache.write_text(text)
|
||||||
|
else:
|
||||||
|
text = cache.read_text()
|
||||||
|
|
||||||
paginator = commands.Paginator("", "", 4096)
|
paginator = commands.Paginator("", "", 4096)
|
||||||
for line in transcript.text.splitlines():
|
for line in text.splitlines():
|
||||||
paginator.add_line(textwrap.shorten(line, 4096))
|
paginator.add_line(textwrap.shorten(line, 4096))
|
||||||
embeds = list(map(lambda p: discord.Embed(description=p), paginator.pages))
|
embeds = list(map(lambda p: discord.Embed(description=p), paginator.pages))
|
||||||
return await ctx.respond(embeds=embeds or [discord.Embed(description="No text found.")])
|
return await ctx.respond(embeds=embeds or [discord.Embed(description="No text found.")])
|
||||||
|
|
Loading…
Reference in a new issue