-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_tutor_example.py
More file actions
100 lines (84 loc) · 2.63 KB
/
simple_tutor_example.py
File metadata and controls
100 lines (84 loc) · 2.63 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
"""
SIMPLE MATH TUTOR BOT EXAMPLE
==============================
Quick start guide - just run this file!
"""
from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor
import torch
print("Starting Math Tutor Bot...")
print("Loading model (this may take a few minutes on first run)...\n")
# Load model
model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
"Qwen/Qwen2.5-Omni-3B",
torch_dtype="auto",
device_map="auto"
)
processor = Qwen2_5OmniProcessor.from_pretrained("Qwen/Qwen2.5-Omni-3B")
print("✓ Model loaded!\n")
print("="*60)
# Define a simple function to ask questions
def ask_tutor(question):
"""Ask the tutor a question and get a text response"""
conversation = [
{
"role": "system",
"content": "You are a friendly math tutor. Explain concepts clearly with step-by-step examples."
},
{
"role": "user",
"content": question
}
]
# Prepare 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]
return response
# Try some example questions
print("MATH TUTOR BOT - Interactive Examples")
print("="*60)
# Question 1
print("\n📝 Student Question:")
q1 = "How do I solve the equation 2x + 5 = 13?"
print(f" {q1}")
print("\n🤖 Tutor Answer:")
answer1 = ask_tutor(q1)
print(f" {answer1}")
# Question 2
print("\n" + "="*60)
print("\n📝 Student Question:")
q2 = "What is the formula for the area of a circle?"
print(f" {q2}")
print("\n🤖 Tutor Answer:")
answer2 = ask_tutor(q2)
print(f" {answer2}")
# Question 3
print("\n" + "="*60)
print("\n📝 Student Question:")
q3 = "Explain what a fraction is to a 5th grader"
print(f" {q3}")
print("\n🤖 Tutor Answer:")
answer3 = ask_tutor(q3)
print(f" {answer3}")
print("\n" + "="*60)
print("\n✓ Examples complete!")
print("\nTo use in your own code:")
print(" 1. Load the model (do once at startup)")
print(" 2. Call ask_tutor(your_question)")
print(" 3. Display the response to the student")
print("\nNext steps:")
print(" • Add speech input/output (see qwen_usage_guide.py)")
print(" • Add image understanding for diagrams")
print(" • Build a web/desktop interface")
print(" • Create interactive lessons")