haepada commited on
Commit
5693c3d
·
verified ·
1 Parent(s): 16cbc9b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -27
app.py CHANGED
@@ -1,63 +1,54 @@
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()
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import librosa
4
  from transformers import pipeline
5
 
6
+ # Initialize models
7
  emotion_analyzer = pipeline("audio-classification", model="MIT/ast-finetuned-speech-commands-v2")
 
 
8
  speech_recognizer = pipeline("automatic-speech-recognition",
9
  model="kresnik/wav2vec2-large-xlsr-korean")
10
 
11
  def analyze_voice(audio_file):
12
+ """Voice analysis function"""
13
  try:
14
+ # Load audio
15
  y, sr = librosa.load(audio_file)
16
 
17
+ # 1. Voice emotion analysis
18
  emotions = emotion_analyzer(y)
19
  primary_emotion = emotions[0]
20
 
21
+ # 2. Speech to text
22
  text_result = speech_recognizer(y)
23
 
24
+ # 3. Extract audio features
25
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
26
  energy = np.mean(librosa.feature.rms(y=y))
27
 
28
  return {
29
+ "emotion": primary_emotion['label'],
30
+ "emotion_probability": f"{primary_emotion['score']:.2f}",
31
+ "transcribed_text": text_result['text'],
32
+ "energy_level": f"{energy:.2f}",
33
+ "status": "Analysis complete"
34
  }
35
  except Exception as e:
36
  return {
37
  "error": str(e),
38
+ "status": "Error occurred"
39
  }
40
 
41
+ # Create Gradio interface
42
  interface = gr.Interface(
43
  fn=analyze_voice,
44
+ inputs=gr.Audio(source="microphone", type="filepath", label="Voice Input"),
45
+ outputs=gr.JSON(label="Analysis Results"),
46
+ title="Digital Gut - Voice Emotion Analysis",
47
+ description="Performs emotion analysis and text conversion from voice input.",
48
  theme=gr.themes.Soft(),
49
  analytics_enabled=True
50
  )
51
 
52
+ # Launch app
53
  if __name__ == "__main__":
54
  interface.launch()