Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 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 |
-
# ๋ชจ๋ธ
|
| 9 |
-
|
|
|
|
| 10 |
sbert_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
|
|
|
|
|
|
|
| 11 |
df = pd.read_csv("book_db_final.csv")
|
| 12 |
|
| 13 |
_EMOTION_DESCS = {
|
|
@@ -20,55 +22,59 @@ _EMOTION_DESCS = {
|
|
| 20 |
"๋ถ๋
ธ": "๋ถ๋
ธ์ ์ ํญ, ํฌ์๊ณผ ๊ฐ๋ฑ",
|
| 21 |
"๊ธฐ๋": "์ฑ์ฅ๊ณผ ๋์ , ๋ชจํ๊ณผ ํฌ๋ง",
|
| 22 |
}
|
|
|
|
| 23 |
_LABEL_EMBS = sbert_model.encode(list(_EMOTION_DESCS.values()), convert_to_tensor=True)
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
# 1. ์์ฑ
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
if not final_text:
|
| 38 |
-
return "
|
| 39 |
|
| 40 |
-
#
|
| 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 |
-
#
|
| 46 |
recs = df[df["emotion"] == best_emo].head(3)
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
book_list = ""
|
| 50 |
for _, row in recs.iterrows():
|
| 51 |
-
book_list += f"
|
| 52 |
-
|
| 53 |
return result_text, book_list
|
| 54 |
|
| 55 |
-
#
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("#
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column():
|
| 61 |
-
|
| 62 |
-
audio_in = gr.Audio(label="๋ง์ดํฌ
|
| 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=
|
| 71 |
-
inputs=[
|
| 72 |
outputs=[analysis_out, books_out]
|
| 73 |
)
|
| 74 |
|
|
|
|
| 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 |
+
# 1. ๋ชจ๋ธ ๋ฐ ๋ฐ์ดํฐ ๋ก๋
|
| 8 |
+
# ๋น ๋ฅธ ์๋๋ฅผ ์ํด whisper-tiny ์ฌ์ฉ
|
| 9 |
+
stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 10 |
sbert_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
|
| 11 |
+
|
| 12 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ก๋ (ํ์ผ๋ช
ํ์ธ ํ์)
|
| 13 |
df = pd.read_csv("book_db_final.csv")
|
| 14 |
|
| 15 |
_EMOTION_DESCS = {
|
|
|
|
| 22 |
"๋ถ๋
ธ": "๋ถ๋
ธ์ ์ ํญ, ํฌ์๊ณผ ๊ฐ๋ฑ",
|
| 23 |
"๊ธฐ๋": "์ฑ์ฅ๊ณผ ๋์ , ๋ชจํ๊ณผ ํฌ๋ง",
|
| 24 |
}
|
| 25 |
+
|
| 26 |
_LABEL_EMBS = sbert_model.encode(list(_EMOTION_DESCS.values()), convert_to_tensor=True)
|
| 27 |
|
| 28 |
+
def process_voice_only(audio_input):
|
| 29 |
+
# 1. ์์ฑ ์
๋ ฅ ํ์ธ
|
| 30 |
+
if audio_input is None:
|
| 31 |
+
return "์์ฑ์ ๋
น์ํด์ฃผ์ธ์.", ""
|
| 32 |
+
|
| 33 |
+
# 2. STT (Speech to Text) ๋ณํ
|
| 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 |
+
# 3. ๊ฐ์ ๋ถ์
|
| 45 |
user_emb = sbert_model.encode(final_text, convert_to_tensor=True)
|
| 46 |
scores = util.cos_sim(user_emb, _LABEL_EMBS)[0]
|
| 47 |
best_emo = list(_EMOTION_DESCS.keys())[scores.argmax()]
|
| 48 |
|
| 49 |
+
# 4. ๋์ ์ถ์ฒ
|
| 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 {row['url']}\n\n"
|
| 57 |
+
|
| 58 |
return result_text, book_list
|
| 59 |
|
| 60 |
+
# --- Gradio UI ๊ตฌ์ฑ ---
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# Boolook: ์์ฑ ์ ์ฉ ๋ง์ ๋ถ์ ์ฑ
์ถ์ฒ")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column():
|
| 66 |
+
# ์ค๋์ค ์
๋ ฅ๋ง ์ ์ง
|
| 67 |
+
audio_in = gr.Audio(label="๋ง์ดํฌ๋ก ๋ง์์ ๋ค๋ ค์ฃผ์ธ์", sources=["microphone"])
|
| 68 |
+
submit_btn = gr.Button("๋ถ์ ๋ฐ ์ถ์ฒ๋ฐ๊ธฐ", variant="primary")
|
| 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],
|
| 78 |
outputs=[analysis_out, books_out]
|
| 79 |
)
|
| 80 |
|