Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,146 +3,159 @@ import os, json, torch, asyncio
|
|
| 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
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
if HF_TOKEN:
|
| 12 |
login(HF_TOKEN)
|
| 13 |
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
-
torch.backends.cuda.enable_flash_sdp(False)
|
| 16 |
-
|
| 17 |
-
# 1
|
| 18 |
-
REPO
|
| 19 |
-
CHUNK_TOKENS
|
| 20 |
-
START_TOKEN
|
| 21 |
-
NEW_BLOCK
|
| 22 |
-
EOS_TOKEN
|
| 23 |
-
AUDIO_BASE
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def __init__(self, audio_ids: torch.Tensor, min_blocks:int=1):
|
| 30 |
super().__init__()
|
| 31 |
-
self.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
app = FastAPI()
|
| 46 |
|
| 47 |
@app.get("/")
|
| 48 |
-
|
| 49 |
-
return {"
|
| 50 |
|
| 51 |
@app.on_event("startup")
|
| 52 |
-
|
| 53 |
global tok, model, snac, masker
|
| 54 |
-
print("⏳
|
|
|
|
| 55 |
tok = AutoTokenizer.from_pretrained(REPO)
|
| 56 |
snac = SNAC.from_pretrained("hubertsiuzdak/snac_24khz").to(device)
|
| 57 |
model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
REPO,
|
|
|
|
|
|
|
| 59 |
low_cpu_mem_usage=True,
|
| 60 |
-
device_map={"":0} if device=="cuda" else None,
|
| 61 |
-
torch_dtype=torch.bfloat16 if device=="cuda" else None,
|
| 62 |
)
|
| 63 |
model.config.pad_token_id = model.config.eos_token_id
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
print("✅
|
| 67 |
-
|
| 68 |
-
# 4
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
ids
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
return ids,
|
| 76 |
-
|
| 77 |
-
def decode_block(
|
| 78 |
l1,l2,l3=[],[],[]
|
| 79 |
-
l1.append(
|
| 80 |
-
l2.append(
|
| 81 |
-
l3 += [
|
| 82 |
-
l2.append(
|
| 83 |
-
l3 += [
|
| 84 |
-
|
| 85 |
with torch.no_grad():
|
|
|
|
|
|
|
| 86 |
audio = snac.decode(codes).squeeze().detach().cpu().numpy()
|
|
|
|
| 87 |
return (audio*32767).astype("int16").tobytes()
|
| 88 |
|
| 89 |
-
# 5
|
| 90 |
@app.websocket("/ws/tts")
|
| 91 |
async def tts(ws: WebSocket):
|
| 92 |
await ws.accept()
|
| 93 |
try:
|
| 94 |
req = json.loads(await ws.receive_text())
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
while True:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
input_ids
|
| 103 |
-
attention_mask
|
| 104 |
-
past_key_values= past,
|
| 105 |
-
max_new_tokens
|
| 106 |
-
logits_processor=[masker],
|
| 107 |
do_sample=True, temperature=0.7, top_p=0.95,
|
| 108 |
-
use_cache=True
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
if
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
for t in new:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
if t == NEW_BLOCK:
|
|
|
|
|
|
|
| 126 |
buf.append(t - AUDIO_BASE)
|
| 127 |
if len(buf) == 7:
|
| 128 |
await ws.send_bytes(decode_block(buf))
|
| 129 |
buf.clear()
|
| 130 |
-
masker.
|
| 131 |
-
|
| 132 |
-
ids, attn = None, None # ab jetzt 1‑Token‑Step
|
| 133 |
|
| 134 |
except (StopIteration, WebSocketDisconnect):
|
| 135 |
pass
|
| 136 |
except Exception as e:
|
| 137 |
-
print("❌
|
| 138 |
if ws.client_state.name != "DISCONNECTED":
|
| 139 |
await ws.close(code=1011)
|
| 140 |
finally:
|
| 141 |
if ws.client_state.name != "DISCONNECTED":
|
| 142 |
-
try:
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
# 6
|
| 146 |
if __name__ == "__main__":
|
| 147 |
-
import uvicorn
|
| 148 |
-
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
|
|
|
| 3 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 4 |
from huggingface_hub import login
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, LogitsProcessor
|
|
|
|
| 6 |
from snac import SNAC
|
| 7 |
|
| 8 |
+
# 0) Login + Device ---------------------------------------------------
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
if HF_TOKEN:
|
| 11 |
login(HF_TOKEN)
|
| 12 |
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
torch.backends.cuda.enable_flash_sdp(False) # PyTorch‑2.2‑Bug
|
| 15 |
+
|
| 16 |
+
# 1) Konstanten -------------------------------------------------------
|
| 17 |
+
REPO = "SebastianBodza/Kartoffel_Orpheus-3B_german_natural-v0.1"
|
| 18 |
+
CHUNK_TOKENS = 50
|
| 19 |
+
START_TOKEN = 128259
|
| 20 |
+
NEW_BLOCK = 128257
|
| 21 |
+
EOS_TOKEN = 128258
|
| 22 |
+
AUDIO_BASE = 128266
|
| 23 |
+
AUDIO_IDS = torch.arange(AUDIO_BASE, AUDIO_BASE + 4096)
|
| 24 |
+
|
| 25 |
+
# 2) Logit‑Mask (NEW_BLOCK + Audio; EOS erst nach 1. Block) ----------
|
| 26 |
+
class AudioMask(LogitsProcessor):
|
| 27 |
+
def __init__(self, audio_ids: torch.Tensor):
|
|
|
|
| 28 |
super().__init__()
|
| 29 |
+
self.allow = torch.cat([
|
| 30 |
+
torch.tensor([NEW_BLOCK], device=audio_ids.device),
|
| 31 |
+
audio_ids
|
| 32 |
+
])
|
| 33 |
+
self.eos = torch.tensor([EOS_TOKEN], device=audio_ids.device)
|
| 34 |
+
self.sent_blocks = 0
|
| 35 |
+
|
| 36 |
+
def __call__(self, input_ids, logits):
|
| 37 |
+
allowed = self.allow
|
| 38 |
+
if self.sent_blocks: # ab 1. Block EOS zulassen
|
| 39 |
+
allowed = torch.cat([allowed, self.eos])
|
| 40 |
+
mask = logits.new_full(logits.shape, float("-inf"))
|
| 41 |
+
mask[:, allowed] = 0
|
| 42 |
+
return logits + mask
|
| 43 |
+
|
| 44 |
+
# 3) FastAPI Grundgerüst ---------------------------------------------
|
| 45 |
app = FastAPI()
|
| 46 |
|
| 47 |
@app.get("/")
|
| 48 |
+
def hello():
|
| 49 |
+
return {"status": "ok"}
|
| 50 |
|
| 51 |
@app.on_event("startup")
|
| 52 |
+
def load_models():
|
| 53 |
global tok, model, snac, masker
|
| 54 |
+
print("⏳ Lade Modelle …", flush=True)
|
| 55 |
+
|
| 56 |
tok = AutoTokenizer.from_pretrained(REPO)
|
| 57 |
snac = SNAC.from_pretrained("hubertsiuzdak/snac_24khz").to(device)
|
| 58 |
model = AutoModelForCausalLM.from_pretrained(
|
| 59 |
REPO,
|
| 60 |
+
device_map={"": 0} if device == "cuda" else None,
|
| 61 |
+
torch_dtype=torch.bfloat16 if device == "cuda" else None,
|
| 62 |
low_cpu_mem_usage=True,
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
model.config.pad_token_id = model.config.eos_token_id
|
| 65 |
+
masker = AudioMask(AUDIO_IDS.to(device))
|
| 66 |
+
|
| 67 |
+
print("✅ Modelle geladen", flush=True)
|
| 68 |
+
|
| 69 |
+
# 4) Helper -----------------------------------------------------------
|
| 70 |
+
def build_prompt(text: str, voice: str):
|
| 71 |
+
prompt_ids = tok(f"{voice}: {text}", return_tensors="pt").input_ids.to(device)
|
| 72 |
+
ids = torch.cat([torch.tensor([[START_TOKEN]], device=device),
|
| 73 |
+
prompt_ids,
|
| 74 |
+
torch.tensor([[128009, 128260]], device=device)], 1)
|
| 75 |
+
attn = torch.ones_like(ids)
|
| 76 |
+
return ids, attn
|
| 77 |
+
|
| 78 |
+
def decode_block(block7: list[int]) -> bytes:
|
| 79 |
l1,l2,l3=[],[],[]
|
| 80 |
+
l1.append(block7[0])
|
| 81 |
+
l2.append(block7[1]-4096)
|
| 82 |
+
l3 += [block7[2]-8192, block7[3]-12288]
|
| 83 |
+
l2.append(block7[4]-16384)
|
| 84 |
+
l3 += [block7[5]-20480, block7[6]-24576]
|
| 85 |
+
|
| 86 |
with torch.no_grad():
|
| 87 |
+
codes = [torch.tensor(x, device=device).unsqueeze(0)
|
| 88 |
+
for x in (l1,l2,l3)]
|
| 89 |
audio = snac.decode(codes).squeeze().detach().cpu().numpy()
|
| 90 |
+
|
| 91 |
return (audio*32767).astype("int16").tobytes()
|
| 92 |
|
| 93 |
+
# 5) WebSocket‑Endpoint ----------------------------------------------
|
| 94 |
@app.websocket("/ws/tts")
|
| 95 |
async def tts(ws: WebSocket):
|
| 96 |
await ws.accept()
|
| 97 |
try:
|
| 98 |
req = json.loads(await ws.receive_text())
|
| 99 |
+
text = req.get("text", "")
|
| 100 |
+
voice = req.get("voice", "Jakob")
|
| 101 |
+
|
| 102 |
+
ids, attn = build_prompt(text, voice)
|
| 103 |
+
past = None
|
| 104 |
+
offset_len = ids.size(1) # wie viele Tokens existieren schon
|
| 105 |
+
last_tok = None
|
| 106 |
+
buf = []
|
| 107 |
|
| 108 |
while True:
|
| 109 |
+
# --- Mini‑Generate -------------------------------------------
|
| 110 |
+
gen = model.generate(
|
| 111 |
+
input_ids = ids if past is None else torch.tensor([[last_tok]], device=device),
|
| 112 |
+
attention_mask = attn if past is None else None,
|
| 113 |
+
past_key_values = past,
|
| 114 |
+
max_new_tokens = CHUNK_TOKENS,
|
| 115 |
+
logits_processor= [masker],
|
| 116 |
do_sample=True, temperature=0.7, top_p=0.95,
|
| 117 |
+
use_cache=True
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# ----- neue Tokens heraus schneiden --------------------------
|
| 121 |
+
new = gen[0, offset_len:].tolist()
|
| 122 |
+
if not new: # nichts -> fertig
|
| 123 |
+
break
|
| 124 |
+
offset_len += len(new)
|
| 125 |
+
|
| 126 |
+
# ----- weiter mit Cache (letzte PKV steht im Modell) ---------
|
| 127 |
+
past = model._past_key_values
|
| 128 |
+
last_tok = new[-1]
|
| 129 |
+
|
| 130 |
+
print("new tokens:", new[:25], flush=True)
|
| 131 |
+
|
| 132 |
+
# ----- Token‑Handling ----------------------------------------
|
| 133 |
for t in new:
|
| 134 |
+
if t == EOS_TOKEN:
|
| 135 |
+
raise StopIteration
|
| 136 |
+
if t == NEW_BLOCK:
|
| 137 |
+
buf.clear()
|
| 138 |
+
continue
|
| 139 |
buf.append(t - AUDIO_BASE)
|
| 140 |
if len(buf) == 7:
|
| 141 |
await ws.send_bytes(decode_block(buf))
|
| 142 |
buf.clear()
|
| 143 |
+
masker.sent_blocks = 1 # ab jetzt EOS zulässig
|
|
|
|
|
|
|
| 144 |
|
| 145 |
except (StopIteration, WebSocketDisconnect):
|
| 146 |
pass
|
| 147 |
except Exception as e:
|
| 148 |
+
print("❌ WS‑Error:", e, flush=True)
|
| 149 |
if ws.client_state.name != "DISCONNECTED":
|
| 150 |
await ws.close(code=1011)
|
| 151 |
finally:
|
| 152 |
if ws.client_state.name != "DISCONNECTED":
|
| 153 |
+
try:
|
| 154 |
+
await ws.close()
|
| 155 |
+
except RuntimeError:
|
| 156 |
+
pass
|
| 157 |
|
| 158 |
+
# 6) Dev‑Start --------------------------------------------------------
|
| 159 |
if __name__ == "__main__":
|
| 160 |
+
import uvicorn, sys
|
| 161 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, log_level="info")
|