Nguyen5 commited on
Commit
35f9d6c
·
1 Parent(s): 70e8124
Files changed (1) hide show
  1. app.py +15 -41
app.py CHANGED
@@ -59,57 +59,31 @@ def format_sources(src):
59
 
60
  return "\n".join(out)
61
 
62
- # =====================================================
63
- # TEXT CHATBOT
64
- # =====================================================
65
 
66
- def chatbot_text(user_message, history):
67
- if not user_message:
68
  return history, ""
69
 
70
- answer_text, sources = answer(
71
- question=user_message,
72
- retriever=_retriever,
73
- chat_model=_llm,
74
- )
75
-
76
- quellen_block = format_sources_markdown(sources)
77
-
78
- history = history + [
79
- {"role": "user", "content": user_message},
80
- {"role": "assistant", "content": answer_text + quellen_block},
81
- ]
82
-
83
  return history, ""
84
 
85
- # =====================================================
86
- # VOICE CHATBOT
87
- # =====================================================
88
 
89
- def chatbot_voice(audio_path, history):
90
- # 1. Speech → Text
91
- text = transcribe_audio(audio_path)
92
- if not text:
93
- return history, None, ""
94
 
95
- # Lưu vào lịch sử chat
96
- history = history + [{"role": "user", "content": text}]
97
-
98
- # 2. RAG trả lời
99
- answer_text, sources = answer(
100
- question=text,
101
- retriever=_retriever,
102
- chat_model=_llm,
103
- )
104
- quellen_block = format_sources_markdown(sources)
105
 
106
- bot_msg = answer_text + quellen_block
107
- history = history + [{"role": "assistant", "content": bot_msg}]
108
 
109
- # 3. Text → Speech
110
- audio = synthesize_speech(bot_msg)
111
 
112
- return history, audio, ""
113
 
114
  # =====================================================
115
  # LAST ANSWER → TTS
 
59
 
60
  return "\n".join(out)
61
 
62
+ # ===================== TEXT CHAT =====================
 
 
63
 
64
+ def chatbot_text(msg, history):
65
+ if not msg:
66
  return history, ""
67
 
68
+ ans, sources = answer(msg, retriever, llm)
69
+ history.append({"role": "user", "content": msg})
70
+ history.append({"role": "assistant", "content": ans + format_sources(sources)})
 
 
 
 
 
 
 
 
 
 
71
  return history, ""
72
 
73
+ # ===================== VOICE CHAT =====================
 
 
74
 
75
+ def chatbot_voice(audio, history):
76
+ text = transcribe_audio(audio)
77
+ history.append({"role": "user", "content": text})
 
 
78
 
79
+ ans, sources = answer(text, retriever, llm)
80
+ bot_msg = ans + format_sources(sources)
 
 
 
 
 
 
 
 
81
 
82
+ history.append({"role": "assistant", "content": bot_msg})
 
83
 
84
+ tts_audio = synthesize_speech(bot_msg)
85
+ return history, tts_audio, ""
86
 
 
87
 
88
  # =====================================================
89
  # LAST ANSWER → TTS