Spaces:
Running
Running
Update app.py
Browse files
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(
|
| 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(
|
| 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(
|
| 328 |
if not books:
|
| 329 |
return json.dumps({"error": "์ถ์ฒํ ์ฑ
์ ์ฐพ์ง ๋ชปํ์ต๋๋ค."}, ensure_ascii=False, indent=2)
|
| 330 |
|
| 331 |
output = {
|
| 332 |
-
"
|
| 333 |
"emotion": emotion,
|
| 334 |
"emotion_score": round(combined.get(emotion, 0.0), 3),
|
| 335 |
-
"
|
| 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 |
-
|
| 432 |
|
| 433 |
-
if not
|
| 434 |
return json.dumps({"error": "โ ์์ฑ์ด ์ธ์๋์ง ์์์ต๋๋ค."}, ensure_ascii=False, indent=2), [], ""
|
| 435 |
|
| 436 |
-
t_scores = text_emotion_scores(
|
| 437 |
a_scores = audio_emotion_scores(y, sr)
|
| 438 |
top_label, combined = fused_emotion(t_scores, a_scores)
|
| 439 |
|
| 440 |
-
books = get_recommendations(
|
| 441 |
-
books_json = _render_books_json(
|
| 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 |
|