K-Sort-Arena / serve /utils.py
ksort's picture
add image_cache
e1665ec
raw
history blame
10.1 kB
import os
import json
import datetime
import requests
import numpy as np
import gradio as gr
from pathlib import Path
from model.model_registry import *
from .constants import LOGDIR, LOG_SERVER_ADDR, APPEND_JSON, SAVE_IMAGE, SAVE_VIDEO, SAVE_LOG
from typing import Union
enable_btn = gr.update(interactive=True, visible=True)
disable_btn = gr.update(interactive=False)
invisible_btn = gr.update(interactive=False, visible=False)
no_change_btn = gr.update(value="No Change", interactive=True, visible=True)
def build_about():
about_markdown = f"""
# About Us
"""
gr.Markdown(about_markdown, elem_id="about_markdown")
acknowledgment_md = """
### Acknowledgment
<div class="image-container">
<p> Our codebase is built upon <a href="https://github.com/lm-sys/FastChat" target="_blank">FastChat</a>, <a href="https://github.com/TIGER-AI-Lab/ImagenHub" target="_blank">ImagenHub</a>.</p>
</div>
"""
# 定义一个HTML组件来创建链接和处理点击事件
html_code = """
<p> This platform is designed for academic usage, for details please refer to <a href="#" id="open_link">disclaimer</a>.</p>
<p id="link_text" style="display:none;">这里是弹出的文本链接: <a href="https://example.com" target="_blank">example.com</a></p>
<script>
document.getElementById('open_link').addEventListener('click', function() {
document.getElementById('link_text').style.display = 'block';
});
</script>
"""
block_css = """
#notice_markdown {
font-size: 110%
}
#notice_markdown th {
display: none;
}
#notice_markdown td {
padding-top: 6px;
padding-bottom: 6px;
}
#model_description_markdown {
font-size: 110%
}
#leaderboard_markdown {
font-size: 110%
}
#leaderboard_markdown td {
padding-top: 6px;
padding-bottom: 6px;
}
#leaderboard_dataframe td {
line-height: 0.1em;
}
#about_markdown {
font-size: 110%
}
#ack_markdown {
font-size: 110%
}
#input_box textarea {
}
footer {
display:none !important
}
.image-about img {
margin: 0 30px;
margin-top: 30px;
height: 60px;
max-height: 100%;
width: auto;
float: left;
.input-image, .image-preview {
margin: 0 30px;
height: 30px;
max-height: 100%;
width: auto;
max-width: 30%;}
}
.custom-button {
border-radius: 8px;
}
.best-button {
border-radius: 8px;
}
.row {
display: flex;
justify-content: space-between;
}
.send-button {
background: rgb(168, 230, 207);
color: rgb(0, 198, 171);
}
.submit-button {
color: red;
}
#custom-width {width: 100px !important;}
#centered-row {
display: flex;
justify-content: center;
}
#btncolor1 {background: rgb(168, 230, 207);}
#btncolor2 {background: rgb(253, 255, 171);}
#btncolor3 {background: rgb(255, 211, 182);}
#btncolor4 {background: rgb(255, 170, 165);}
#btnblue {background: linear-gradient(to bottom right, rgb(222, 235, 247), rgb(189,215,238)); color: rgb(0, 112, 192); border: 1px solid rgb(189,215,238);}
#btnpink {background: rgb(255, 168, 184);}
#centered-text { display: flex; justify-content: center; align-items: center; height: 100%; width: 100%; font-size: 150%; }
"""
#
#btncolor1 {background: rgb(128, 214, 255);}
#btncolor2 {background: rgb(237, 247, 152);}
#btncolor3 {background: rgb(250, 181, 122);}
#btncolor4 {background: rgb(240, 104, 104);}
#btncolor1 {background: rgb(112, 161, 215);}
#btncolor2 {background: rgb(161, 222, 147);}
#btncolor3 {background: rgb(247, 244, 139);}
#btncolor4 {background: rgb(244, 124, 124);}
#btncolor1 {background: rgb(168, 230, 207);}
#btncolor2 {background: rgb(253, 255, 171);}
#btncolor3 {background: rgb(255, 211, 182);}
#btncolor4 {background: rgb(255, 170, 165);}
#btncolor1 {background: rgb(255, 212, 96);}
#btncolor2 {background: rgb(240, 123, 63);}
#btncolor3 {background: rgb(234, 84, 85);}
#btncolor4 {background: rgb(45, 64, 89);}
#btncolor1 {background: rgb(255, 189, 57);}
#btncolor2 {background: rgb(230, 28, 93);}
#btncolor3 {background: rgb(147, 0, 119);}
#btncolor4 {background: rgb(58, 0, 136);}
# max-width: 100px;
# .custom-button {
# padding: 10px 15px;
# text-align: center;
# text-decoration: none;
# display: inline-block;
# font-size: 16px;
# cursor: pointer;
# border-radius: 8px;
# }
# {
# background-color: green; /* 背景颜色 */
# color: white; /* 文字颜色 */
# border: none; /* 无边框 */
# padding: 10px 20px; /* 内边距 */
# text-align: center; /* 文本居中 */
# text-decoration: none; /* 无下划线 */
# display: inline-block; /* 行内块 */
# font-size: 16px; /* 字体大小 */
# margin: 4px 2px; /* 外边距 */
# cursor: pointer; /* 鼠标指针 */
# border-radius: 5px; /* 圆角边框 */
# }
# .custom-button:hover {
# background-color: darkgreen; /* 悬停时的背景颜色 */
# }
def enable_loop_buttons():
return tuple(gr.update(loop=True) for i in range(4))
def enable_vote_buttons():
return tuple(gr.update(visible=True, interactive=i<=4) for i in range(6))
def disable_vote_buttons():
return tuple(gr.update(visible=False, interactive=False) for i in range(6))
def disable_vote():
return tuple(gr.update(interactive=False) for i in range(18))
def enable_vote_mode_buttons(mode, textbox):
print(mode)
if not textbox.strip():
return tuple(gr.update(visible=False, interactive=False) for _ in range(24))
else:
if mode == "Best":
return (gr.update(visible=True, interactive=True),) * 5 + \
(gr.update(visible=False, interactive=False),) * 16 + \
(gr.update(visible=True, interactive=False),) * 2 + \
(gr.update(visible=True, interactive=True),)
elif mode == "Rank":
return (gr.update(visible=False, interactive=False),) * 5 + \
(gr.update(visible=True, interactive=True),) * 16 + \
(gr.update(visible=True, interactive=True),) * 3
def disable_vote_mode_buttons():
return tuple(gr.update(visible=False, interactive=False) for _ in range(24))
def enable_order_buttons():
return tuple(gr.update(interactive=True) for _ in range(5))
def disable_order_buttons(textbox, cache="False"):
if cache=="True":
return (gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=True))
if not textbox.strip():
return (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True))
else:
return (gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=True))
def enable_video_order_buttons():
return tuple(gr.update(interactive=True) for _ in range(4))
def disable_video_order_buttons():
return (gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=True))
def clear_history():
return None, "", None
def clear_history_side_by_side():
return None, None, "", None, None
def clear_history_side_by_side_anony():
return None, None, None, None, gr.update(visible=True, interactive=True, value=""), gr.update(visible=True, interactive=True, value=""), None, None, None, None, \
gr.Markdown("", visible=False), gr.Markdown("", visible=False), gr.Markdown("", visible=False), gr.Markdown("", visible=False)
def clear_history_ie():
return None, "", "", "", None, None
def clear_history_side_by_side_ie():
return None, None, "", "", "", None, None, None
def clear_history_side_by_side_ie_anony():
return None, None, "", "", "", None, None, None, gr.Markdown("", visible=False), gr.Markdown("", visible=False)
def get_ip(request: gr.Request):
if request:
if "cf-connecting-ip" in request.headers:
ip = request.headers["cf-connecting-ip"] or request.client.host
else:
ip = request.client.host
else:
ip = None
return ip
def get_conv_log_filename():
t = datetime.datetime.now()
name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-conv.json")
return name
def save_image_file_on_log_server(image_file:str):
return 1
image_file = Path(image_file).absolute().relative_to(os.getcwd())
image_file = str(image_file)
# Open the image file in binary mode
url = f"{LOG_SERVER_ADDR}/{SAVE_IMAGE}"
with open(image_file, 'rb') as f:
# Make the POST request, sending the image file and the image path
response = requests.post(url, files={'image': f}, data={'image_path': image_file})
return response
def save_video_file_on_log_server(video_file:str):
return 1
video_file = Path(video_file).absolute().relative_to(os.getcwd())
video_file = str(video_file)
# Open the video file in binary mode
url = f"{LOG_SERVER_ADDR}/{SAVE_VIDEO}"
with open(video_file, 'rb') as f:
# Make the POST request, sending the video file and the video path
response = requests.post(url, files={'video': f}, data={'video_path': video_file})
return response
def append_json_item_on_log_server(json_item: Union[dict, str], log_file: str):
return 1
if isinstance(json_item, dict):
json_item = json.dumps(json_item)
log_file = Path(log_file).absolute().relative_to(os.getcwd())
log_file = str(log_file)
url = f"{LOG_SERVER_ADDR}/{APPEND_JSON}"
# Make the POST request, sending the JSON string and the log file name
response = requests.post(url, data={'json_str': json_item, 'file_name': log_file})
return response
def save_log_str_on_log_server(log_str: str, log_file: str):
return 1
log_file = Path(log_file).absolute().relative_to(os.getcwd())
log_file = str(log_file)
url = f"{LOG_SERVER_ADDR}/{SAVE_LOG}"
# Make the POST request, sending the log message and the log file name
response = requests.post(url, data={'message': log_str, 'log_path': log_file})
return response