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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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
import time
from typing import List, Dict, Optional
from aixblock_ml.model import AIxBlockMLBase
from ultralytics import YOLO
import os
# from git import Repo
import zipfile
import subprocess
import requests
# from IPython.display import Image
import yaml
import threading
import shutil

import asyncio
import logging
import signal
import logging
from main import train_tf_distributed

from centrifuge import CentrifugeError, Client, ClientEventHandler, SubscriptionEventHandler

import base64
import hmac
import json
import hashlib
from dashboard import promethus_grafana

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[
    logging.FileHandler("{0}/{1}.log".format("logs", "tensorflow-ddp")),
    logging.StreamHandler()
])

# def promethus(job):
#     from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
#     from prometheus_client.exposition import basic_auth_handler

#     def my_auth_handler(url, method, timeout, headers, data):
#         username = 'admin'
#         password = 'admin'
#         return basic_auth_handler(url, method, timeout, headers, data, username, password)

#     registry = CollectorRegistry()
#     g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
#     g.set_to_current_time()
#     push_to_gateway('103.160.78.156:9091', job=job, registry=registry, handler=my_auth_handler)

def base64url_encode(data):
    return base64.urlsafe_b64encode(data).rstrip(b"=")

def generate_jwt(user, channel=""):
    """Note, in tests we generate token on client-side - this is INSECURE
    and should not be used in production. Tokens must be generated on server-side."""
    hmac_secret = "d0a70289-9806-41f6-be6d-f4de5fe298fb"  # noqa: S105 - this is just a secret used in tests.
    header = {"typ": "JWT", "alg": "HS256"}
    payload = {"sub": user}
    if channel:
        # Subscription token
        payload["channel"] = channel
    encoded_header = base64url_encode(json.dumps(header).encode("utf-8"))
    encoded_payload = base64url_encode(json.dumps(payload).encode("utf-8"))
    signature_base = encoded_header + b"." + encoded_payload
    signature = hmac.new(hmac_secret.encode("utf-8"), signature_base, hashlib.sha256).digest()
    encoded_signature = base64url_encode(signature)
    jwt_token = encoded_header + b"." + encoded_payload + b"." + encoded_signature
    return jwt_token.decode("utf-8")

# logging.basicConfig(
#     level=logging.INFO,
#     format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
# )
# cf_logger = logging.getLogger("centrifuge")
# cf_logger.setLevel(logging.DEBUG)

async def get_client_token() -> str:
    return generate_jwt("42")

async def get_subscription_token(channel: str) -> str:
    return generate_jwt("42", channel)

class ClientEventLoggerHandler(ClientEventHandler):
    async def on_connected(self, ctx):
        logging.info("Connected to server")

class SubscriptionEventLoggerHandler(SubscriptionEventHandler):
    async def on_subscribed(self, ctx):
        logging.info("Subscribed to channel")

def setup_client(channel_log):
    client = Client(
        "ws://69.197.168.145:8000/connection/websocket",
        events=ClientEventLoggerHandler(),
        get_token=get_client_token,
        use_protobuf=False,
    )

    sub = client.new_subscription(
        channel_log,
        events=SubscriptionEventLoggerHandler(),
        get_token=get_subscription_token,
    )

    return client, sub

async def send_log(sub, log_message):
    try:
        await sub.publish(data={"log": log_message})
    except CentrifugeError as e:
        logging.error("Error publish: %s", e)

# def log_training_progress(log_message):
#     client.disconnect()  # Đóng kết nối

async def log_training_progress(sub, log_message):
    await send_log(sub, log_message)

def run_train(command, channel_log="training_logs"):
    def run():
        try:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            client, sub = setup_client(channel_log)
            async def main():
                await client.connect()
                await sub.subscribe()
                await log_training_progress(sub, "Training started")
                log_file_path = "logs/tensorflow-ddp.log"
                last_position = 0  # Vị trí đã đọc đến trong file log
                await log_training_progress(sub, "Training training")
                promethus_grafana.promethus_push_to("training")
                
                while True:
                    try:
                        current_size = os.path.getsize(log_file_path)
                        if current_size > last_position:
                            with open(log_file_path, "r") as log_file:
                                log_file.seek(last_position)
                                new_lines = log_file.readlines()
                                # print(new_lines)
                                for line in new_lines:
                                    print("--------------" ,f"{line.strip()}")
                        #             # Thay thế đoạn này bằng code để gửi log
                                    await log_training_progress(sub, f"{line.strip()}")
                            last_position = current_size  

                        time.sleep(5)
                    except Exception as e:
                        print(e)

                promethus_grafana.promethus_push_to("finish")
                await log_training_progress(sub, "Training completed")
                await client.disconnect()
                loop.stop()

            try:
                loop.run_until_complete(main())
            finally:
                loop.close()  # Đảm bảo vòng lặp được đóng lại hoàn toàn
    
        except Exception as e:
            print(e)

    thread_start = threading.Thread(target=run)
    thread_start.start()
    subprocess.run(command)
    try:
        promethus_grafana.promethus_push_to("finish")
    except:
        pass
# def run_train(tf_config, image_size, per_worker_batch_size, num_workers, global_batch_size, epochs, train_dir, dataset_path):
# def run_train(command):
#     # command = [
#     #         'python', 'main.py',
#     #         '--tf_config', json.dumps(tf_config),
#     #         '--imgz', str(image_size),
#     #         '--per_worker', str(per_worker_batch_size),
#     #         '--num_workers', str(num_workers),
#     #         '--global_batch_size', str(global_batch_size),
#     #         '--epochs', str(epochs),
#     #         '--train_dir', train_dir,
#     #         '--dataset_path', dataset_path
#     #     ]

#     result = subprocess.run(command)
#     try:
#         loop = asyncio.new_event_loop()
#         asyncio.set_event_loop(loop)

#         client, sub = setup_client()

#         async def main():
#             await client.connect()
#             await sub.subscribe()
#             await log_training_progress(sub, "Training started")
#             promethus_grafana.promethus_push_to("training")
#             await log_training_progress(sub, "Training training")
#             # train_tf_distributed(tf_config, image_size, per_worker_batch_size, num_workers, global_batch_size, epochs, train_dir)
            
#             promethus_grafana.promethus_push_to("finish")
            
#             # await log_training_progress(sub, "Training completed")
#             await client.disconnect()
#             loop.stop()  # Dừng vòng lặp khi client ngắt kết nối

#         try:
#             loop.run_until_complete(main())
#         finally:
#             loop.close()  # Đảm bảo vòng lặp được đóng lại hoàn toàn
#     except Exception as e:
#         print(e)



def fetch_logs(channel_log):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    client, sub = setup_client(channel_log)

    async def run():
        await client.connect()
        await sub.subscribe()
        history = await sub.history(limit=-1)
        logs = []
        for pub in history.publications:
            log_message = pub.data.get('log')
            if log_message:
                logs.append(log_message)
        await client.disconnect()
        return logs

    return loop.run_until_complete(run())

HOST_NAME = os.environ.get('HOST_NAME',"http://127.0.0.1:8080")

css = """
@import "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css";

.aixblock__title .md p {
    display: block;
    text-align: center;
    font-size: 2em;
    font-weight: 700;
}

.aixblock__tabs .tab-nav {
    justify-content: center;
    gap: 8px;
    padding-bottom: 1rem;
    border-bottom: none !important;
}

.aixblock__tabs .tab-nav > button {
    border-radius: 8px;
    border: 1px solid #DEDEEC;
    height: 32px;
    padding: 8px 10px;
    text-align: center;
    line-height: 1em;
}

.aixblock__tabs .tab-nav > button.selected {
    background-color: #5050FF;
    border-color: #5050FF;
    color: #FFFFFF;
}

.aixblock__tabs .tabitem {
    padding: 0;
    border: none;
}

.aixblock__tabs .tabitem .gap.panel {
    background: none;
    padding: 0;
}

.aixblock__input-image,
.aixblock__output-image {
    border: solid 2px #DEDEEC !important;
}

.aixblock__input-image {
    border-style: dashed !important;
}

footer {
    display: none !important;
}

button.secondary,
button.primary {
    border: none !important;
    background-image: none !important;
    color: white !important;
    box-shadow: none !important;
    border-radius: 8px !important;
}

button.primary {
    background-color: #5050FF !important;
}

button.secondary {
    background-color: #F5A !important;
}

.aixblock__input-buttons {
    justify-content: flex-end;
}

.aixblock__input-buttons > button {
    flex: 0 !important;
}

.aixblock__trial-train {
    text-align: center;
    margin-top: 2rem;
}
"""

js = """
window.addEventListener("DOMContentLoaded", function() {
    function process() {
        let buttonsContainer = document.querySelector('.aixblock__input-image')?.parentElement?.nextElementSibling;
        
        if (!buttonsContainer) {
            setTimeout(function() {
                process();
            }, 100);
            return;
        }
        
        document.querySelectorAll('.aixblock__input-image').forEach(function(ele) {
            ele.parentElement.nextElementSibling.classList.add('aixblock__input-buttons');
        });
    }
    
    process();
});
"""

def download_checkpoint(weight_zip_path, project_id, checkpoint_id, token):
    url = f"{HOST_NAME}/api/checkpoint_model_marketplace/download/{checkpoint_id}?project_id={project_id}"
    payload = {}
    headers = {
        'accept': 'application/json',
        # 'Authorization': 'Token 5d3604c4c57def9a192950ef7b90d7f1e0bb05c1'
        'Authorization': f'Token {token}'
    }
    response = requests.request("GET", url, headers=headers, data=payload) 
    checkpoint_name = response.headers.get('X-Checkpoint-Name')

    if response.status_code == 200:
        with open(weight_zip_path, 'wb') as f:
            f.write(response.content)
        return checkpoint_name
    
    else: 
        return None

def download_dataset(data_zip_dir, project_id, dataset_id, token):
    # data_zip_path = os.path.join(data_zip_dir, "data.zip")
    url = f"{HOST_NAME}/api/dataset_model_marketplace/download/{dataset_id}?project_id={project_id}"
    print(url)
    payload = {}
    headers = {
        'accept': 'application/json',
        'Authorization': f'Token {token}'
    }

    response = requests.request("GET", url, headers=headers, data=payload)
    dataset_name = response.headers.get('X-Dataset-Name')
    if response.status_code == 200:
        with open(data_zip_dir, 'wb') as f:
            f.write(response.content)
        return dataset_name
    else:
        return None

def upload_checkpoint(checkpoint_model_dir, project_id, token):
    url = f"{HOST_NAME}/api/checkpoint_model_marketplace/upload/"

    payload = {
        "type_checkpoint": "ml_checkpoint",
        "project_id": f'{project_id}',
        "is_training": True
    }
    headers = {
        'accept': 'application/json',
        'Authorization': f'Token {token}'
    }

    checkpoint_name = None

    # response = requests.request("POST", url, headers=headers, data=payload) 
    with open(checkpoint_model_dir, 'rb') as file:
        files = {'file': file}
        response = requests.post(url, headers=headers, files=files, data=payload)
        checkpoint_name = response.headers.get('X-Checkpoint-Name')

    return checkpoint_name

class MyModel(AIxBlockMLBase):

    is_init_model_trial = False
    model_trial_public_url = ""
    model_trial_local_url = ""

    def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) -> List[Dict]:
        """ 
        """
        print(f'''\
        Run prediction on {tasks}
        Received context: {context}
        Project ID: {self.project_id}
        Label config: {self.label_config}
        Parsed JSON Label config: {self.parsed_label_config}''')
        return []

    def fit(self, event, data, **kwargs):
        """

        
        """

        # use cache to retrieve the data from the previous fit() runs
        old_data = self.get('my_data')
        old_model_version = self.get('model_version')
        print(f'Old data: {old_data}')
        print(f'Old model version: {old_model_version}')

        # store new data to the cache
        self.set('my_data', 'my_new_data_value')
        self.set('model_version', 'my_new_model_version')
        print(f'New data: {self.get("my_data")}')
        print(f'New model version: {self.get("model_version")}')

        print('fit() completed successfully.')

    def action(self, project, command, collection, **kwargs):

        print(f"""
                project: {project},
                command: {command},
                collection: {collection},
                kwargs: {kwargs}
            """)
              
        if command.lower() == "train":
            try:
                clone_dir = os.path.join(os.getcwd())

                epochs = kwargs.get("num_epochs", 10)
                imgsz = kwargs.get("imgsz", 224)
                project_id = kwargs.get("project_id")
                token = kwargs.get("token")
                checkpoint_version = kwargs.get("checkpoint_version")
                checkpoint_id = kwargs.get("checkpoint")
                dataset_version = kwargs.get("dataset_version")
                dataset_id = kwargs.get("dataset")
                channel_log = kwargs.get("channel_log", "training_logs")
                world_size = kwargs.get("world_size", "1")
                rank = kwargs.get("rank", "0")
                master_add = kwargs.get("master_add")
                master_port = kwargs.get("master_port", "12345")
                workers = kwargs.get("workers", "[127.0.0.1:23456]")
                ps = kwargs.get("ps", "[127.0.0.1:23456]")
                type_node = kwargs.get("type_node", "worker")
                index = kwargs.get("index", "0")
                # entry_file = kwargs.get("entry_file", "main.py")
                configs = kwargs.get("configs")

                # Nếu workers không phải là danh sách, chuyển đổi nó thành danh sách
                if not isinstance(workers, list):
                    workers = [workers]

                print(workers)
                
                # local_rank = kwargs.get("local_rank", "0")

                def func_train_model(clone_dir, project_id, imgsz, epochs, token, checkpoint_version, checkpoint_id, dataset_version, dataset_id):
                    os.makedirs(f'{clone_dir}/data_zip', exist_ok=True)

                    weight_path = os.path.join(clone_dir, f"models")
                    dataset_path = "data"

                    if checkpoint_version and checkpoint_id:
                        print("Get Checkpoint ...")
                        weight_path = os.path.join(clone_dir, f"models/{checkpoint_version}")
                        if not os.path.exists(weight_path):
                            weight_zip_path = os.path.join(clone_dir, "data_zip/weights.zip")
                            checkpoint_name = download_checkpoint(weight_zip_path, project_id, checkpoint_id, token)
                            if checkpoint_name:
                                print(weight_zip_path)
                                with zipfile.ZipFile(weight_zip_path, 'r') as zip_ref:
                                    zip_ref.extractall(weight_path)

                    if dataset_version and dataset_id:
                        print("Get Dataset ...")
                        dataset_path = os.path.join(clone_dir, f"datasets/{dataset_version}")
                        if not os.path.exists(dataset_path):
                            data_zip_dir = os.path.join(clone_dir, "data_zip/data.zip")
                            dataset_name = download_dataset(data_zip_dir, project_id, dataset_id, token)
                            if dataset_name: 
                                # if not os.path.exists(dataset_path):
                                with zipfile.ZipFile(data_zip_dir, 'r') as zip_ref:
                                    zip_ref.extractall(dataset_path)
                        print(dataset_path)
                    # files = [os.path.join(weight_path, filename) for filename in os.listdir(weight_path) if os.path.isfile(os.path.join(weight_path, filename))]
                    # train_dir = os.path.join(os.getcwd(),f"yolov9/runs/train")

                    # script_path = os.path.join(os.getcwd(),f"yolov9/train.py")

                    train_dir = os.path.join(os.getcwd(), "models")
                    log_dir = os.path.join(os.getcwd(), "logs")
                    os.makedirs(train_dir, exist_ok=True)

                    # train_dir = os.path.join(os.getcwd(),f"{project_id}")
                    # os.makedirs(train_dir, exist_ok=True)

                    # train_dir = os.path.join(os.getcwd(), "models")
                    tf_config = {
                        'cluster': {
                            'worker': workers,
                            # 'ps': ps,
                            # 'chief': [f'{master_add}:{master_port}'],
                            # 'evaluator': ['127.0.0.1:12345']
                        },
                        'task': {'type': f'{type_node}', 'index': index},
                        # 'task': {'type': 'ps', 'index': 0}
                    }

                    if ps:
                       tf_config["cluster"]["ps"] = ps

                    os.environ.pop('TF_CONFIG', None)
                    os.environ['TF_CONFIG'] = json.dumps(tf_config)
                    image_size = (imgsz, imgsz)
                    PER_WORKER_BATCH_SIZE = 16
                    NUM_WORKERS = len(tf_config['cluster']['worker'])
                    GLOBAL_BATCH_SIZE = PER_WORKER_BATCH_SIZE * NUM_WORKERS

                    if configs and configs["entry_file"] != "":
                        command = [
                            'python', f'{configs["entry_file"]}',
                            '--tf_config', json.dumps(tf_config)
                        ]
                        if configs["arguments"] and len(configs["arguments"])>0:
                            args = configs["arguments"]
                            for arg in args:
                                command.append(arg['name'])
                                command.append(arg['value'])
                    else:
                        command = [
                            'python', 'main3.py',
                            '--tf_config', json.dumps(tf_config),
                            '--imgz', str(image_size),
                            '--per_worker', str(PER_WORKER_BATCH_SIZE),
                            '--num_workers', str(NUM_WORKERS),
                            '--global_batch_size', str(GLOBAL_BATCH_SIZE),
                            '--epochs', str(epochs),
                            '--train_dir', train_dir,
                            '--dataset_path', dataset_path
                        ]
                    
                    print(command)
                    # run_train(tf_config, image_size, PER_WORKER_BATCH_SIZE, NUM_WORKERS, GLOBAL_BATCH_SIZE, epochs, train_dir, dataset_path)
                    run_train(command, channel_log)
                    # result = subprocess.run(['lsof', '-t', '-i:' + str(master_port)], capture_output=True, text=True)

                    # if result.returncode == 0:
                    #     pids = result.stdout.strip().split('\n')
                    #     if pids:
                    #         for pid in pids:
                    #             subprocess.run(['kill', '-9', pid])

                    # run_train2(command)
                    checkpoint_model = f"{train_dir}/model.keras"

                    if os.path.exists(checkpoint_model):
                        # print(checkpoint_model)
                        checkpoint_name = upload_checkpoint(checkpoint_model, project_id, token)
                        if checkpoint_name:
                            weight_path_final = os.path.join(clone_dir, "models", checkpoint_name)
                            os.makedirs(weight_path_final, exist_ok=True)
                            shutil.copy(checkpoint_model, weight_path_final)

                train_thread = threading.Thread(target=func_train_model, args=(clone_dir, project_id, imgsz, epochs, token, checkpoint_version, checkpoint_id, dataset_version, dataset_id, ))

                train_thread.start()

                return {"message": "train completed successfully"}
            
            except Exception as e:
                print(e)
                return {"message": f"train failed: {e}"}
        
        elif command.lower() == "tensorboard":
            def run_tensorboard():
                train_dir = os.path.join(os.getcwd(), "{project_id}")
                p = subprocess.Popen("tensorboard --logdir /app/logs --host 0.0.0.0 --port=6006 --load_fast=false", stdout=subprocess.PIPE, stderr=None, shell=True)
                out = p.communicate()
                print(out)
                
            tensorboard_thread = threading.Thread(target=run_tensorboard)
            tensorboard_thread.start()

            return {"message": "tensorboardx started successfully"}
        
        elif command.lower() == "predict":
            try:
                if kwargs.get("checkpoint_version") and kwargs.get("checkpoint"):
                    checkpoint_version = kwargs.get("checkpoint_version")
                    checkpoint_id = kwargs.get("checkpoint")
                    weight_path = os.path.join(clone_dir, f"models/{checkpoint_version}")
                    if not os.path.exists(weight_path):
                        weight_zip_path = os.path.join(clone_dir, "data_zip/weights.zip")
                        checkpoint_name = download_checkpoint(weight_zip_path, project_id, checkpoint_id, token)
                        if checkpoint_name:
                            # weight_path = os.path.join(clone_dir, f"models/{checkpoint_name}")
                            # if not os.path.exists(weight_path):
                            with zipfile.ZipFile(weight_zip_path, 'r') as zip_ref:
                                zip_ref.extractall(weight_path)

                    model = YOLO(weight_path)
                else:
                    model = YOLO("yolov8n.pt")
                
                data = kwargs.get("data", "")

                if data == "":
                    data = "https://ultralytics.com/images/zidane.jpg"

                result = model(data["img"])

                return {"message": "predict completed successfully",
                        "result": {"boxes": result[0].boxes.xyxy.tolist(),
                                    "names": result[0].names,
                                    "labels": result[0].boxes.cls.tolist()}}
                # else:
                #     return {"message": "predict failed", "result": None}
            except:
                return {"message": "predict failed", "result": None}
            
        elif command.lower() == "dashboard":
            link = promethus_grafana.generate_link_public("ml_00")
            return {"Share_url": link}
        
        elif command.lower() == "toolbar":
            model = YOLO("yolov8n.pt")
            data = kwargs.get("data", "")
            result = model(data["img"])

            return {"message": "predict completed successfully",
                        "result": {"boxes": result[0].boxes.xyxy.tolist(),
                                    "names": result[0].names,
                                    "labels": result[0].boxes.cls.tolist()}}
        
        elif command.lower() == "logs":
            channel_log = kwargs.get("channel_log", "training_logs")
            logs = fetch_logs(channel_log)
            return {"message": "command not supported", "result": logs}
        
        elif command.lower() == "stop":
            subprocess.run(["pkill", "-9", "-f", "main.py"])
            return {"message": "command not supported", "result": "Done"}
        else:
            return {"message": "command not supported", "result": None}

            # return {"message": "train completed successfully"}

    def model(self, project, **kwargs):
        import gradio as gr

        css = """
        .feedback .tab-nav {
            justify-content: center;
        }

        .feedback button.selected{
            background-color:rgb(115,0,254); !important;
            color: #ffff !important;
        }

        .feedback button{
            font-size: 16px !important;
            color: black !important;
            border-radius: 12px !important;
            display: block !important;
            margin-right: 17px !important;
            border: 1px solid var(--border-color-primary);
        }

        .feedback div {
            border: none !important;
            justify-content: center;
            margin-bottom: 5px;
        }

        .feedback .panel{
            background: none !important;
        }


        .feedback .unpadded_box{
            border-style: groove !important;
            width: 500px;
            height: 345px;
            margin: auto;
        }

        .feedback .secondary{
            background: rgb(225,0,170);
            color: #ffff !important;
        }

        .feedback .primary{
            background: rgb(115,0,254);
            color: #ffff !important;
        }

        .upload_image button{
            border: 1px var(--border-color-primary) !important;
        }
        .upload_image {
            align-items: center !important;
            justify-content: center !important;
            border-style: dashed !important;
            width: 500px;
            height: 345px;
            padding: 10px 10px 10px 10px
        }
        .upload_image .wrap{
            align-items: center !important;
            justify-content: center !important;
            border-style: dashed !important;
            width: 500px;
            height: 345px;
            padding: 10px 10px 10px 10px
        }

        .webcam_style .wrap{
            border: none !important;
            align-items: center !important;
            justify-content: center !important;
            height: 345px;
        }

        .webcam_style .feedback button{
            border: none !important;
            height: 345px;
        }

        .webcam_style .unpadded_box {
            all: unset !important;
        }

        .btn-custom {
            background: rgb(0,0,0) !important;
            color: #ffff !important;
            width: 200px;
        }

        .title1 {
            margin-right: 90px !important;
        }

        .title1 block{
            margin-right: 90px !important;
        }

        """

        with gr.Blocks(css=css) as demo2:
            with gr.Row():
                with gr.Column(scale=10):
                    gr.Markdown(
                        """
                        # Theme preview: `AIxBlock`
                        """
                    )

            import numpy as np

            def predict(input_img):
                import cv2
                result = self.action(project, "predict", collection="", data={"img": input_img})
                print(result)
                if result['result']:
                    boxes = result['result']['boxes']
                    names = result['result']['names']
                    labels = result['result']['labels']

                    for box, label in zip(boxes, labels):
                        box = [int(i) for i in box]
                        label = int(label)
                        input_img = cv2.rectangle(input_img, box, color=(255, 0, 0), thickness=2)
                        # input_img = cv2.(input_img, names[label], (box[0], box[1]), color=(255, 0, 0), size=1)
                        input_img = cv2.putText(input_img, names[label], (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 1,
                                                (0, 255, 0), 2)

                return input_img

            def get_checkpoint_list(project):
                print("GETTING CHECKPOINT LIST")
                print(f"Proejct: {project}")
                import os
                checkpoint_list = [i for i in os.listdir("yolov8/models") if i.endswith(".pt")]
                checkpoint_list = [f"<a href='./yolov8/checkpoints/{i}' download>{i}</a>" for i in
                                   checkpoint_list]
                if os.path.exists(f"yolov8/{project}"):
                    for folder in os.listdir(f"yolov8/{project}"):
                        if "train" in folder:
                            project_checkpoint_list = [i for i in
                                                       os.listdir(f"yolov8/{project}/{folder}/weights") if
                                                       i.endswith(".pt")]
                            project_checkpoint_list = [
                                f"<a href='./yolov8/{project}/{folder}/weights/{i}' download>{folder}-{i}</a>"
                                for i in project_checkpoint_list]
                            checkpoint_list.extend(project_checkpoint_list)

                return "<br>".join(checkpoint_list)

            with gr.Tabs(elem_classes=["feedback"]) as parent_tabs:
                with gr.TabItem("Demo", id=0):
                    with gr.Row():
                        gr.Markdown("## Input", elem_classes=["title1"])
                        gr.Markdown("## Output", elem_classes=["title1"])

                    gr.Interface(predict,
                                 gr.Image(elem_classes=["upload_image"], sources="upload", container=False, height=345,
                                          show_label=False),
                                 gr.Image(elem_classes=["upload_image"], container=False, height=345, show_label=False),
                                 allow_flagging=False
                                 )

                with gr.TabItem("Webcam", id=1):
                    gr.Image(elem_classes=["webcam_style"], sources="webcam", container=False, show_label=False,
                             height=450)

                with gr.TabItem("Video", id=2):
                    gr.Image(elem_classes=["upload_image"], sources="clipboard", height=345, container=False,
                             show_label=False)

        gradio_app, local_url, share_url = demo2.launch(share=True, quiet=True, prevent_thread_lock=True,
                                                        server_name='0.0.0.0', show_error=True)

        return {"share_url": share_url, 'local_url': local_url}

    def model_trial(self, project, **kwargs):
        while self.is_init_model_trial:
            time.sleep(1)

        if len(self.model_trial_local_url) > 0 and len(self.model_trial_public_url) > 0:
            return {"share_url": self.model_trial_public_url, 'local_url': self.model_trial_local_url}

        self.is_init_model_trial = True
        print(kwargs)
        import gradio as gr

        def mt_predict(input_img):
            import cv2
            result = self.action(project, "predict", collection="", data={"img": input_img})
            print(result)
            if result['result']:
                boxes = result['result']['boxes']
                names = result['result']['names']
                labels = result['result']['labels']

                for box, label in zip(boxes, labels):
                    box = [int(i) for i in box]
                    label = int(label)
                    input_img = cv2.rectangle(input_img, box, color=(255, 0, 0), thickness=2)
                    # input_img = cv2.(input_img, names[label], (box[0], box[1]), color=(255, 0, 0), size=1)
                    input_img = cv2.putText(input_img, names[label], (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 1,
                                            (0, 255, 0), 2)

            return input_img

        def mt_trial_training(dataset_choosen):
            print(f"Training with {dataset_choosen}")
            result = self.action(project, "train", collection="", data=dataset_choosen)
            return result['message']

        def mt_download_btn(evt: gr.SelectData):
            print(f"Downloading {dataset_choosen}")
            return f'<a href="/yolov8/datasets/{evt.value}" class="aixblock__download-button"><i class="fa fa-download"></i> Download</a>'

        def mt_get_checkpoint_list(project):
            print("GETTING CHECKPOINT LIST")
            print(f"Proejct: {project}")
            import os
            checkpoint_list = [i for i in os.listdir("yolov8/models") if i.endswith(".pt")]
            checkpoint_list = [f"<a href='./yolov8/checkpoints/{i}' download>{i}</a>" for i in
                               checkpoint_list]
            if os.path.exists(f"yolov8/{project}"):
                for folder in os.listdir(f"yolov8/{project}"):
                    if "train" in folder:
                        project_checkpoint_list = [i for i in
                                                   os.listdir(f"yolov8/{project}/{folder}/weights") if
                                                   i.endswith(".pt")]
                        project_checkpoint_list = [
                            f"<a href='./yolov8/{project}/{folder}/weights/{i}' download>{folder}-{i}</a>"
                            for i in project_checkpoint_list]
                        checkpoint_list.extend(project_checkpoint_list)

            return "<br>".join(checkpoint_list)

        def mt_tab_changed(tab):
            if tab == "Download":
                mt_get_checkpoint_list(project=project)

        def mt_upload_file(file):
            return "File uploaded!"

        with gr.Blocks(css=css, js=js) as demo:
            gr.Markdown("AIxBlock", elem_classes=["aixblock__title"])

            with gr.Tabs(elem_classes=["aixblock__tabs"]):
                with gr.TabItem("Demo", id=0):
                    # with gr.Row():
                    #     gr.Markdown("Input", elem_classes=["aixblock__input-title"])
                    #     gr.Markdown("Output", elem_classes=["aixblock__output-title"])

                    gr.Interface(
                        mt_predict,
                        gr.Image(elem_classes=["aixblock__input-image"], container=False, height=345),
                        gr.Image(elem_classes=["aixblock__output-image"], container=False, height=345),
                        allow_flagging="never",
                    )

                # with gr.TabItem("Webcam", id=1):
                #     gr.Image(elem_classes=["webcam_style"], sources="webcam", container = False, show_label = False, height = 450)

                # with gr.TabItem("Video", id=2):
                #     gr.Image(elem_classes=["upload_image"], sources="clipboard", height = 345,container = False, show_label = False)

                # with gr.TabItem("About", id=3):
                #     gr.Label("About Page")

                with gr.TabItem("Trial Train", id=2):
                    gr.Markdown("Dataset template to prepare your own and initiate training")
                    # with gr.Row():
                    # get all filename in datasets folder
                    datasets = [(f"dataset{i}", name) for i, name in enumerate(os.listdir('./datasets'))]
                    dataset_choosen = gr.Dropdown(datasets, label="Choose dataset", show_label=False, interactive=True,
                                                  type="value")
                    # gr.Button("Download this dataset", variant="primary").click(download_btn, dataset_choosen, gr.HTML())
                    download_link = gr.HTML("""<em>Please select a dataset to download</em>""")
                    dataset_choosen.select(mt_download_btn, None, download_link)

                    # when the button is clicked, download the dataset from dropdown
                    # download_btn
                    gr.Markdown("Upload your sample dataset to have a trial training")
                    # gr.File(file_types=['tar','zip'])

                    gr.Interface(
                        mt_predict,
                        gr.File(elem_classes=["aixblock__input-image"], file_types=['tar', 'zip'], container=False),
                        gr.Label(elem_classes=["aixblock__output-image"], container=False),
                        allow_flagging="never",
                    )

                    with gr.Column(elem_classes=["aixblock__trial-train"]):
                        gr.Button("Trial Train", variant="primary").click(mt_trial_training, dataset_choosen, None)
                        gr.HTML(value=f"<em>You can attemp up to {2} FLOps</em>")

                # with gr.TabItem("Download"):
                #     with gr.Column():
                #         gr.Markdown("## Download")
                #         with gr.Column():
                #             gr.HTML(get_checkpoint_list(project))

        gradio_app, local_url, share_url = demo.launch(share=True, quiet=True, prevent_thread_lock=True,
                                                       server_name='0.0.0.0', show_error=True)
        self.model_trial_public_url = share_url
        self.model_trial_local_url = local_url
        self.is_init_model_trial = False

        return {"share_url": share_url, 'local_url': local_url}

    def download(self, project, **kwargs):
        return super(self).download(project, **kwargs)