Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
# ๋ชจ๋ธ
|
| 7 |
-
|
| 8 |
-
|
| 9 |
df = pd.read_csv("book_db_final.csv")
|
| 10 |
|
| 11 |
-
# ๊ฐ์ ์ค๋ช
(๊ธฐ์กด analyzer.py ๋ก์ง)
|
| 12 |
_EMOTION_DESCS = {
|
| 13 |
"๊ธฐ์จ": "ํ๋ณตํ๊ณ ์ฆ๊ฒ๊ณ ์ ์พํ ๊ธฐ๋ถ",
|
| 14 |
"์ ๋ขฐ": "๋ฐ๋ปํ๊ณ ์์ ์ ์ด๋ฉฐ ๊ฐ์กฑ๊ณผ ์ฐ์ ๊ฐ์ ์ ๋๊ฐ",
|
|
@@ -19,29 +20,56 @@ _EMOTION_DESCS = {
|
|
| 19 |
"๋ถ๋
ธ": "๋ถ๋
ธ์ ์ ํญ, ํฌ์๊ณผ ๊ฐ๋ฑ",
|
| 20 |
"๊ธฐ๋": "์ฑ์ฅ๊ณผ ๋์ , ๋ชจํ๊ณผ ํฌ๋ง",
|
| 21 |
}
|
| 22 |
-
_LABEL_EMBS =
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
# 1.
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
scores = util.cos_sim(user_emb, _LABEL_EMBS)[0]
|
| 28 |
best_emo = list(_EMOTION_DESCS.keys())[scores.argmax()]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
recs = df[df["emotion"] == best_emo].head(3)
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
for _, row in recs.iterrows():
|
| 35 |
-
|
| 36 |
|
| 37 |
-
return result_text
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import librosa
|
| 4 |
+
import numpy as np
|
| 5 |
from sentence_transformers import SentenceTransformer, util
|
| 6 |
+
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# ๋ชจ๋ธ ๋ก๋ (Hugging Face ์๋ฒ์์ ์คํ๋จ)
|
| 9 |
+
stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-tiny") # ๋น ๋ฅธ ์๋๋ฅผ ์ํด tiny ์ฌ์ฉ
|
| 10 |
+
sbert_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
|
| 11 |
df = pd.read_csv("book_db_final.csv")
|
| 12 |
|
|
|
|
| 13 |
_EMOTION_DESCS = {
|
| 14 |
"๊ธฐ์จ": "ํ๋ณตํ๊ณ ์ฆ๊ฒ๊ณ ์ ์พํ ๊ธฐ๋ถ",
|
| 15 |
"์ ๋ขฐ": "๋ฐ๋ปํ๊ณ ์์ ์ ์ด๋ฉฐ ๊ฐ์กฑ๊ณผ ์ฐ์ ๊ฐ์ ์ ๋๊ฐ",
|
|
|
|
| 20 |
"๋ถ๋
ธ": "๋ถ๋
ธ์ ์ ํญ, ํฌ์๊ณผ ๊ฐ๋ฑ",
|
| 21 |
"๊ธฐ๋": "์ฑ์ฅ๊ณผ ๋์ , ๋ชจํ๊ณผ ํฌ๋ง",
|
| 22 |
}
|
| 23 |
+
_LABEL_EMBS = sbert_model.encode(list(_EMOTION_DESCS.values()), convert_to_tensor=True)
|
| 24 |
|
| 25 |
+
def process_voice_and_recommend(text_input, audio_input):
|
| 26 |
+
# 1. ์์ฑ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด STT๋ก ํ
์คํธ ๋ณํ
|
| 27 |
+
final_text = text_input
|
| 28 |
+
if audio_input is not None:
|
| 29 |
+
sr, y = audio_input
|
| 30 |
+
y = y.astype(np.float32)
|
| 31 |
+
y /= np.max(np.abs(y)) if np.max(np.abs(y)) > 0 else 1
|
| 32 |
+
|
| 33 |
+
# Whisper ๋ชจ๋ธ๋ก ์์ฑ์ ํ
์คํธ๋ก ๋ณํ
|
| 34 |
+
stt_result = stt_model({"sampling_rate": sr, "raw": y})
|
| 35 |
+
final_text = stt_result["text"]
|
| 36 |
+
|
| 37 |
+
if not final_text:
|
| 38 |
+
return "ํ
์คํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ์์ฑ์ ๋
น์ํด์ฃผ์ธ์.", ""
|
| 39 |
+
|
| 40 |
+
# 2. ๊ฐ์ ๋ถ์
|
| 41 |
+
user_emb = sbert_model.encode(final_text, convert_to_tensor=True)
|
| 42 |
scores = util.cos_sim(user_emb, _LABEL_EMBS)[0]
|
| 43 |
best_emo = list(_EMOTION_DESCS.keys())[scores.argmax()]
|
| 44 |
|
| 45 |
+
# 3. ๋์ ์ถ์ฒ
|
| 46 |
recs = df[df["emotion"] == best_emo].head(3)
|
| 47 |
+
result_text = f"๐จ ๋ถ์๋ ๋ฌธ์ฅ: \"{final_text}\"\n๐ญ ๊ฐ์ : {best_emo}\n\n"
|
| 48 |
|
| 49 |
+
book_list = ""
|
| 50 |
for _, row in recs.iterrows():
|
| 51 |
+
book_list += f"๐ {row['title']}\n๐ {row['url']}\n\n"
|
| 52 |
|
| 53 |
+
return result_text, book_list
|
| 54 |
+
|
| 55 |
+
# ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("# ๐ Boolook: ์์ฑ ๊ธฐ๋ฐ ๋ง์ ๋ถ์ ์ฑ
์ถ์ฒ")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
text_in = gr.Textbox(label="์ง์ ์
๋ ฅ", placeholder="์ค๋ ๊ธฐ๋ถ์ด ์ด๋ ์ ๊ฐ์?")
|
| 62 |
+
audio_in = gr.Audio(label="๋ง์ดํฌ ๋
น์", sources=["microphone"])
|
| 63 |
+
submit_btn = gr.Button("๋ถ์ ๋ฐ ์ถ์ฒ๋ฐ๊ธฐ")
|
| 64 |
+
|
| 65 |
+
with gr.Column():
|
| 66 |
+
analysis_out = gr.Textbox(label="๋ถ์ ๊ฒฐ๊ณผ")
|
| 67 |
+
books_out = gr.Textbox(label="์ถ์ฒ ๋์ ๋ฆฌ์คํธ")
|
| 68 |
+
|
| 69 |
+
submit_btn.click(
|
| 70 |
+
fn=process_voice_and_recommend,
|
| 71 |
+
inputs=[text_in, audio_in],
|
| 72 |
+
outputs=[analysis_out, books_out]
|
| 73 |
+
)
|
| 74 |
|
| 75 |
demo.launch()
|