yuccaaa commited on
Commit
20addd1
·
verified ·
1 Parent(s): 91ba179

Upload ms-swift/examples/deploy/client/mllm/openai_client.py with huggingface_hub

Browse files
ms-swift/examples/deploy/client/mllm/openai_client.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Alibaba, Inc. and its affiliates.
2
+ import os
3
+ from typing import Literal
4
+
5
+ from openai import OpenAI
6
+
7
+ os.environ['CUDA_VISIBLE_DEVICES'] = '0'
8
+
9
+
10
+ def infer(client, model: str, messages):
11
+ resp = client.chat.completions.create(model=model, messages=messages, max_tokens=512, temperature=0)
12
+ query = messages[0]['content']
13
+ response = resp.choices[0].message.content
14
+ print(f'query: {query}')
15
+ print(f'response: {response}')
16
+ return response
17
+
18
+
19
+ # streaming
20
+ def infer_stream(client, model: str, messages):
21
+ gen = client.chat.completions.create(model=model, messages=messages, stream=True, temperature=0)
22
+ print(f'messages: {messages}\nresponse: ', end='')
23
+ for chunk in gen:
24
+ print(chunk.choices[0].delta.content, end='', flush=True)
25
+ print()
26
+
27
+
28
+ def get_message(mm_type: Literal['text', 'image', 'video', 'audio']):
29
+ if mm_type == 'text':
30
+ message = {'role': 'user', 'content': 'who are you?'}
31
+ elif mm_type == 'image':
32
+ message = {
33
+ 'role':
34
+ 'user',
35
+ 'content': [{
36
+ 'type': 'image',
37
+ 'image': 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png'
38
+ }, {
39
+ 'type': 'text',
40
+ 'text': 'How many sheep are there in the picture?'
41
+ }]
42
+ }
43
+
44
+ elif mm_type == 'video':
45
+ # # use base64
46
+ # import base64
47
+ # with open('baby.mp4', 'rb') as f:
48
+ # vid_base64 = base64.b64encode(f.read()).decode('utf-8')
49
+ # video = f'data:video/mp4;base64,{vid_base64}'
50
+
51
+ # use url
52
+ video = 'https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/baby.mp4'
53
+ message = {
54
+ 'role': 'user',
55
+ 'content': [{
56
+ 'type': 'video',
57
+ 'video': video
58
+ }, {
59
+ 'type': 'text',
60
+ 'text': 'Describe this video.'
61
+ }]
62
+ }
63
+ elif mm_type == 'audio':
64
+ message = {
65
+ 'role':
66
+ 'user',
67
+ 'content': [{
68
+ 'type': 'audio',
69
+ 'audio': 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/weather.wav'
70
+ }, {
71
+ 'type': 'text',
72
+ 'text': 'What does this audio say?'
73
+ }]
74
+ }
75
+ return message
76
+
77
+
78
+ def run_client(host: str = '127.0.0.1', port: int = 8000):
79
+ client = OpenAI(
80
+ api_key='EMPTY',
81
+ base_url=f'http://{host}:{port}/v1',
82
+ )
83
+ model = client.models.list().data[0].id
84
+ print(f'model: {model}')
85
+
86
+ query = 'who are you?'
87
+ messages = [{'role': 'user', 'content': query}]
88
+ response = infer(client, model, messages)
89
+ messages.append({'role': 'assistant', 'content': response})
90
+ messages.append(get_message(mm_type='video'))
91
+ infer_stream(client, model, messages)
92
+
93
+
94
+ if __name__ == '__main__':
95
+ from swift.llm import run_deploy, DeployArguments
96
+ with run_deploy(DeployArguments(model='Qwen/Qwen2.5-VL-3B-Instruct', verbose=False, log_interval=-1)) as port:
97
+ run_client(port=port)