aryo100 commited on
Commit
1a4031e
·
1 Parent(s): bd1d892

update external tts

Browse files
Files changed (1) hide show
  1. api.py +69 -5
api.py CHANGED
@@ -37,6 +37,69 @@ edge_tts_voices = [
37
  ]
38
 
39
  BACK4APP_TTS_URL = os.getenv("BACK4APP_TTS_URL")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  async def generate_tts_with_back4app(text: str, voice: str, tts_wav: str):
42
  try:
@@ -103,12 +166,13 @@ async def tts_api(req: TTSRequest):
103
  # 1. Generate TTS
104
  try:
105
  # Ganti pakai Back4App TTS
106
- communicate = edge_tts.Communicate(req.text, req.tts_voice)
107
- with open(tts_wav, "wb") as f:
108
- async for chunk in communicate.stream():
109
- if chunk["type"] == "audio":
110
- f.write(chunk["data"])
111
  # await generate_tts_with_back4app(req.text, req.tts_voice, tts_wav)
 
112
  except Exception as e:
113
  raise HTTPException(status_code=500, detail=f"TTS error: {e}")
114
 
 
37
  ]
38
 
39
  BACK4APP_TTS_URL = os.getenv("BACK4APP_TTS_URL")
40
+ HF_SPACE_TTS_URL = os.getenv("HF_SPACE_TTS_URL")
41
+
42
+ async def generate_tts_with_hf_space(text: str, speaker: str, tts_wav: str):
43
+ """
44
+ Generate TTS menggunakan Hugging Face Space API.
45
+
46
+ Args:
47
+ text: Teks yang akan diubah menjadi suara
48
+ speaker: Nama speaker (contoh: "gadis")
49
+ tts_wav: Path file output untuk menyimpan audio
50
+ """
51
+ try:
52
+ # 1. Kirim request ke API TTS
53
+ response = requests.post(
54
+ f"{HF_SPACE_TTS_URL}/api/tts",
55
+ json={"text": text, "speaker": speaker},
56
+ headers={"Content-Type": "application/json"},
57
+ timeout=60
58
+ )
59
+
60
+ if response.status_code != 200:
61
+ raise HTTPException(
62
+ status_code=500,
63
+ detail=f"HF Space TTS failed: {response.status_code} - {response.text}"
64
+ )
65
+
66
+ response.raise_for_status()
67
+ data = response.json()
68
+
69
+ # 2. Validasi response
70
+ if not data.get("success"):
71
+ raise HTTPException(
72
+ status_code=500,
73
+ detail=f"TTS API returned error: {data.get('message', 'Unknown error')}"
74
+ )
75
+
76
+ # 3. Ambil download URL dari response
77
+ download_url = data.get("download_url")
78
+ if not download_url:
79
+ raise HTTPException(
80
+ status_code=500,
81
+ detail="Response missing download_url"
82
+ )
83
+
84
+ # 4. Download file audio
85
+ # Jika download_url relatif, tambahkan base URL
86
+ if download_url.startswith("/"):
87
+ full_download_url = f"{HF_SPACE_TTS_URL}{download_url}"
88
+ else:
89
+ full_download_url = download_url
90
+
91
+ r = requests.get(full_download_url, stream=True, timeout=60)
92
+ r.raise_for_status()
93
+
94
+ # 5. Simpan file ke tts_wav
95
+ with open(tts_wav, "wb") as f:
96
+ for chunk in r.iter_content(8192):
97
+ f.write(chunk)
98
+
99
+ except requests.exceptions.RequestException as e:
100
+ raise HTTPException(status_code=500, detail=f"TTS error via HF Space: {e}")
101
+ except Exception as e:
102
+ raise HTTPException(status_code=500, detail=f"TTS error via HF Space: {e}")
103
 
104
  async def generate_tts_with_back4app(text: str, voice: str, tts_wav: str):
105
  try:
 
166
  # 1. Generate TTS
167
  try:
168
  # Ganti pakai Back4App TTS
169
+ # communicate = edge_tts.Communicate(req.text, req.tts_voice)
170
+ # with open(tts_wav, "wb") as f:
171
+ # async for chunk in communicate.stream():
172
+ # if chunk["type"] == "audio":
173
+ # f.write(chunk["data"])
174
  # await generate_tts_with_back4app(req.text, req.tts_voice, tts_wav)
175
+ await generate_tts_with_hf_space(req.text, req.tts_voice, tts_wav)
176
  except Exception as e:
177
  raise HTTPException(status_code=500, detail=f"TTS error: {e}")
178