File size: 1,747 Bytes
7e4fadb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
load_dotenv()

import time
from transformers import pipeline, GenerationConfig, AutoTokenizer

# 1. Load your trained model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B")

generator = pipeline(
    "text-generation",
    model="./trained_model",
    tokenizer=tokenizer,
    clean_up_tokenization_spaces=False,
)

# 2. Configure generation settings
generation_config = GenerationConfig(
    max_new_tokens=50,
    do_sample=True,
)

# 3. Give it a starting prompt
prompt = "what is 1+10"

# 4. Generate the response with timing
input_ids = tokenizer.encode(prompt, return_tensors="pt")
tokens_in = input_ids.shape[1]

start_time = time.time()
results = generator(prompt, generation_config=generation_config)
elapsed = time.time() - start_time

# 5. Calculate stats
generated_text = results[0]["generated_text"]
output_ids = tokenizer.encode(generated_text)
tokens_out = len(output_ids)
new_tokens = tokens_out - tokens_in
tokens_per_sec = new_tokens / elapsed if elapsed > 0 else 0

# 6. Print the output and stats
print("\n" + "=" * 60)
print("  GENERATED TEXT")
print("=" * 60)
print(generated_text)
print("=" * 60)
print(f"  📊 Stats:")
print(f"     Prompt tokens (in):   {tokens_in}")
print(f"     Output tokens (out):  {tokens_out}")
print(f"     New tokens generated: {new_tokens}")
print(f"     Generation time:      {elapsed:.2f}s")
print(f"     Speed:                {tokens_per_sec:.1f} tokens/sec")
print(f"     Model:                ./trained_model")
print(f"     Tokenizer:            Qwen/Qwen3-8B")
print(f"     Sampling:             {'yes' if generation_config.do_sample else 'no'}")
print(f"     Max new tokens:       {generation_config.max_new_tokens}")
print("=" * 60)