Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
from sentence_transformers import SentenceTransformer, util
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
df = pd.read_csv("book_db_final.csv")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
def process_voice_only(audio_input):
|
| 29 |
-
# 1. ์์ฑ ์
๋ ฅ ํ์ธ
|
| 30 |
if audio_input is None:
|
| 31 |
return "์์ฑ์ ๋
น์ํด์ฃผ์ธ์.", ""
|
| 32 |
|
| 33 |
-
#
|
| 34 |
sr, y = audio_input
|
| 35 |
y = y.astype(np.float32)
|
| 36 |
y /= np.max(np.abs(y)) if np.max(np.abs(y)) > 0 else 1
|
| 37 |
-
|
| 38 |
stt_result = stt_model({"sampling_rate": sr, "raw": y})
|
| 39 |
final_text = stt_result["text"]
|
| 40 |
|
| 41 |
if not final_text.strip():
|
| 42 |
-
return "์์ฑ์ด ์ธ์๋์ง ์์์ต๋๋ค.
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
best_emo =
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
recs = df[df["emotion"] == best_emo].head(3)
|
| 51 |
-
|
| 52 |
result_text = f"์ธ์๋ ๋ฌธ์ฅ: \"{final_text}\"\n๋ถ์๋ ๊ฐ์ : {best_emo}\n\n"
|
| 53 |
-
|
| 54 |
book_list = ""
|
| 55 |
for _, row in recs.iterrows():
|
| 56 |
-
book_list += f"{row['title']}\n
|
| 57 |
-
|
| 58 |
return result_text, book_list
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("# Boolook: ์์ฑ
|
| 63 |
-
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column():
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
with gr.Column():
|
| 71 |
analysis_out = gr.Textbox(label="๋ถ์ ๊ฒฐ๊ณผ")
|
| 72 |
-
books_out = gr.Textbox(label="์ถ์ฒ ๋์
|
| 73 |
|
| 74 |
-
# ๋ฒํผ ํด๋ฆญ ์ ์์ฑ ์ฒ๋ฆฌ ํจ์ ์ฐ๊ฒฐ
|
| 75 |
submit_btn.click(
|
| 76 |
fn=process_voice_only,
|
| 77 |
inputs=[audio_in],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# ===============================
|
| 7 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 8 |
+
# ===============================
|
| 9 |
+
stt_model = pipeline(
|
| 10 |
+
"automatic-speech-recognition",
|
| 11 |
+
model="openai/whisper-large-v3-turbo",
|
| 12 |
+
device=0
|
| 13 |
+
)
|
| 14 |
|
| 15 |
+
emotion_model = pipeline(
|
| 16 |
+
"text-classification",
|
| 17 |
+
model="monologg/koelectra-base-v3-goemotions",
|
| 18 |
+
device=0,
|
| 19 |
+
top_k=1
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# ๋ฐ์ดํฐ ๋ก๋
|
| 23 |
df = pd.read_csv("book_db_final.csv")
|
| 24 |
|
| 25 |
+
# ๊ฐ์ ๋งคํ (Plutchik ์ ์ง)
|
| 26 |
+
EMOTION_MAP = {
|
| 27 |
+
"joy": "๊ธฐ์จ",
|
| 28 |
+
"sadness": "์ฌํ",
|
| 29 |
+
"anger": "๋ถ๋
ธ",
|
| 30 |
+
"fear": "๊ณตํฌ",
|
| 31 |
+
"surprise": "๋๋",
|
| 32 |
+
"disgust": "ํ์ค",
|
| 33 |
+
"love": "์ ๋ขฐ",
|
| 34 |
+
"optimism": "๊ธฐ๋"
|
| 35 |
}
|
| 36 |
|
| 37 |
+
# ===============================
|
| 38 |
+
# ๋ฉ์ธ ์ฒ๋ฆฌ ํจ์
|
| 39 |
+
# ===============================
|
| 40 |
def process_voice_only(audio_input):
|
|
|
|
| 41 |
if audio_input is None:
|
| 42 |
return "์์ฑ์ ๋
น์ํด์ฃผ์ธ์.", ""
|
| 43 |
|
| 44 |
+
# STT
|
| 45 |
sr, y = audio_input
|
| 46 |
y = y.astype(np.float32)
|
| 47 |
y /= np.max(np.abs(y)) if np.max(np.abs(y)) > 0 else 1
|
| 48 |
+
|
| 49 |
stt_result = stt_model({"sampling_rate": sr, "raw": y})
|
| 50 |
final_text = stt_result["text"]
|
| 51 |
|
| 52 |
if not final_text.strip():
|
| 53 |
+
return "์์ฑ์ด ์ธ์๋์ง ์์์ต๋๋ค.", ""
|
| 54 |
+
|
| 55 |
+
# ๊ฐ์ ๋ถ์ (classifier ์ฌ์ฉ)
|
| 56 |
+
emo_result = emotion_model(final_text)[0][0]
|
| 57 |
+
raw_label = emo_result["label"].lower()
|
| 58 |
+
best_emo = EMOTION_MAP.get(raw_label, "๊ธฐ๋")
|
| 59 |
+
|
| 60 |
+
# ์ถ์ฒ
|
| 61 |
recs = df[df["emotion"] == best_emo].head(3)
|
| 62 |
+
|
| 63 |
result_text = f"์ธ์๋ ๋ฌธ์ฅ: \"{final_text}\"\n๋ถ์๋ ๊ฐ์ : {best_emo}\n\n"
|
| 64 |
+
|
| 65 |
book_list = ""
|
| 66 |
for _, row in recs.iterrows():
|
| 67 |
+
book_list += f"{row['title']}\n{row['url']}\n\n"
|
| 68 |
+
|
| 69 |
return result_text, book_list
|
| 70 |
|
| 71 |
+
|
| 72 |
+
# ===============================
|
| 73 |
+
# UI
|
| 74 |
+
# ===============================
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("# Boolook: ์์ฑ ๊ธฐ๋ฐ ๊ฐ์ ๋ถ์ ์ฑ
์ถ์ฒ")
|
| 77 |
+
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column():
|
| 80 |
+
audio_in = gr.Audio(label="๋ง์ดํฌ ์
๋ ฅ", sources=["microphone"])
|
| 81 |
+
submit_btn = gr.Button("๋ถ์", variant="primary")
|
| 82 |
+
|
|
|
|
| 83 |
with gr.Column():
|
| 84 |
analysis_out = gr.Textbox(label="๋ถ์ ๊ฒฐ๊ณผ")
|
| 85 |
+
books_out = gr.Textbox(label="์ถ์ฒ ๋์")
|
| 86 |
|
|
|
|
| 87 |
submit_btn.click(
|
| 88 |
fn=process_voice_only,
|
| 89 |
inputs=[audio_in],
|