Fetii commited on
Commit
4ef1c50
·
verified ·
1 Parent(s): d0d339e

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +22 -78
backend.py CHANGED
@@ -1,21 +1,16 @@
1
- # ==========================================
2
- # FinTalk - Backend (llama-cpp + GPT summary)
3
- # ==========================================
4
-
5
  import os
6
  import re
7
  from typing import Dict
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
15
 
16
 
17
  # -----------------------------------------------------
18
- # 1) MODEL YOLU (GGUF) - KENDİ YOLUNU YAZ
19
  # -----------------------------------------------------
20
  MODEL_REPO = "QuantFactory/Llama-3-8B-Instruct-Finance-RAG-GGUF"
21
  MODEL_FILE = "Llama-3-8B-Instruct-Finance-RAG.Q4_K_S.gguf"
@@ -192,81 +187,30 @@ def export_to_pdf(result: dict, filename="FinTalk_Report.pdf"):
192
  story.append(Paragraph("<i>Generated by FinTalk – AI Economic Roundtable</i>", styles["Normal"]))
193
  doc.build(story)
194
 
195
- try:
196
- import edge_tts
197
- USE_EDGE = True
198
- except Exception:
199
- from gtts import gTTS
200
- USE_EDGE = False
201
-
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
265
- # -----------------------------------------------------
266
- if __name__ == "__main__":
267
- topic = input("What's discussion topic ?\n>")
268
- result = fintalk_discussion(topic)
269
- result["topic"] = topic
270
- export_to_pdf(result)
271
- asyncio.run(generate_tts_files(result))
272
-
 
 
 
 
 
1
  import os
2
  import re
3
  from typing import Dict
4
  from llama_cpp import Llama
5
  from openai import OpenAI
6
  import edge_tts
 
7
  import asyncio
8
  import reportlab
9
  from huggingface_hub import hf_hub_download
10
 
11
 
12
  # -----------------------------------------------------
13
+ # 1) MODEL YOLU (GGUF)
14
  # -----------------------------------------------------
15
  MODEL_REPO = "QuantFactory/Llama-3-8B-Instruct-Finance-RAG-GGUF"
16
  MODEL_FILE = "Llama-3-8B-Instruct-Finance-RAG.Q4_K_S.gguf"
 
187
  story.append(Paragraph("<i>Generated by FinTalk – AI Economic Roundtable</i>", styles["Normal"]))
188
  doc.build(story)
189
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
  def generate_tts_files(result):
 
 
 
 
 
192
  try:
193
+ pipe = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
194
+ print("🎙️ Offline TTS modeli yüklendi (FastSpeech2)")
195
+ except Exception as e:
196
+ print("⚠️ TTS modeli yüklenemedi:", e)
197
+ return
198
+
199
+ texts = {
200
+ "moderator_intro": result["moderator_intro"],
201
+ "bullish_view": result["bullish_view"],
202
+ "bearish_view": result["bearish_view"],
203
+ "moderator_wrap": result["moderator_wrap"]
204
+ }
205
 
206
+ for key, text in texts.items():
207
  try:
208
+ print(f"🔊 {key} ses üretiliyor...")
209
+ audio_output = pipe(text)
210
+ audio = audio_output["audio"]
211
+ filename = f"{key}.wav"
212
+ sf.write(filename, audio, samplerate=16000)
213
+ print(f" {filename} oluşturuldu (transformers offline)")
214
+ time.sleep(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  except Exception as e:
216
+ print(f"TTS hatası ({key}):", e)