sonics-song-arena / utils.py
bishmoy's picture
Initial Upload
b88f9b7 verified
raw
history blame
3.89 kB
import gradio as gr
import json
from datetime import datetime
from pathlib import Path
from uuid import uuid4
import datetime, json
import time
import os
from huggingface_hub import CommitScheduler
from functools import partial
def enable_buttons_side_by_side():
return tuple(gr.update(visible=True, interactive=True) for i in range(6))
def disable_buttons_side_by_side():
return tuple(gr.update(visible=i>=4, interactive=False) for i in range(6))
def get_conv_log_filename():
LOG_FILENAME = 'data/log.json'
return LOG_FILENAME
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)
DS_ID = os.getenv('DS_ID')
TOKEN = os.getenv('TOKEN')
scheduler = CommitScheduler(
repo_id= DS_ID,
repo_type="dataset",
folder_path= os.path.dirname(get_conv_log_filename()),
path_in_repo="data",
token = TOKEN,
every = 10,
)
def vote_last_response(state, vote_type, request: gr.Request):
with scheduler.lock:
with open(get_conv_log_filename(), "a") as fout:
data = {
"tstamp": round(time.time(), 4),
"type": vote_type,
"state0": state[0].dict(),
"state1": state[1].dict(),
"ip": get_ip(request),
}
fout.write(json.dumps(data) + "\n")
class AudioStateIG:
def __init__(self, model_name):
self.conv_id = uuid4().hex
self.model_name = model_name
def dict(self):
base = {
"conv_id": self.conv_id,
"model_name": self.model_name,
}
return base
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_song():
audio_a = "/content/marine.mp3"
state = AudioStateIG("Random_Model")
return state, audio_a
def generate_songs(state0, state1):
state0, audio_a = get_song()
state1, audio_b = get_song()
return state0, audio_a, state1, audio_b, "Model A: Vote to Reveal", "Model B: Vote to Reveal"
def random_sample_button(prompt):
audio_a = "/content/marine.mp3"
audio_b = "/content/marine.mp3"
return audio_a, audio_b
def leftvote_last_response(
state0, state1, request: gr.Request
):
vote_last_response(
[state0, state1], "leftvote", request
)
return (disable_btn,) * 4 + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True))
def rightvote_last_response(
state0, state1, request: gr.Request
):
vote_last_response(
[state0, state1], "rightvote", request
)
return (disable_btn,) * 4 + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True))
def tievote_last_response(
state0, state1, request: gr.Request
):
vote_last_response(
[state0, state1], "tievote", request
)
return (disable_btn,) * 4 + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True))
def bothbadvote_last_response(
state0, state1, request: gr.Request
):
vote_last_response(
[state0, state1], "bothbadvote", request
)
return (disable_btn,) * 4 + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True))