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 
 
 
 
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import html
import json
import os
import queue
import shutil
import sys
import threading
import uuid
from pathlib import Path
from time import sleep

os.environ['GRADIO_TEMP_DIR'] = os.getcwd()
video_root_path = os.path.join(os.getcwd(), 'video_root')
os.makedirs(video_root_path, exist_ok=True)

from omagent_core.clients.devices.app.callback import AppCallback
from omagent_core.clients.devices.app.input import AppInput
from omagent_core.clients.devices.app.schemas import ContentStatus, MessageType
from omagent_core.engine.automator.task_handler import TaskHandler
from omagent_core.engine.http.models.workflow_status import terminal_status
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.services.connectors.redis import RedisConnector
from omagent_core.utils.build import build_from_file
from omagent_core.utils.container import container
from omagent_core.utils.logger import logging
from omagent_core.utils.registry import registry

registry.import_module()

container.register_connector(name="redis_stream_client", connector=RedisConnector)
# container.register_stm(stm='RedisSTM')
container.register_callback(callback=AppCallback)
container.register_input(input=AppInput)

import gradio as gr


class WebpageClient:
    def __init__(
            self,
            interactor: ConductorWorkflow = None,
            processor: ConductorWorkflow = None,
            config_path: str = "./config",
            workers: list = [],
    ) -> None:
        self._interactor = interactor
        self._processor = processor
        self._config_path = config_path
        self._workers = workers
        self._workflow_instance_id = None
        self._worker_config = build_from_file(self._config_path)
        self._task_to_domain = {}
        self._incomplete_message = ""
        self._custom_css = """
            #OmAgent {
                height: 100vh !important;
                max-height: calc(100vh - 190px) !important;
                overflow-y: auto;
            }

            .running-message {
                margin: 0;
                padding: 2px 4px;
                white-space: pre-wrap;
                word-wrap: break-word;
                font-family: inherit;
            }

            /* Remove the background and border of the message box */
            .message-wrap {
                background: none !important;
                border: none !important;
                padding: 0 !important;
                margin: 0 !important;
            }

            /* Remove the bubble style of the running message */
            .message:has(.running-message) {
                background: none !important;
                border: none !important;
                padding: 0 !important;
                box-shadow: none !important;
            }
        """
        self.workflow_instance_id = str(uuid.uuid4())
        self.processor_instance_id = str(uuid.uuid4())
        worker_config = build_from_file(self._config_path)
        self.initialization(workers, worker_config)
    
    def initialization(self, workers, worker_config):
        self.workers = {}
        for worker in workers:
            worker.workflow_instance_id = self.workflow_instance_id
            self.workers[type(worker).__name__] = worker
        
        for config in worker_config:
            worker_cls = registry.get_worker(config['name'])
            worker = worker_cls(**config)
            worker.workflow_instance_id = self.workflow_instance_id
            self.workers[config['name']] = worker
        
    def gradio_app(self):
        
        with gr.Blocks() as demo:

            def load_local_video() -> dict:
                result = {}
                for root, _, files in os.walk(video_root_path):
                    for file in filter(lambda x: x.split('.')[-1] in (
                            'mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'webm', 'm4v'), files):
                        file_obs_path = os.path.join(root, file)
                        result[Path(file_obs_path).name] = file_obs_path
                return result
            
            video_dict = load_local_video()
            current_video = None
            state = gr.State(value={
                'video_dict': video_dict,
                'current_video': current_video
            })
            with gr.Row():
                with gr.Column():
                    with gr.Column():
                        def display_video_map(video_title):
                            # change display video
                            video_path = state.value.get('video_dict', {}).get(video_title)
                            
                            exception_queue = queue.Queue()
                            workflow_input = {'video_path': video_path}
                            processor_result = None
                            
                            def run_workflow(workflow_input):
                                nonlocal processor_result
                                try:
                                    processor_result = self._processor.start_workflow_with_input(
                                        workflow_input=workflow_input, workers=self.workers
                                    )
                                except Exception as e:
                                    exception_queue.put(e)  # add exception to queue
                                    logging.error(f"Error starting workflow: {e}")
                                    raise e
                            
                            workflow_thread = threading.Thread(target=run_workflow, args=(workflow_input,), daemon=True)
                            workflow_thread.start()
                            
                            processor_workflow_instance_id = self.processor_instance_id
                            while True:
                                status = self._processor.get_workflow(
                                    workflow_id=processor_workflow_instance_id).status
                                if status in terminal_status:
                                    break
                                sleep(1)
                            
                            state.value['video_dict'] = load_local_video()
                            state.value.update(current_video=video_path)
                            state.value.update(processor_result=processor_result)
                            state.value.update(processor_workflow_instance_id=processor_workflow_instance_id)
                        
                            return video_path, state
                            
                        select_video = gr.Dropdown(
                            state.value['video_dict'].keys(),
                            value=None
                        )
                        display_video = gr.Video(
                            state.value['current_video'],
                        )
                        select_video.change(
                            fn=display_video_map,
                            inputs=[select_video],
                            outputs=[display_video, state]
                        )
                
                with gr.Column():
                    chatbot = gr.Chatbot(
                        type="messages",
                    )
                    
                    chat_input = gr.Textbox(
                        interactive=True,
                        placeholder="Enter message...",
                        show_label=False,
                    )
                    
                    chat_msg = chat_input.submit(
                        self.add_message,
                        [chatbot, chat_input, state],
                        [chatbot, chat_input]
                    )
                    bot_msg = chat_msg.then(
                        self.bot, (chatbot, state), chatbot, api_name="bot_response"
                    )
                    bot_msg.then(
                        lambda: gr.Textbox(interactive=True), None, [chat_input]
                    )
            
            demo.launch(
                max_file_size='1gb'
            )
        
    def start_interactor(self):

        try:
            self.gradio_app()
        except KeyboardInterrupt:
            logging.info("\nDetected Ctrl+C, stopping workflow...")
            if self._workflow_instance_id is not None:
                self._interactor._executor.terminate(
                    workflow_id=self._workflow_instance_id
                )
            raise
    
    def stop_interactor(self):
        # self._task_handler_interactor.stop_processes()
        print("stop_interactor")
        sys.exit(0)
        
    def start_processor(self):
        self._task_handler_processor = TaskHandler(
            worker_config=self._worker_config, workers=self._workers, task_to_domain=self._task_to_domain
        )
        self._task_handler_processor.start_processes()
        
        try:
            with gr.Blocks(title="OmAgent", css=self._custom_css) as chat_interface:
                chatbot = gr.Chatbot(
                    elem_id="OmAgent",
                    bubble_full_width=False,
                    type="messages",
                    height="100%",
                )
                
                chat_input = gr.MultimodalTextbox(
                    interactive=True,
                    file_count="multiple",
                    placeholder="Enter message or upload file...",
                    show_label=False,
                )
                
                chat_msg = chat_input.submit(
                    self.add_processor_message,
                    [chatbot, chat_input],
                    [chatbot, chat_input],
                )
                bot_msg = chat_msg.then(
                    self.processor_bot, chatbot, chatbot, api_name="bot_response"
                )
                bot_msg.then(
                    lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input]
                )
            chat_interface.launch(server_port=7861)
        except KeyboardInterrupt:
            logging.info("\nDetected Ctrl+C, stopping workflow...")
            if self._workflow_instance_id is not None:
                self._processor._executor.terminate(
                    workflow_id=self._workflow_instance_id
                )
            raise
    
    def stop_processor(self):
        self._task_handler_processor.stop_processes()
    
    def add_message(self, history, message, state):
        if isinstance(state, gr.State):
            if state.value.get('current_video') is None:
                history.append({"role": "user", "content": message})
                history.append({"role": "assistant", "content": 'Please select a video'})
                return history, gr.Textbox(value=None, interactive=False)
        else:
            if state.get('current_video') is None:
                history.append({"role": "user", "content": message})
                history.append({"role": "assistant", "content": 'Please select a video'})
                return history, gr.Textbox(value=None, interactive=False)
        if self._workflow_instance_id is None:
            workflow_input = {
                'question': message,
                "video_md5": state.value.get('processor_result', {}).get("video_md5"),
                "video_path": state.value.get('processor_result', {}).get("video_path"),
                "instance_id": state.value.get('processor_result', {}).get("instance_id"),
                "processor_workflow_instance_id": state.value.get("processor_workflow_instance_id")
            }
            exception_queue = queue.Queue()
            
            def run_workflow(workflow_input):
                try:
                    self._interactor.start_workflow_with_input(
                        workflow_input=workflow_input, workers=self.workers
                    )
                except Exception as e:
                    exception_queue.put(e)  # add exception to queue
                    logging.error(f"Error starting workflow: {e}")
                    raise e
            
            workflow_thread = threading.Thread(target=run_workflow, args=(workflow_input,),daemon=True)
            workflow_thread.start()

            self._workflow_instance_id = self.workflow_instance_id
        contents = []
        history.append({"role": "user", "content": message})
        contents.append({"data": message, "type": "text"})
        result = {
            "agent_id": self._workflow_instance_id,
            "messages": [{"role": "user", "content": contents}],
            "kwargs": {},
        }
        container.get_connector("redis_stream_client")._client.xadd(
            f"{self._workflow_instance_id}_input",
            {"payload": json.dumps(result, ensure_ascii=False)},
        )
        return history, gr.Textbox(value=None, interactive=False)
    
    def add_processor_message(self, history, message):
        if self._workflow_instance_id is None:
            self._workflow_instance_id = self._processor.start_workflow_with_input(
                workflow_input={}, task_to_domain=self._task_to_domain
            )
        image_items = []
        for idx, x in enumerate(message["files"]):
            history.append({"role": "user", "content": {"path": x}})
            image_items.append(
                {"type": "image_url", "resource_id": str(idx), "data": str(x)}
            )
        result = {"content": image_items}
        container.get_connector("redis_stream_client")._client.xadd(
            f"image_process", {"payload": json.dumps(result, ensure_ascii=False)}
        )
        return history, gr.MultimodalTextbox(value=None, interactive=False)
    
    def bot(self, history, state):
        if isinstance(state, gr.State):
            if state.value.get('current_video') is None:
                yield history
                return
        else:
            if state.get('current_video') is None:
                yield history
                return
            
        stream_name = f"{self._workflow_instance_id}_output"
        consumer_name = f"{self._workflow_instance_id}_agent"  # consumer name
        group_name = "omappagent"  # replace with your consumer group name
        running_stream_name = f"{self._workflow_instance_id}_running"
        self._check_redis_stream_exist(stream_name, group_name)
        self._check_redis_stream_exist(running_stream_name, group_name)
        while True:
            # read running stream
            running_messages = self._get_redis_stream_message(
                group_name, consumer_name, running_stream_name
            )
            for stream, message_list in running_messages:
                for message_id, message in message_list:
                    payload_data = self._get_message_payload(message)
                    if payload_data is None:
                        continue
                    progress = html.escape(payload_data.get("progress", ""))
                    message = html.escape(payload_data.get("message", ""))
                    formatted_message = (
                        f'<pre class="running-message">{progress}: {message}</pre>'
                    )
                    history.append({"role": "assistant", "content": formatted_message})
                    yield history
                    
                    container.get_connector("redis_stream_client")._client.xack(
                        running_stream_name, group_name, message_id
                    )
            # read output stream
            messages = self._get_redis_stream_message(
                group_name, consumer_name, stream_name
            )
            finish_flag = False
            
            for stream, message_list in messages:
                for message_id, message in message_list:
                    incomplete_flag = False
                    payload_data = self._get_message_payload(message)
                    if payload_data is None:
                        continue
                    if payload_data["content_status"] == ContentStatus.INCOMPLETE.value:
                        incomplete_flag = True
                    message_item = payload_data["message"]
                    if message_item["type"] == MessageType.IMAGE_URL.value:
                        history.append(
                            {
                                "role": "assistant",
                                "content": {"path": message_item["content"]},
                            }
                        )
                    else:
                        if incomplete_flag:
                            self._incomplete_message = (
                                    self._incomplete_message + message_item["content"]
                            )
                            if history and history[-1]["role"] == "assistant":
                                history[-1]["content"] = self._incomplete_message
                            else:
                                history.append(
                                    {
                                        "role": "assistant",
                                        "content": self._incomplete_message,
                                    }
                                )
                        else:
                            if self._incomplete_message != "":
                                self._incomplete_message = (
                                        self._incomplete_message + message_item["content"]
                                )
                                if history and history[-1]["role"] == "assistant":
                                    history[-1]["content"] = self._incomplete_message
                                else:
                                    history.append(
                                        {
                                            "role": "assistant",
                                            "content": self._incomplete_message,
                                        }
                                    )
                                self._incomplete_message = ""
                            else:
                                history.append(
                                    {
                                        "role": "assistant",
                                        "content": message_item["content"],
                                    }
                                )
                    
                    yield history
                    
                    container.get_connector("redis_stream_client")._client.xack(
                        stream_name, group_name, message_id
                    )
                    
                    # check finish flag
                    if (
                            "interaction_type" in payload_data
                            and payload_data["interaction_type"] == 1
                    ):
                        finish_flag = True
                    if (
                            "content_status" in payload_data
                            and payload_data["content_status"]
                            == ContentStatus.END_ANSWER.value
                    ):
                        self._workflow_instance_id = None
                        finish_flag = True
            
            if finish_flag:
                break
            sleep(0.01)
    
    def processor_bot(self, history: list):
        history.append({"role": "assistant", "content": f"processing..."})
        yield history
        while True:
            status = self._processor.get_workflow(
                workflow_id=self._workflow_instance_id
            ).status
            if status in terminal_status:
                history.append({"role": "assistant", "content": f"completed"})
                yield history
                self._workflow_instance_id = None
                break
            sleep(0.01)
    
    def _get_redis_stream_message(
            self, group_name: str, consumer_name: str, stream_name: str
    ):
        messages = container.get_connector("redis_stream_client")._client.xreadgroup(
            group_name, consumer_name, {stream_name: ">"}, count=1
        )
        messages = [
            (
                stream,
                [
                    (
                        message_id,
                        {
                            k.decode("utf-8"): v.decode("utf-8")
                            for k, v in message.items()
                        },
                    )
                    for message_id, message in message_list
                ],
            )
            for stream, message_list in messages
        ]
        return messages
    
    def _check_redis_stream_exist(self, stream_name: str, group_name: str):
        try:
            container.get_connector("redis_stream_client")._client.xgroup_create(
                stream_name, group_name, id="0", mkstream=True
            )
        except Exception as e:
            logging.debug(f"Consumer group may already exist: {e}")
    
    def _get_message_payload(self, message: dict):
        logging.info(f"Received running message: {message}")
        payload = message.get("payload")
        # check payload data
        if not payload:
            logging.error("Payload is empty")
            return None
        try:
            payload_data = json.loads(payload)
        except json.JSONDecodeError as e:
            logging.error(f"Payload is not a valid JSON: {e}")
            return None
        return payload_data