Spaces:
Running
Running
Update app_qwen_tts.py
Browse files- app_qwen_tts.py +17 -18
app_qwen_tts.py
CHANGED
|
@@ -21,7 +21,6 @@ TTS_API_URL = "https://rahul7star-Chatterbox-Multilingual-TTS-API.hf.space/tts"
|
|
| 21 |
# =========================================================
|
| 22 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
DOC_PATH = os.path.join(BASE_DIR, DOC_FILE)
|
| 24 |
-
|
| 25 |
if not os.path.exists(DOC_PATH):
|
| 26 |
raise RuntimeError(f"❌ {DOC_FILE} not found next to app.py")
|
| 27 |
|
|
@@ -62,7 +61,7 @@ DOC_CHUNKS = chunk_text(DOC_TEXT)
|
|
| 62 |
DOC_EMBEDS = embedder.encode(DOC_CHUNKS, normalize_embeddings=True, show_progress_bar=True)
|
| 63 |
|
| 64 |
# =========================================================
|
| 65 |
-
#
|
| 66 |
# =========================================================
|
| 67 |
def retrieve_context(question, k=TOP_K):
|
| 68 |
q_emb = embedder.encode([question], normalize_embeddings=True)
|
|
@@ -83,7 +82,7 @@ def extract_final_answer(text: str) -> str:
|
|
| 83 |
return lines[-1] if lines else text
|
| 84 |
|
| 85 |
# =========================================================
|
| 86 |
-
# Generate text
|
| 87 |
# =========================================================
|
| 88 |
def answer_question(question):
|
| 89 |
context = retrieve_context(question)
|
|
@@ -113,12 +112,13 @@ def answer_question(question):
|
|
| 113 |
return extract_final_answer(decoded)
|
| 114 |
|
| 115 |
# =========================================================
|
| 116 |
-
#
|
| 117 |
# =========================================================
|
| 118 |
def tts_via_api(text: str):
|
| 119 |
try:
|
| 120 |
payload = {"text": text}
|
| 121 |
-
|
|
|
|
| 122 |
resp.raise_for_status()
|
| 123 |
data = resp.json()
|
| 124 |
audio_b64 = data.get("audio", "")
|
|
@@ -134,29 +134,26 @@ def tts_via_api(text: str):
|
|
| 134 |
return None
|
| 135 |
|
| 136 |
# =========================================================
|
| 137 |
-
# Gradio
|
| 138 |
# =========================================================
|
| 139 |
def chat(user_message, history):
|
| 140 |
if not user_message.strip():
|
| 141 |
return "", history
|
| 142 |
-
|
| 143 |
try:
|
| 144 |
-
#
|
|
|
|
|
|
|
|
|
|
| 145 |
answer_text = answer_question(user_message)
|
| 146 |
|
| 147 |
-
# Generate audio
|
| 148 |
audio_path = tts_via_api(answer_text)
|
| 149 |
|
| 150 |
-
# Append
|
| 151 |
-
history.append((
|
| 152 |
-
|
| 153 |
-
# Append user message
|
| 154 |
-
history.append((f"**You:** {user_message}", None))
|
| 155 |
-
|
| 156 |
except Exception as e:
|
| 157 |
print(e)
|
| 158 |
-
history.append(("⚠️ Error generating response", None))
|
| 159 |
-
|
| 160 |
return "", history
|
| 161 |
|
| 162 |
def reset_chat():
|
|
@@ -167,9 +164,11 @@ def reset_chat():
|
|
| 167 |
# =========================================================
|
| 168 |
def build_ui():
|
| 169 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 170 |
-
gr.Markdown("# 📄 Qwen Document Assistant + TTS\nAsk
|
| 171 |
|
|
|
|
| 172 |
chatbot = gr.Chatbot(height=450, type="tuples")
|
|
|
|
| 173 |
msg = gr.Textbox(placeholder="Ask a question...", lines=2)
|
| 174 |
send = gr.Button("Send")
|
| 175 |
clear = gr.Button("🧹 Clear")
|
|
|
|
| 21 |
# =========================================================
|
| 22 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
DOC_PATH = os.path.join(BASE_DIR, DOC_FILE)
|
|
|
|
| 24 |
if not os.path.exists(DOC_PATH):
|
| 25 |
raise RuntimeError(f"❌ {DOC_FILE} not found next to app.py")
|
| 26 |
|
|
|
|
| 61 |
DOC_EMBEDS = embedder.encode(DOC_CHUNKS, normalize_embeddings=True, show_progress_bar=True)
|
| 62 |
|
| 63 |
# =========================================================
|
| 64 |
+
# Retrieval
|
| 65 |
# =========================================================
|
| 66 |
def retrieve_context(question, k=TOP_K):
|
| 67 |
q_emb = embedder.encode([question], normalize_embeddings=True)
|
|
|
|
| 82 |
return lines[-1] if lines else text
|
| 83 |
|
| 84 |
# =========================================================
|
| 85 |
+
# Generate text
|
| 86 |
# =========================================================
|
| 87 |
def answer_question(question):
|
| 88 |
context = retrieve_context(question)
|
|
|
|
| 112 |
return extract_final_answer(decoded)
|
| 113 |
|
| 114 |
# =========================================================
|
| 115 |
+
# TTS API call
|
| 116 |
# =========================================================
|
| 117 |
def tts_via_api(text: str):
|
| 118 |
try:
|
| 119 |
payload = {"text": text}
|
| 120 |
+
# no timeout, allow long requests
|
| 121 |
+
resp = requests.post(TTS_API_URL, json=payload)
|
| 122 |
resp.raise_for_status()
|
| 123 |
data = resp.json()
|
| 124 |
audio_b64 = data.get("audio", "")
|
|
|
|
| 134 |
return None
|
| 135 |
|
| 136 |
# =========================================================
|
| 137 |
+
# Gradio chat
|
| 138 |
# =========================================================
|
| 139 |
def chat(user_message, history):
|
| 140 |
if not user_message.strip():
|
| 141 |
return "", history
|
|
|
|
| 142 |
try:
|
| 143 |
+
# 1️⃣ User text
|
| 144 |
+
history.append((user_message, None)) # None placeholder for bot column
|
| 145 |
+
|
| 146 |
+
# 2️⃣ Generate answer text
|
| 147 |
answer_text = answer_question(user_message)
|
| 148 |
|
| 149 |
+
# 3️⃣ Generate audio
|
| 150 |
audio_path = tts_via_api(answer_text)
|
| 151 |
|
| 152 |
+
# 4️⃣ Append bot response
|
| 153 |
+
history.append((None, (answer_text, audio_path))) # user=None, bot=(text, audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
except Exception as e:
|
| 155 |
print(e)
|
| 156 |
+
history.append((None, ("⚠️ Error generating response", None)))
|
|
|
|
| 157 |
return "", history
|
| 158 |
|
| 159 |
def reset_chat():
|
|
|
|
| 164 |
# =========================================================
|
| 165 |
def build_ui():
|
| 166 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 167 |
+
gr.Markdown("# 📄 Qwen Document Assistant + TTS\nAsk a question and listen to the answer.")
|
| 168 |
|
| 169 |
+
# Chat history: left=User, right=Bot
|
| 170 |
chatbot = gr.Chatbot(height=450, type="tuples")
|
| 171 |
+
|
| 172 |
msg = gr.Textbox(placeholder="Ask a question...", lines=2)
|
| 173 |
send = gr.Button("Send")
|
| 174 |
clear = gr.Button("🧹 Clear")
|