Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import librosa
|
| 4 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq, pipeline
|
| 5 |
+
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# 1) STT ๋ชจ๋ธ ๋ก๋ (Whisper ๊ธฐ๋ฐ)
|
| 8 |
+
# -------------------------------
|
| 9 |
+
processor = AutoProcessor.from_pretrained("openai/whisper-small")
|
| 10 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained("openai/whisper-small")
|
| 11 |
+
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# 2) ๊ฐ์ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
| 14 |
+
# -------------------------------
|
| 15 |
+
sentiment_pipe = pipeline(
|
| 16 |
+
"sentiment-analysis",
|
| 17 |
+
model="monologg/koelectra-base-v3-discriminator",
|
| 18 |
+
tokenizer="monologg/koelectra-base-v3-discriminator"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# -------------------------------
|
| 22 |
+
# 3) ์ค๋์ค -> ํ
์คํธ
|
| 23 |
+
# -------------------------------
|
| 24 |
+
def transcribe_audio(audio_path):
|
| 25 |
+
speech, _ = librosa.load(audio_path, sr=16000)
|
| 26 |
+
input_features = processor(speech, sampling_rate=16000, return_tensors="pt").input_features
|
| 27 |
+
predicted_ids = model.generate(input_features)
|
| 28 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 29 |
+
return transcription
|
| 30 |
+
|
| 31 |
+
# -------------------------------
|
| 32 |
+
# 4) ๊ฐ์ ๋ ์ด๋ธ -> ์์ ๋งคํ
|
| 33 |
+
# -------------------------------
|
| 34 |
+
def label_to_color(label):
|
| 35 |
+
if label in ["4 stars", "5 stars", "LABEL_4", "LABEL_5"]:
|
| 36 |
+
return "green" # ๊ธ์
|
| 37 |
+
elif label in ["1 star", "2 stars", "LABEL_1", "LABEL_2"]:
|
| 38 |
+
return "red" # ๋ถ์
|
| 39 |
+
else: # ์ค๋ฆฝ
|
| 40 |
+
return "orange"
|
| 41 |
+
|
| 42 |
+
# -------------------------------
|
| 43 |
+
# 5) ํ
์คํธ ๊ฐ์ ๋ถ์ (๋ฌธ์ฅ ์ ์ฒด)
|
| 44 |
+
# -------------------------------
|
| 45 |
+
def sentiment_whole_text(text):
|
| 46 |
+
res = sentiment_pipe(text)[0]
|
| 47 |
+
label = res['label']
|
| 48 |
+
score = res['score']
|
| 49 |
+
color = label_to_color(label)
|
| 50 |
+
styled_text = f"<span style='color:{color}'>{text}</span>"
|
| 51 |
+
legend = (
|
| 52 |
+
"<div style='margin-top:10px;'>"
|
| 53 |
+
"<b>์์ ์ค๋ช
:</b> "
|
| 54 |
+
"<span style='color:green'>๋
น์=๊ธ์ </span>, "
|
| 55 |
+
"<span style='color:red'>๋นจ๊ฐ=๋ถ์ </span>, "
|
| 56 |
+
"<span style='color:orange'>์ฃผํฉ=์ค๋ฆฝ/๋ณดํต</span>"
|
| 57 |
+
"</div>"
|
| 58 |
+
)
|
| 59 |
+
return styled_text + legend, f"๊ฐ์ : {label}, ์ ๋ขฐ๋: {score:.2f}"
|
| 60 |
+
|
| 61 |
+
# -------------------------------
|
| 62 |
+
# 6) ํ
์คํธ ๊ฐ์ ๋ถ์ (๋จ์ด๋ณ)
|
| 63 |
+
# -------------------------------
|
| 64 |
+
def sentiment_word_level(text):
|
| 65 |
+
words = text.split()
|
| 66 |
+
styled_words = []
|
| 67 |
+
for w in words:
|
| 68 |
+
res = sentiment_pipe(w)[0]
|
| 69 |
+
label = res['label']
|
| 70 |
+
color = label_to_color(label)
|
| 71 |
+
styled_words.append(f"<span style='color:{color}'>{w}</span>")
|
| 72 |
+
styled_text = " ".join(styled_words)
|
| 73 |
+
legend = (
|
| 74 |
+
"<div style='margin-top:10px;'>"
|
| 75 |
+
"<b>์์ ์ค๋ช
:</b> "
|
| 76 |
+
"<span style='color:green'>๋
น์=๊ธ์ </span>, "
|
| 77 |
+
"<span style='color:red'>๋นจ๊ฐ=๋ถ์ </span>, "
|
| 78 |
+
"<span style='color:orange'>์ฃผํฉ=์ค๋ฆฝ/๋ณดํต</span>"
|
| 79 |
+
"</div>"
|
| 80 |
+
)
|
| 81 |
+
return styled_text + legend, "๋จ์ด๋ณ ๊ฐ์ ํ์ ์๋ฃ"
|
| 82 |
+
|
| 83 |
+
# -------------------------------
|
| 84 |
+
# 7) ์ค๋์ค -> ํ
์คํธ + ๊ฐ์ ๋ถ์ (๋ฌธ์ฅ+๋จ์ด)
|
| 85 |
+
# -------------------------------
|
| 86 |
+
def process_audio_full(audio_file):
|
| 87 |
+
text = transcribe_audio(audio_file)
|
| 88 |
+
whole_text_result, whole_text_score = sentiment_whole_text(text)
|
| 89 |
+
word_level_result, word_level_status = sentiment_word_level(text)
|
| 90 |
+
return text, whole_text_result, whole_text_score, word_level_result, word_level_status
|
| 91 |
+
|
| 92 |
+
# -------------------------------
|
| 93 |
+
# 8) Gradio UI ๊ตฌ์ฑ
|
| 94 |
+
# -------------------------------
|
| 95 |
+
with gr.Blocks() as demo:
|
| 96 |
+
gr.Markdown("# ๐ค ์ค๋์ค/ํ
์คํธ โ ๊ฐ์ ๋ถ์")
|
| 97 |
+
|
| 98 |
+
with gr.Tabs():
|
| 99 |
+
# ------------------- ์ค๋์ค -> ํ
์คํธ -------------------
|
| 100 |
+
with gr.Tab("์ค๋์ค โ ํ
์คํธ"):
|
| 101 |
+
audio_input_1 = gr.Audio(label="์์ฑ ์
๋ก๋", type="filepath")
|
| 102 |
+
audio_text_output = gr.Textbox(label="๋ณํ๋ ํ
์คํธ")
|
| 103 |
+
audio_transcribe_btn = gr.Button("ํ
์คํธ ์ถ์ถ")
|
| 104 |
+
audio_transcribe_btn.click(fn=transcribe_audio, inputs=[audio_input_1], outputs=[audio_text_output])
|
| 105 |
+
|
| 106 |
+
# ------------------- ํ
์คํธ -> ๊ฐ์ ๋ถ์ -------------------
|
| 107 |
+
with gr.Tab("ํ
์คํธ โ ๊ฐ์ ๋ถ์"):
|
| 108 |
+
text_input = gr.Textbox(label="ํ
์คํธ ์
๋ ฅ")
|
| 109 |
+
sentiment_whole_output = gr.HTML(label="๋ฌธ์ฅ ๋จ์ ๊ฐ์ ๋ถ์")
|
| 110 |
+
sentiment_whole_score = gr.Markdown(label="๊ฐ์ ๊ฒฐ๊ณผ")
|
| 111 |
+
sentiment_word_output = gr.HTML(label="๋จ์ด ๋จ์ ๊ฐ์ ๋ถ์")
|
| 112 |
+
sentiment_btn = gr.Button("๊ฐ์ ๋ถ์")
|
| 113 |
+
def analyze_text(text):
|
| 114 |
+
whole_res, whole_score = sentiment_whole_text(text)
|
| 115 |
+
word_res, word_status = sentiment_word_level(text)
|
| 116 |
+
return whole_res, whole_score, word_res
|
| 117 |
+
sentiment_btn.click(
|
| 118 |
+
fn=analyze_text,
|
| 119 |
+
inputs=[text_input],
|
| 120 |
+
outputs=[sentiment_whole_output, sentiment_whole_score, sentiment_word_output]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# ------------------- ์ค๋์ค โ ํ
์คํธ + ๊ฐ์ ๋ถ์ -------------------
|
| 124 |
+
with gr.Tab("์ค๋์ค โ ํ
์คํธ + ๊ฐ์ ๋ถ์"):
|
| 125 |
+
audio_input_2 = gr.Audio(label="์์ฑ ์
๋ก๋", type="filepath")
|
| 126 |
+
audio_text_output_2 = gr.Textbox(label="๋ณํ๋ ํ
์คํธ")
|
| 127 |
+
sentiment_whole_output_2 = gr.HTML(label="๋ฌธ์ฅ ๋จ์ ๊ฐ์ ๋ถ์")
|
| 128 |
+
sentiment_whole_score_2 = gr.Markdown(label="๊ฐ์ ๊ฒฐ๊ณผ")
|
| 129 |
+
sentiment_word_output_2 = gr.HTML(label="๋จ์ด ๋จ์ ๊ฐ์ ๋ถ์")
|
| 130 |
+
audio_process_btn = gr.Button("๋ถ์ ์์")
|
| 131 |
+
def process_audio_tab(audio_file):
|
| 132 |
+
text, whole_res, whole_score, word_res, word_status = process_audio_full(audio_file)
|
| 133 |
+
return text, whole_res, whole_score, word_res
|
| 134 |
+
audio_process_btn.click(
|
| 135 |
+
fn=process_audio_tab,
|
| 136 |
+
inputs=[audio_input_2],
|
| 137 |
+
outputs=[audio_text_output_2, sentiment_whole_output_2, sentiment_whole_score_2, sentiment_word_output_2]
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
demo.launch()
|