haepada commited on
Commit
d7b7fbe
·
verified ·
1 Parent(s): 4991658

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -97,24 +97,25 @@ os.makedirs("generated_images", exist_ok=True)
97
 
98
  # 음성 분석 관련 함수들
99
  def calculate_baseline_features(audio_data):
100
- """기준점 음성 특성 분석"""
101
  try:
102
  if isinstance(audio_data, tuple):
103
  sr, y = audio_data
 
 
104
  elif isinstance(audio_data, str):
105
  y, sr = librosa.load(audio_data, sr=16000)
106
  else:
107
  print("Unsupported audio format")
108
  return None
109
 
110
- # 음성이 없는 경우 처리
111
  if len(y) == 0:
112
  print("Empty audio data")
113
  return None
114
 
115
  features = {
116
  "energy": float(np.mean(librosa.feature.rms(y=y))),
117
- "tempo": float(librosa.beat.tempo(y=y, sr=sr)[0]),
 
118
  "pitch": float(np.mean(librosa.feature.zero_crossing_rate(y=y))),
119
  "volume": float(np.mean(np.abs(y))),
120
  "mfcc": librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13).mean(axis=1).tolist()
@@ -205,13 +206,13 @@ def map_acoustic_to_emotion(features, baseline_features=None):
205
  return emotions
206
 
207
  def analyze_voice(audio_data, state):
208
- """통합 음성 분석"""
209
  if audio_data is None:
210
  return state, "음성을 먼저 녹음해주세요.", "", "", ""
211
 
212
  try:
213
- # 오디오 데이터 처리
214
- sr, y = audio_data # 직접 튜플 언패킹
 
215
 
216
  if len(y) == 0:
217
  return state, "음성이 감지되지 않았습니다.", "", "", ""
@@ -219,11 +220,12 @@ def analyze_voice(audio_data, state):
219
  # 음향학적 특성 분석
220
  acoustic_features = {
221
  "energy": float(np.mean(librosa.feature.rms(y=y))),
222
- "tempo": float(librosa.beat.tempo(y=y, sr=sr)[0]),
223
  "pitch": float(np.mean(librosa.feature.zero_crossing_rate(y=y))),
224
  "volume": float(np.mean(np.abs(y)))
225
  }
226
 
 
227
  # 음성 인식
228
  if speech_recognizer:
229
  try:
@@ -401,7 +403,8 @@ def create_interface():
401
  label="축원 문장 녹음하기",
402
  sources=["microphone"],
403
  type="numpy",
404
- streaming=False
 
405
  )
406
  set_baseline_btn = gr.Button("기준점 설정 완료", variant="primary")
407
  baseline_status = gr.Markdown("")
@@ -441,7 +444,8 @@ def create_interface():
441
  label="소원을 나누고 싶은 마음을 말해주세요",
442
  sources=["microphone"],
443
  type="numpy",
444
- streaming=False
 
445
  )
446
  with gr.Row():
447
  clear_btn = gr.Button("녹음 지우기", variant="secondary")
@@ -515,6 +519,7 @@ def create_interface():
515
 
516
  try:
517
  sr, y = audio
 
518
  features = calculate_baseline_features((sr, y))
519
  if features:
520
  current_state = {**current_state, "baseline_features": features}
@@ -634,6 +639,7 @@ if __name__ == "__main__":
634
  demo = create_interface()
635
  demo.launch(
636
  debug=True,
 
637
  server_name="0.0.0.0",
638
  server_port=7860
639
  )
 
97
 
98
  # 음성 분석 관련 함수들
99
  def calculate_baseline_features(audio_data):
 
100
  try:
101
  if isinstance(audio_data, tuple):
102
  sr, y = audio_data
103
+ # 데이터 타입을 float32로 변환
104
+ y = y.astype(np.float32)
105
  elif isinstance(audio_data, str):
106
  y, sr = librosa.load(audio_data, sr=16000)
107
  else:
108
  print("Unsupported audio format")
109
  return None
110
 
 
111
  if len(y) == 0:
112
  print("Empty audio data")
113
  return None
114
 
115
  features = {
116
  "energy": float(np.mean(librosa.feature.rms(y=y))),
117
+ # tempo 함수 업데이트
118
+ "tempo": float(librosa.feature.tempo(y=y, sr=sr)[0]),
119
  "pitch": float(np.mean(librosa.feature.zero_crossing_rate(y=y))),
120
  "volume": float(np.mean(np.abs(y))),
121
  "mfcc": librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13).mean(axis=1).tolist()
 
206
  return emotions
207
 
208
  def analyze_voice(audio_data, state):
 
209
  if audio_data is None:
210
  return state, "음성을 먼저 녹음해주세요.", "", "", ""
211
 
212
  try:
213
+ sr, y = audio_data
214
+ # 데이터 타입을 float32로 변환
215
+ y = y.astype(np.float32)
216
 
217
  if len(y) == 0:
218
  return state, "음성이 감지되지 않았습니다.", "", "", ""
 
220
  # 음향학적 특성 분석
221
  acoustic_features = {
222
  "energy": float(np.mean(librosa.feature.rms(y=y))),
223
+ "tempo": float(librosa.feature.tempo(y=y, sr=sr)[0]),
224
  "pitch": float(np.mean(librosa.feature.zero_crossing_rate(y=y))),
225
  "volume": float(np.mean(np.abs(y)))
226
  }
227
 
228
+
229
  # 음성 인식
230
  if speech_recognizer:
231
  try:
 
403
  label="축원 문장 녹음하기",
404
  sources=["microphone"],
405
  type="numpy",
406
+ streaming=False,
407
+ preload=True # 추가
408
  )
409
  set_baseline_btn = gr.Button("기준점 설정 완료", variant="primary")
410
  baseline_status = gr.Markdown("")
 
444
  label="소원을 나누고 싶은 마음을 말해주세요",
445
  sources=["microphone"],
446
  type="numpy",
447
+ streaming=False,
448
+ preload=True # 추가
449
  )
450
  with gr.Row():
451
  clear_btn = gr.Button("녹음 지우기", variant="secondary")
 
519
 
520
  try:
521
  sr, y = audio
522
+ y = y.astype(np.float32) # float32로 변환
523
  features = calculate_baseline_features((sr, y))
524
  if features:
525
  current_state = {**current_state, "baseline_features": features}
 
639
  demo = create_interface()
640
  demo.launch(
641
  debug=True,
642
+ share=True, # 추가
643
  server_name="0.0.0.0",
644
  server_port=7860
645
  )