englissi commited on
Commit
ebb2d0b
·
verified ·
1 Parent(s): ae9eaeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -5,34 +5,28 @@ import numpy as np
5
  import os
6
 
7
  def text_to_speech(prompt):
8
- try:
9
- # gTTS 이용해 Bulgarian 텍스트를 음성으로 변환
10
- tts = gTTS(text=prompt, lang="bg")
11
- audio_file = "output.mp3"
12
- tts.save(audio_file)
13
- except Exception as e:
14
- return f"TTS 생성 오류: {e}"
15
-
16
- try:
17
- # pydub으로 mp3 파일 불러오기
18
- sound = AudioSegment.from_mp3(audio_file)
19
-
20
- # pydub가 제공하는 raw 데이터를 NumPy 배열로 변환 (int16)
21
- samples = np.array(sound.get_array_of_samples())
22
-
23
- # 만약 스테레오라면 모노로 변환 (채널 평균)
24
- if sound.channels > 1:
25
- samples = samples.reshape((-1, sound.channels))
26
- samples = samples.mean(axis=1)
27
 
28
- # int16 데이터를 float32로 정규화 (범위: [-1.0, 1.0])
29
- samples = samples.astype(np.float32) / 32768.0
30
- sample_rate = sound.frame_rate
31
- except Exception as e:
32
- return f"오디오 처리 오류: {e}"
33
- finally:
34
- if os.path.exists(audio_file):
35
- os.remove(audio_file)
 
 
 
 
 
 
36
 
37
  return samples, sample_rate
38
 
 
5
  import os
6
 
7
  def text_to_speech(prompt):
8
+ # gTTS를 이용해 Bulgarian 텍스트를 음성으로 변환
9
+ tts = gTTS(text=prompt, lang="bg")
10
+ audio_file = "output.mp3"
11
+ tts.save(audio_file)
12
+
13
+ # pydub으로 mp3 파일 불러오기
14
+ sound = AudioSegment.from_mp3(audio_file)
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # pydub가 제공하는 raw 데이터를 NumPy 배열로 변환 (int16)
17
+ samples = np.array(sound.get_array_of_samples())
18
+
19
+ # 만약 스테레오라면 모노로 변환 (채널 평균)
20
+ if sound.channels > 1:
21
+ samples = samples.reshape((-1, sound.channels))
22
+ samples = samples.mean(axis=1)
23
+
24
+ # int16 데이터를 float32로 정규화 (범위: [-1.0, 1.0])
25
+ samples = samples.astype(np.float32) / 32768.0
26
+ sample_rate = sound.frame_rate
27
+
28
+ # 임시 파일 삭제
29
+ os.remove(audio_file)
30
 
31
  return samples, sample_rate
32