Create app.py
Browse files
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 |
-
"
|
39 |
-
"
|
40 |
-
"
|
41 |
-
"
|
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()
|