Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,52 @@
|
|
1 |
-
# app.py
|
2 |
-
import os, json,
|
3 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
4 |
from huggingface_hub import login
|
5 |
-
from transformers import
|
6 |
from transformers.generation.utils import Cache
|
7 |
from snac import SNAC
|
8 |
|
9 |
-
# ββ 0.
|
10 |
-
|
11 |
-
|
12 |
-
login(HF_TOKEN)
|
13 |
|
14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
self.allowed = allowed_ids
|
33 |
-
|
34 |
-
def __call__(self, input_ids, scores):
|
35 |
-
# scores shape: [batch, vocab]
|
36 |
mask = torch.full_like(scores, float("-inf"))
|
37 |
-
mask[:, self.allowed] = 0
|
38 |
return scores + mask
|
39 |
|
40 |
ALLOWED_IDS = torch.cat(
|
41 |
-
[VALID_AUDIO_IDS,
|
|
|
42 |
).to(device)
|
43 |
-
MASKER =
|
44 |
|
45 |
-
# ββ 3.
|
46 |
app = FastAPI()
|
47 |
|
48 |
@app.get("/")
|
49 |
-
async def
|
50 |
-
return {"msg": "OrpheusβTTS
|
|
|
|
|
|
|
51 |
|
52 |
@app.on_event("startup")
|
53 |
async def load_models():
|
@@ -63,11 +62,11 @@ async def load_models():
|
|
63 |
model.config.pad_token_id = model.config.eos_token_id
|
64 |
model.config.use_cache = True
|
65 |
|
66 |
-
# ββ 4.
|
67 |
-
def
|
68 |
-
|
69 |
-
ids
|
70 |
-
ids
|
71 |
[
|
72 |
torch.tensor([[START_TOKEN]], device=device),
|
73 |
ids,
|
@@ -77,29 +76,32 @@ def build_prompt(text:str, voice:str):
|
|
77 |
)
|
78 |
return ids, torch.ones_like(ids)
|
79 |
|
80 |
-
def
|
81 |
-
l1,l2,l3=[],[],[]
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
@app.websocket("/ws/tts")
|
96 |
async def tts(ws: WebSocket):
|
97 |
await ws.accept()
|
98 |
try:
|
99 |
req = json.loads(await ws.receive_text())
|
100 |
ids, attn = build_inputs(req.get("text", ""), req.get("voice", "Jakob"))
|
|
|
101 |
|
102 |
-
past = None
|
103 |
buf = []
|
104 |
|
105 |
while True:
|
@@ -112,14 +114,18 @@ async def tts(ws: WebSocket):
|
|
112 |
do_sample=True, top_p=0.95, temperature=0.7,
|
113 |
return_dict_in_generate=True,
|
114 |
use_cache=True,
|
115 |
-
return_legacy_cache=True,
|
116 |
)
|
117 |
|
118 |
-
#
|
119 |
-
past = gen.past_key_values
|
|
|
|
|
|
|
|
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
|
124 |
for t in new_tok:
|
125 |
if t == EOS_TOKEN:
|
@@ -132,24 +138,20 @@ async def tts(ws: WebSocket):
|
|
132 |
await ws.send_bytes(decode_block(buf))
|
133 |
buf.clear()
|
134 |
|
135 |
-
# ab jetzt nur Cache
|
136 |
-
ids, attn = None, None
|
137 |
|
138 |
-
except (
|
139 |
-
pass
|
140 |
except Exception as e:
|
141 |
print("WSβError:", e)
|
142 |
-
if ws.client_state.name
|
143 |
-
await ws.close(code=1011)
|
144 |
finally:
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
# Starlette hat bereits ein CloseβFrame verschickt
|
150 |
-
pass
|
151 |
-
|
152 |
-
# ββ 6.Β Lokaler Test βββββββββββββββββββββββββββββββββββββββββββββββββ
|
153 |
if __name__ == "__main__":
|
154 |
-
import uvicorn
|
155 |
-
|
|
|
|
1 |
+
# app.py -------------------------------------------------------------
|
2 |
+
import os, json, torch
|
3 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
4 |
from huggingface_hub import login
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, LogitsProcessor
|
6 |
from transformers.generation.utils import Cache
|
7 |
from snac import SNAC
|
8 |
|
9 |
+
# ββ 0. Auth & Device ββββββββββββββββββββββββββββββββββββββββββββββββ
|
10 |
+
if (tok := os.getenv("HF_TOKEN")):
|
11 |
+
login(tok)
|
|
|
12 |
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
torch.backends.cuda.enable_flash_sdp(False) # PyTorchβ2.2 fix
|
15 |
+
|
16 |
+
# ββ 1. Konstanten βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
17 |
+
REPO = "SebastianBodza/Kartoffel_Orpheus-3B_german_natural-v0.1"
|
18 |
+
CHUNK_TOKENS = 50 # β€Β 50Β βΒ <Β 1Β s Latenz
|
19 |
+
START_TOKEN = 128259
|
20 |
+
NEW_BLOCK_TOKEN = 128257
|
21 |
+
EOS_TOKEN = 128258
|
22 |
+
AUDIO_BASE = 128266
|
23 |
+
VALID_AUDIO_IDS = torch.arange(AUDIO_BASE, AUDIO_BASE + 4096)
|
24 |
+
|
25 |
+
# ββ 2. LogitβMaske (nur Audioβ und SteuerβToken) ββββββββββββββββββ
|
26 |
+
class AudioMask(LogitsProcessor):
|
27 |
+
def __init__(self, allowed: torch.Tensor): # allowed @device!
|
28 |
+
self.allowed = allowed
|
29 |
+
|
30 |
+
def __call__(self, _ids, scores):
|
|
|
|
|
|
|
|
|
31 |
mask = torch.full_like(scores, float("-inf"))
|
32 |
+
mask[:, self.allowed] = 0.0
|
33 |
return scores + mask
|
34 |
|
35 |
ALLOWED_IDS = torch.cat(
|
36 |
+
[VALID_AUDIO_IDS,
|
37 |
+
torch.tensor([NEW_BLOCK_TOKEN, EOS_TOKEN])]
|
38 |
).to(device)
|
39 |
+
MASKER = AudioMask(ALLOWED_IDS)
|
40 |
|
41 |
+
# ββ 3. FastAPI GrundgerΓΌst ββββββββββββββββββββββββββββββββββββββββββ
|
42 |
app = FastAPI()
|
43 |
|
44 |
@app.get("/")
|
45 |
+
async def root():
|
46 |
+
return {"msg": "OrpheusβTTS ready"}
|
47 |
+
|
48 |
+
# global handles
|
49 |
+
tok = model = snac = None
|
50 |
|
51 |
@app.on_event("startup")
|
52 |
async def load_models():
|
|
|
62 |
model.config.pad_token_id = model.config.eos_token_id
|
63 |
model.config.use_cache = True
|
64 |
|
65 |
+
# ββ 4. Helper βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
66 |
+
def build_inputs(text: str, voice: str):
|
67 |
+
prompt = f"{voice}: {text}"
|
68 |
+
ids = tok(prompt, return_tensors="pt").input_ids.to(device)
|
69 |
+
ids = torch.cat(
|
70 |
[
|
71 |
torch.tensor([[START_TOKEN]], device=device),
|
72 |
ids,
|
|
|
76 |
)
|
77 |
return ids, torch.ones_like(ids)
|
78 |
|
79 |
+
def decode_block(b7: list[int]) -> bytes:
|
80 |
+
l1, l2, l3 = [], [], []
|
81 |
+
l1.append(b7[0])
|
82 |
+
l2.append(b7[1] - 4096)
|
83 |
+
l3.extend([b7[2] - 8192, b7[3] - 12288])
|
84 |
+
l2.append(b7[4] - 16384)
|
85 |
+
l3.extend([b7[5] - 20480, b7[6] - 24576])
|
86 |
+
|
87 |
+
codes = [torch.tensor(x, device=device).unsqueeze(0) for x in (l1, l2, l3)]
|
88 |
+
audio = snac.decode(codes).squeeze().cpu().numpy()
|
89 |
+
return (audio * 32767).astype("int16").tobytes()
|
90 |
+
|
91 |
+
def new_tokens_only(full_seq, prev_len):
|
92 |
+
"""liefert Liste der Tokens, die *neu* hinzukamen"""
|
93 |
+
return full_seq[prev_len:].tolist()
|
94 |
+
|
95 |
+
# ββ 5. WebSocketβEndpoint βββββββββββββββββββββββββββββββββββββββββββ
|
96 |
@app.websocket("/ws/tts")
|
97 |
async def tts(ws: WebSocket):
|
98 |
await ws.accept()
|
99 |
try:
|
100 |
req = json.loads(await ws.receive_text())
|
101 |
ids, attn = build_inputs(req.get("text", ""), req.get("voice", "Jakob"))
|
102 |
+
prompt_len = ids.size(1) # LΓ€nge des Prompts
|
103 |
|
104 |
+
past = None
|
105 |
buf = []
|
106 |
|
107 |
while True:
|
|
|
114 |
do_sample=True, top_p=0.95, temperature=0.7,
|
115 |
return_dict_in_generate=True,
|
116 |
use_cache=True,
|
117 |
+
return_legacy_cache=True, # wichtig <4.49
|
118 |
)
|
119 |
|
120 |
+
# Cache fΓΌr den nΓ€chsten Loop
|
121 |
+
past = gen.past_key_values if not isinstance(gen.past_key_values, Cache) else gen.past_key_values.to_legacy()
|
122 |
+
|
123 |
+
seq = gen.sequences[0].tolist()
|
124 |
+
new_tok = new_tokens_only(seq, prompt_len)
|
125 |
+
prompt_len = len(seq) # nΓ€chstes Delta
|
126 |
|
127 |
+
if not new_tok: # (selten) nichts erzeugt β weiter
|
128 |
+
continue
|
129 |
|
130 |
for t in new_tok:
|
131 |
if t == EOS_TOKEN:
|
|
|
138 |
await ws.send_bytes(decode_block(buf))
|
139 |
buf.clear()
|
140 |
|
141 |
+
ids = None; attn = None # ab jetzt nur noch Cache
|
|
|
142 |
|
143 |
+
except (StopAsyncIteration, WebSocketDisconnect):
|
144 |
+
pass
|
145 |
except Exception as e:
|
146 |
print("WSβError:", e)
|
147 |
+
if ws.client_state.name == "CONNECTED":
|
148 |
+
await ws.close(code=1011)
|
149 |
finally:
|
150 |
+
if ws.client_state.name == "CONNECTED":
|
151 |
+
await ws.close()
|
152 |
+
|
153 |
+
# ββ 6. Local run ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
154 |
if __name__ == "__main__":
|
155 |
+
import uvicorn, sys
|
156 |
+
port = int(sys.argv[1]) if len(sys.argv) > 1 else 7860
|
157 |
+
uvicorn.run("app:app", host="0.0.0.0", port=port)
|