forked from InternLM/agentlego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
175 lines (154 loc) · 4.91 KB
/
server.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import argparse
import base64
import inspect
from io import BytesIO
from typing import Dict
from urllib.parse import quote_plus
import uvicorn
from fastapi import APIRouter, FastAPI, File, Form, UploadFile
from typing_extensions import Annotated
from agentlego.apis import load_tool
from agentlego.parsers import NaiveParser
from agentlego.tools.base import BaseTool
from agentlego.types import AudioIO, CatgoryToIO, ImageIO
prog_description = """\
Start a server for several tools.
"""
def parse_args():
parser = argparse.ArgumentParser(description=prog_description)
parser.add_argument(
'tools',
type=str,
nargs='+',
help='The tools to deploy',
)
parser.add_argument(
'--port',
default=16180,
type=int,
help='The port number',
)
parser.add_argument(
'--device',
default='cuda:0',
type=str,
help='The device to deploy the tools',
)
parser.add_argument(
'--no-setup',
action='store_true',
help='Avoid setup tools during starting the server.',
)
args = parser.parse_args()
return args
args = parse_args()
tools: Dict[str, BaseTool] = {}
for name in args.tools:
tool = load_tool(name, device=args.device, parser=NaiveParser)
if not args.no_setup:
tool.setup()
tool._is_setup = True
tools[quote_plus(tool.name.replace(' ', ''))] = tool
app = FastAPI()
tool_router = APIRouter()
@app.get('/')
def index():
response = []
for tool_name, tool in tools.items():
response.append(
dict(
domain=tool_name,
toolmeta=tool.toolmeta.__dict__,
parameters=[p.__dict__ for p in tool.parameters.values()],
))
return response
def add_tool(tool_name: str):
tool: BaseTool = tools[tool_name]
def _call(**kwargs):
args = {}
for p in tool.parameters.values():
data = kwargs[p.name]
if p.category == 'image':
from PIL import Image
data = ImageIO(Image.open(data.file))
elif p.category == 'audio':
import torchaudio
file_format = data.filename.rpartition('.')[-1] or None
raw, sr = torchaudio.load(data.file, format=file_format)
data = AudioIO(raw, sampling_rate=sr)
else:
data = CatgoryToIO[p.category](data)
args[p.name] = data
outs = tool(**args)
if not isinstance(outs, tuple):
outs = [outs]
res = []
for out, out_category in zip(outs, tool.toolmeta.outputs):
if out_category == 'image':
file = BytesIO()
out.to_pil().save(file, format='png')
res.append(
dict(
type='image',
data=base64.encodebytes(
file.getvalue()).decode('ascii'),
))
elif out_category == 'audio':
import torchaudio
file = BytesIO()
torchaudio.save(
file, out.to_tensor(), out.sampling_rate, format='wav')
res.append(
dict(
type='audio',
data=base64.encodebytes(
file.getvalue()).decode('ascii'),
))
else:
res.append(out)
return res
def call(**kwargs):
try:
return _call(**kwargs)
except Exception as e:
return dict(error=repr(e))
call_args = {}
call_params = []
for p in tool.parameters.values():
if p.category in ['image', 'audio']:
annotation = Annotated[UploadFile, File(media_type=p.category)]
else:
type_ = {
'text': str,
'int': int,
'bool': bool,
'float': float
}[p.category]
annotation = Annotated[type_, Form()]
call_args[p.name] = annotation
call_params.append(
inspect.Parameter(
p.name,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
default=p.default if p.optional else inspect._empty,
annotation=annotation,
))
call.__signature__ = inspect.Signature(call_params)
call.__annotations__ = call_args
tool_router.add_api_route(
f'/{tool_name}/call',
endpoint=call,
methods=['POST'],
)
tool_router.add_api_route(
f'/{tool_name}/meta',
endpoint=lambda: dict(
toolmeta=tool.toolmeta.__dict__,
parameters=[p.__dict__ for p in tool.parameters.values()]),
methods=['GET'],
)
for tool_name in tools:
add_tool(tool_name)
app.include_router(tool_router)
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=args.port)