Fetii commited on
Commit
8d87425
·
verified ·
1 Parent(s): 2a46d51

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +50 -42
backend.py CHANGED
@@ -202,55 +202,63 @@ except Exception:
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
 
202
  import asyncio
203
  import time
204
 
205
+ import asyncio
206
+ import time
207
+
208
  def generate_tts_files(result):
209
  """
210
+ FinTalk TTS sistemi:
211
+ - edge_tts varsa ve çalışıyorsa kullanır
212
+ - aksi halde pyttsx3'e düşer (offline)
213
  """
214
  try:
215
  import edge_tts
216
+ EDGE_AVAILABLE = True
217
+ print("🎧 edge-tts algılandı (yüksek kalite)")
218
  except Exception:
219
+ EDGE_AVAILABLE = False
220
+ print("🎙️ edge-tts yok, offline moda geçiliyor")
221
+
222
+ if EDGE_AVAILABLE:
223
+ try:
224
+ async def edge_generate():
225
+ voices = {
226
+ "moderator_intro": "en-US-AriaNeural",
227
+ "bullish_view": "en-US-GuyNeural",
228
+ "bearish_view": "en-GB-LibbyNeural",
229
+ "moderator_wrap": "en-US-AriaNeural",
230
+ }
231
+ for key, voice in voices.items():
232
+ text = result[key]
233
+ filename = f"{key}.mp3"
234
+ try:
235
+ communicate = edge_tts.Communicate(text, voice=voice, rate="+0%")
236
+ await communicate.save(filename)
237
+ print(f"✅ {filename} oluşturuldu (edge-tts)")
238
+ except Exception as e:
239
+ print(f"edge-tts hatası ({key}): {e}")
240
+ raise # bir kez hata olursa offline'e geçelim
241
+ await asyncio.sleep(1)
242
+
243
+ asyncio.run(edge_generate())
244
+ return # edge tts çalıştıysa burada bitiriyoruz
245
+
246
+ except Exception as e:
247
+ print(f"⚠️ edge-tts başarısız, offline TTS'e geçiliyor: {e}")
248
+
249
+ # fallback: pyttsx3 (offline, garantili)
250
+ import pyttsx3
251
+ engine = pyttsx3.init()
252
+ voices_local = engine.getProperty("voices")
253
+ for key in ["moderator_intro", "bullish_view", "bearish_view", "moderator_wrap"]:
254
+ voice_id = voices_local[0].id if voices_local else None
255
+ engine.setProperty("voice", voice_id)
256
+ filename = f"{key}.mp3"
257
+ engine.save_to_file(result[key], filename)
258
+ engine.runAndWait()
259
+ print(f"✅ {filename} oluşturuldu (pyttsx3 offline)")
260
+ time.sleep(1)
261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
  # -----------------------------------------------------
264
  # 8) Hızlı Test