mirror of
https://github.com/osmarks/autobotrobot
synced 2025-05-30 05:04:10 +00:00
Meme search API changes
This commit is contained in:
parent
6e30123e56
commit
d64e7895f0
@ -232,15 +232,14 @@ AutoBotRobot is operated by gollark/osmarks.
|
|||||||
search_many = ctx.invoked_with == "memes"
|
search_many = ctx.invoked_with == "memes"
|
||||||
raw_memes = await util.user_config_lookup(ctx, "enable_raw_memes") == "true"
|
raw_memes = await util.user_config_lookup(ctx, "enable_raw_memes") == "true"
|
||||||
async with self.session.post(util.config["memetics"]["meme_search_backend"], json={
|
async with self.session.post(util.config["memetics"]["meme_search_backend"], json={
|
||||||
"text": [[query, 1]],
|
"terms": [{"text": query, "weight": 1}],
|
||||||
"top_k": 4 if search_many else 1
|
"k": 4 if search_many else 1
|
||||||
}) as res:
|
}) as res:
|
||||||
results = await res.json()
|
results = await res.json()
|
||||||
files = [ x["file"] for x in results ]
|
|
||||||
if raw_memes:
|
if raw_memes:
|
||||||
o_files = [ discord.File(Path(util.config["memetics"]["memes_local"]) / Path(util.config["memetics"]["meme_base"]) / f) for f in files ]
|
o_files = [ discord.File(Path(util.config["memetics"]["memes_local"]) / Path(util.config["memetics"]["meme_base"]) / m[1]) for m in results["matches"] ]
|
||||||
else:
|
else:
|
||||||
o_files = [ discord.File(Path(util.config["memetics"]["memes_local"]) / Path(util.config["memetics"]["thumb_base"]) / util.meme_thumbnail(f)) for f in files ]
|
o_files = [ discord.File(Path(util.config["memetics"]["memes_local"]) / util.meme_thumbnail(results, m)) for m in results["matches"] ]
|
||||||
await ctx.send(files=o_files)
|
await ctx.send(files=o_files)
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
21
src/util.py
21
src/util.py
@ -17,6 +17,7 @@ import pytz
|
|||||||
import collections
|
import collections
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import string
|
import string
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
config = {}
|
config = {}
|
||||||
|
|
||||||
@ -341,12 +342,26 @@ async def generate(response: aiohttp.ClientSession, prompt):
|
|||||||
async with response.post(config["ai"]["llm_backend"], json={
|
async with response.post(config["ai"]["llm_backend"], json={
|
||||||
"prompt": prompt,
|
"prompt": prompt,
|
||||||
"max_tokens": 200,
|
"max_tokens": 200,
|
||||||
"stop": ["\n"]
|
"stop": ["\n"],
|
||||||
|
"client": "abr"
|
||||||
}) as res:
|
}) as res:
|
||||||
data = await res.json()
|
data = await res.json()
|
||||||
return data["choices"][0]["text"]
|
return data["choices"][0]["text"]
|
||||||
|
|
||||||
filesafe_charset = string.ascii_letters + string.digits + "-"
|
filesafe_charset = string.ascii_letters + string.digits + "-"
|
||||||
|
|
||||||
def meme_thumbnail(original):
|
TARGET_FORMAT = "jpegh"
|
||||||
return ''.join([ i if i in filesafe_charset else '_' for i in (config["memetics"]["meme_base"] + "/" + original) ]) + "." + config["memetics"]["target_format"] + config["memetics"]["target_format_ext"]
|
def meme_thumbnail(results, result):
|
||||||
|
try:
|
||||||
|
format_id = results["formats"].index(TARGET_FORMAT)
|
||||||
|
except ValueError:
|
||||||
|
format_id = None
|
||||||
|
|
||||||
|
if not format_id:
|
||||||
|
return Path(config["memetics"]["meme_base"]) / result[1]
|
||||||
|
else:
|
||||||
|
format_id = 1 << format_id
|
||||||
|
if result[3] & format_id != 0:
|
||||||
|
return Path(config["memetics"]["thumb_base"]) / f"{result[2]}{TARGET_FORMAT}.{results['extensions'][TARGET_FORMAT]}"
|
||||||
|
else:
|
||||||
|
return Path(config["memetics"]["meme_base"]) / result[1]
|
Loading…
x
Reference in New Issue
Block a user