rahul7star commited on
Commit
b7ef6fe
·
verified ·
1 Parent(s): b75f9b3

Update app_qwen_tts.py

Browse files
Files changed (1) hide show
  1. app_qwen_tts.py +20 -21
app_qwen_tts.py CHANGED
@@ -5,13 +5,12 @@ import numpy as np
5
  import requests
6
  import base64
7
  import io
 
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
  from sentence_transformers import SentenceTransformer
10
- import soundfile as sf
11
 
12
  # =========================================================
13
  # Configuration
14
- # =========================================================
15
  MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
16
  DOC_FILE = "general.md"
17
  MAX_NEW_TOKENS = 200
@@ -20,7 +19,6 @@ TTS_API_URL = "https://rahul7star-Chatterbox-Multilingual-TTS-API.hf.space/tts"
20
 
21
  # =========================================================
22
  # Paths
23
- # =========================================================
24
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
25
  DOC_PATH = os.path.join(BASE_DIR, DOC_FILE)
26
  if not os.path.exists(DOC_PATH):
@@ -28,7 +26,6 @@ if not os.path.exists(DOC_PATH):
28
 
29
  # =========================================================
30
  # Load Qwen Model
31
- # =========================================================
32
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
33
  model = AutoModelForCausalLM.from_pretrained(
34
  MODEL_ID,
@@ -40,12 +37,10 @@ model.eval()
40
 
41
  # =========================================================
42
  # Embeddings
43
- # =========================================================
44
  embedder = SentenceTransformer("all-MiniLM-L6-v2")
45
 
46
  # =========================================================
47
  # Document chunking
48
- # =========================================================
49
  def chunk_text(text, chunk_size=300, overlap=50):
50
  words = text.split()
51
  chunks = []
@@ -63,7 +58,6 @@ DOC_EMBEDS = embedder.encode(DOC_CHUNKS, normalize_embeddings=True, show_progres
63
 
64
  # =========================================================
65
  # Retrieve context
66
- # =========================================================
67
  def retrieve_context(question, k=TOP_K):
68
  q_emb = embedder.encode([question], normalize_embeddings=True)
69
  scores = np.dot(DOC_EMBEDS, q_emb[0])
@@ -72,7 +66,6 @@ def retrieve_context(question, k=TOP_K):
72
 
73
  # =========================================================
74
  # Extract answer
75
- # =========================================================
76
  def extract_final_answer(text: str) -> str:
77
  text = text.strip()
78
  markers = ["assistant:", "answer:", "final answer:"]
@@ -84,7 +77,6 @@ def extract_final_answer(text: str) -> str:
84
 
85
  # =========================================================
86
  # Qwen inference
87
- # =========================================================
88
  def answer_question(question):
89
  context = retrieve_context(question)
90
  messages = [
@@ -104,7 +96,6 @@ def answer_question(question):
104
 
105
  # =========================================================
106
  # TTS via API
107
- # =========================================================
108
  def tts_via_api(text: str):
109
  try:
110
  resp = requests.post(TTS_API_URL, json={"text": text}, timeout=60)
@@ -120,8 +111,7 @@ def tts_via_api(text: str):
120
  return None
121
 
122
  # =========================================================
123
- # Chat function
124
- # =========================================================
125
  def chat(user_message, history):
126
  if not user_message.strip():
127
  return "", history
@@ -137,12 +127,12 @@ def chat(user_message, history):
137
  else:
138
  audio_output = None
139
 
140
- # 3️⃣ Append nicely formatted response
141
- history.append((user_message, [f"**Bot:** {answer_text}", audio_output]))
142
 
143
  except Exception as e:
144
  print(e)
145
- history.append((user_message, ["⚠️ Error generating answer or audio.", None]))
146
  return "", history
147
 
148
  def reset_chat():
@@ -150,24 +140,33 @@ def reset_chat():
150
 
151
  # =========================================================
152
  # Gradio UI
153
- # =========================================================
154
  def build_ui():
155
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
156
  gr.Markdown("# 📄 Qwen Document Assistant + TTS\nAsk a question and get a text + playable audio response.")
157
 
158
- chatbot = gr.Chatbot(height=450, type="tuples")
 
159
  msg = gr.Textbox(placeholder="Ask a question...", lines=2)
160
  send = gr.Button("Send")
161
  clear = gr.Button("🧹 Clear")
162
 
163
- send.click(chat, [msg, chatbot], [msg, chatbot])
164
- msg.submit(chat, [msg, chatbot], [msg, chatbot])
 
 
 
 
 
 
 
 
 
 
 
165
  clear.click(reset_chat, outputs=chatbot)
166
 
167
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
168
 
169
- # =========================================================
170
- # Entrypoint
171
  # =========================================================
172
  if __name__ == "__main__":
173
  print(f"✅ Loaded {len(DOC_CHUNKS)} chunks from {DOC_FILE}")
 
5
  import requests
6
  import base64
7
  import io
8
+ import soundfile as sf
9
  from transformers import AutoTokenizer, AutoModelForCausalLM
10
  from sentence_transformers import SentenceTransformer
 
11
 
12
  # =========================================================
13
  # Configuration
 
14
  MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
15
  DOC_FILE = "general.md"
16
  MAX_NEW_TOKENS = 200
 
19
 
20
  # =========================================================
21
  # Paths
 
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):
 
26
 
27
  # =========================================================
28
  # Load Qwen Model
 
29
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
30
  model = AutoModelForCausalLM.from_pretrained(
31
  MODEL_ID,
 
37
 
38
  # =========================================================
39
  # Embeddings
 
40
  embedder = SentenceTransformer("all-MiniLM-L6-v2")
41
 
42
  # =========================================================
43
  # Document chunking
 
44
  def chunk_text(text, chunk_size=300, overlap=50):
45
  words = text.split()
46
  chunks = []
 
58
 
59
  # =========================================================
60
  # Retrieve context
 
61
  def retrieve_context(question, k=TOP_K):
62
  q_emb = embedder.encode([question], normalize_embeddings=True)
63
  scores = np.dot(DOC_EMBEDS, q_emb[0])
 
66
 
67
  # =========================================================
68
  # Extract answer
 
69
  def extract_final_answer(text: str) -> str:
70
  text = text.strip()
71
  markers = ["assistant:", "answer:", "final answer:"]
 
77
 
78
  # =========================================================
79
  # Qwen inference
 
80
  def answer_question(question):
81
  context = retrieve_context(question)
82
  messages = [
 
96
 
97
  # =========================================================
98
  # TTS via API
 
99
  def tts_via_api(text: str):
100
  try:
101
  resp = requests.post(TTS_API_URL, json={"text": text}, timeout=60)
 
111
  return None
112
 
113
  # =========================================================
114
+ # Chat function (text + audio separate boxes)
 
115
  def chat(user_message, history):
116
  if not user_message.strip():
117
  return "", history
 
127
  else:
128
  audio_output = None
129
 
130
+ # 3️⃣ Append as separate text + audio
131
+ history.append((user_message, answer_text, audio_output))
132
 
133
  except Exception as e:
134
  print(e)
135
+ history.append((user_message, "⚠️ Error generating answer or audio.", None))
136
  return "", history
137
 
138
  def reset_chat():
 
140
 
141
  # =========================================================
142
  # Gradio UI
 
143
  def build_ui():
144
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
145
  gr.Markdown("# 📄 Qwen Document Assistant + TTS\nAsk a question and get a text + playable audio response.")
146
 
147
+ chatbot = gr.Chatbot(height=500, type="messages") # 'messages' so we can use custom formatting
148
+
149
  msg = gr.Textbox(placeholder="Ask a question...", lines=2)
150
  send = gr.Button("Send")
151
  clear = gr.Button("🧹 Clear")
152
 
153
+ def format_history(history):
154
+ formatted = []
155
+ for user_msg, bot_text, bot_audio in history:
156
+ formatted.append([f"**You:** {user_msg}", None])
157
+ formatted.append([f"**Bot:** {bot_text}", bot_audio])
158
+ return formatted
159
+
160
+ def chat_with_format(msg_input, history):
161
+ _, history = chat(msg_input, history)
162
+ return "", format_history(history)
163
+
164
+ send.click(chat_with_format, [msg, chatbot], [msg, chatbot])
165
+ msg.submit(chat_with_format, [msg, chatbot], [msg, chatbot])
166
  clear.click(reset_chat, outputs=chatbot)
167
 
168
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
169
 
 
 
170
  # =========================================================
171
  if __name__ == "__main__":
172
  print(f"✅ Loaded {len(DOC_CHUNKS)} chunks from {DOC_FILE}")