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8f1d3f
7c1eee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
 
 
7c1eee1
f8f1d3f
 
 
7c1eee1
 
6dc2db5
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
25646a8
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
25646a8
7c1eee1
 
 
 
 
 
f8f1d3f
7c1eee1
 
 
f8f1d3f
 
 
7c1eee1
6dc2db5
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
7c1eee1
 
 
25646a8
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
7c1eee1
 
 
25646a8
7c1eee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8f1d3f
7c1eee1
 
 
 
f8f1d3f
7c1eee1
 
 
 
 
 
 
 
 
 
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
 
 
 
 
 
 
 
7c1eee1
 
6dc2db5
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
25646a8
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
25646a8
7c1eee1
 
 
6dc2db5
7c1eee1
 
f8f1d3f
7c1eee1
 
 
f8f1d3f
 
 
7c1eee1
 
6dc2db5
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
7c1eee1
 
 
25646a8
7c1eee1
 
6dc2db5
7c1eee1
 
 
 
 
6dc2db5
7c1eee1
 
 
25646a8
7c1eee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
## All Generation Gradio Interface
import uuid
import time

from .utils import *
from .vote_utils import t2s_logger, t2s_multi_logger, i2s_logger, i2s_multi_logger
from constants import IMAGE_DIR, OFFLINE_DIR, TEXT_PROMPT_PATH, IMAGE_PROMPT_PATH

with open(TEXT_PROMPT_PATH, 'r') as f:
    prompt_list = json.load(f)

with open(IMAGE_PROMPT_PATH, 'r') as f:
    lines = f.readlines()

image_list = {}
for line in lines:
    idx = line.split('.png')[0].split('_')[-1]
    url = line.split(')')[0].split('(')[-1]
    image_list[eval(idx)] = url


class State:
    def __init__(self, 
                 model_name, i2s_mode=False, offline=False, 
                 prompt=None, image=None, offline_idx=None, 
                 normal_video=None , rgb_video=None,
                 evaluted_dims=0):
        self.conv_id = uuid.uuid4().hex
        self.model_name = model_name
        self.i2s_mode = i2s_mode
        self.offline = offline
        
        self.prompt = prompt
        self.image = image
        self.offline_idx = offline_idx
        # self.output = None
        self.normal_video = normal_video
        self.rgb_video = rgb_video

        self.evaluted_dims = evaluted_dims

    def dict(self):
        base = {
            "conv_id": self.conv_id,
            "model_name": self.model_name,
            "i2s_mode": self.i2s_mode,
            "offline": self.offline,
            "prompt": self.prompt,
            "evaluted_dims": self.evaluted_dims,
            }
        if self.offline:
            base['offline_idx'] = self.offline_idx
        return base

# class StateI2S:
#     def __init__(self, model_name):
#         self.conv_id = uuid.uuid4().hex
#         self.model_name = model_name
#         self.image = None
#         self.output = None

#     def dict(self):
#         base = {
#             "conv_id": self.conv_id,
#             "model_name": self.model_name,

#             }
#         return base

def sample_t2s_model(state_0, state_1, model_list):
    model_name_0, model_name_1 = random.sample(eval(model_list), 2)

    if state_0 is None:
        state_0 = State(model_name_0, i2s_mode=False)
    if state_1 is None:
        state_1 = State(model_name_1, i2s_mode=False)

    state_0.model_name = model_name_0
    state_0.i2s_mode = False
    state_1.model_name = model_name_1
    state_1.i2s_mode = False
    return state_0, state_1, model_name_0, model_name_1

def sample_i2s_model(state_0, state_1, model_list):
    model_name_0, model_name_1 = random.sample(eval(model_list), 2)

    if state_0 is None:
        state_0 = State(model_name_0, i2s_mode=True)
    if state_1 is None:
        state_1 = State(model_name_1, i2s_mode=True)

    state_0.model_name = model_name_0
    state_0.i2s_mode = True
    state_1.model_name = model_name_1
    state_1.i2s_mode = True
    return state_0, state_1, model_name_0, model_name_1

def sample_prompt(state, model_name):
    if state is None:
        state = State(model_name)

    idx = random.randint(0, len(prompt_list)-1)
    prompt = prompt_list[idx]

    state.model_name = model_name
    state.prompt = prompt
    state.i2s_mode = False
    state.offline = True, 
    state.offline_idx = idx
    return state, prompt

def sample_prompt_side_by_side(state_0, state_1, model_name_0, model_name_1):
    if state_0 is None:
        state_0 = State(model_name_0)
    if state_1 is None:
        state_1 = State(model_name_1)

    idx = random.randint(0, len(prompt_list)-1)
    prompt = prompt_list[idx]

    state_0.i2s_mode, state_1.i2s_mode = False, False
    state_0.offline, state_1.offline = True, True
    state_0.offline_idx, state_1.offline_idx = idx, idx
    state_0.prompt, state_1.prompt = prompt, prompt
    return state_0, state_1, prompt

def sample_image(state, model_name):
    if state is None:
        state = State(model_name)

    idx = random.sample(image_list.keys(), 1)[0]
    img_url = image_list[idx]

    state.model_name = model_name
    state.image = img_url
    state.i2s_mode = True
    state.offline = True, 
    state.offline_idx = idx
    return state, img_url

def sample_image_side_by_side(state_0, state_1, model_name_0, model_name_1):
    if state_0 is None:
        state_0 = State(model_name_0)
    if state_1 is None:
        state_1 = State(model_name_1)


    idx = random.sample(image_list.keys(), 1)[0]
    img_url = image_list[idx]

    state_0.i2s_mode, state_1.i2s_mode = True, True
    state_0.offline, state_1.offline = True, True
    state_0.offline_idx, state_1.offline_idx = idx, idx
    state_0.image, state_1.image = img_url, img_url
    return state_0, state_1, img_url

def generate_t2s(gen_func, render_func, 
                 state, 
                 text, 
                 model_name, 
                 request: gr.Request):
    if not text or text.strip()=="":
        raise gr.Warning("Prompt cannot be empty.")
    if not model_name:
        raise gr.Warning("Model name cannot be empty.")
    
    if state is None:
        state = State(model_name, i2s_mode=False, offline=False)

    text = text.strip()
    ip = get_ip(request)
    t2s_logger.info(f"generate. ip: {ip}")

    state.model_name = model_name
    state.prompt = text
    state.evaluted_dims = 0
    try:
        idx = prompt_list.index(text)
        state.offline = True
        state.offline_idx = idx
    except:
        state.offline = False
        state.offline_idx = None

    if state.offline and state.offline_idx:
        start_time = time.time()
        videos = gen_func(text, model_name, offline=state.offline, offline_idx=state.offline_idx)
        # normal_video = os.path.join(OFFLINE_DIR, "text2shape", model_name, "normal", f"{state.offline_idx}.mp4")
        # rgb_video = os.path.join(OFFLINE_DIR, "text2shape", model_name, "rgb", f"{state.offline_idx}.mp4")
        
        state.normal_video = videos['normal']
        state.rgb_video = videos['rgb']        
        yield state, videos['normal'], videos['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name,
            "type": "offline",
            "gen_params": {},
            "state": state.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
    else:
        start_time = time.time()
        shape = gen_func(text, model_name)
        generate_time = time.time() - start_time

        videos = render_func(shape, model_name)
        finish_time = time.time()
        render_time = finish_time - start_time - generate_time

        state.normal_video = videos['normal']
        state.rgb_video = videos['rgb']    
        yield state, videos['normal'], videos['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name,
            "type": "online",
            "gen_params": {},
            "state": state.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }

    with open(get_conv_log_filename(), "a") as fout:
        fout.write(json.dumps(data) + "\n")
    append_json_item_on_log_server(data, get_conv_log_filename())
        
    # output_file = f'{IMAGE_DIR}/text2shape/{state.conv_id}.png'
    # os.makedirs(os.path.dirname(output_file), exist_ok=True)
    # with open(output_file, 'w') as f:
    #     state.output.save(f, 'PNG')
    # save_image_file_on_log_server(output_file)
        
def generate_t2s_multi(gen_func, render_func, 
                       state_0, state_1,
                       text, 
                       model_name_0, model_name_1, 
                       request: gr.Request):
    if not text or text.strip()=="":
        raise gr.Warning("Prompt cannot be empty.")
    if not model_name_0:
        raise gr.Warning("Model name A cannot be empty.")
    if not model_name_1:
        raise gr.Warning("Model name B cannot be empty.")
    
    if state_0 is None:
        state_0 = State(model_name_0, i2s_mode=False, offline=False)
    if state_1 is None:
        state_1 = State(model_name_1, i2s_mode=False, offline=False)
    
    text = text.strip()
    ip = get_ip(request)
    t2s_multi_logger.info(f"generate. ip: {ip}")
    
    state_0.model_name, state_1.model_name = model_name_0, model_name_1
    state_0.prompt, state_1.prompt = text, text
    state_0.evaluted_dims, state_1.evaluted_dims = 0, 0
    try:
        idx = prompt_list.index(text)
        state_0.offline, state_1.offline = True, True
        state_0.offline_idx, state_1.offline_idx = idx, idx
    except:
        state_0.offline, state_1.offline = False, False
        state_0.offline_idx, state_1.offline_idx = None, None

    if state_0.offline and state_0.offline_idx:
        start_time = time.time()
        videos_0, videos_1 = gen_func(text, model_name_0, model_name_1, offline=state_0.offline, offline_idx=state_0.offline_idx)
        # normal_video_0 = os.path.join(OFFLINE_DIR, "text2shape", model_name_0, "normal", f"{state_0.offline_idx}.mp4")
        # rgb_video_0 = os.path.join(OFFLINE_DIR, "text2shape", model_name_0, "rgb", f"{state_0.offline_idx}.mp4")
        # normal_video_1 = os.path.join(OFFLINE_DIR, "text2shape", model_name_1, "normal", f"{state_1.offline_idx}.mp4")
        # rgb_video_1 = os.path.join(OFFLINE_DIR, "text2shape", model_name_1, "rgb", f"{state_1.offline_idx}.mp4")
        
        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1,videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "offline",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "offline",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
    else:
        start_time = time.time()
        shape_0, shape_1 = gen_func(text, model_name_0, model_name_1)
        generate_time = time.time() - start_time

        videos_0, videos_1 = render_func(shape_0, model_name_0, shape_1, model_name_1)
        finish_time = time.time()
        render_time = finish_time - start_time - generate_time
        
        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1, videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "online",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "online",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
    
    with open(get_conv_log_filename(), "a") as fout:
        fout.write(json.dumps(data_0) + "\n")
        fout.write(json.dumps(data_1) + "\n")
    append_json_item_on_log_server(data_0, get_conv_log_filename())
    append_json_item_on_log_server(data_1, get_conv_log_filename())

    # for i, state in enumerate([state_0, state_1]):
    #     output_file = f'{IMAGE_DIR}/text2shape/{state.conv_id}.png'
    #     os.makedirs(os.path.dirname(output_file), exist_ok=True)
    #     with open(output_file, 'w') as f:
    #         state.output.save(f, 'PNG')
    #     save_image_file_on_log_server(output_file)
            
def generate_t2s_multi_annoy(gen_func, render_func,
                             state_0, state_1, 
                             text, 
                             model_name_0, model_name_1, 
                             request: gr.Request):
    if not text or text.strip()=="":
        raise gr.Warning("Prompt cannot be empty.")
    if state_0 is None:
        state_0 = State(model_name_0, i2s_mode=False, offline=False)
    if state_1 is None:
        state_1 = State(model_name_1, i2s_mode=False, offline=False)
    
    text = text.strip()
    ip = get_ip(request)
    t2s_multi_logger.info(f"generate. ip: {ip}")

    state_0.model_name, state_1.model_name = model_name_0, model_name_1
    state_0.prompt, state_1.prompt = text, text
    state_0.evaluted_dims, state_1.evaluted_dims = 0, 0
    try:
        idx = prompt_list.index(text)
        state_0.offline, state_1.offline = True, True
        state_0.offline_idx, state_1.offline_idx = idx, idx
    except:
        state_0.offline, state_1.offline = False, False
        state_0.offline_idx, state_1.offline_idx = None, None

    if state_0.offline and state_0.offline_idx:
        start_time = time.time()
        videos_0, videos_1, model_name_0, model_name_1 = gen_func(text, model_name_0, model_name_1, 
                                      i2s_model=False, offline=state_0.offline, offline_idx=state_0.offline_idx)
        # normal_video_0 = os.path.join(OFFLINE_DIR, "text2shape", model_name_0, "normal", f"{state_0.offline_idx}.mp4")
        # rgb_video_0 = os.path.join(OFFLINE_DIR, "text2shape", model_name_0, "rgb", f"{state_0.offline_idx}.mp4")
        # normal_video_1 = os.path.join(OFFLINE_DIR, "text2shape", model_name_1, "normal", f"{state_1.offline_idx}.mp4")
        # rgb_video_1 = os.path.join(OFFLINE_DIR, "text2shape", model_name_1, "rgb", f"{state_1.offline_idx}.mp4")
        
        state_0.model_name, state_1.model_name = model_name_0, model_name_1
        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1, videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb'], \
            gr.Markdown(f"### Model A: {model_name_0}"), gr.Markdown(f"### Model B: {model_name_1}")

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "offline",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "offline",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
    else:
        start_time = time.time()
        shape_0, shape_1, model_name_0, model_name_1 = gen_func(text, model_name_0, model_name_1, i2s_mode=False)
        generate_time = time.time() - start_time

        videos_0, videos_1 = render_func(shape_0, model_name_0, shape_1, model_name_1)
        finish_time = time.time()
        render_time = finish_time - start_time - generate_time
        
        state_0.model_name, state_1.model_name = model_name_0, model_name_1
        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1, videos_0[0], videos_0[1], videos_1[0], videos_1[1], \
            gr.Markdown(f"### Model A: {model_name_0}"), gr.Markdown(f"### Model B: {model_name_1}")

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "online",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "online",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
    
    with open(get_conv_log_filename(), "a") as fout:
        fout.write(json.dumps(data_0) + "\n")
        fout.write(json.dumps(data_1) + "\n")
    append_json_item_on_log_server(data_0, get_conv_log_filename())
    append_json_item_on_log_server(data_1, get_conv_log_filename())

    # for i, state in enumerate([state_0, state_1]):
    #     output_file = f'{IMAGE_DIR}/text2shape/{state.conv_id}.png'
    #     os.makedirs(os.path.dirname(output_file), exist_ok=True)
    #     with open(output_file, 'w') as f:
    #         state.output.save(f, 'PNG')
    #     save_image_file_on_log_server(output_file)
            
            
def generate_i2s(gen_func, render_func, state, image, model_name, request: gr.Request):
    if image is None:
        raise gr.Warning("Image cannot be empty.")
    if not model_name:
        raise gr.Warning("Model name cannot be empty.")
    if state is None:
        state = State(model_name, i2s_mode=True, offline=False)
    
    ip = get_ip(request)
    t2s_logger.info(f"generate. ip: {ip}")

    state.model_name = model_name
    state.image = image
    state.evaluted_dims = 0

    if state.offline and state.offline_idx:
        start_time = time.time()
        videos = gen_func(image, model_name, offline=state.offline, offline_idx=state.offline_idx)
        # normal_video = os.path.join(OFFLINE_DIR, "image2shape", model_name, "normal", f"{state.offline_idx}.mp4")
        # rgb_video = os.path.join(OFFLINE_DIR, "image2shape", model_name, "rgb", f"{state.offline_idx}.mp4")
        
        state.normal_video = videos['normal']
        state.rgb_video = videos['rgb']
        yield state, videos['normal'], videos['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name,
            "type": "offline",
            "gen_params": {},
            "state": state.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
    else:
        start_time = time.time()
        shape = gen_func(image, model_name)
        generate_time = time.time() - start_time

        videos = render_func(shape, model_name)
        finish_time = time.time()
        render_time = finish_time - start_time - generate_time

        state.normal_video = videos['normal']
        state.rgb_video = videos['rgb']
        yield state, videos['normal'], videos['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name,
            "type": "online",
            "gen_params": {},
            "state": state.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }

    with open(get_conv_log_filename(), "a") as fout:
        fout.write(json.dumps(data) + "\n")
    append_json_item_on_log_server(data, get_conv_log_filename())
  
    # src_img_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_src.png'
    # os.makedirs(os.path.dirname(src_img_file), exist_ok=True)
    # with open(src_img_file, 'w') as f:
    #     state.source_image.save(f, 'PNG')
    # output_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_out.png'
    # with open(output_file, 'w') as f:
    #     state.output.save(f, 'PNG')
    # save_image_file_on_log_server(src_img_file)
    # save_image_file_on_log_server(output_file)
        
def generate_i2s_multi(gen_func, render_func, 
                       state_0, state_1, 
                       image, 
                       model_name_0, model_name_1,
                       request: gr.Request):
    if image is None:
        raise gr.Warning("Image cannot be empty.")
    if not model_name_0:
        raise gr.Warning("Model name A cannot be empty.")
    if not model_name_1:
        raise gr.Warning("Model name B cannot be empty.")
    
    if state_0 is None:
        state_0 = State(model_name_0, i2s_mode=True, offline=False)
    if state_1 is None:
        state_1 = State(model_name_1, i2s_mode=True, offline=False)

    ip = get_ip(request)
    t2s_multi_logger.info(f"generate. ip: {ip}")

    state_0.model_name, state_1.model_name = model_name_0, model_name_1
    state_0.image, state_1.image = image, image
    state_0.evaluted_dims, state_1.evaluted_dims = 0, 0

    if state_0.offline and state_0.offline_idx:
        start_time = time.time()
        videos_0, videos_1 = gen_func(image, model_name_0, model_name_1, offline=state_0.offline, offline_idx=state_0.offline_idx)
        # normal_video_0 = os.path.join(OFFLINE_DIR, "image2shape", model_name_0, "normal", f"{state_0.offline_idx}.mp4")
        # rgb_video_0 = os.path.join(OFFLINE_DIR, "image2shape", model_name_0, "rgb", f"{state_0.offline_idx}.mp4")
        # normal_video_1 = os.path.join(OFFLINE_DIR, "image2shape", model_name_1, "normal", f"{state_1.offline_idx}.mp4")
        # rgb_video_1 = os.path.join(OFFLINE_DIR, "image2shape", model_name_1, "rgb", f"{state_1.offline_idx}.mp4")
        
        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1, videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb'], \
            gr.Markdown(f"### Model A: {model_name_0}"), gr.Markdown(f"### Model B: {model_name_1}")

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "offline",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "offline",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
    else:
        start_time = time.time()
        shape_0, shape_1 = gen_func(image, model_name_0, model_name_1)
        generate_time = time.time() - start_time

        videos_0, videos_1 = render_func(shape_0, model_name_0, shape_1, model_name_1)
        finish_time = time.time()
        render_time = finish_time - start_time - generate_time

        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1, videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb']

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "online",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "online",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
    
    with open(get_conv_log_filename(), "a") as fout:
        fout.write(json.dumps(data_0) + "\n")
        fout.write(json.dumps(data_1) + "\n")
    append_json_item_on_log_server(data_0, get_conv_log_filename())
    append_json_item_on_log_server(data_1, get_conv_log_filename())
        
    # for i, state in enumerate([state_0, state_1]):
    #     src_img_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_src.png'
    #     os.makedirs(os.path.dirname(src_img_file), exist_ok=True)
    #     with open(src_img_file, 'w') as f:
    #         state.source_image.save(f, 'PNG')
    #     output_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_out.png'
    #     with open(output_file, 'w') as f:
    #         state.output.save(f, 'PNG')
    #     save_image_file_on_log_server(src_img_file)
    #     save_image_file_on_log_server(output_file)
            

def generate_i2s_multi_annoy(gen_func, render_func,
                             state_0, state_1, 
                             image, 
                             model_name_0, model_name_1, 
                             request: gr.Request):
    if image is None:
        raise gr.Warning("Image cannot be empty.")
    if state_0 is None:
        state_0 = State(model_name_0, i2s_mode=True, offline=False)
    if state_1 is None:
        state_1 = State(model_name_1, i2s_mode=True, offline=False)

    ip = get_ip(request)
    t2s_multi_logger.info(f"generate. ip: {ip}")

    state_0.model_name, state_1.model_name = model_name_0, model_name_1
    state_0.image, state_1.image = image, image
    state_0.evaluted_dims, state_1.evaluted_dims = 0, 0

    if state_0.offline and state_0.offline_idx and state_1.offline and state_1.offline_idx:
        start_time = time.time()
        videos_0, videos_1, model_name_0, model_name_1 = gen_func(image, model_name_0, model_name_1, 
                                      i2s_model=True, offline=state_0.offline, offline_idx=state_0.offline_idx)
        # normal_video_0 = os.path.join(OFFLINE_DIR, "image2shape", model_name_0, "normal", f"{state_0.offline_idx}.mp4")
        # rgb_video_0 = os.path.join(OFFLINE_DIR, "image2shape", model_name_0, "rgb", f"{state_0.offline_idx}.mp4")
        # normal_video_1 = os.path.join(OFFLINE_DIR, "image2shape", model_name_1, "normal", f"{state_1.offline_idx}.mp4")
        # rgb_video_1 = os.path.join(OFFLINE_DIR, "image2shape", model_name_1, "rgb", f"{state_1.offline_idx}.mp4")
        state_0.model_name, state_1.model_name = model_name_0, model_name_1
        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1, videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb'], \
            gr.Markdown(f"### Model A: {model_name_0}"), gr.Markdown(f"### Model B: {model_name_1}")

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "offline",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "offline",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "ip": get_ip(request),
        }
    else:
        start_time = time.time()
        shape_0, shape_1 = gen_func(image, model_name_0, model_name_1, i2s_model=True)
        generate_time = time.time() - start_time

        videos_0, videos_1 = render_func(shape_0, model_name_0, shape_1, model_name_1)
        finish_time = time.time()
        render_time = finish_time - start_time - generate_time

        state_0.normal_video, state_0.rgb_video = videos_0['normal'], videos_0['rgb']
        state_1.normal_video, state_1.rgb_video = videos_1['normal'], videos_1['rgb']
        yield state_0, state_1,  videos_0['normal'], videos_0['rgb'], videos_1['normal'], videos_1['rgb'], \
            gr.Markdown(f"### Model A: {model_name_0}"), gr.Markdown(f"### Model B: {model_name_1}")

        finish_tstamp = time.time()
        # logger.info(f"===output===: {output}")
        data_0 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_0,
            "type": "online",
            "gen_params": {},
            "state": state_0.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
        data_1 = {
            "tstamp": round(finish_tstamp, 4),
            "model": model_name_1,
            "type": "online",
            "gen_params": {},
            "state": state_1.dict(),
            "start": round(start_time, 4),
            "finish": round(finish_tstamp, 4),
            "time": round(finish_time - start_time, 4),
            "generate_time": round(generate_time, 4),
            "render_time": round(render_time, 4),
            "ip": get_ip(request),
        }
    
    with open(get_conv_log_filename(), "a") as fout:
        fout.write(json.dumps(data_0) + "\n")
        fout.write(json.dumps(data_1) + "\n")
    append_json_item_on_log_server(data_0, get_conv_log_filename())
    append_json_item_on_log_server(data_1, get_conv_log_filename())
        
    # for i, state in enumerate([state_0, state_1]):
    #     src_img_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_src.png'
    #     os.makedirs(os.path.dirname(src_img_file), exist_ok=True)
    #     with open(src_img_file, 'w') as f:
    #         state.source_image.save(f, 'PNG')
    #     output_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_out.png'
    #     with open(output_file, 'w') as f:
    #         state.output.save(f, 'PNG')
    #     save_image_file_on_log_server(src_img_file)
    #     save_image_file_on_log_server(output_file)