Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Define the repository ID
5REPO_ID = "iko-01/iko-v5e-1"
6
7# Load the model and tokenizer
8print("Loading model and tokenizer from", REPO_ID)
9tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
10model = AutoModelForCausalLM.from_pretrained(REPO_ID)
11
12# Determine device for generation
13device = "cpu"
14if torch.cuda.is_available():
15 device = "cuda"
16 print("Using CUDA device for generation")
17model.to(device)
18
19def generate_from_user(user_text, max_new_tokens=250, do_sample=False):
20 prompt = f"### User:\n{user_text.strip()}\n\n### Assistant:\n<think>"
21 inputs = tokenizer(prompt, return_tensors="pt")
22 input_ids = inputs.input_ids.to(device)
23 attention_mask = inputs.attention_mask.to(device)
24
25 gen = model.generate(
26 input_ids,
27 attention_mask=attention_mask,
28 max_new_tokens=max_new_tokens,
29 do_sample=do_sample,
30 top_p=0.95,
31 temperature=0.8,
32 pad_token_id=tokenizer.pad_token_id,
33 eos_token_id=tokenizer.eos_token_id,
34 num_return_sequences=1,
35 repetition_penalty=1.1
36 )
37 out = tokenizer.decode(gen[0], skip_special_tokens=False)
38
39 # Isolate the generated assistant part
40 assistant_part = out.split("### Assistant:")[1].strip() if "### Assistant:" in out else out
41
42 # Clean up <think> tags if they are still open
43 if "<think>" in assistant_part and "</think>" not in assistant_part:
44 assistant_part = assistant_part.replace("<think>", "", 1)
45
46 return assistant_part
47
48# Define three English test questions
49test_questions = [
50 "",
51 "How can I calculate two numbers in Python code?",
52 "What do you think about the death penalty in Egypt?"
53]
54
55# Generate and print responses
56print("\n--- Testing Model with Questions ---")
57for question in test_questions:
58 print("\nUSER:", question)
59 response = generate_from_user(question, max_new_tokens=250, do_sample=False)
60 print("MODEL OUTPUT:\n", response)
61 print("-" * 60)
62
63print("\nTesting complete.")