Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,864 Bytes
d40e945 cc1e3ba d40e945 cc1e3ba d40e945 cc1e3ba d40e945 cc1e3ba d40e945 cc1e3ba d40e945 cc1e3ba d40e945 cc1e3ba d40e945 cc1e3ba d40e945 1973202 d40e945 cc1e3ba d40e945 48445ab d40e945 48445ab 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 132 133 134 135 136 137 138 139 140 141 142 143 |
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, text):
return is_better(model1, model2, userid, text, True)
def b_is_better(model1, model2, userid, text):
return is_better(model1, model2, userid, text, False)
def is_better(model1, model2, userid, text, chose_a):
if(
(
not model1 in AVAILABLE_MODELS.keys()
and not model1 in AVAILABLE_MODELS.values()
)
or (
not model2 in AVAILABLE_MODELS.keys()
and not model2 in AVAILABLE_MODELS.values()
)
):
raise gr.Error('Sorry, please try voting again.')
# userid is unique for each cast vote pair
userid = mkuuid(userid)
if model1 and model2:
conn = get_db()
cursor = conn.cursor()
sql_query = 'INSERT INTO votelog (username, chosen, rejected) VALUES (?, ?, ?)'
if chose_a:
cursor.execute(sql_query, (str(userid), model1, model2))
else:
cursor.execute(sql_query, (str(userid), model2, model1))
with scheduler.lock:
conn.commit()
# also retrieve primary key ID
cursor.execute('SELECT last_insert_rowid()')
votelogid = cursor.fetchone()[0]
cursor.close()
if chose_a:
upvote_model(model1, str(userid))
downvote_model(model2, str(userid))
else:
upvote_model(model2, str(userid))
downvote_model(model1, str(userid))
log_text(text)
# log_text(text, votelogid)
return reload(model1, model2, userid, chose_a=chose_a, chose_b=(not chose_a))
# Reload
def reload(chosenmodel1=None, chosenmodel2=None, userid=None, chose_a=False, chose_b=False):
chosenmodel1 = make_link_to_space(chosenmodel1)
chosenmodel2 = make_link_to_space(chosenmodel2)
out = [
gr.update(interactive=False, visible=False),
gr.update(interactive=False, visible=False)
]
style = 'text-align: center; font-size: 1rem; margin-bottom: 0; padding: var(--input-padding)'
if chose_a == True:
out.append(gr.HTML(value=f'<p style="{style}">Your vote: {chosenmodel1}</p>', visible=True))
out.append(gr.HTML(value=f'<p style="{style}">{chosenmodel2}</p>', visible=True))
else:
out.append(gr.HTML(value=f'<p style="{style}">{chosenmodel1}</p>', visible=True))
out.append(gr.HTML(value=f'<p style="{style}">Your vote: {chosenmodel2}</p>', 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] |