Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,618 Bytes
d40e945 cdaf2d8 |
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 |
from .utils import *
from .config import *
from .models import *
from .db import *
from .init import *
import gradio as gr
# Logging
def log_text(text):
conn = get_db()
cursor = conn.cursor()
cursor.execute('INSERT INTO spokentext (spokentext) VALUES (?)', (text,))
if scheduler:
with scheduler.lock:
conn.commit()
else:
conn.commit()
cursor.close()
# Vote
def upvote_model(model, uname, battle=False):
conn = get_db()
cursor = conn.cursor()
if battle: uname = "unknown_battle"
cursor.execute('UPDATE model SET upvote = upvote + 1 WHERE name = ?', (model,))
if cursor.rowcount == 0:
cursor.execute('INSERT OR REPLACE INTO model (name, upvote, downvote) VALUES (?, 1, 0)', (model,))
cursor.execute('INSERT INTO vote (username, model, vote) VALUES (?, ?, ?)', (uname, model, 1,))
if scheduler:
with scheduler.lock:
conn.commit()
else:
conn.commit()
cursor.close()
def downvote_model(model, uname, battle=False):
conn = get_db()
cursor = conn.cursor()
if battle: uname = "unknown_battle"
cursor.execute('UPDATE model SET downvote = downvote + 1 WHERE name = ?', (model,))
if cursor.rowcount == 0:
cursor.execute('INSERT OR REPLACE INTO model (name, upvote, downvote) VALUES (?, 0, 1)', (model,))
cursor.execute('INSERT INTO vote (username, model, vote) VALUES (?, ?, ?)', (uname, model, -1,))
if scheduler:
with scheduler.lock:
conn.commit()
else:
conn.commit()
cursor.close()
# Battle Mode
def a_is_better_battle(model1, model2, userid):
return a_is_better(model1, model2, 'unknown_battle', True)
def b_is_better_battle(model1, model2, userid):
return b_is_better(model1, model2, 'unknown_battle', True)
# A/B better
def a_is_better(model1, model2, userid, battle=False):
print("A is better", model1, model2)
if not model1 in AVAILABLE_MODELS.keys() and not model1 in AVAILABLE_MODELS.values():
raise gr.Error('Sorry, please try voting again.')
userid = mkuuid(userid)
if model1 and model2:
conn = get_db()
cursor = conn.cursor()
cursor.execute('INSERT INTO votelog (username, chosen, rejected) VALUES (?, ?, ?)', (str(userid), model1, model2,))
if scheduler:
with scheduler.lock:
conn.commit()
else:
conn.commit()
cursor.close()
upvote_model(model1, str(userid), battle)
downvote_model(model2, str(userid), battle)
return reload(model1, model2, userid, chose_a=True)
def b_is_better(model1, model2, userid, battle=False):
print("B is better", model1, model2)
if not model1 in AVAILABLE_MODELS.keys() and not model1 in AVAILABLE_MODELS.values():
raise gr.Error('Sorry, please try voting again.')
userid = mkuuid(userid)
if model1 and model2:
conn = get_db()
cursor = conn.cursor()
cursor.execute('INSERT INTO votelog (username, chosen, rejected) VALUES (?, ?, ?)', (str(userid), model2, model1,))
if scheduler:
with scheduler.lock:
conn.commit()
else:
conn.commit()
cursor.close()
upvote_model(model2, str(userid), battle)
downvote_model(model1, str(userid), battle)
return reload(model1, model2, userid, chose_b=True)
# Reload
def reload(chosenmodel1=None, chosenmodel2=None, userid=None, chose_a=False, chose_b=False):
out = [
gr.update(interactive=False, visible=False),
gr.update(interactive=False, visible=False)
]
if chose_a == True:
out.append(gr.update(value=f'Your vote: {chosenmodel1}', interactive=False, visible=True))
out.append(gr.update(value=f'{chosenmodel2}', interactive=False, visible=True))
else:
out.append(gr.update(value=f'{chosenmodel1}', interactive=False, visible=True))
out.append(gr.update(value=f'Your vote: {chosenmodel2}', interactive=False, visible=True))
out.append(gr.update(visible=True))
return out
def unlock_vote(autoplay, btn_index, aplayed, bplayed):
if autoplay == False:
return [gr.update(), gr.update(), aplayed, bplayed]
# sample played
if btn_index == 0:
aplayed = True
if btn_index == 1:
bplayed = True
# both audio samples played
if bool(aplayed) and bool(bplayed):
# print('Both audio samples played, voting unlocked')
return [gr.update(interactive=True), gr.update(interactive=True), True, True]
return [gr.update(), gr.update(), aplayed, bplayed] |