Update backend.py
Browse files- backend.py +33 -7
backend.py
CHANGED
|
@@ -9,6 +9,9 @@ import reportlab
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from transformers import pipeline
|
| 11 |
import soundfile as sf
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
# -----------------------------------------------------
|
|
@@ -191,14 +194,37 @@ def export_to_pdf(result: dict, filename="FinTalk_Report.pdf"):
|
|
| 191 |
|
| 192 |
|
| 193 |
def generate_tts_files(result):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
try:
|
| 199 |
print(f"🔊 {key} ses üretiliyor...")
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
| 203 |
except Exception as e:
|
| 204 |
print(f"TTS hatası ({key}):", e)
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from transformers import pipeline
|
| 11 |
import soundfile as sf
|
| 12 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 13 |
+
import torch
|
| 14 |
+
from datasets import load_dataset
|
| 15 |
|
| 16 |
|
| 17 |
# -----------------------------------------------------
|
|
|
|
| 194 |
|
| 195 |
|
| 196 |
def generate_tts_files(result):
|
| 197 |
+
try:
|
| 198 |
+
processor = SpeechT5Processor.from_pretrained("facebook/speecht5_tts")
|
| 199 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("facebook/speecht5_tts")
|
| 200 |
+
vocoder = SpeechT5HifiGan.from_pretrained("facebook/speecht5_hifigan")
|
| 201 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 202 |
+
print("🎙️ Offline TTS modeli yüklendi (SpeechT5 + HiFiGAN)")
|
| 203 |
+
except Exception as e:
|
| 204 |
+
print("⚠️ Model yüklenemedi:", e)
|
| 205 |
+
return
|
| 206 |
+
|
| 207 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 208 |
+
model.to(device)
|
| 209 |
+
vocoder.to(device)
|
| 210 |
+
|
| 211 |
+
# default speaker embedding
|
| 212 |
+
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0).to(device)
|
| 213 |
+
|
| 214 |
+
texts = {
|
| 215 |
+
"moderator_intro": result["moderator_intro"],
|
| 216 |
+
"bullish_view": result["bullish_view"],
|
| 217 |
+
"bearish_view": result["bearish_view"],
|
| 218 |
+
"moderator_wrap": result["moderator_wrap"]
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
for key, text in texts.items():
|
| 222 |
try:
|
| 223 |
print(f"🔊 {key} ses üretiliyor...")
|
| 224 |
+
inputs = processor(text=text, return_tensors="pt").to(device)
|
| 225 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embedding, vocoder=vocoder)
|
| 226 |
+
filename = f"{key}.wav"
|
| 227 |
+
sf.write(filename, speech.cpu().numpy(), samplerate=16000)
|
| 228 |
+
print(f"✅ {filename} oluşturuldu (SpeechT5 offline)")
|
| 229 |
except Exception as e:
|
| 230 |
print(f"TTS hatası ({key}):", e)
|