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1d3f
7c1eee1
f8f1d3f
 
 
 
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
 
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
 
7c1eee1
 
 
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
7c1eee1
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
7c1eee1
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
7c1eee1
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
7c1eee1
 
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
6296cf9
f8f1d3f
 
 
 
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
6296cf9
f8f1d3f
 
 
 
 
7c1eee1
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
6296cf9
f8f1d3f
 
 
 
 
7c1eee1
 
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
7c1eee1
f8f1d3f
 
 
6296cf9
f8f1d3f
 
 
 
 
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
import datetime
import time
import json
import uuid
import gradio as gr
from pathlib import Path
from .utils import *
from .log_utils import build_logger
from constants import IMAGE_DIR, EVALUATE_DIMS

t2s_logger = build_logger("gradio_web_server_text2shape", "gr_web_text2shape.log") # t2s = image generation, loggers for single model direct chat
t2s_multi_logger = build_logger("gradio_web_server_text2shape_multi", "gr_web_text2shape_multi.log") # t2s_multi = image generation multi, loggers for side-by-side and battle
i2s_logger = build_logger("gradio_web_server_image2shape", "gr_web_image2shape.log") # i2s = image editing, loggers for single model direct chat
i2s_multi_logger = build_logger("gradio_web_server_image2shape_multi", "gr_web_image2shape_multi.log") # i2s_multi = image editing multi, loggers for side-by-side and battle

def vote_last_response_t2s(state, dim, vote_type, model_selector, request: gr.Request):
    with open(get_conv_log_filename(), "a") as fout:
        data = {
            "tstamp": round(time.time(), 4),
            "dim": dim,
            "type": vote_type,
            "model": model_selector,
            "state": state.dict(),
            "ip": get_ip(request),
        }
        fout.write(json.dumps(data) + "\n")
        append_json_item_on_log_server(data, get_conv_log_filename())
        
def vote_last_response_t2s_multi(states, dim, vote_type, model_selectors, request: gr.Request):
    with open(get_conv_log_filename(), "a") as fout:
        data = {
            "tstamp": round(time.time(), 4),
            "dim": dim,
            "type": vote_type,
            "models": [x for x in model_selectors],
            "states": [x.dict() for x in states],
            "ip": get_ip(request),
        }
        fout.write(json.dumps(data) + "\n")
        append_json_item_on_log_server(data, get_conv_log_filename())
    # for state in states:
    #     output_file = f'{IMAGE_DIR}/text2shape/{state.conv_id}.png'
    #     with open(output_file, 'w') as f:
    #         state.output.save(f, 'PNG')
    #     save_image_file_on_log_server(output_file)

def vote_last_response_i2s(state, dim, vote_type, model_selector, request: gr.Request):
    with open(get_conv_log_filename(), "a") as fout:
        data = {
            "tstamp": round(time.time(), 4),
            "dim": dim,
            "type": vote_type,
            "model": model_selector,
            "state": state.dict(),
            "ip": get_ip(request),
        }
        fout.write(json.dumps(data) + "\n")
        append_json_item_on_log_server(data, get_conv_log_filename())
    # output_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}.png'
    # source_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_source.png'
    # with open(output_file, 'w') as f:
    #     state.output.save(f, 'PNG')
    # with open(source_file, 'w') as sf:
    #     state.source_image.save(sf, 'PNG')
    # save_image_file_on_log_server(output_file)
    # save_image_file_on_log_server(source_file)
        
def vote_last_response_i2s_multi(states, dim, vote_type, model_selectors, request: gr.Request):
    with open(get_conv_log_filename(), "a") as fout:
        data = {
            "tstamp": round(time.time(), 4),
            "dim": dim,
            "type": vote_type,
            "models": [x for x in model_selectors],
            "states": [x.dict() for x in states],
            "ip": get_ip(request),
        }
        fout.write(json.dumps(data) + "\n")
        append_json_item_on_log_server(data, get_conv_log_filename())
    # for state in states:
    #     output_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}.png'
    #     source_file = f'{IMAGE_DIR}/image2shape/{state.conv_id}_source.png'
    #     with open(output_file, 'w') as f:
    #         state.output.save(f, 'PNG')
    #     with open(source_file, 'w') as sf:
    #         state.source_image.save(sf, 'PNG')
    #     save_image_file_on_log_server(output_file)
    #     save_image_file_on_log_server(source_file)
            

## Text-to-Shape Generation (t2s) Single Model Direct Chat
def upvote_last_response_t2s(state, model_selector, dim_md, request: gr.Request):
    ip = get_ip(request)
    t2s_logger.info(f"upvote [{dim_md}]. ip: {ip}")
    vote_last_response_t2s(state, dim_md, "upvote", model_selector, request)
    state.evaluted_dims += 1
    return (state,) + (disable_btn,) * 3

def downvote_last_response_t2s(state, model_selector, dim_md, request: gr.Request):
    ip = get_ip(request)
    t2s_logger.info(f"downvote [{dim_md}]. ip: {ip}")
    vote_last_response_t2s(state, dim_md, "downvote", model_selector, request)
    state.evaluted_dims += 1
    return (state,) + (disable_btn,) * 3

def flag_last_response_t2s(state, model_selector, dim_md, request: gr.Request):
    ip = get_ip(request)
    t2s_logger.info(f"flag [{dim_md}]. ip: {ip}")
    vote_last_response_t2s(state, dim_md, "flag", model_selector, request)
    state.evaluted_dims += 1
    return (state,) + (disable_btn,) * 3


## Text-to-Shape Generation Multi (t2s_multi) Side-by-Side and Battle
def leftvote_last_response_t2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"leftvote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "leftvote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4

def rightvote_last_response_t2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"rightvote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "rightvote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4

def tievote_last_response_t2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"tievote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "tievote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4

def bothbad_vote_last_response_t2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"bothbad_vote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "bothbad_vote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4


def leftvote_last_response_t2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"leftvote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "leftvote", [model_selector0, model_selector1], request
    )

    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")

def rightvote_last_response_t2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"rightvote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "rightvote", [model_selector0, model_selector1], request
    )

    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")

def tievote_last_response_t2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"tievote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "tievote", [model_selector0, model_selector1], request
    )

    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")
    
def bothbad_vote_last_response_t2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    t2s_multi_logger.info(f"bothbad_vote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_t2s_multi(
        [state0, state1], dim_md, "bothbad_vote", [model_selector0, model_selector1], request
    )
    
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")

## Image-to-Shape (i2s) Single Model Direct Chat
def upvote_last_response_i2s(state, model_selector, dim_md, request: gr.Request):
    ip = get_ip(request)
    i2s_logger.info(f"upvote [{dim_md}]. ip: {ip}")
    vote_last_response_i2s(state, dim_md, "upvote", model_selector, request)
    state.evaluted_dims += 1
    return (state,) + (disable_btn,) * 3

def downvote_last_response_i2s(state, model_selector, dim_md, request: gr.Request):
    ip = get_ip(request)
    i2s_logger.info(f"downvote [{dim_md}]. ip: {ip}")
    vote_last_response_i2s(state, dim_md, "downvote", model_selector, request)
    state.evaluted_dims += 1
    return (state,) + (disable_btn,) * 3

def flag_last_response_i2s(state, model_selector, dim_md, request: gr.Request):
    ip = get_ip(request)
    i2s_logger.info(f"flag [{dim_md}]. ip: {ip}")
    vote_last_response_i2s(state, dim_md, "flag", model_selector, request)
    state.evaluted_dims += 1
    return (state,) + (disable_btn,) * 3


## Image-to-Shape Multi (i2s_multi) Side-by-Side and Battle
def leftvote_last_response_i2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"leftvote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "leftvote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4
        
def rightvote_last_response_i2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"rightvote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "rightvote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4

def tievote_last_response_i2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"tievote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "tievote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4

def bothbad_vote_last_response_i2s_named(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"bothbad_vote [{dim_md}] (named). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "bothbad_vote", [model_selector0, model_selector1], request
    )
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    return (state0, state1) + (disable_btn,) * 4


def leftvote_last_response_i2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"leftvote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "leftvote", [model_selector0, model_selector1], request
    )

    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")
        

def rightvote_last_response_i2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"rightvote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "rightvote", [model_selector0, model_selector1], request
    )

    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")
        

def tievote_last_response_i2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"tievote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "tievote", [model_selector0, model_selector1], request
    )
    
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")
        

def bothbad_vote_last_response_i2s_anony(
    state0, state1, model_selector0, model_selector1, dim_md, request: gr.Request
):
    i2s_multi_logger.info(f"bothbad_vote [{dim_md}] (anony). ip: {get_ip(request)}")
    vote_last_response_i2s_multi(
        [state0, state1], dim_md, "bothbad_vote", [model_selector0, model_selector1], request
    )
    
    state0.evaluted_dims += 1
    state1.evaluted_dims += 1
    if state0.evaluted_dims == state1.evaluted_dims == EVALUATE_DIMS:
        names = (gr.Markdown(f"### Model A: {state0.model_name}", visible=True), gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
        return (state0, state1) + (disable_btn,) * 4 + names
    else:
        return (state0, state1) + (disable_btn,) * 4 + ("", "")
        


share_js = """
function (a, b, c, d) {
    const captureElement = document.querySelector('#share-region-named');
    html2canvas(captureElement)
        .then(canvas => {
            canvas.style.display = 'none'
            document.body.appendChild(canvas)
            return canvas
        })
        .then(canvas => {
            const image = canvas.toDataURL('image/png')
            const a = document.createElement('a')
            a.setAttribute('download', 'chatbot-arena.png')
            a.setAttribute('href', image)
            a.click()
            canvas.remove()
        });
    return [a, b, c, d];
}
"""
def share_click_t2s_multi(state0, state1, model_selector0, model_selector1, request: gr.Request):
    t2s_multi_logger.info(f"share (anony). ip: {get_ip(request)}")
    if state0 is not None and state1 is not None:
        vote_last_response_t2s_multi(
            [state0, state1], "share", [model_selector0, model_selector1], request
        )

def share_click_i2s_multi(state0, state1, model_selector0, model_selector1, request: gr.Request):
    i2s_multi_logger.info(f"share (anony). ip: {get_ip(request)}")
    if state0 is not None and state1 is not None:
        vote_last_response_i2s_multi(
            [state0, state1], "share", [model_selector0, model_selector1], request
        )