大模型实战营-初夏营-第六次课作业

这是大模型实战营-初夏营第六次课的作业

基础作业

笔记实践部分

进阶作业

AgentLego WebUI使用

我们将把基础作业中直接使用的目标检测模型部署成为网页demo

  1. 修改/root/agent/agentlego/webui/modules/agents/lagent_agent.py文件的第105行位置,将 internlm2-chat-20b 修改为 internlm2-chat-7b

  2. 运行如下命令

1
2
3
4
5
conda activate agent
lmdeploy serve api_server /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-7b \
--server-name 127.0.0.1 \
--model-name internlm2-chat-7b \
--cache-max-entry-count 0.1

部署InternLM2-Chat-7B推理服务

  1. 新建终端启动gradio界面:
1
2
3
conda activate agent
cd /root/agent/agentlego/webui
python one_click.py
  1. 配置Agent和目标检测工具

  1. 体验Agent

好像出幻觉了,这个图上摩托车检测出来并正确回答了,但是人物检测出来但是没有正确回答

用AgentLego自定义工具并调用

  1. 创建工具文件

我们要定义的工具是基于MagicMaker接口的文生图工具,工具定义如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import requests

import numpy as np

from agentlego.types import Annotated, ImageIO, Info
from agentlego.utils import require
from .base import BaseTool


class MagicMakerImageGeneration(BaseTool):

default_desc = ('This tool can call the api of magicmaker to '
'generate an image according to the given keywords.')

styles_option = [
'dongman', # 动漫
'guofeng', # 国风
'xieshi', # 写实
'youhua', # 油画
'manghe', # 盲盒
]
aspect_ratio_options = [
'16:9', '4:3', '3:2', '1:1',
'2:3', '3:4', '9:16'
]

@require('opencv-python')
def __init__(self,
style='guofeng',
aspect_ratio='4:3'):
super().__init__()
if style in self.styles_option:
self.style = style
else:
raise ValueError(f'The style must be one of {self.styles_option}')

if aspect_ratio in self.aspect_ratio_options:
self.aspect_ratio = aspect_ratio
else:
raise ValueError(f'The aspect ratio must be one of {aspect_ratio}')

def apply(self,
keywords: Annotated[str,
Info('A series of Chinese keywords separated by comma.')]
) -> ImageIO:
import cv2
response = requests.post(
url='https://magicmaker.openxlab.org.cn/gw/edit-anything/api/v1/bff/sd/generate',
data=json.dumps({
"official": True,
"prompt": keywords,
"style": self.style,
"poseT": False,
"aspectRatio": self.aspect_ratio
}),
headers={'content-type': 'application/json'}
)
image_url = response.json()['data']['imgUrl']
image_response = requests.get(image_url)
image = cv2.cvtColor(cv2.imdecode(np.frombuffer(image_response.content, np.uint8), cv2.IMREAD_COLOR),cv2.COLOR_BGR2RGB)
return ImageIO(image)
  1. 只需要在/root/agent/agentlego/agentlego/tools/__init__.py中将刚刚定义的工具import进来,并在__all__中加上工具名称即可

  2. 体验工具效果

后续步骤与上节一样,分别打开两个端口部署LLM推理服务以及gradio界面,之后进入Tools界面打开刚刚定义的工具即可。

测试如下:

prompt: 画一张二次元少女图

结果: image

只能说MagicMaker二次元浓度不够啊,国风倒是挺强的