haepada commited on
Commit
bb63470
·
verified ·
1 Parent(s): ba5008d
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 파일 구조
2
+ digital-gut/
3
+ ├── app.py # 메인 Gradio 앱
4
+ ├── requirements.txt # 필요한 패키지
5
+ └── README.md # 설명 문서
6
+
7
+ # app.py
8
+ import gradio as gr
9
+ import numpy as np
10
+ import librosa
11
+ from transformers import pipeline
12
+
13
+ # 음성 감정 분석 모델
14
+ emotion_analyzer = pipeline("audio-classification", model="MIT/ast-finetuned-speech-commands-v2")
15
+
16
+ # 한국어 음성인식 모델
17
+ speech_recognizer = pipeline("automatic-speech-recognition",
18
+ model="kresnik/wav2vec2-large-xlsr-korean")
19
+
20
+ def analyze_voice(audio_file):
21
+ """음성 분석 함수"""
22
+ try:
23
+ # 오디오 로드
24
+ y, sr = librosa.load(audio_file)
25
+
26
+ # 1. 음성 감정 분석
27
+ emotions = emotion_analyzer(y)
28
+ primary_emotion = emotions[0]
29
+
30
+ # 2. 음성 텍스트 변환
31
+ text_result = speech_recognizer(y)
32
+
33
+ # 3. 기본 오디오 특성 추출
34
+ mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
35
+ energy = np.mean(librosa.feature.rms(y=y))
36
+
37
+ return {
38
+ "감정": primary_emotion['label'],
39
+ "감정_확률": f"{primary_emotion['score']:.2f}",
40
+ "음성_텍스트": text_result['text'],
41
+ "에너지_레벨": f"{energy:.2f}",
42
+ "상태": "분석 완료"
43
+ }
44
+ except Exception as e:
45
+ return {
46
+ "error": str(e),
47
+ "상태": "오류 발생"
48
+ }
49
+
50
+ # Gradio 인터페이스 생성
51
+ interface = gr.Interface(
52
+ fn=analyze_voice,
53
+ inputs=gr.Audio(source="microphone", type="filepath", label="음성 입력"),
54
+ outputs=gr.JSON(label="분석 결과"),
55
+ title="디지털 굿판 - 음성 감정 분석",
56
+ description="음성을 통한 감정 분석과 텍스트 변환을 수행합니다.",
57
+ theme=gr.themes.Soft(),
58
+ analytics_enabled=True
59
+ )
60
+
61
+ # 앱 실행
62
+ if __name__ == "__main__":
63
+ interface.launch()