minjune121 commited on
Commit
845db5f
ยท
verified ยท
1 Parent(s): f428a77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -280,7 +280,7 @@ def fused_emotion(t_scores: Dict[str, float], a_scores: Dict[str, float]) -> Tup
280
  # ============================================================
281
  # ์ถ”์ฒœ
282
  # ============================================================
283
- def get_recommendations(user_text: str, emotion: str, top_n: int = 3) -> List[Dict]:
284
  with _data_lock:
285
  ready = _data_ready
286
  _df = df
@@ -291,7 +291,7 @@ def get_recommendations(user_text: str, emotion: str, top_n: int = 3) -> List[Di
291
 
292
  try:
293
  session_w = _session.score_multiplier(emotion)
294
- user_vec = sbert_model.encode(user_text, convert_to_tensor=True, show_progress_bar=False)
295
  cos_sims = sbert_util.cos_sim(user_vec, _emb)[0]
296
  if torch.cuda.is_available():
297
  cos_sims = cos_sims.cpu()
@@ -324,15 +324,15 @@ def get_recommendations(user_text: str, emotion: str, top_n: int = 3) -> List[Di
324
  # ============================================================
325
  # ์ถ”์ฒœ ๊ฒฐ๊ณผ โ†’ JSON ๋ Œ๋”๋ง
326
  # ============================================================
327
- def _render_books_json(user_text: str, emotion: str, combined: Dict[str, float], books: List[Dict]) -> str:
328
  if not books:
329
  return json.dumps({"error": "์ถ”์ฒœํ•  ์ฑ…์„ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค."}, ensure_ascii=False, indent=2)
330
 
331
  output = {
332
- "user_text": user_text,
333
  "emotion": emotion,
334
  "emotion_score": round(combined.get(emotion, 0.0), 3),
335
- "recommendations": [
336
  {
337
  "isbn": b["isbn"],
338
  "title": b["title"],
@@ -428,17 +428,17 @@ def process_voice(audio_input):
428
  y = y / max_v
429
 
430
  stt_result = stt_model({"sampling_rate": sr, "raw": y})
431
- user_text = stt_result["text"].strip()
432
 
433
- if not user_text:
434
  return json.dumps({"error": "โŒ ์Œ์„ฑ์ด ์ธ์‹๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."}, ensure_ascii=False, indent=2), [], ""
435
 
436
- t_scores = text_emotion_scores(user_text)
437
  a_scores = audio_emotion_scores(y, sr)
438
  top_label, combined = fused_emotion(t_scores, a_scores)
439
 
440
- books = get_recommendations(user_text, top_label, top_n=3)
441
- books_json = _render_books_json(user_text, top_label, combined, books)
442
 
443
  return books_json, books, top_label
444
 
 
280
  # ============================================================
281
  # ์ถ”์ฒœ
282
  # ============================================================
283
+ def get_recommendations(user_input: str, emotion: str, top_n: int = 3) -> List[Dict]:
284
  with _data_lock:
285
  ready = _data_ready
286
  _df = df
 
291
 
292
  try:
293
  session_w = _session.score_multiplier(emotion)
294
+ user_vec = sbert_model.encode(user_input, convert_to_tensor=True, show_progress_bar=False)
295
  cos_sims = sbert_util.cos_sim(user_vec, _emb)[0]
296
  if torch.cuda.is_available():
297
  cos_sims = cos_sims.cpu()
 
324
  # ============================================================
325
  # ์ถ”์ฒœ ๊ฒฐ๊ณผ โ†’ JSON ๋ Œ๋”๋ง
326
  # ============================================================
327
+ def _render_books_json(user_input: str, emotion: str, combined: Dict[str, float], books: List[Dict]) -> str:
328
  if not books:
329
  return json.dumps({"error": "์ถ”์ฒœํ•  ์ฑ…์„ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค."}, ensure_ascii=False, indent=2)
330
 
331
  output = {
332
+ "user_input": user_input,
333
  "emotion": emotion,
334
  "emotion_score": round(combined.get(emotion, 0.0), 3),
335
+ "recommendation_books": [
336
  {
337
  "isbn": b["isbn"],
338
  "title": b["title"],
 
428
  y = y / max_v
429
 
430
  stt_result = stt_model({"sampling_rate": sr, "raw": y})
431
+ user_input = stt_result["text"].strip()
432
 
433
+ if not user_input:
434
  return json.dumps({"error": "โŒ ์Œ์„ฑ์ด ์ธ์‹๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."}, ensure_ascii=False, indent=2), [], ""
435
 
436
+ t_scores = text_emotion_scores(user_input)
437
  a_scores = audio_emotion_scores(y, sr)
438
  top_label, combined = fused_emotion(t_scores, a_scores)
439
 
440
+ books = get_recommendations(user_input, top_label, top_n=3)
441
+ books_json = _render_books_json(user_input, top_label, combined, books)
442
 
443
  return books_json, books, top_label
444