remove old utils
Browse files- utils_old.py +0 -201
utils_old.py
DELETED
@@ -1,201 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import json
|
3 |
-
from datetime import datetime
|
4 |
-
from pathlib import Path
|
5 |
-
from uuid import uuid4
|
6 |
-
import json
|
7 |
-
import time
|
8 |
-
import os
|
9 |
-
from huggingface_hub import CommitScheduler
|
10 |
-
from functools import partial
|
11 |
-
import pandas as pd
|
12 |
-
import numpy as np
|
13 |
-
from huggingface_hub import snapshot_download
|
14 |
-
|
15 |
-
def enable_buttons_side_by_side():
|
16 |
-
return tuple(gr.update(visible=True, interactive=True) for i in range(6))
|
17 |
-
|
18 |
-
def disable_buttons_side_by_side():
|
19 |
-
return tuple(gr.update(visible=i>=4, interactive=False) for i in range(6))
|
20 |
-
|
21 |
-
|
22 |
-
os.makedirs('data', exist_ok = True)
|
23 |
-
LOG_FILENAME = os.path.join('data', f'log_{datetime.now().isoformat()}.json')
|
24 |
-
FLAG_FILENAME = os.path.join('data', f'flagged_{datetime.now().isoformat()}.json')
|
25 |
-
|
26 |
-
enable_btn = gr.update(interactive=True, visible=True)
|
27 |
-
disable_btn = gr.update(interactive=False)
|
28 |
-
invisible_btn = gr.update(interactive=False, visible=False)
|
29 |
-
no_change_btn = gr.update(value="No Change", interactive=True, visible=True)
|
30 |
-
|
31 |
-
DS_ID = os.getenv('DS_ID')
|
32 |
-
TOKEN = os.getenv('TOKEN')
|
33 |
-
SONG_SOURCE = os.getenv("SONG_SOURCE")
|
34 |
-
LOCAL_DIR = './'
|
35 |
-
|
36 |
-
snapshot_download(repo_id=SONG_SOURCE, repo_type="dataset", token = TOKEN, local_dir = LOCAL_DIR)
|
37 |
-
print(os.listdir(LOCAL_DIR))
|
38 |
-
|
39 |
-
|
40 |
-
scheduler = CommitScheduler(
|
41 |
-
repo_id= DS_ID,
|
42 |
-
repo_type="dataset",
|
43 |
-
folder_path= os.path.dirname(LOG_FILENAME),
|
44 |
-
path_in_repo="data",
|
45 |
-
token = TOKEN,
|
46 |
-
every = 10,
|
47 |
-
)
|
48 |
-
|
49 |
-
df = pd.read_csv(os.path.join(LOCAL_DIR,'singfake_english.csv'))
|
50 |
-
df.filename = os.path.join(LOCAL_DIR, 'Songs') + '/' + df.filename + '.mp3'
|
51 |
-
|
52 |
-
|
53 |
-
indices = list(df.index)
|
54 |
-
main_indices = indices.copy()
|
55 |
-
|
56 |
-
def init_indices():
|
57 |
-
global indices, main_indices
|
58 |
-
indices = main_indices
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
def pick_and_remove_two():
|
63 |
-
global indices
|
64 |
-
if len(indices) < 2:
|
65 |
-
init_indices()
|
66 |
-
|
67 |
-
np.random.shuffle(indices)
|
68 |
-
sel_indices = indices[:2].copy()
|
69 |
-
indices = indices[2:]
|
70 |
-
return sel_indices
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
def vote_last_response(state, vote_type, request: gr.Request):
|
76 |
-
with scheduler.lock:
|
77 |
-
with open(LOG_FILENAME, "a") as fout:
|
78 |
-
data = {
|
79 |
-
"tstamp": round(time.time(), 4),
|
80 |
-
"type": vote_type,
|
81 |
-
"state0": state[0].dict(),
|
82 |
-
"state1": state[1].dict(),
|
83 |
-
"ip": get_ip(request),
|
84 |
-
}
|
85 |
-
fout.write(json.dumps(data) + "\n")
|
86 |
-
|
87 |
-
def flag_last_response(state, vote_type, request: gr.Request):
|
88 |
-
with scheduler.lock:
|
89 |
-
with open(FLAG_FILENAME, "a") as fout:
|
90 |
-
data = {
|
91 |
-
"tstamp": round(time.time(), 4),
|
92 |
-
"type": vote_type,
|
93 |
-
"state": state.dict(),
|
94 |
-
"ip": get_ip(request),
|
95 |
-
}
|
96 |
-
fout.write(json.dumps(data) + "\n")
|
97 |
-
|
98 |
-
|
99 |
-
class AudioStateIG:
|
100 |
-
def __init__(self, model_name):
|
101 |
-
self.conv_id = uuid4().hex
|
102 |
-
self.model_name = model_name
|
103 |
-
|
104 |
-
def dict(self):
|
105 |
-
base = {
|
106 |
-
"conv_id": self.conv_id,
|
107 |
-
"model_name": self.model_name,
|
108 |
-
}
|
109 |
-
return base
|
110 |
-
|
111 |
-
def get_ip(request: gr.Request):
|
112 |
-
if request:
|
113 |
-
if "cf-connecting-ip" in request.headers:
|
114 |
-
ip = request.headers["cf-connecting-ip"] or request.client.host
|
115 |
-
else:
|
116 |
-
ip = request.client.host
|
117 |
-
else:
|
118 |
-
ip = None
|
119 |
-
return ip
|
120 |
-
|
121 |
-
|
122 |
-
def get_song(idx, df = df):
|
123 |
-
row = df.loc[idx]
|
124 |
-
audio_path = row.filename
|
125 |
-
state = AudioStateIG(row['Bonafide Or Spoof'])
|
126 |
-
return state, audio_path
|
127 |
-
|
128 |
-
def generate_songs(state0, state1):
|
129 |
-
idx0, idx1 = pick_and_remove_two()
|
130 |
-
state0, audio_a = get_song(idx0)
|
131 |
-
state1, audio_b = get_song(idx1)
|
132 |
-
|
133 |
-
return state0, audio_a, state1, audio_b, "Model A: Vote to Reveal", "Model B: Vote to Reveal"
|
134 |
-
|
135 |
-
def random_sample_button(prompt):
|
136 |
-
|
137 |
-
audio_a = "marine.mp3"
|
138 |
-
audio_b = "marine.mp3"
|
139 |
-
return audio_a, audio_b
|
140 |
-
|
141 |
-
def leftvote_last_response(
|
142 |
-
state0, state1, request: gr.Request
|
143 |
-
):
|
144 |
-
vote_last_response(
|
145 |
-
[state0, state1], "leftvote", request
|
146 |
-
)
|
147 |
-
return (disable_btn,) * 6 + (
|
148 |
-
gr.Markdown(f"### Model A: {state0.model_name}", visible=True),
|
149 |
-
gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
|
150 |
-
|
151 |
-
def rightvote_last_response(
|
152 |
-
state0, state1, request: gr.Request
|
153 |
-
):
|
154 |
-
vote_last_response(
|
155 |
-
[state0, state1], "rightvote", request
|
156 |
-
)
|
157 |
-
return (disable_btn,) * 6 + (
|
158 |
-
gr.Markdown(f"### Model A: {state0.model_name}", visible=True),
|
159 |
-
gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
|
160 |
-
|
161 |
-
def tievote_last_response(
|
162 |
-
state0, state1, request: gr.Request
|
163 |
-
):
|
164 |
-
vote_last_response(
|
165 |
-
[state0, state1], "tievote", request
|
166 |
-
)
|
167 |
-
return (disable_btn,) * 6 + (
|
168 |
-
gr.Markdown(f"### Model A: {state0.model_name}", visible=True),
|
169 |
-
gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
|
170 |
-
|
171 |
-
def bothbadvote_last_response(
|
172 |
-
state0, state1, request: gr.Request
|
173 |
-
):
|
174 |
-
vote_last_response(
|
175 |
-
[state0, state1], "bothbadvote", request
|
176 |
-
)
|
177 |
-
return (disable_btn,) * 6 + (
|
178 |
-
gr.Markdown(f"### Model A: {state0.model_name}", visible=True),
|
179 |
-
gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
|
180 |
-
|
181 |
-
def leftheard_last_response(
|
182 |
-
state, request: gr.Request
|
183 |
-
):
|
184 |
-
vote_last_response(
|
185 |
-
[state], "leftheard", request
|
186 |
-
)
|
187 |
-
return (disable_btn,) * 6 + (
|
188 |
-
gr.Markdown(f"### Model A: {state0.model_name}", visible=True),
|
189 |
-
gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
|
190 |
-
|
191 |
-
|
192 |
-
def rightheard_last_response(
|
193 |
-
state, request: gr.Request
|
194 |
-
):
|
195 |
-
vote_last_response(
|
196 |
-
[state], "rightheard", request
|
197 |
-
)
|
198 |
-
return (disable_btn,) * 6 + (
|
199 |
-
gr.Markdown(f"### Model A: {state0.model_name}", visible=True),
|
200 |
-
gr.Markdown(f"### Model B: {state1.model_name}", visible=True))
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|