File size: 8,245 Bytes
304b6b3
 
 
12f0ce7
304b6b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df0e464
304b6b3
 
 
 
d9b3bd5
b0567c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e88bab2
b0567c9
 
 
 
 
 
 
e88bab2
d9b3bd5
 
 
 
b0567c9
d9b3bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336bb36
ba89e47
336bb36
 
 
 
 
b0567c9
d9b3bd5
cdf4f83
d9b3bd5
 
 
 
 
 
 
 
 
b0567c9
 
d9b3bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0567c9
d9b3bd5
 
 
 
b0567c9
 
e88bab2
b0567c9
 
 
d9b3bd5
 
9a6682b
 
 
 
d9b3bd5
 
e88bab2
d9b3bd5
 
 
 
 
d64a45c
b0567c9
 
 
 
 
 
 
 
 
 
 
 
 
d9b3bd5
 
 
 
 
 
 
 
 
 
 
b0567c9
d9b3bd5
 
8435349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f2d448
 
8435349
4f2d448
 
8435349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import gradio as gr
import os
from PIL import Image  # This was missing
import spaces
import copy
import numpy as np  # Required if you're doing image array work

from kimi_vl.serve.frontend import reload_javascript
from kimi_vl.serve.utils import (
    configure_logger,
    pil_to_base64,
    parse_ref_bbox,
    strip_stop_words,
    is_variable_assigned,
)
from kimi_vl.serve.gradio_utils import (
    cancel_outputing,
    delete_last_conversation,
    reset_state,
    reset_textbox,
    transfer_input,
    wrap_gen_fn,
)
from kimi_vl.serve.chat_utils import (
    generate_prompt_with_history,
    convert_conversation_to_prompts,
    to_gradio_chatbot,
    to_gradio_history,
)
from kimi_vl.serve.inference import kimi_vl_generate, load_model
from kimi_vl.serve.examples import get_examples

TITLE = """<h1 align="left" style="min-width:200px; margin-top:0;">Chat with Kimi-VL-A3B-Thinking🤔 </h1>"""
DESCRIPTION_TOP = """<a href="https://github.com/MoonshotAI/Kimi-VL" target="_blank">Kimi-VL-A3B-Thinking</a> is a multi-modal LLM that can understand text, video images, and generate text with thinking processes. \n this specific space was hacked to also accept videos, and the system prompt has been changed to favor video analysis."""
DESCRIPTION = """"""
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
DEPLOY_MODELS = dict()
logger = configure_logger()

def resize_image(image: Image.Image, max_size: int = 640, min_size: int = 28):
    width, height = image.size
    if width < min_size or height < min_size:
        scale = min_size / min(width, height)
        new_width = int(width * scale)
        new_height = int(height * scale)
        image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
    elif max_size > 0 and (width > max_size or height > max_size):
        scale = max_size / max(width, height)
        new_width = int(width * scale)
        new_height = int(height * scale)
        image = image.resize((new_width, new_height))
    return image

def load_frames(video_file, max_num_frames=64, long_edge=448):
    from decord import VideoReader
    vr = VideoReader(video_file)
    duration = len(vr)
    fps = vr.get_avg_fps()
    length = int(duration / fps)
    num_frames = min(max_num_frames, length)

    frame_indices = [int(duration / num_frames * (i + 0.5)) for i in range(num_frames)]
    frames_data = vr.get_batch(frame_indices).asnumpy()

    imgs = []
    for idx in range(num_frames):
        img = resize_image(Image.fromarray(frames_data[idx]).convert("RGB"), long_edge)
        imgs.append(img)

    return imgs

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str, default="Kimi-VL-A3B-Thinking")
    parser.add_argument("--local-path", type=str, default="", help="huggingface ckpt, optional")
    parser.add_argument("--ip", type=str, default="0.0.0.0")
    parser.add_argument("--port", type=int, default=7860)
    return parser.parse_args()

def fetch_model(model_name: str):
    global args, DEPLOY_MODELS
    if args.local_path:
        model_path = args.local_path
    else:
        model_path = f"moonshotai/{args.model}"

    if model_name in DEPLOY_MODELS:
        model_info = DEPLOY_MODELS[model_name]
        print(f"{model_name} has been loaded.")
    else:
        print(f"{model_name} is loading...")
        DEPLOY_MODELS[model_name] = load_model(model_path)
        print(f"Load {model_name} successfully...")
        model_info = DEPLOY_MODELS[model_name]
    return model_info

def highlight_thinking(msg: str) -> str:
    msg = copy.deepcopy(msg)
    if "◁think▷" in msg:
        msg = msg.replace("◁think▷", "<b style='color:blue;'>🤔Thinking...</b>\n")
    if "◁/think▷" in msg:
        msg = msg.replace("◁/think▷", "\n<b style='color:purple;'>💡Summary</b>\n")
    return msg

@wrap_gen_fn
@spaces.GPU(duration=180)
def predict(
    text,
    images,
    chatbot,
    history,
    top_p,
    temperature,
    max_length_tokens,
    max_context_length_tokens,
    video_num_frames,
    video_long_edge,
    chunk_size: int = 512,
):
    print("running the prediction function")
    try:
        model, processor = fetch_model(args.model)
        if text == "":
            yield chatbot, history, "Empty context."
            return
    except KeyError:
        yield [[text, "No Model Found"]], [], "No Model Found"
        return

    if images is None:
        images = []

    pil_images = []
    for img_or_file in images:
        try:
            if isinstance(img_or_file, Image.Image):
                pil_images.append(img_or_file)
            else:
                image = Image.open(img_or_file.name).convert("RGB")
                pil_images.append(image)
        except:
            try:
                pil_images = load_frames(img_or_file, video_num_frames, video_long_edge)
                break
            except Exception as e:
                print(f"Error loading image or video: {e}")

    conversation = generate_prompt_with_history(
        text=text,
        images=pil_images,
        history=history,
        processor=processor,
        max_length=max_context_length_tokens,
    )

    all_conv, last_image = convert_conversation_to_prompts(conversation)
    stop_words = conversation.stop_str
    gradio_chatbot_output = to_gradio_chatbot(conversation)

    full_response = ""
    for x in kimi_vl_generate(
        conversations=all_conv,
        model=model,
        processor=processor,
        stop_words=stop_words,
        max_length=max_length_tokens,
        temperature=temperature,
        top_p=top_p,
    ):
        full_response += x
        response = strip_stop_words(full_response, stop_words)
        conversation.update_last_message(response)
        gradio_chatbot_output[-1][1] = highlight_thinking(response)
        yield gradio_chatbot_output, to_gradio_history(conversation), "Generating..."

    if last_image is not None:
        vg_image = parse_ref_bbox(response, last_image)
        if vg_image is not None:
            vg_base64 = pil_to_base64(vg_image, "vg", max_size=800, min_size=400)
            gradio_chatbot_output[-1][1] += vg_base64
            yield gradio_chatbot_output, to_gradio_history(conversation), "Generating..."

    logger.info("flushed result to gradio")

    if is_variable_assigned("x"):
        print(f"temperature: {temperature}, top_p: {top_p}, max_length_tokens: {max_length_tokens}")

    yield gradio_chatbot_output, to_gradio_history(conversation), "Generate: Success"

# ------------------------------
# Interface + Launch
# ------------------------------

if __name__ == "__main__":
    args = parse_args()
    reload_javascript()

    with gr.Blocks(title="Kimi-VL") as demo:
        gr.Markdown(TITLE)
        gr.Markdown(DESCRIPTION_TOP)

        with gr.Row():
            text_input = gr.Textbox(label="Enter your message", scale=4)
            image_input = gr.File(label="Upload image or video", file_types=["image", "video"], file_count="multiple")

        chatbot_output = gr.Chatbot(label="Kimi-VL Output")
        history_state = gr.State([])
        top_p = gr.Slider(0, 1, value=0.9, label="Top-p")
        temperature = gr.Slider(0.1, 1.5, value=0.6, label="Temperature")
        max_length_tokens = gr.Slider(16, 4096, value=2048, step=64, label="Max Length")
        max_context_length_tokens = gr.Slider(128, 4096, value=2048, step=64, label="Max Context")
        video_num_frames = gr.Slider(4, 64, value=24, step=4, label="Frames (for video)")
        video_long_edge = gr.Slider(128, 1024, value=1024, step=32, label="Long edge resize (video)")

        submit_btn = gr.Button("Submit")

        submit_btn.click(
            predict,
            inputs=[
                text_input,
                image_input,
                chatbot_output,
                history_state,
                top_p,
                temperature,
                max_length_tokens,
                max_context_length_tokens,
                video_num_frames,
                video_long_edge,
            ],
            outputs=[chatbot_output, history_state, gr.Textbox(visible=False)],
        )

    demo.queue().launch(server_name=args.ip, server_port=args.port)