-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqwen_usage_guide.py
More file actions
384 lines (312 loc) · 11.5 KB
/
qwen_usage_guide.py
File metadata and controls
384 lines (312 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
COMPREHENSIVE GUIDE: How to Use Qwen2.5-Omni for Math Tutor Bot
================================================================
This guide shows all 4 interaction modes:
1. Text-to-Text
2. Text-to-Speech
3. Speech-to-Text
4. Speech-to-Speech
5. Image-to-Text (bonus: for math diagrams)
"""
from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor
from qwen_omni_utils import process_mm_info
import torch
# ============================================================================
# SETUP: Load Model Once
# ============================================================================
def load_model():
"""Load Qwen2.5-Omni model (do this once at startup)"""
print("Loading Qwen2.5-Omni-3B model...")
model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
"Qwen/Qwen2.5-Omni-3B",
torch_dtype="auto", # Automatically choose best dtype
device_map="auto" # Automatically use GPU if available
)
processor = Qwen2_5OmniProcessor.from_pretrained("Qwen/Qwen2.5-Omni-3B")
print("Model loaded successfully!")
return model, processor
# ============================================================================
# MODE 1: TEXT-TO-TEXT (Student types, bot responds with text)
# ============================================================================
def text_to_text(model, processor, question):
"""
Student types a question, bot responds with text
Perfect for: Written explanations, step-by-step solutions
"""
print("\n" + "="*60)
print("MODE 1: TEXT-TO-TEXT")
print("="*60)
# Create conversation
conversation = [
{
"role": "system",
"content": "You are a helpful math tutor. Explain concepts clearly and step-by-step."
},
{
"role": "user",
"content": question
}
]
# Process input
text_input = processor.apply_chat_template(
conversation,
add_generation_prompt=True,
tokenize=False
)
inputs = processor(text=text_input, return_tensors="pt").to(model.device)
# Generate response
with torch.no_grad():
generated_ids = model.generate(**inputs, max_new_tokens=512)
# Decode response
response = processor.batch_decode(
generated_ids[:, inputs['input_ids'].shape[1]:],
skip_special_tokens=True
)[0]
print(f"\nStudent: {question}")
print(f"\nTutor: {response}")
return response
# ============================================================================
# MODE 2: TEXT-TO-SPEECH (Student types, bot responds with voice)
# ============================================================================
def text_to_speech(model, processor, question, output_audio_file="response.wav"):
"""
Student types a question, bot responds with voice
Perfect for: Audio explanations, pronunciation, engaging lessons
"""
print("\n" + "="*60)
print("MODE 2: TEXT-TO-SPEECH")
print("="*60)
conversation = [
{
"role": "system",
"content": "You are a helpful math tutor."
},
{
"role": "user",
"content": question
}
]
# Add audio generation prompt
text_input = processor.apply_chat_template(
conversation,
add_generation_prompt=True,
add_audio_generation_prompt=True, # Request audio output!
tokenize=False
)
inputs = processor(text=text_input, return_tensors="pt").to(model.device)
# Generate text and audio
with torch.no_grad():
text_ids, audio_output = model.generate(
**inputs,
max_new_tokens=512,
return_audio=True # Request audio return
)
# Get text response
text_response = processor.batch_decode(
text_ids[:, inputs['input_ids'].shape[1]:],
skip_special_tokens=True
)[0]
print(f"\nStudent: {question}")
print(f"\nTutor (text): {text_response}")
# Save audio
if audio_output is not None:
import soundfile as sf
sf.write(output_audio_file, audio_output[0].cpu().numpy(), 24000)
print(f"Tutor (audio): Saved to {output_audio_file}")
return text_response, audio_output
# ============================================================================
# MODE 3: SPEECH-TO-TEXT (Student speaks, bot responds with text)
# ============================================================================
def speech_to_text(model, processor, audio_file_path):
"""
Student speaks a question (from audio file), bot responds with text
Perfect for: Voice questions, accessibility
"""
print("\n" + "="*60)
print("MODE 3: SPEECH-TO-TEXT")
print("="*60)
# Process audio input
conversation = [
{
"role": "system",
"content": "You are a helpful math tutor."
},
{
"role": "user",
"content": [
{"type": "audio", "audio": audio_file_path}
]
}
]
# Process multimodal input
mm_data = process_mm_info(conversation, model.config)
text_input = processor.apply_chat_template(
mm_data["conversation"],
add_generation_prompt=True,
tokenize=False
)
inputs = processor(
text=text_input,
audios=mm_data.get("audios"),
return_tensors="pt"
).to(model.device)
# Generate text response
with torch.no_grad():
generated_ids = model.generate(**inputs, max_new_tokens=512)
response = processor.batch_decode(
generated_ids[:, inputs['input_ids'].shape[1]:],
skip_special_tokens=True
)[0]
print(f"\nStudent: [Audio file: {audio_file_path}]")
print(f"\nTutor: {response}")
return response
# ============================================================================
# MODE 4: SPEECH-TO-SPEECH (Student speaks, bot responds with voice)
# ============================================================================
def speech_to_speech(model, processor, audio_file_path, output_audio_file="response.wav"):
"""
Student speaks a question, bot responds with voice
Perfect for: Full voice interaction, natural conversation
"""
print("\n" + "="*60)
print("MODE 4: SPEECH-TO-SPEECH")
print("="*60)
conversation = [
{
"role": "system",
"content": "You are a helpful math tutor."
},
{
"role": "user",
"content": [
{"type": "audio", "audio": audio_file_path}
]
}
]
# Process multimodal input
mm_data = process_mm_info(conversation, model.config)
text_input = processor.apply_chat_template(
mm_data["conversation"],
add_generation_prompt=True,
add_audio_generation_prompt=True, # Request audio output!
tokenize=False
)
inputs = processor(
text=text_input,
audios=mm_data.get("audios"),
return_tensors="pt"
).to(model.device)
# Generate text and audio response
with torch.no_grad():
text_ids, audio_output = model.generate(
**inputs,
max_new_tokens=512,
return_audio=True
)
text_response = processor.batch_decode(
text_ids[:, inputs['input_ids'].shape[1]:],
skip_special_tokens=True
)[0]
print(f"\nStudent: [Audio file: {audio_file_path}]")
print(f"\nTutor (text): {text_response}")
# Save audio
if audio_output is not None:
import soundfile as sf
sf.write(output_audio_file, audio_output[0].cpu().numpy(), 24000)
print(f"Tutor (audio): Saved to {output_audio_file}")
return text_response, audio_output
# ============================================================================
# BONUS MODE 5: IMAGE-TO-TEXT (Student shows math diagram, bot explains)
# ============================================================================
def image_to_text(model, processor, image_path, question="What is this?"):
"""
Student uploads an image (e.g., math diagram), bot explains
Perfect for: Understanding diagrams, graphs, geometric shapes
"""
print("\n" + "="*60)
print("BONUS MODE 5: IMAGE-TO-TEXT (Math Diagrams)")
print("="*60)
conversation = [
{
"role": "system",
"content": "You are a helpful math tutor. Analyze images and explain clearly."
},
{
"role": "user",
"content": [
{"type": "image", "image": image_path},
{"type": "text", "text": question}
]
}
]
# Process multimodal input
mm_data = process_mm_info(conversation, model.config)
text_input = processor.apply_chat_template(
mm_data["conversation"],
add_generation_prompt=True,
tokenize=False
)
inputs = processor(
text=text_input,
images=mm_data.get("images"),
return_tensors="pt"
).to(model.device)
# Generate response
with torch.no_grad():
generated_ids = model.generate(**inputs, max_new_tokens=512)
response = processor.batch_decode(
generated_ids[:, inputs['input_ids'].shape[1]:],
skip_special_tokens=True
)[0]
print(f"\nStudent: [Image: {image_path}] {question}")
print(f"\nTutor: {response}")
return response
# ============================================================================
# MAIN EXAMPLE: How to Use in Your Tutor Bot
# ============================================================================
if __name__ == "__main__":
print("="*60)
print("QWEN2.5-OMNI MATH TUTOR BOT - USAGE EXAMPLES")
print("="*60)
# Load model once at startup
model, processor = load_model()
# Example 1: Text-to-Text
print("\n\n### EXAMPLE 1: Student types a question ###")
text_to_text(
model, processor,
"Explain how to solve 3x + 7 = 22 step by step"
)
# Example 2: Text-to-Speech
print("\n\n### EXAMPLE 2: Student wants audio explanation ###")
text_to_speech(
model, processor,
"What is the Pythagorean theorem?",
output_audio_file="pythagorean_explanation.wav"
)
# Example 3: Speech-to-Text (requires audio file)
# Uncomment if you have an audio file:
# print("\n\n### EXAMPLE 3: Student asks via voice ###")
# speech_to_text(model, processor, "student_question.wav")
# Example 4: Speech-to-Speech (requires audio file)
# Uncomment if you have an audio file:
# print("\n\n### EXAMPLE 4: Full voice conversation ###")
# speech_to_speech(model, processor, "student_question.wav", "tutor_answer.wav")
# Example 5: Image-to-Text (requires image file)
# Uncomment if you have a math diagram image:
# print("\n\n### EXAMPLE 5: Student uploads math diagram ###")
# image_to_text(model, processor, "triangle_diagram.png", "Explain this geometric shape")
print("\n\n" + "="*60)
print("GUIDE COMPLETE!")
print("="*60)
print("\nKey Points:")
print(" • Load model ONCE at startup")
print(" • Use different functions for different interaction modes")
print(" • Text responses are always generated")
print(" • Audio output requires add_audio_generation_prompt=True")
print(" • Audio files use 24000 Hz sample rate")
print(" • Images can be PNG, JPG, or other common formats")
print("\nFor your math tutor bot:")
print(" 1. Load model on app startup")
print(" 2. Choose interaction mode based on student preference")
print(" 3. Generate responses with appropriate function")
print(" 4. Display text and/or play audio to student")