Update backend.py
Browse files- backend.py +45 -19
backend.py
CHANGED
|
@@ -8,7 +8,7 @@ from typing import Dict
|
|
| 8 |
from llama_cpp import Llama
|
| 9 |
from openai import OpenAI
|
| 10 |
import edge_tts
|
| 11 |
-
|
| 12 |
import asyncio
|
| 13 |
import reportlab
|
| 14 |
from huggingface_hub import hf_hub_download
|
|
@@ -199,32 +199,58 @@ except Exception:
|
|
| 199 |
from gtts import gTTS
|
| 200 |
USE_EDGE = False
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
voices = {
|
| 205 |
"moderator_intro": "en-US-AriaNeural",
|
| 206 |
"bullish_view": "en-US-GuyNeural",
|
| 207 |
"bearish_view": "en-GB-LibbyNeural",
|
| 208 |
-
"moderator_wrap": "en-US-AriaNeural"
|
| 209 |
}
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
continue
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
|
| 229 |
# -----------------------------------------------------
|
| 230 |
# 8) Hızlı Test
|
|
|
|
| 8 |
from llama_cpp import Llama
|
| 9 |
from openai import OpenAI
|
| 10 |
import edge_tts
|
| 11 |
+
import pyttsx3
|
| 12 |
import asyncio
|
| 13 |
import reportlab
|
| 14 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 199 |
from gtts import gTTS
|
| 200 |
USE_EDGE = False
|
| 201 |
|
| 202 |
+
import asyncio
|
| 203 |
+
import time
|
| 204 |
+
|
| 205 |
+
# TTS fallback: edge_tts (lokal) → pyttsx3 (offline)
|
| 206 |
+
def generate_tts_files(result):
|
| 207 |
+
"""
|
| 208 |
+
Hugging Face ortamında (internet kısıtlı) pyttsx3 kullanır,
|
| 209 |
+
lokalde ise edge-tts ile yüksek kaliteli ses üretir.
|
| 210 |
+
"""
|
| 211 |
+
try:
|
| 212 |
+
import edge_tts
|
| 213 |
+
USE_EDGE = True
|
| 214 |
+
print("🎧 edge-tts modu aktif (lokal yüksek kalite)")
|
| 215 |
+
except Exception:
|
| 216 |
+
import pyttsx3
|
| 217 |
+
USE_EDGE = False
|
| 218 |
+
print("🎙️ pyttsx3 modu aktif (offline fallback)")
|
| 219 |
+
|
| 220 |
voices = {
|
| 221 |
"moderator_intro": "en-US-AriaNeural",
|
| 222 |
"bullish_view": "en-US-GuyNeural",
|
| 223 |
"bearish_view": "en-GB-LibbyNeural",
|
| 224 |
+
"moderator_wrap": "en-US-AriaNeural"
|
| 225 |
}
|
| 226 |
|
| 227 |
+
if USE_EDGE:
|
| 228 |
+
async def edge_generate():
|
| 229 |
+
for key, voice in voices.items():
|
| 230 |
+
text = result[key]
|
| 231 |
+
filename = f"{key}.mp3"
|
| 232 |
+
try:
|
| 233 |
+
communicate = edge_tts.Communicate(text, voice=voice, rate="+0%")
|
| 234 |
+
await communicate.save(filename)
|
| 235 |
+
print(f"✅ {filename} oluşturuldu (edge-tts)")
|
| 236 |
+
except Exception as e:
|
| 237 |
+
print(f"edge-tts hatası ({key}):", e)
|
| 238 |
+
await asyncio.sleep(1) # küçük gecikme
|
| 239 |
+
asyncio.run(edge_generate())
|
| 240 |
|
| 241 |
+
else:
|
| 242 |
+
engine = pyttsx3.init()
|
| 243 |
+
voices_local = engine.getProperty('voices')
|
| 244 |
+
for key in result.keys():
|
| 245 |
+
if key not in ["moderator_intro", "bullish_view", "bearish_view", "moderator_wrap"]:
|
| 246 |
continue
|
| 247 |
+
voice_id = voices_local[0].id if len(voices_local) > 0 else None
|
| 248 |
+
engine.setProperty('voice', voice_id)
|
| 249 |
+
filename = f"{key}.mp3"
|
| 250 |
+
engine.save_to_file(result[key], filename)
|
| 251 |
+
engine.runAndWait()
|
| 252 |
+
print(f"✅ {filename} oluşturuldu (pyttsx3 offline)")
|
| 253 |
+
time.sleep(1)
|
| 254 |
|
| 255 |
# -----------------------------------------------------
|
| 256 |
# 8) Hızlı Test
|