Pendrokar commited on
Commit
bf13dc3
1 Parent(s): 0ba527d

new files sample_caching

Browse files
app/sample_caching.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import itertools
3
+ import random
4
+ from typing import List, Tuple, Set, Dict
5
+ from hashlib import md5, sha1
6
+ # from .synth import clear_stuff
7
+
8
+ class User:
9
+ def __init__(self, user_id: str):
10
+ self.user_id = user_id
11
+ self.voted_pairs: Set[Tuple[str, str]] = set()
12
+
13
+ class Sample:
14
+ def __init__(self, filename: str, transcript: str, modelName: str):
15
+ self.filename = filename
16
+ self.transcript = transcript
17
+ self.modelName = modelName
18
+
19
+ # cache audio samples for quick voting
20
+ cached_samples: List[Sample] = []
21
+ voting_users = {
22
+ # userid as the key and USER() as the value
23
+ }
24
+ # List[Tuple[Sample, Sample]]
25
+ all_pairs = []
26
+
27
+
28
+ def get_userid(session_hash: str, request):
29
+ # JS cookie
30
+ if (session_hash != ''):
31
+ # print('auth by session cookie')
32
+ return sha1(bytes(session_hash.encode('ascii')), usedforsecurity=False).hexdigest()
33
+
34
+ if request.username:
35
+ # print('auth by username')
36
+ # by HuggingFace username - requires `auth` to be enabled therefore denying access to anonymous users
37
+ return sha1(bytes(request.username.encode('ascii')), usedforsecurity=False).hexdigest()
38
+ else:
39
+ # print('auth by ip')
40
+ # by IP address - unreliable when gradio within HTML iframe
41
+ # return sha1(bytes(request.client.host.encode('ascii')), usedforsecurity=False).hexdigest()
42
+ # by browser session cookie - Gradio on HF is run in an HTML iframe, access to parent session required to reach session token
43
+ # return sha1(bytes(request.headers.encode('ascii'))).hexdigest()
44
+ # by browser session hash - Not a cookie, session hash changes on page reload
45
+ return sha1(bytes(request.session_hash.encode('ascii')), usedforsecurity=False).hexdigest()
46
+
47
+ # Give user a cached audio sample pair they have yet to vote on
48
+ def give_cached_sample(session_hash: str, autoplay: bool, request: gr.Request):
49
+ # add new userid to voting_users from Browser session hash
50
+ # stored only in RAM
51
+ userid = get_userid(session_hash, request)
52
+
53
+ if userid not in voting_users:
54
+ voting_users[userid] = User(userid)
55
+
56
+ def get_next_pair(user: User):
57
+ # FIXME: all_pairs var out of scope
58
+ # all_pairs = generate_matching_pairs(cached_samples)
59
+
60
+ # for pair in all_pairs:
61
+ for pair in generate_matching_pairs(cached_samples):
62
+ hash1 = md5(bytes((pair[0].modelName + pair[0].transcript).encode('ascii'))).hexdigest()
63
+ hash2 = md5(bytes((pair[1].modelName + pair[1].transcript).encode('ascii'))).hexdigest()
64
+ pair_key = (hash1, hash2)
65
+ if (
66
+ pair_key not in user.voted_pairs
67
+ # or in reversed order
68
+ and (pair_key[1], pair_key[0]) not in user.voted_pairs
69
+ ):
70
+ return pair
71
+ return None
72
+
73
+ pair = get_next_pair(voting_users[userid])
74
+ if pair is None:
75
+ comp_defaults = []
76
+ for i in range(0, 14):
77
+ comp_defaults.append(gr.update())
78
+ return [
79
+ *comp_defaults,
80
+ # *clear_stuff(),
81
+ # disable get cached sample button
82
+ gr.update(interactive=False)
83
+ ]
84
+
85
+ return (
86
+ gr.update(visible=True, value=pair[0].transcript, elem_classes=['blurred-text']),
87
+ "Synthesize",
88
+ gr.update(visible=True), # r2
89
+ pair[0].modelName, # model1
90
+ pair[1].modelName, # model2
91
+ gr.update(visible=True, value=pair[0].filename, interactive=False, autoplay=autoplay), # aud1
92
+ gr.update(visible=True, value=pair[1].filename, interactive=False, autoplay=False), # aud2
93
+ gr.update(visible=True, interactive=False), #abetter
94
+ gr.update(visible=True, interactive=False), #bbetter
95
+ gr.update(visible=False), #prevmodel1
96
+ gr.update(visible=False), #prevmodel2
97
+ gr.update(visible=False), #nxt round btn
98
+ # reset aplayed, bplayed audio playback events
99
+ False, #aplayed
100
+ False, #bplayed
101
+ # fetch cached btn
102
+ gr.update(interactive=True)
103
+ )
104
+
105
+ def generate_matching_pairs(samples: List[Sample]) -> List[Tuple[Sample, Sample]]:
106
+ transcript_groups: Dict[str, List[Sample]] = {}
107
+ samples = random.sample(samples, k=len(samples))
108
+ for sample in samples:
109
+ if sample.transcript not in transcript_groups:
110
+ transcript_groups[sample.transcript] = []
111
+ transcript_groups[sample.transcript].append(sample)
112
+
113
+ matching_pairs: List[Tuple[Sample, Sample]] = []
114
+ for group in transcript_groups.values():
115
+ matching_pairs.extend(list(itertools.combinations(group, 2)))
116
+
117
+ return matching_pairs
118
+
119
+
120
+
121
+ # note the vote on cached sample pair
122
+ def voted_on_cached(modelName1: str, modelName2: str, transcript: str, session_hash: str, request: gr.Request):
123
+ userid = get_userid(session_hash, request)
124
+ # print(f'userid voted on cached: {userid}')
125
+
126
+ if userid not in voting_users:
127
+ voting_users[userid] = User(userid)
128
+
129
+ hash1 = md5(bytes((modelName1 + transcript).encode('ascii'))).hexdigest()
130
+ hash2 = md5(bytes((modelName2 + transcript).encode('ascii'))).hexdigest()
131
+
132
+ voting_users[userid].voted_pairs.add((hash1, hash2))
133
+ return []
app/ui_contenders.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from .config import *
3
+ from .messages import *
4
+
5
+ with gr.Blocks() as tts_info:
6
+ gr.Markdown(TTS_INFO)
7
+ gr.HTML(TTS_DATASET_IFRAME)
test_tts_maskgct.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from gradio_client import Client, handle_file
3
+
4
+ client = Client("amphion/maskgct", hf_token=os.getenv('HF_TOKEN'))
5
+ endpoints = client.view_api(all_endpoints=True, print_info=False, return_format='dict')
6
+ # print(endpoints)
7
+ result = client.predict(
8
+ prompt_wav=handle_file('https://cdn-uploads.huggingface.co/production/uploads/63d52e0c4e5642795617f668/V6-rMmI-P59DA4leWDIcK.wav'),
9
+ target_text="Hello!!",
10
+ target_len=-1,
11
+ n_timesteps=25,
12
+ api_name="/predict"
13
+ )
test_tts_styletts_kokoro.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from gradio_client import Client, file
3
+
4
+ client = Client("hexgrad/kokoro", hf_token=os.getenv('HF_TOKEN'))
5
+ # endpoints = client.view_api(all_endpoints=True, print_info=False, return_format='dict')
6
+ # print(endpoints)
7
+ result = client.predict(
8
+ text='"I hate it when people lie to me."',
9
+ voice="af",
10
+ ps=None,
11
+ speed=1,
12
+ trim=0,
13
+ use_gpu=False,
14
+ # *[
15
+ # "Oh, hello there!!",
16
+ # "af", #voice
17
+ # None, #ps
18
+ # 1, #speed
19
+ # 3000, #trim
20
+ # False, #use_gpu; fast enough with multithreaded with CPU
21
+ # ],
22
+ api_name="/generate"
23
+ )
24
+
25
+ print(result)
26
+
27
+ # text="Oh, hello there!!",
28
+ # voice="af",
29
+ # ps=None,
30
+ # speed=1,
31
+ # trim=3000,
32
+ # use_gpu=False,