haepada commited on
Commit
ca36284
·
verified ·
1 Parent(s): 92657ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -48
app.py CHANGED
@@ -315,56 +315,77 @@ def create_interface():
315
  return None
316
 
317
  def analyze_voice(audio_path, state):
318
- """음성 분석"""
319
- if audio_path is None:
320
- return state, "음성을 먼저 녹음해주세요.", "", "", ""
321
-
322
- try:
323
- y, sr = librosa.load(audio_path, sr=16000)
324
-
325
- acoustic_features = {
326
- "energy": float(np.mean(librosa.feature.rms(y=y))),
327
- "tempo": float(librosa.beat.tempo(y)[0]),
328
- "pitch": float(np.mean(librosa.feature.zero_crossing_rate(y))),
329
- "volume": float(np.mean(np.abs(y)))
330
- }
331
-
332
- # 기준점이 있는 경우 상대적 분석
333
- baseline = state.get("baseline_features")
334
- emotions = map_acoustic_to_emotion(acoustic_features, baseline)
335
-
336
- transcription = speech_recognizer(y)
337
- text = transcription["text"]
338
- text_sentiment = text_analyzer(text)[0]
339
-
340
- voice_result = (
341
- f"음성 감정: {emotions['primary']} "
342
- f"(강도: {emotions['intensity']:.1f}%, 신뢰도: {emotions['confidence']:.2f})\n"
343
- f"특징: {', '.join(emotions['characteristics'])}\n"
344
- f"상세 분석:\n"
345
- f"- 에너지 레벨: {emotions['details']['energy_level']}\n"
346
- f"- 말하기 속도: {emotions['details']['speech_rate']}\n"
347
- f"- 음높이 변화: {emotions['details']['pitch_variation']}\n"
348
- f"- 음성 크기: {emotions['details']['voice_volume']}"
349
- )
350
-
351
- if baseline:
352
- voice_result += "\n\n[기준점 대비 분석]\n"
353
- voice_result += f"기준 상태와 비교한 감정 강도 변화: {emotions['intensity']-50:.1f}%"
354
-
355
- text_result = f"텍스트 감정 분석 (1-5): {text_sentiment['score']}"
356
- prompt = generate_detailed_prompt(text, emotions, text_sentiment)
357
-
358
- return state, text, voice_result, text_result, prompt
359
- except Exception as e:
360
- return state, f"오류 발생: {str(e)}", "", "", ""
361
 
362
- # 이벤트 연결
363
- start_btn.click(
364
- fn=start_journey,
365
- inputs=[name_input],
366
- outputs=[user_display, tabs]
 
 
 
 
 
 
 
 
 
 
 
 
 
367
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
 
369
  set_baseline_btn.click(
370
  fn=set_baseline,
 
315
  return None
316
 
317
  def analyze_voice(audio_path, state):
318
+ """음성 분석"""
319
+ if audio_path is None:
320
+ return {
321
+ "transcribed_text": "음성을 먼저 녹음해주세요.",
322
+ "voice_emotion": "",
323
+ "text_emotion": "",
324
+ "prompt": ""
325
+ }
326
+
327
+ try:
328
+ y, sr = librosa.load(audio_path, sr=16000)
329
+
330
+ acoustic_features = {
331
+ "energy": float(np.mean(librosa.feature.rms(y=y))),
332
+ "tempo": float(librosa.beat.tempo(y)[0]),
333
+ "pitch": float(np.mean(librosa.feature.zero_crossing_rate(y))),
334
+ "volume": float(np.mean(np.abs(y)))
335
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
 
337
+ # 기준점이 있는 경우 상대적 분석
338
+ baseline = state.get("baseline_features")
339
+ emotions = map_acoustic_to_emotion(acoustic_features, baseline)
340
+
341
+ # 음성-텍스트 변환
342
+ transcription = speech_recognizer(y)
343
+ text = transcription["text"]
344
+ text_sentiment = text_analyzer(text)[0]
345
+
346
+ voice_result = (
347
+ f"음성 감정: {emotions['primary']} "
348
+ f"(강도: {emotions['intensity']:.1f}%, 신뢰도: {emotions['confidence']:.2f})\n"
349
+ f"특징: {', '.join(emotions['characteristics'])}\n"
350
+ f"상세 분석:\n"
351
+ f"- 에너지 레벨: {emotions['details']['energy_level']}\n"
352
+ f"- 말하기 속도: {emotions['details']['speech_rate']}\n"
353
+ f"- 음높이 변화: {emotions['details']['pitch_variation']}\n"
354
+ f"- 음성 크기: {emotions['details']['voice_volume']}"
355
  )
356
+
357
+ if baseline:
358
+ voice_result += "\n\n[기준점 대비 분석]\n"
359
+ voice_result += f"기준 상태와 비교한 감정 강도 변화: {emotions['intensity']-50:.1f}%"
360
+
361
+ text_result = f"텍스트 감정 분석 (1-5): {text_sentiment['score']}"
362
+ prompt = generate_detailed_prompt(text, emotions, text_sentiment)
363
+
364
+ return {
365
+ "transcribed_text": text,
366
+ "voice_emotion": voice_result,
367
+ "text_emotion": text_result,
368
+ "prompt": prompt
369
+ }
370
+ except Exception as e:
371
+ return {
372
+ "transcribed_text": f"오류 발생: {str(e)}",
373
+ "voice_emotion": "",
374
+ "text_emotion": "",
375
+ "prompt": ""
376
+ }
377
+
378
+ # 이벤트 연결 부분도 수정
379
+ analyze_btn.click(
380
+ fn=analyze_voice,
381
+ inputs=[voice_input, state],
382
+ outputs={
383
+ "transcribed_text": transcribed_text,
384
+ "voice_emotion": voice_emotion,
385
+ "text_emotion": text_emotion,
386
+ "final_prompt": final_prompt
387
+ }
388
+ )
389
 
390
  set_baseline_btn.click(
391
  fn=set_baseline,