rahul7star commited on
Commit
1ae007f
·
verified ·
1 Parent(s): 11f9924

Update app_qwen_tts.py

Browse files
Files changed (1) hide show
  1. app_qwen_tts.py +20 -14
app_qwen_tts.py CHANGED
@@ -6,6 +6,7 @@ import base64
6
  import requests
7
  from transformers import AutoTokenizer, AutoModelForCausalLM
8
  from sentence_transformers import SentenceTransformer
 
9
 
10
  # =========================================================
11
  # Configuration
@@ -117,7 +118,6 @@ def answer_question(question):
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()
@@ -134,26 +134,31 @@ def tts_via_api(text: str):
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,7 +169,8 @@ 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")
@@ -173,8 +179,8 @@ def build_ui():
173
  send = gr.Button("Send")
174
  clear = gr.Button("🧹 Clear")
175
 
176
- send.click(chat, [msg, chatbot], [msg, chatbot])
177
- msg.submit(chat, [msg, chatbot], [msg, chatbot])
178
  clear.click(reset_chat, outputs=chatbot)
179
 
180
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
6
  import requests
7
  from transformers import AutoTokenizer, AutoModelForCausalLM
8
  from sentence_transformers import SentenceTransformer
9
+ import asyncio
10
 
11
  # =========================================================
12
  # Configuration
 
118
  def tts_via_api(text: str):
119
  try:
120
  payload = {"text": text}
 
121
  resp = requests.post(TTS_API_URL, json=payload)
122
  resp.raise_for_status()
123
  data = resp.json()
 
134
  return None
135
 
136
  # =========================================================
137
+ # Async chat
138
  # =========================================================
139
+ async def chat_async(user_message, history):
140
  if not user_message.strip():
141
  return "", history
 
 
 
142
 
143
+ # Show user message immediately
144
+ history.append((user_message, None))
145
+
146
+ try:
147
+ # 1️⃣ Generate text answer immediately
148
  answer_text = answer_question(user_message)
149
 
150
+ # Append answer text only (audio pending)
151
+ history.append((None, (answer_text, None)))
152
 
153
+ # 2️⃣ Generate audio in background
154
+ audio_path = await asyncio.to_thread(tts_via_api, answer_text)
155
+
156
+ # Update the last bot message with audio
157
+ history[-1] = (None, (answer_text, audio_path))
158
  except Exception as e:
159
  print(e)
160
  history.append((None, ("⚠️ Error generating response", None)))
161
+
162
  return "", history
163
 
164
  def reset_chat():
 
169
  # =========================================================
170
  def build_ui():
171
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
172
+ gr.Markdown("# 📄 Qwen Document Assistant + TTS (Async Audio)")
173
+ gr.Markdown("Ask a question and hear the answer. Text appears immediately, audio may take a few minutes.")
174
 
175
  # Chat history: left=User, right=Bot
176
  chatbot = gr.Chatbot(height=450, type="tuples")
 
179
  send = gr.Button("Send")
180
  clear = gr.Button("🧹 Clear")
181
 
182
+ send.click(chat_async, [msg, chatbot], [msg, chatbot])
183
+ msg.submit(chat_async, [msg, chatbot], [msg, chatbot])
184
  clear.click(reset_chat, outputs=chatbot)
185
 
186
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)