CoolFace
Apppublic

Kate-03/GenAI-Arena

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
utils.py193 linesDownload Raw Back to serve
1import os2import json3import datetime4import requests5import numpy as np6import gradio as gr7from pathlib import Path8from model.model_registry import *9from .constants import LOGDIR, LOG_SERVER_ADDR, APPEND_JSON, SAVE_IMAGE, SAVE_VIDEO, SAVE_LOG10from typing import Union11 12 13enable_btn = gr.update(interactive=True, visible=True)14disable_btn = gr.update(interactive=False)15invisible_btn = gr.update(interactive=False, visible=False)16no_change_btn = gr.update(value="No Change", interactive=True, visible=True)17 18 19def build_about():20    about_markdown = f"""21# About Us22This is a project from TIGER Lab at University of Waterloo. 23 24## Contributors:25[Tianle Li](https://scholar.google.com/citations?user=g213g7YAAAAJ&hl=en), [Dongfu Jiang](https://jdf-prog.github.io/), [Yuansheng Ni](https://yuanshengni.github.io/), [Max Ku](https://kuwingfung.github.io/), [Shizhuo Sun](), [Richard Fan](https://www.linkedin.com/in/richard-fan2020/).26 27## Contact:28Email: dongfu.jiang@uwaterloo.ca (Dongfu Jiang), m3ku@uwaterloo.ca (Max Ku), t29li@uwaterloo.ca (Tianle Li)29 30## Advisors31[Wenhu Chen](https://wenhuchen.github.io/)32 33## Sponsorship34We are keep looking for sponsorship to support the arena project for the long term. Please contact us if you are interested in supporting this project.35 36## Acknowledgment37Our codebase is built upon <a href="https://github.com/lm-sys/FastChat" target="_blank">FastChat</a>, <a href="https://github.com/vinesmsuic/ImagenHub/tree/main" target="_blank">ImagenHub</a> and <a href="https://github.com/TIGER-AI-Lab/VideoGenHub" target="_blank">VideoGenHub</a>.38"""39 40    gr.Markdown(about_markdown, elem_id="about_markdown")41 42 43acknowledgment_md = """44### Acknowledgment45<div class="image-container">46    <p> Our codebase is built upon <a href="https://github.com/lm-sys/FastChat" target="_blank">FastChat</a>, <a href="https://github.com/vinesmsuic/ImagenHub/tree/main" target="_blank">ImagenHub</a> and <a href="https://github.com/TIGER-AI-Lab/VideoGenHub" target="_blank">VideoGenHub</a>.</p>47</div>48"""49 50block_css = """51#notice_markdown {52    font-size: 110%53}54#notice_markdown th {55    display: none;56}57#notice_markdown td {58    padding-top: 6px;59    padding-bottom: 6px;60}61#model_description_markdown {62    font-size: 110%63}64#leaderboard_markdown {65    font-size: 110%66}67#leaderboard_markdown td {68    padding-top: 6px;69    padding-bottom: 6px;70}71#leaderboard_dataframe td {72    line-height: 0.1em;73}74#about_markdown {75    font-size: 110%76}77#ack_markdown {78    font-size: 110%79}80#input_box textarea {81}82footer {83    display:none !important84}85.image-about img {86    margin: 0 30px;87    margin-top:  30px;88    height: 60px;89    max-height: 100%;90    width: auto;91    float: left;92.input-image, .image-preview {93    margin: 0 30px;94    height: 30px;95    max-height: 100%;96    width: auto;97    max-width: 30%;}98}99"""100 101def enable_buttons_side_by_side():102    return tuple(gr.update(visible=True, interactive=True) for i in range(6))103 104def disable_buttons_side_by_side():105    return tuple(gr.update(visible=i>=4, interactive=False) for i in range(6))106 107def enable_buttons():108    return tuple(gr.update(interactive=True) for _ in range(5))109 110def disable_buttons():111    return tuple(gr.update(interactive=False) for _ in range(5))112 113def clear_history():114    return None, "", None115   116def clear_history_side_by_side():117    return None, None, "", None, None118 119def clear_history_side_by_side_anony():120    return None, None, "", None, None, gr.Markdown("", visible=False), gr.Markdown("", visible=False)121 122def clear_history_ie():123    return None, "", "", "", None, None124   125def clear_history_side_by_side_ie():126    return None, None, "", "", "", None, None, None127 128def clear_history_side_by_side_ie_anony():129    return None, None, "", "", "", None, None, None, gr.Markdown("", visible=False), gr.Markdown("", visible=False)130 131def get_ip(request: gr.Request):132    if request:133        if "cf-connecting-ip" in request.headers:134            ip = request.headers["cf-connecting-ip"] or request.client.host135        else:136            ip = request.client.host137    else:138        ip = None139    return ip140 141def get_conv_log_filename():142    t = datetime.datetime.now()143    name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-conv.json")144    if not os.path.exists(os.path.dirname(name)):145        os.makedirs(os.path.dirname(name))146    return name147 148def get_nsfw_conv_log_filename():149    t = datetime.datetime.now()150    name = os.path.join(LOGDIR, f"nsfw/{t.year}-{t.month:02d}-{t.day:02d}-conv.json")151    if not os.path.exists(os.path.dirname(name)):152        os.makedirs(os.path.dirname(name))153    return name154 155def save_image_file_on_log_server(image_file:str):156 157    image_file = Path(image_file).absolute().relative_to(os.getcwd())158    image_file = str(image_file)159    # Open the image file in binary mode160    url = f"{LOG_SERVER_ADDR}/{SAVE_IMAGE}"161    with open(image_file, 'rb') as f:162        # Make the POST request, sending the image file and the image path163        response = requests.post(url, files={'image': f}, data={'image_path': image_file})164    return response165 166def save_video_file_on_log_server(video_file:str):167 168    video_file = Path(video_file).absolute().relative_to(os.getcwd())169    video_file = str(video_file)170    # Open the video file in binary mode171    url = f"{LOG_SERVER_ADDR}/{SAVE_VIDEO}"172    with open(video_file, 'rb') as f:173        # Make the POST request, sending the video file and the video path174        response = requests.post(url, files={'video': f}, data={'video_path': video_file})175    return response176 177def append_json_item_on_log_server(json_item: Union[dict, str], log_file: str):178    if isinstance(json_item, dict):179        json_item = json.dumps(json_item)180    log_file = Path(log_file).absolute().relative_to(os.getcwd())181    log_file = str(log_file)182    url = f"{LOG_SERVER_ADDR}/{APPEND_JSON}"183    # Make the POST request, sending the JSON string and the log file name184    response = requests.post(url, data={'json_str': json_item, 'file_name': log_file})185    return response186 187def save_log_str_on_log_server(log_str: str, log_file: str):188    log_file = Path(log_file).absolute().relative_to(os.getcwd())189    log_file = str(log_file)190    url = f"{LOG_SERVER_ADDR}/{SAVE_LOG}"191    # Make the POST request, sending the log message and the log file name192    response = requests.post(url, data={'message': log_str, 'log_path': log_file})193    return response