Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 8,840 Bytes
d40e945 |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
from .models import *
from .utils import *
from .config import *
from .init import *
import gradio as gr
from pydub import AudioSegment
import random, os, threading, tempfile
from langdetect import detect
from .vote import log_text
def random_m():
return random.sample(list(set(AVAILABLE_MODELS.keys())), 2)
def check_toxicity(text):
if not TOXICITY_CHECK:
return False
return toxicity.predict(text)['toxicity'] > 0.8
def synthandreturn(text):
text = text.strip()
if len(text) > MAX_SAMPLE_TXT_LENGTH:
raise gr.Error(f'You exceeded the limit of {MAX_SAMPLE_TXT_LENGTH} characters')
if len(text) < MIN_SAMPLE_TXT_LENGTH:
raise gr.Error(f'Please input a text longer than {MIN_SAMPLE_TXT_LENGTH} characters')
if (
# test toxicity if not prepared text
text not in sents
and check_toxicity(text)
):
print(f'Detected toxic content! "{text}"')
raise gr.Error('Your text failed the toxicity test')
if not text:
raise gr.Error(f'You did not enter any text')
# Check language
try:
if not detect(text) == "en":
gr.Warning('Warning: The input text may not be in English')
except:
pass
# Get two random models
mdl1, mdl2 = random.sample(list(AVAILABLE_MODELS.keys()), 2)
log_text(text)
print("[debug] Using", mdl1, mdl2)
def predict_and_update_result(text, model, result_storage):
try:
if model in AVAILABLE_MODELS:
result = router.predict(text, AVAILABLE_MODELS[model].lower(), api_name="/synthesize")
else:
result = router.predict(text, model.lower(), api_name="/synthesize")
except:
raise gr.Error('Unable to call API, please try again :)')
print('Done with', model)
# try:
# doresample(result)
# except:
# pass
try:
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
audio = AudioSegment.from_file(result)
current_sr = audio.frame_rate
if current_sr > 24000:
audio = audio.set_frame_rate(24000)
try:
print('Trying to normalize audio')
audio = match_target_amplitude(audio, -20)
except:
print('[WARN] Unable to normalize audio')
audio.export(f.name, format="wav")
os.unlink(result)
result = f.name
except:
pass
if model in AVAILABLE_MODELS.keys(): model = AVAILABLE_MODELS[model]
print(model)
print(f"Running model {model}")
result_storage[model] = result
# try:
# doloudnorm(result)
# except:
# pass
mdl1k = mdl1
mdl2k = mdl2
print(mdl1k, mdl2k)
if mdl1 in AVAILABLE_MODELS.keys(): mdl1k=AVAILABLE_MODELS[mdl1]
if mdl2 in AVAILABLE_MODELS.keys(): mdl2k=AVAILABLE_MODELS[mdl2]
results = {}
print(f"Sending models {mdl1k} and {mdl2k} to API")
thread1 = threading.Thread(target=predict_and_update_result, args=(text, mdl1k, results))
thread2 = threading.Thread(target=predict_and_update_result, args=(text, mdl2k, results))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
#debug
# print(results)
# print(list(results.keys())[0])
# y, sr = librosa.load(results[list(results.keys())[0]], sr=None)
# print(sr)
# print(list(results.keys())[1])
# y, sr = librosa.load(results[list(results.keys())[1]], sr=None)
# print(sr)
#debug
# outputs = [text, btn, r2, model1, model2, aud1, aud2, abetter, bbetter, prevmodel1, prevmodel2, nxtroundbtn]
print(f"Retrieving models {mdl1k} and {mdl2k} from API")
return (
text,
"Synthesize",
gr.update(visible=True), # r2
mdl1, # model1
mdl2, # model2
gr.update(visible=True, value=results[mdl1k]), # aud1
gr.update(visible=True, value=results[mdl2k]), # aud2
gr.update(visible=True, interactive=False), #abetter
gr.update(visible=True, interactive=False), #bbetter
gr.update(visible=False), #prevmodel1
gr.update(visible=False), #prevmodel2
gr.update(visible=False), #nxt round btn
)
# Battle Mode
def synthandreturn_battle(text, mdl1, mdl2):
if mdl1 == mdl2:
raise gr.Error('You can\'t pick two of the same models.')
text = text.strip()
if len(text) > MAX_SAMPLE_TXT_LENGTH:
raise gr.Error(f'You exceeded the limit of {MAX_SAMPLE_TXT_LENGTH} characters')
if len(text) < MIN_SAMPLE_TXT_LENGTH:
raise gr.Error(f'Please input a text longer than {MIN_SAMPLE_TXT_LENGTH} characters')
if (
# test toxicity if not prepared text
text not in sents
and check_toxicity(text)
):
print(f'Detected toxic content! "{text}"')
raise gr.Error('Your text failed the toxicity test')
if not text:
raise gr.Error(f'You did not enter any text')
# Check language
try:
if not detect(text) == "en":
gr.Warning('Warning: The input text may not be in English')
except:
pass
# Get two random models
log_text(text)
print("[debug] Using", mdl1, mdl2)
def predict_and_update_result(text, model, result_storage):
try:
if model in AVAILABLE_MODELS:
result = router.predict(text, AVAILABLE_MODELS[model].lower(), api_name="/synthesize")
else:
result = router.predict(text, model.lower(), api_name="/synthesize")
except:
raise gr.Error('Unable to call API, please try again :)')
print('Done with', model)
# try:
# doresample(result)
# except:
# pass
try:
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
audio = AudioSegment.from_file(result)
current_sr = audio.frame_rate
if current_sr > 24000:
audio = audio.set_frame_rate(24000)
try:
print('Trying to normalize audio')
audio = match_target_amplitude(audio, -20)
except:
print('[WARN] Unable to normalize audio')
audio.export(f.name, format="wav")
os.unlink(result)
result = f.name
except:
pass
if model in AVAILABLE_MODELS.keys(): model = AVAILABLE_MODELS[model]
print(model)
print(f"Running model {model}")
result_storage[model] = result
# try:
# doloudnorm(result)
# except:
# pass
mdl1k = mdl1
mdl2k = mdl2
print(mdl1k, mdl2k)
if mdl1 in AVAILABLE_MODELS.keys(): mdl1k=AVAILABLE_MODELS[mdl1]
if mdl2 in AVAILABLE_MODELS.keys(): mdl2k=AVAILABLE_MODELS[mdl2]
results = {}
print(f"Sending models {mdl1k} and {mdl2k} to API")
thread1 = threading.Thread(target=predict_and_update_result, args=(text, mdl1k, results))
thread2 = threading.Thread(target=predict_and_update_result, args=(text, mdl2k, results))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(f"Retrieving models {mdl1k} and {mdl2k} from API")
return (
text,
"Synthesize",
gr.update(visible=True), # r2
mdl1, # model1
mdl2, # model2
gr.update(visible=True, value=results[mdl1k]), # aud1
gr.update(visible=True, value=results[mdl2k]), # aud2
gr.update(visible=True, interactive=False), #abetter
gr.update(visible=True, interactive=False), #bbetter
gr.update(visible=False), #prevmodel1
gr.update(visible=False), #prevmodel2
gr.update(visible=False), #nxt round btn
)
# Unlock vote
def unlock_vote(btn_index, aplayed, bplayed):
# sample played
if btn_index == 0:
aplayed = gr.State(value=True)
if btn_index == 1:
bplayed = gr.State(value=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), gr.update(), gr.update()]
return [gr.update(), gr.update(), aplayed, bplayed]
def randomsent():
return random.choice(sents), '🎲'
def randomsent_battle():
return tuple(randomsent()) + tuple(random_m())
def clear_stuff():
return "", "Synthesize", gr.update(visible=False), '', '', gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) |