minjune121 commited on
Commit
062c63f
ยท
verified ยท
1 Parent(s): 9d5d6fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -21
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 kiwipiepy import Kiwi
5
 
6
- # ๋ชจ๋ธ ๋ฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
7
- model = SentenceTransformer("jhgan/ko-sroberta-multitask")
8
- kiwi = Kiwi()
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 = model.encode(list(_EMOTION_DESCS.values()), convert_to_tensor=True)
23
 
24
- def recommend(text):
25
- # 1. ๊ฐ์ • ๋ถ„์„
26
- user_emb = model.encode(text, convert_to_tensor=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  scores = util.cos_sim(user_emb, _LABEL_EMBS)[0]
28
  best_emo = list(_EMOTION_DESCS.keys())[scores.argmax()]
29
 
30
- # 2. ๋„์„œ ์ถ”์ฒœ (ํ•ด๋‹น ๊ฐ์ • ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ์ƒ์œ„ 3๊ถŒ)
31
  recs = df[df["emotion"] == best_emo].head(3)
 
32
 
33
- result_text = f"๐ŸŽจ ๋ถ„์„๋œ ๊ฐ์ •: {best_emo}\n\n"
34
  for _, row in recs.iterrows():
35
- result_text += f"๐Ÿ“– {row['title']}\n๐Ÿ”— {row['url']}\n\n"
36
 
37
- return result_text
38
-
39
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
40
- demo = gr.Interface(
41
- fn=recommend,
42
- inputs=gr.Textbox(label="์ง€๊ธˆ ๊ธฐ๋ถ„์ด ์–ด๋– ์‹ ๊ฐ€์š”?", placeholder="์˜ค๋Š˜ ๊ณต๋ถ€ํ•˜๋А๋ผ ๋„ˆ๋ฌด ์ง€์น˜๋„ค์š”..."),
43
- outputs=gr.Textbox(label="์ถ”์ฒœ ๊ฒฐ๊ณผ"),
44
- title="Boolook: ๋งˆ์Œ์„ ์ฝ๋Š” ์ฑ… ์ถ”์ฒœ"
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()