Skip to content

通过 api 方式调用

MeetWq edited this page May 3, 2024 · 1 revision

如果是通过 pip 安装的 meme-generator,可以执行 meme run 启动 web 服务器

如果是以源码方式运行,可以运行 python -m meme_generator.app 启动 web 服务器

web 框架用的是 FastApi , 可查看自动生成的交互式 API 文档(访问 http://127.0.0.1:2233/docs

可以调用 api 接口使用,以下是简单的 python 调用示例:

import asyncio
import json

import httpx


async def main():
    files = [("images", open("avatar.jpg", "rb"))]
    texts = []
    args = {"circle": True}
    data = {"texts": texts, "args": json.dumps(args)}

    url = "http://127.0.0.1:2233/memes/petpet/"
    async with httpx.AsyncClient() as client:
        resp = await client.post(url, files=files, data=data)

    with open("result.gif", "wb") as f:
        f.write(resp.content)


if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    loop.run_until_complete(main())

更完整的使用方式可以参考 nonebot-plugin-memes-api