From e3b2396f322607d7a43202c1b92a0fff5cb523c0 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 14 Sep 2024 19:59:30 +0800 Subject: [PATCH 1/2] feat: custom t2i tmpl --- util/t2i/renderer.py | 13 +++++++ util/t2i/strategies/base_strategy.py | 5 ++- util/t2i/strategies/local_strategy.py | 3 ++ util/t2i/strategies/network_strategy.py | 52 ++++++++++++------------- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/util/t2i/renderer.py b/util/t2i/renderer.py index bf141bce2..8142a147c 100644 --- a/util/t2i/renderer.py +++ b/util/t2i/renderer.py @@ -12,7 +12,20 @@ class TextToImageRenderer: self.local_strategy = LocalRenderStrategy() self.context = RenderContext(self.network_strategy) + async def render_custom_template(self, tmpl_str: str, tmpl_data: dict, return_url: bool = False): + '''使用自定义文转图模板。该方法会通过网络调用 t2i 终结点图文渲染API。 + @param tmpl_str: HTML Jinja2 模板。 + @param tmpl_data: jinja2 模板数据。 + + @return: 图片 URL 或者文件路径,取决于 return_url 参数。 + + @example: 参见 https://astrbot.soulter.top 插件开发部分。 + ''' + await self.network_strategy.render_custom_template(**locals()) + async def render(self, text: str, use_network: bool = True, return_url: bool = False): + '''使用默认文转图模板。 + ''' if use_network: try: return await self.context.render(text, return_url=return_url) diff --git a/util/t2i/strategies/base_strategy.py b/util/t2i/strategies/base_strategy.py index 35fbb1b26..67e843b7a 100644 --- a/util/t2i/strategies/base_strategy.py +++ b/util/t2i/strategies/base_strategy.py @@ -4,4 +4,7 @@ class RenderStrategy(ABC): @abstractmethod def render(self, text: str, return_url: bool) -> str: pass - \ No newline at end of file + + @abstractmethod + def render_custom_template(self, tmpl_str: str, tmpl_data: dict, return_url: bool) -> str: + pass \ No newline at end of file diff --git a/util/t2i/strategies/local_strategy.py b/util/t2i/strategies/local_strategy.py index 0a27601d2..061ddc726 100644 --- a/util/t2i/strategies/local_strategy.py +++ b/util/t2i/strategies/local_strategy.py @@ -6,6 +6,9 @@ from PIL import ImageFont, Image, ImageDraw from util.io import save_temp_img class LocalRenderStrategy(RenderStrategy): + + async def render_custom_template(self, tmpl_str: str, tmpl_data: dict, return_url: bool=True) -> str: + raise NotImplementedError def get_font(self, size: int) -> ImageFont.FreeTypeFont: # common and default fonts on Windows, macOS and Linux diff --git a/util/t2i/strategies/network_strategy.py b/util/t2i/strategies/network_strategy.py index b05022b3a..124c8bfe4 100644 --- a/util/t2i/strategies/network_strategy.py +++ b/util/t2i/strategies/network_strategy.py @@ -5,40 +5,40 @@ from .base_strategy import RenderStrategy from type.config import VERSION from util.io import download_image_by_url +ASTRBOT_T2I_DEFAULT_ENDPOINT = "https://t2i.soulter.top/text2img" + class NetworkRenderStrategy(RenderStrategy): - def __init__(self) -> None: - super().__init__() - self.BASE_RENDER_URL = "https://t2i.soulter.top/text2img" + def __init__(self, base_url: str = ASTRBOT_T2I_DEFAULT_ENDPOINT) -> None: + super().__init__() + self.BASE_RENDER_URL = base_url self.TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "template") + async def render_custom_template(self, tmpl_str: str, tmpl_data: dict, return_url: bool=True) -> str: + '''使用自定义文转图模板''' + post_data = { + "tmpl": tmpl_str, + "json": return_url, + "tmpldata": tmpl_data, + "options": { + "full_page": True, + "type": "jpeg", + "quality": 40, + } + } + if return_url: + async with aiohttp.ClientSession() as session: + async with session.post(f"{self.BASE_RENDER_URL}/generate", json=post_data) as resp: + ret = await resp.json() + return f"{self.BASE_RENDER_URL}/{ret['data']['id']}" + return await download_image_by_url(f"{self.BASE_RENDER_URL}/generate", post=True, post_data=post_data) + + async def render(self, text: str, return_url: bool=False) -> str: ''' 返回图像的文件路径 ''' with open(os.path.join(self.TEMPLATE_PATH, "base.html"), "r", encoding='utf-8') as f: tmpl_str = f.read() - assert(tmpl_str) - text = text.replace("`", "\`") - - post_data = { - "tmpl": tmpl_str, - "json": return_url, - "tmpldata": { - "text": text, - "version": f"v{VERSION}", - }, - "options": { - "full_page": True, - "type": "jpeg", - "quality": 40, - } - } - - if return_url: - async with aiohttp.ClientSession() as session: - async with session.post(f"{self.BASE_RENDER_URL}/generate", json=post_data) as resp: - ret = await resp.json() - return f"{self.BASE_RENDER_URL}/{ret['data']['id']}" - return await download_image_by_url(f"{self.BASE_RENDER_URL}/generate", post=True, post_data=post_data) + return await self.render_custom_template(tmpl_str, {"text": text, "version": f"v{VERSION}"}, return_url) \ No newline at end of file From 5f0f5398e8e8d4e5edeb96c8910c84503c0c11d3 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 14 Sep 2024 08:21:34 -0400 Subject: [PATCH 2/2] fix: custom t2i --- util/t2i/renderer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/util/t2i/renderer.py b/util/t2i/renderer.py index 8142a147c..d4711a795 100644 --- a/util/t2i/renderer.py +++ b/util/t2i/renderer.py @@ -21,7 +21,9 @@ class TextToImageRenderer: @example: 参见 https://astrbot.soulter.top 插件开发部分。 ''' - await self.network_strategy.render_custom_template(**locals()) + local = locals() + local.pop('self') + return await self.network_strategy.render_custom_template(**local) async def render(self, text: str, use_network: bool = True, return_url: bool = False): '''使用默认文转图模板。