File size: 33,657 Bytes
c055e89
 
8e14d20
c055e89
7927ab2
cd5bd26
 
ca8a2ca
1ace391
 
ebd29b9
74744eb
5fed627
 
 
 
60b0d11
 
 
 
c58df40
f0afce4
c58df40
f0afce4
 
 
74744eb
f1fda32
 
ca8a2ca
 
 
 
c991ae0
df76778
ca8a2ca
df76778
24aea25
9c3275d
6c97f10
 
52bdd0e
f29be9c
df76778
f564711
f29be9c
ca8a2ca
c055e89
53a98fd
7b5c32e
74744eb
cd5bd26
 
0e3778b
 
 
9dd70a7
69be213
1d58cd7
5cd0e1e
5e35153
5cd0e1e
 
6433dba
1d58cd7
cd5bd26
 
abcae38
cd5bd26
 
1ec31d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95f048
 
1ec31d9
 
67d05e3
 
 
 
 
 
 
 
 
2ca5c30
 
 
f95f048
 
2ca5c30
 
abcae38
 
 
 
 
 
 
 
 
 
0e3778b
 
 
 
 
 
 
 
1ec31d9
cd5bd26
 
 
 
 
 
 
 
 
 
 
 
 
69be213
cd5bd26
ca8a2ca
 
 
6433dba
cd5bd26
 
 
e5109be
18d89f0
4255bad
18d89f0
7524c65
20dc216
375d701
 
 
 
 
 
 
 
 
20dc216
cde05f1
18d89f0
7524c65
c991ae0
 
7524c65
18d89f0
7524c65
18d89f0
c055e89
cd5bd26
0217d78
7524c65
c055e89
264149b
c055e89
 
3a0e8ae
c055e89
7524c65
29a5f2c
f7860cf
 
 
29a5f2c
76df6f6
 
7524c65
76df6f6
83020c7
29a5f2c
83020c7
29a5f2c
83020c7
 
ccf5c81
83020c7
 
 
ccf5c81
83020c7
c055e89
 
0636bf7
7524c65
0636bf7
cea913e
c62c303
 
 
7524c65
2a9845b
7524c65
c055e89
20dc216
a2f2037
20dc216
2987626
 
 
aea0389
b33525a
20dc216
c055e89
20dc216
cd5bd26
 
69be213
 
 
 
cd5bd26
1cb88a1
 
 
cd5bd26
 
 
ca8a2ca
cd5bd26
 
 
1cb88a1
cd5bd26
18d89f0
 
 
cd5bd26
18d89f0
 
 
20dc216
 
18d89f0
 
 
 
 
 
 
20dc216
 
29a5f2c
 
 
20dc216
1cb88a1
 
 
20dc216
e080e91
18d89f0
29a5f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48707ff
29a5f2c
 
 
 
e080e91
ee22100
9952550
e080e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29a5f2c
2bb5d82
cd5bd26
2bb5d82
 
 
 
20dc216
2bb5d82
 
 
 
2c89463
ecca97f
9068c64
71abf2f
9952550
 
ecca97f
2c89463
0c804b7
2c89463
 
 
40f7a3f
 
2c89463
 
 
03d6969
2c89463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03d6969
 
2c89463
433517c
 
af37f5e
 
3425a41
20dc216
 
 
 
 
63f3958
cd5bd26
 
20dc216
2ca5c30
 
 
 
 
 
 
3425a41
20dc216
 
 
 
 
63f3958
cd5bd26
 
20dc216
cd5bd26
433517c
6a2a6e9
0903ad6
 
af37f5e
99f01ee
8ae6da1
 
9ae8c4d
8ae6da1
 
 
af37f5e
 
2b0c2d0
433517c
6a2a6e9
0903ad6
 
af37f5e
99f01ee
8ae6da1
 
9ae8c4d
8ae6da1
 
 
af37f5e
 
2b0c2d0
433517c
af37f5e
99f01ee
af37f5e
 
10801ae
433517c
af37f5e
99f01ee
af37f5e
 
10801ae
2b0c2d0
18d89f0
5404187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4e7fbc
ee22100
2b0c2d0
ee22100
6aa0fa6
2b0c2d0
009ac4f
6c142f7
 
009ac4f
6c142f7
0830050
1925e72
2b0c2d0
b0986f3
20dc216
 
cd5bd26
0ae578e
aea0389
b33525a
6691dd6
0ae578e
 
 
637cb4e
b0986f3
0636bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ace391
4478ce8
1ace391
 
 
4478ce8
ebd29b9
0408757
 
dc46588
 
 
09cd5ce
ca8a2ca
619c449
c3e4d11
 
74744eb
637cb4e
bda9f8b
 
 
 
 
dc46588
b72e390
375d701
 
8e14d20
 
 
8f8bb42
8e14d20
 
ca8a2ca
88f76ee
2ca5c30
 
73c86b6
9dc87ec
6436502
 
 
 
9dc87ec
 
c38b1b1
0408757
 
 
 
5fed627
 
 
0408757
 
 
60b0d11
 
 
 
 
5fed627
 
 
 
 
c908a11
4bd9385
ac01906
5fed627
b7f8e0c
 
 
 
ac01906
 
 
 
 
73c86b6
294e0bf
ac01906
 
53e5e28
c823dc3
 
 
 
0408d22
0eaad07
 
 
 
 
 
 
0408d22
53e5e28
 
294e0bf
7927ab2
 
 
 
 
 
10efffb
 
36efd7f
 
 
 
7927ab2
 
09cd5ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36efd7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1fda32
 
1f4eba7
 
36efd7f
f249bd1
803001e
b46a5dc
803001e
20dc216
36efd7f
52fc37b
 
36efd7f
5147f0b
20dc216
7366b46
f1fda32
172fbec
c09bde4
f1fda32
7366b46
ce4bd8b
 
 
 
a629389
 
 
 
52fc37b
88f76ee
a629389
 
 
52fc37b
88f76ee
6b71cc7
c4e7fbc
b0de8ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1925e72
9594ec2
36efd7f
52fc37b
 
36efd7f
c4e7fbc
6b71cc7
ca8a2ca
 
0636bf7
 
 
 
20dc216
0636bf7
20dc216
c055e89
 
9983762
 
 
 
 
 
 
02bfca3
20dc216
9983762
0ae578e
2519d7f
 
d0dc5de
353a56a
2519d7f
cd5bd26
0ae578e
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
import gradio as gr
import pandas as pd
from langdetect import detect
from datasets import load_dataset
import threading, time, uuid, sqlite3, shutil, os, random, asyncio, threading
from pathlib import Path
from huggingface_hub import CommitScheduler, delete_file, hf_hub_download
from gradio_client import Client
import pyloudnorm as pyln
import soundfile as sf
import librosa
from detoxify import Detoxify
import os
import tempfile
from pydub import AudioSegment

def match_target_amplitude(sound, target_dBFS):
    change_in_dBFS = target_dBFS - sound.dBFS
    return sound.apply_gain(change_in_dBFS)

# from gradio_space_ci import enable_space_ci

# enable_space_ci()



toxicity = Detoxify('original')
with open('harvard_sentences.txt') as f:
    sents = f.read().strip().splitlines()
####################################
# Constants
####################################
AVAILABLE_MODELS = {
    'XTTSv2': 'xtts',
    # 'WhisperSpeech': 'whisperspeech',
    'ElevenLabs': 'eleven',
    # 'OpenVoice': 'openvoice',
    'OpenVoice V2': 'openvoicev2',
    'Play.HT 2.0': 'playht',
    'MetaVoice': 'metavoice',
    'MeloTTS': 'melo',
    'StyleTTS 2': 'styletts2',
    'GPT-SoVITS': 'sovits',
    # 'Vokan TTS': 'vokan',
    'VoiceCraft 2.0': 'voicecraft',
    'Parler TTS': 'parler'
}

SPACE_ID = os.getenv('SPACE_ID')
MAX_SAMPLE_TXT_LENGTH = 300
MIN_SAMPLE_TXT_LENGTH = 10
DB_DATASET_ID = os.getenv('DATASET_ID')
DB_NAME = "database.db"

# If /data available => means local storage is enabled => let's use it!
DB_PATH = f"/data/{DB_NAME}" if os.path.isdir("/data") else DB_NAME
print(f"Using {DB_PATH}")
# AUDIO_DATASET_ID = "ttseval/tts-arena-new"
CITATION_TEXT = """@misc{tts-arena,
	title        = {Text to Speech Arena},
	author       = {mrfakename and Srivastav, Vaibhav and Fourrier, ClΓ©mentine and Pouget, Lucain and Lacombe, Yoach and main and Gandhi, Sanchit},
	year         = 2024,
	publisher    = {Hugging Face},
	howpublished = "\\url{https://huggingface.co/spaces/TTS-AGI/TTS-Arena}"
}"""

####################################
# Functions
####################################

def create_db_if_missing():
    conn = get_db()
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS model (
            name TEXT UNIQUE,
            upvote INTEGER,
            downvote INTEGER
        );
    ''')
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS vote (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            username TEXT,
            model TEXT,
            vote INTEGER,
            timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
    ''')
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS votelog (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            username TEXT,
            chosen TEXT,
            rejected TEXT,
            timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
    ''')
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS spokentext (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            spokentext TEXT,
            timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
    ''')
def get_db():
    return sqlite3.connect(DB_PATH)



####################################
# Space initialization
####################################

# Download existing DB
if not os.path.isfile(DB_PATH):
    print("Downloading DB...")
    try:
        cache_path = hf_hub_download(repo_id=DB_DATASET_ID, repo_type='dataset', filename=DB_NAME)
        shutil.copyfile(cache_path, DB_PATH)
        print("Downloaded DB")
    except Exception as e:
        print("Error while downloading DB:", e)

# Create DB table (if doesn't exist)
create_db_if_missing()
    
# Sync local DB with remote repo every 5 minute (only if a change is detected)
scheduler = CommitScheduler(
    repo_id=DB_DATASET_ID,
    repo_type="dataset",
    folder_path=Path(DB_PATH).parent,
    every=5,
    allow_patterns=DB_NAME,
)

# Load audio dataset
# audio_dataset = load_dataset(AUDIO_DATASET_ID)

####################################
# Router API
####################################
router = Client("TTS-AGI/tts-router", hf_token=os.getenv('HF_TOKEN'))
####################################
# Gradio app
####################################
MUST_BE_LOGGEDIN = "Please login with Hugging Face to participate in the TTS Arena."
DESCR = """
# TTS Arena: Benchmarking TTS Models in the Wild

Vote to help the community find the best available text-to-speech model!
""".strip()
# INSTR = """
# ## Instructions

# * Listen to two anonymous models
# * Vote on which synthesized audio sounds more natural to you
# * If there's a tie, click Skip

# **When you're ready to begin, login and begin voting!** The model names will be revealed once you vote.
# """.strip()
INSTR = """
## πŸ—³οΈ Vote

* Input text (English only) to synthesize audio (or press 🎲 for random text).
* Listen to the two audio clips, one after the other.
* Vote on which audio sounds more natural to you.
* _Note: Model names are revealed after the vote is cast._

Note: It may take up to 30 seconds to synthesize audio.
""".strip()
request = ''
if SPACE_ID:
    request = f"""
### Request a model

Please [create a Discussion](https://huggingface.co/spaces/{SPACE_ID}/discussions/new) to request a model.
"""
ABOUT = f"""
## πŸ“„ About

The TTS Arena evaluates leading speech synthesis models. It is inspired by LMsys's [Chatbot Arena](https://chat.lmsys.org/).

### Motivation

The field of speech synthesis has long lacked an accurate method to measure the quality of different models. Objective metrics like WER (word error rate) are unreliable measures of model quality, and subjective measures such as MOS (mean opinion score) are typically small-scale experiments conducted with few listeners. As a result, these measurements are generally not useful for comparing two models of roughly similar quality. To address these drawbacks, we are inviting the community to rank models in an easy-to-use interface, and opening it up to the public in order to make both the opportunity to rank models, as well as the results, more easily accessible to everyone.

### The Arena

The leaderboard allows a user to enter text, which will be synthesized by two models. After listening to each sample, the user can vote on which model sounds more natural. Due to the risks of human bias and abuse, model names are revealed only after a vote is submitted.

### Credits

Thank you to the following individuals who helped make this project possible:

* VB ([Twitter](https://twitter.com/reach_vb) / [Hugging Face](https://huggingface.co/reach-vb))
* ClΓ©mentine Fourrier ([Twitter](https://twitter.com/clefourrier) / [Hugging Face](https://huggingface.co/clefourrier))
* Lucain Pouget ([Twitter](https://twitter.com/Wauplin) / [Hugging Face](https://huggingface.co/Wauplin))
* Yoach Lacombe ([Twitter](https://twitter.com/yoachlacombe) / [Hugging Face](https://huggingface.co/ylacombe))
* Main Horse ([Twitter](https://twitter.com/main_horse) / [Hugging Face](https://huggingface.co/main-horse))
* Sanchit Gandhi ([Twitter](https://twitter.com/sanchitgandhi99) / [Hugging Face](https://huggingface.co/sanchit-gandhi))
* ApolinΓ‘rio Passos ([Twitter](https://twitter.com/multimodalart) / [Hugging Face](https://huggingface.co/multimodalart))
* Pedro Cuenca ([Twitter](https://twitter.com/pcuenq) / [Hugging Face](https://huggingface.co/pcuenq))

{request}

### Privacy statement

We may store text you enter and generated audio. We store a unique ID for each session. You agree that we may collect, share, and/or publish any data you input for research and/or commercial purposes.

### License

Generated audio clips cannot be redistributed and may be used for personal, non-commercial use only.

Random sentences are sourced from a filtered subset of the [Harvard Sentences](https://www.cs.columbia.edu/~hgs/audio/harvard.html).
""".strip()
LDESC = """
## πŸ† Leaderboard

Vote to help the community determine the best text-to-speech (TTS) models.

The leaderboard displays models in descending order of how natural they sound (based on votes cast by the community).

Important: In order to help keep results fair, the leaderboard hides results by default until the number of votes passes a threshold. Tick the `Reveal preliminary results` to show models without sufficient votes. Please note that preliminary results may be inaccurate.
""".strip()




# def reload_audio_dataset():
#     global audio_dataset
#     audio_dataset = load_dataset(AUDIO_DATASET_ID)
#     return 'Reload Audio Dataset'

def del_db(txt):
    if not txt.lower() == 'delete db':
        raise gr.Error('You did not enter "delete db"')

    # Delete local + remote
    os.remove(DB_PATH)
    delete_file(path_in_repo=DB_NAME, repo_id=DB_DATASET_ID, repo_type='dataset')

    # Recreate
    create_db_if_missing()
    return 'Delete DB'

theme = gr.themes.Base(
    font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
)

model_names = {
    'styletts2': 'StyleTTS 2',
    'tacotron': 'Tacotron',
    'tacotronph': 'Tacotron Phoneme',
    'tacotrondca': 'Tacotron DCA',
    'speedyspeech': 'Speedy Speech',
    'overflow': 'Overflow TTS',
    'vits': 'VITS',
    'vitsneon': 'VITS Neon',
    'neuralhmm': 'Neural HMM',
    'glow': 'Glow TTS',
    'fastpitch': 'FastPitch',
    'jenny': 'Jenny',
    'tortoise': 'Tortoise TTS',
    'xtts2': 'Coqui XTTSv2',
    'xtts': 'Coqui XTTS',
    'openvoice': 'MyShell OpenVoice',
    'elevenlabs': 'ElevenLabs',
    'openai': 'OpenAI',
    'hierspeech': 'HierSpeech++',
    'pheme': 'PolyAI Pheme',
    'speecht5': 'SpeechT5',
    'metavoice': 'MetaVoice-1B',
}
model_licenses = {
    'styletts2': 'MIT',
    'tacotron': 'BSD-3',
    'tacotronph': 'BSD-3',
    'tacotrondca': 'BSD-3',
    'speedyspeech': 'BSD-3',
    'overflow': 'MIT',
    'vits': 'MIT',
    'openvoice': 'MIT',
    'vitsneon': 'BSD-3',
    'neuralhmm': 'MIT',
    'glow': 'MIT',
    'fastpitch': 'Apache 2.0',
    'jenny': 'Jenny License',
    'tortoise': 'Apache 2.0',
    'xtts2': 'CPML (NC)',
    'xtts': 'CPML (NC)',
    'elevenlabs': 'Proprietary',
    'eleven': 'Proprietary',
    'openai': 'Proprietary',
    'hierspeech': 'MIT',
    'pheme': 'CC-BY',
    'speecht5': 'MIT',
    'metavoice': 'Apache 2.0',
    'elevenlabs': 'Proprietary',
    'whisperspeech': 'MIT',
}
model_links = {
    'styletts2': 'https://github.com/yl4579/StyleTTS2',
    'tacotron': 'https://github.com/NVIDIA/tacotron2',
    'speedyspeech': 'https://github.com/janvainer/speedyspeech',
    'overflow': 'https://github.com/shivammehta25/OverFlow',
    'vits': 'https://github.com/jaywalnut310/vits',
    'openvoice': 'https://github.com/myshell-ai/OpenVoice',
    'neuralhmm': 'https://github.com/ketranm/neuralHMM',
    'glow': 'https://github.com/jaywalnut310/glow-tts',
    'fastpitch': 'https://fastpitch.github.io/',
    'tortoise': 'https://github.com/neonbjb/tortoise-tts',
    'xtts2': 'https://huggingface.co/coqui/XTTS-v2',
    'xtts': 'https://huggingface.co/coqui/XTTS-v1',
    'elevenlabs': 'https://elevenlabs.io/',
    'openai': 'https://help.openai.com/en/articles/8555505-tts-api',
    'hierspeech': 'https://github.com/sh-lee-prml/HierSpeechpp',
    'pheme': 'https://github.com/PolyAI-LDN/pheme',
    'speecht5': 'https://github.com/microsoft/SpeechT5',
    'metavoice': 'https://github.com/metavoiceio/metavoice-src',
}
# def get_random_split(existing_split=None):
#     choice = random.choice(list(audio_dataset.keys()))
#     if existing_split and choice == existing_split:
#         return get_random_split(choice)
#     else:
#         return choice

# def get_random_splits():
#     choice1 = get_random_split()
#     choice2 = get_random_split(choice1)
#     return (choice1, choice2)
def model_license(name):
    print(name)
    for k, v in AVAILABLE_MODELS.items():
        if k == name:
            if v in model_licenses:
                return model_licenses[v]
    print('---')
    return 'Unknown'
def get_leaderboard(reveal_prelim = False):
    conn = get_db()
    cursor = conn.cursor()
    sql = 'SELECT name, upvote, downvote FROM model'
    # if not reveal_prelim: sql += ' WHERE EXISTS (SELECT 1 FROM model WHERE (upvote + downvote) > 750)'
    if not reveal_prelim: sql += ' WHERE (upvote + downvote) > 500'
    cursor.execute(sql)
    data = cursor.fetchall()
    df = pd.DataFrame(data, columns=['name', 'upvote', 'downvote'])
    # df['license'] = df['name'].map(model_license)
    df['name'] = df['name'].replace(model_names)
    df['votes'] = df['upvote'] + df['downvote']
    # df['score'] = round((df['upvote'] / df['votes']) * 100, 2) # Percentage score

    ## ELO SCORE
    df['score'] = 1200
    for i in range(len(df)):
        for j in range(len(df)):
            if i != j:
                expected_a = 1 / (1 + 10 ** ((df['score'][j] - df['score'][i]) / 400))
                expected_b = 1 / (1 + 10 ** ((df['score'][i] - df['score'][j]) / 400))
                actual_a = df['upvote'][i] / df['votes'][i]
                actual_b = df['upvote'][j] / df['votes'][j]
                df.at[i, 'score'] += 32 * (actual_a - expected_a)
                df.at[j, 'score'] += 32 * (actual_b - expected_b)
    df['score'] = round(df['score'])
    ## ELO SCORE
    df = df.sort_values(by='score', ascending=False)
    df['order'] = ['#' + str(i + 1) for i in range(len(df))]
    # df = df[['name', 'score', 'upvote', 'votes']]
    # df = df[['order', 'name', 'score', 'license', 'votes']]
    df = df[['order', 'name', 'score', 'votes']]
    return df
def mkuuid(uid):
    if not uid:
        uid = uuid.uuid4()
    return uid
def upvote_model(model, uname):
    conn = get_db()
    cursor = conn.cursor()
    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,))
    with scheduler.lock:
        conn.commit()
    cursor.close()
def log_text(text):
    conn = get_db()
    cursor = conn.cursor()
    cursor.execute('INSERT INTO spokentext (spokentext) VALUES (?)', (text,))
    with scheduler.lock:
        conn.commit()
    cursor.close()
def downvote_model(model, uname):
    conn = get_db()
    cursor = conn.cursor()
    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,))
    with scheduler.lock:
        conn.commit()
    cursor.close()

def a_is_better(model1, model2, userid):
    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,))
        with scheduler.lock:
            conn.commit()
            cursor.close()
        upvote_model(model1, str(userid))
        downvote_model(model2, str(userid))
    return reload(model1, model2, userid, chose_a=True)
def b_is_better(model1, model2, userid):
    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,))
        with scheduler.lock:
            conn.commit()
            cursor.close()
        upvote_model(model2, str(userid))
        downvote_model(model1, str(userid))
    return reload(model1, model2, userid, chose_b=True)
def both_bad(model1, model2, userid):
    userid = mkuuid(userid)
    if model1 and model2:
        downvote_model(model1, str(userid))
        downvote_model(model2, str(userid))
    return reload(model1, model2, userid)
def both_good(model1, model2, userid):
    userid = mkuuid(userid)
    if model1 and model2:
        upvote_model(model1, str(userid))
        upvote_model(model2, str(userid))
    return reload(model1, model2, userid)
def reload(chosenmodel1=None, chosenmodel2=None, userid=None, chose_a=False, chose_b=False):
    # Select random splits
    # row = random.choice(list(audio_dataset['train']))
    # options = list(random.choice(list(audio_dataset['train'])).keys())
    # split1, split2 = random.sample(options, 2)
    # choice1, choice2 = (row[split1], row[split2])
    # if chosenmodel1 in model_names:
    #     chosenmodel1 = model_names[chosenmodel1]
    # if chosenmodel2 in model_names:
    #     chosenmodel2 = model_names[chosenmodel2]
    # out = [
    #     (choice1['sampling_rate'], choice1['array']),
    #     (choice2['sampling_rate'], choice2['array']),
    #     split1,
    #     split2
    # ]
    # if userid: out.append(userid)
    # if chosenmodel1: out.append(f'This model was {chosenmodel1}')
    # if chosenmodel2: out.append(f'This model was {chosenmodel2}')
    # return out
    # return (f'This model was {chosenmodel1}', f'This model was {chosenmodel2}', gr.update(visible=False), gr.update(visible=False))
    # return (gr.update(variant='secondary', value=chosenmodel1, interactive=False), gr.update(variant='secondary', value=chosenmodel2, interactive=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

with gr.Blocks() as leaderboard:
    gr.Markdown(LDESC)
    # df = gr.Dataframe(interactive=False, value=get_leaderboard())
    df = gr.Dataframe(interactive=False, min_width=0, wrap=True, column_widths=[30, 200, 50, 50])
    with gr.Row():
        reveal_prelim = gr.Checkbox(label="Reveal preliminary results", info="Show all models, including models with very few human ratings.", scale=1)
        reloadbtn = gr.Button("Refresh", scale=3)
    reveal_prelim.input(get_leaderboard, inputs=[reveal_prelim], outputs=[df])
    leaderboard.load(get_leaderboard, inputs=[reveal_prelim], outputs=[df])
    reloadbtn.click(get_leaderboard, inputs=[reveal_prelim], outputs=[df])
    # gr.Markdown("DISCLAIMER: The licenses listed may not be accurate or up to date, you are responsible for checking the licenses before using the models. Also note that some models may have additional usage restrictions.")

# with gr.Blocks() as vote:
#     useridstate = gr.State()
#     gr.Markdown(INSTR)
#     # gr.LoginButton()
#     with gr.Row():
#         gr.HTML('<div align="left"><h3>Model A</h3></div>')
#         gr.HTML('<div align="right"><h3>Model B</h3></div>')
#     model1 = gr.Textbox(interactive=False, visible=False, lines=1, max_lines=1)
#     model2 = gr.Textbox(interactive=False, visible=False, lines=1, max_lines=1)
#     # with gr.Group():
#     #     with gr.Row():
#     #         prevmodel1 = gr.Textbox(interactive=False, show_label=False, container=False, value="Vote to reveal model A")
#     #         prevmodel2 = gr.Textbox(interactive=False, show_label=False, container=False, value="Vote to reveal model B", text_align="right")
#     #     with gr.Row():
#     #         aud1 = gr.Audio(interactive=False, show_label=False, show_download_button=False, show_share_button=False, waveform_options={'waveform_progress_color': '#3C82F6'})
#     #         aud2 = gr.Audio(interactive=False, show_label=False, show_download_button=False, show_share_button=False, waveform_options={'waveform_progress_color': '#3C82F6'})
#     with gr.Group():
#         with gr.Row():
#             with gr.Column():
#                 with gr.Group():
#                     prevmodel1 = gr.Textbox(interactive=False, show_label=False, container=False, value="Vote to reveal model A", lines=1, max_lines=1)
#                     aud1 = gr.Audio(interactive=False, show_label=False, show_download_button=False, show_share_button=False, waveform_options={'waveform_progress_color': '#3C82F6'})
#             with gr.Column():
#                 with gr.Group():
#                     prevmodel2 = gr.Textbox(interactive=False, show_label=False, container=False, value="Vote to reveal model B", text_align="right", lines=1, max_lines=1)
#                     aud2 = gr.Audio(interactive=False, show_label=False, show_download_button=False, show_share_button=False, waveform_options={'waveform_progress_color': '#3C82F6'})


#     with gr.Row():
#         abetter = gr.Button("A is Better", variant='primary', scale=4)
#         # skipbtn = gr.Button("Skip", scale=1)
#         bbetter = gr.Button("B is Better", variant='primary', scale=4)
#     with gr.Row():
#         bothbad = gr.Button("Both are Bad", scale=2)
#         skipbtn = gr.Button("Skip", scale=1)
#         bothgood = gr.Button("Both are Good", scale=2)
#     outputs = [aud1, aud2, model1, model2, useridstate, prevmodel1, prevmodel2]
#     abetter.click(a_is_better, outputs=outputs, inputs=[model1, model2, useridstate])
#     bbetter.click(b_is_better, outputs=outputs, inputs=[model1, model2, useridstate])
#     skipbtn.click(b_is_better, outputs=outputs, inputs=[model1, model2, useridstate])

#     bothbad.click(both_bad, outputs=outputs, inputs=[model1, model2, useridstate])
#     bothgood.click(both_good, outputs=outputs, inputs=[model1, model2, useridstate])

#     vote.load(reload, outputs=[aud1, aud2, model1, model2])
def doloudnorm(path):
    data, rate = sf.read(path)
    meter = pyln.Meter(rate)
    loudness = meter.integrated_loudness(data)
    loudness_normalized_audio = pyln.normalize.loudness(data, loudness, -12.0)
    sf.write(path, loudness_normalized_audio, rate)

def doresample(path_to_wav):
    pass
##########################
# 2x speedup (hopefully) #
##########################

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 toxicity.predict(text)['toxicity'] > 0.8
    ):
        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
    )
    # return (
    #     text,
    #     "Synthesize",
    #     gr.update(visible=True), # r2
    #     mdl1, # model1
    #     mdl2, # model2
    #     # 'Vote to reveal model A', # prevmodel1
    #     gr.update(visible=True, value=router.predict(
    #         text,
    #         AVAILABLE_MODELS[mdl1],
    #         api_name="/synthesize"
    #     )), # aud1
    #     # 'Vote to reveal model B', # prevmodel2
    #     gr.update(visible=True, value=router.predict(
    #         text,
    #         AVAILABLE_MODELS[mdl2],
    #         api_name="/synthesize"
    #     )), # aud2
    #     gr.update(visible=True, interactive=True),
    #     gr.update(visible=True, interactive=True),
    #     gr.update(visible=False),
    #     gr.update(visible=False),
    #     gr.update(visible=False), #nxt round btn
    # )

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 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)

def disable():
    return [gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)]
def enable():
    return [gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)]
with gr.Blocks() as vote:
    # sample played
    #aplayed = gr.State(value=False)
    #bplayed = gr.State(value=False)
    # voter ID
    useridstate = gr.State()
    gr.Markdown(INSTR)
    with gr.Group():
        with gr.Row():
            text = gr.Textbox(container=False, show_label=False, placeholder="Enter text to synthesize", lines=1, max_lines=1, scale=9999999, min_width=0)
            randomt = gr.Button('🎲', scale=0, min_width=0, variant='tool')
        randomt.click(randomsent, outputs=[text, randomt])
        btn = gr.Button("Synthesize", variant='primary')
    model1 = gr.Textbox(interactive=False, lines=1, max_lines=1, visible=False)
    #model1 = gr.Textbox(interactive=False, lines=1, max_lines=1, visible=True)
    model2 = gr.Textbox(interactive=False, lines=1, max_lines=1, visible=False)
    #model2 = gr.Textbox(interactive=False, lines=1, max_lines=1, visible=True)
    with gr.Row(visible=False) as r2:
        with gr.Column():
            with gr.Group():
                aud1 = gr.Audio(interactive=False, show_label=False, show_download_button=False, show_share_button=False, waveform_options={'waveform_progress_color': '#3C82F6'})
                abetter = gr.Button("A is better", variant='primary')
                prevmodel1 = gr.Textbox(interactive=False, show_label=False, container=False, value="Vote to reveal model A", text_align="center", lines=1, max_lines=1, visible=False)
        with gr.Column():
            with gr.Group():
                aud2 = gr.Audio(interactive=False, show_label=False, show_download_button=False, show_share_button=False, waveform_options={'waveform_progress_color': '#3C82F6'})
                bbetter = gr.Button("B is better", variant='primary')
                prevmodel2 = gr.Textbox(interactive=False, show_label=False, container=False, value="Vote to reveal model B", text_align="center", lines=1, max_lines=1, visible=False)
    nxtroundbtn = gr.Button('Next round', visible=False)
    # outputs = [text, btn, r2, model1, model2, prevmodel1, aud1, prevmodel2, aud2, abetter, bbetter]
    outputs = [
        text,
        btn,
        r2,
        model1,
        model2,
        aud1,
        aud2,
        abetter,
        bbetter,
        prevmodel1,
        prevmodel2,
        nxtroundbtn
    ]
    """
    text,
        "Synthesize",
        gr.update(visible=True), # r2
        mdl1, # model1
        mdl2, # model2
        gr.update(visible=True, value=results[mdl1]), # aud1
        gr.update(visible=True, value=results[mdl2]), # 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"""
    btn.click(disable, outputs=[btn, abetter, bbetter]).then(synthandreturn, inputs=[text], outputs=outputs).then(enable, outputs=[btn, abetter, bbetter])
    nxtroundbtn.click(clear_stuff, outputs=outputs)

    # Allow interaction with the vote buttons only when both audio samples have finished playing
    #aud1.stop(unlock_vote, outputs=[abetter, bbetter, aplayed, bplayed], inputs=[gr.State(value=0), aplayed, bplayed])
    #aud2.stop(unlock_vote, outputs=[abetter, bbetter, aplayed, bplayed], inputs=[gr.State(value=1), aplayed, bplayed])

    # nxt_outputs = [prevmodel1, prevmodel2, abetter, bbetter]
    nxt_outputs = [abetter, bbetter, prevmodel1, prevmodel2, nxtroundbtn]
    abetter.click(a_is_better, outputs=nxt_outputs, inputs=[model1, model2, useridstate])
    bbetter.click(b_is_better, outputs=nxt_outputs, inputs=[model1, model2, useridstate])
    # skipbtn.click(b_is_better, outputs=outputs, inputs=[model1, model2, useridstate])

    # bothbad.click(both_bad, outputs=outputs, inputs=[model1, model2, useridstate])
    # bothgood.click(both_good, outputs=outputs, inputs=[model1, model2, useridstate])

    # vote.load(reload, outputs=[aud1, aud2, model1, model2])

with gr.Blocks() as about:
    gr.Markdown(ABOUT)
# with gr.Blocks() as admin:
#     rdb = gr.Button("Reload Audio Dataset")
#     # rdb.click(reload_audio_dataset, outputs=rdb)
#     with gr.Group():
#         dbtext = gr.Textbox(label="Type \"delete db\" to confirm", placeholder="delete db")
#         ddb = gr.Button("Delete DB")
#     ddb.click(del_db, inputs=dbtext, outputs=ddb)
with gr.Blocks(theme=theme, css="footer {visibility: hidden}textbox{resize:none}", title="TTS Arena") as demo:
    gr.Markdown(DESCR)
    # gr.TabbedInterface([vote, leaderboard, about, admin], ['Vote', 'Leaderboard', 'About', 'Admin (ONLY IN BETA)'])
    gr.TabbedInterface([vote, leaderboard, about], ['πŸ—³οΈ Vote', 'πŸ† Leaderboard', 'πŸ“„ About'])
    if CITATION_TEXT:
        with gr.Row():
            with gr.Accordion("Citation", open=False):
                gr.Markdown(f"If you use this data in your publication, please cite us!\n\nCopy the BibTeX citation to cite this source:\n\n```bibtext\n{CITATION_TEXT}\n```\n\nPlease remember that all generated audio clips should be assumed unsuitable for redistribution or commercial use.")


demo.queue(api_open=False, default_concurrency_limit=40).launch(show_api=False)