File size: 11,367 Bytes
1625ded
 
 
 
 
 
 
 
 
 
 
 
 
 
1297fe3
1625ded
 
 
 
baae5d7
1625ded
2fe9021
1625ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright (c) Alibaba, Inc. and its affiliates.
import os

import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms
from PIL import Image
import secrets
import tempfile
from http import HTTPStatus
from urllib3.exceptions import HTTPError

from pathlib import Path

#os.environ['DASHSCOPE_HTTP_BASE_URL'] = 'https://dashscope.aliyuncs.com/api/v1'

import dashscope
from dashscope import MultiModalConversation
API_KEY = os.environ['API_KEY']
BASE_URL = os.environ['DASHSCOPE_HTTP_BASE_URL']
dashscope.api_key = API_KEY
dashscope.base_http_api_url = 'https://ga-lmsys-dashscope.aliyuncs.com/api/v1'

is_modelscope_studio = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'

def get_text(text: str, cn_text: str):
    if is_modelscope_studio:
        return cn_text
    return text

def resolve_image(filename):
    return os.path.join(os.path.dirname(__file__), filename)

DEMO_LIST = [
  {
    "description": "Evaluate the integral of the functions graphed using the formula for circles: ",
    "image": resolve_image("./examples/1.webp")
  },
  {
    "description": "回答图中问题",
    "image": resolve_image("./examples/2.png")
  },
  {
    "description": "图片中的滤液E是什么化学物质?",
    "image": resolve_image("./examples/3.png")
  },
  {
    "description": "I want to know the volume of this sofa",
    "image": resolve_image("./examples/4.png")
  },
]

def process_image(image, shouldConvert=False):
    # 获取上传文件的目录
    uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
        Path(tempfile.gettempdir()) / "gradio")
    os.makedirs(uploaded_file_dir, exist_ok=True)

    # 创建临时文件路径
    name = f"tmp{secrets.token_hex(20)}.jpg"
    filename = os.path.join(uploaded_file_dir, name)
    # 保存上传的图片
    if shouldConvert:
        new_img = Image.new('RGB',
                            size=(image.width, image.height),
                            color=(255, 255, 255))
        new_img.paste(image, (0, 0), mask=image)
        image = new_img
    image.save(filename)

    return filename



def generate(image, query):
    imageFile = process_image(image)
    content = [
        {'image': f'file://{imageFile}'},
        {'text': query}
    ]
    messages = [
        {'role': 'user', 'content': content},
    ]
    print('messages:', messages)
    responses = MultiModalConversation.call(
        model='qvq-72b-preview', messages=messages, stream=True,
    )
    for response in responses:
        if not response.status_code == HTTPStatus.OK:
            raise HTTPError(f'response.code: {response.code}\nresponse.message: {response.message}')
        response = response.output.choices[0].message.content
        if len(response) > 0 and response[0]['text']:
            print(response[0]['text'])
            yield response[0]['text']


if __name__ == "__main__":

    def on_clear():
        return {
            input: gr.update(value=None),
            **{
                item: gr.update(value=None)
                for item in input_image
            },
        }

    with gr.Blocks() as demo:
        with ms.Application() as app:
            with antd.ConfigProvider(
                    locale="zh_CN" if is_modelscope_studio else None,
                    theme=dict(token=dict(colorPrimary="#a855f7"))):
                with antd.Card(elem_style=dict(marginBottom=12),
                               styles=dict(body=dict(padding=4))):
                    with antd.Flex(elem_style=dict(width="100%"),
                                   justify="center",
                                   align="center",
                                   gap=14):
                        with ms.Div(elem_style=dict(flexShrink=0)):
                            antd.Image(
                                resolve_image("./cutelogo.jpg"),
                                preview=False,
                                height=60)
                        with ms.Div():
                            antd.Typography.Title(
                                "QVQ-72B-Preview",
                                elem_style=dict(margin=0, fontSize=24),
                                level=1)
                with ms.AutoLoading():
                    with antd.Row(gutter=[8, 8], align="stretch"):
                        with antd.Col(xs=24, md=8):
                            with antd.Space(direction="vertical",
                                            elem_style=dict(width="100%")):
                                with antd.Space(direction="vertical",
                                                elem_style=dict(width="100%"),
                                                elem_id="input-container"):
                                    with ms.Fragment():
                                        input_image = gr.Image(
                                                    type="pil",
                                                    label="Upload",
                                                    sources=["upload"]),
                                    input = antd.Input.Textarea(
                                        placeholder=get_text("Ask a question", "输入一个问题"),
                                        auto_size=dict(maxRows=6, minRows=2),
                                        allow_clear=True)

                                with antd.Flex(align="center",
                                               justify="space-between"):
                                    antd.Typography.Text(
                                        get_text("Warning: This model only supports single-turn dialogue.",  "注:当前模型只支持单轮对话,如需中文回答,提示词加“用中文回答”"), type="warning")
                                    tour_btn = antd.Button(get_text("Tour", "使用指引"),
                                                           variant="filled",
                                                           color="default")

                                with antd.Row(gutter=8):
                                    with antd.Col(span=12):
                                        clear_btn = antd.Button(get_text("Clear", "清除"),
                                                                block=True)
                                    with antd.Col(span=12):
                                        submit_btn = antd.Button(
                                            get_text("Submit", "提交"),
                                            type="primary",
                                            block=True,
                                            elem_id="submit-btn")

                                antd.Divider(get_text("Example", "示例"))

                                with antd.Flex(gap="small", wrap=True):
                                    for item in DEMO_LIST:

                                        def bind_on_example(_item):
                                            def on_example():
                                                return gr.update(
                                                        value=_item[
                                                            'description']
                                                    ), gr.update(
                                                        value=_item['image'])

                                            return on_example

                                        with antd.Card(
                                                hoverable=True,
                                                elem_style=dict(
                                                    width="100%")) as example:
                                            if "description" in item:
                                                antd.Typography.Text(
                                                    item["description"])
                                            if "image" in item:
                                                antd.Image(item["image"],
                                                           preview=False)
                                        example.click(
                                            fn=bind_on_example(item),
                                            outputs=[input, input_image[0]])

                        with antd.Col(xs=24, md=16):
                            with antd.Card(title=get_text("Answer", "答案"),
                                           elem_style=dict(height="100%"),
                                           elem_id="output-container"):
                                output = gr.Markdown(
                                    show_copy_button=True,
                                    latex_delimiters=[{
                                        "left": '$$',
                                        "right": '$$',
                                        "display": True
                                    }, {
                                        "left": '$',
                                        "right": '$',
                                        "display": False,
                                    }, {
                                        "left": '\\(',
                                        "right": '\\)',
                                        "display": False,
                                    }, {
                                        "left": '\\[',
                                        "right": '\\]',
                                        "display": True
                                    }])
                    with antd.Tour(props=dict(open=False)) as tour:
                        antd.Tour.Step(
                            title=get_text("Step 1", "步骤 1"),
                            description=get_text("Upload image and enter text", "传入图片和文本"),
                            get_target=
                            "() => document.querySelector('#input-container')")
                        antd.Tour.Step(
                            title=get_text("Step 2","步骤 2"),
                            description=get_text("Click submit button", "点击提交按钮"),
                            get_target=
                            "() => document.querySelector('#submit-btn')")
                        antd.Tour.Step(
                            title=get_text("Step 3","步骤 3"),
                            description=get_text("Wait for result", "等待结果返回"),
                            get_target=
                            "() => document.querySelector('#output-container')"
                        )

                    tour_btn.click(fn=lambda: gr.update(props=dict(open=True)),
                                   outputs=[tour])
                    gr.on([tour.finish, tour.close],
                          fn=lambda: gr.update(props=dict(open=False)),
                          outputs=[tour])

                    submit_btn.click(
                        fn=generate,
                        inputs=[*input_image, input],
                        outputs=[output])
                    clear_btn.click(
                        fn=on_clear,
                        outputs=[*input_image, input])

                demo.queue(default_concurrency_limit=50).launch()