Views
No views yet
xsanskarx/thinkygemma-4bxsanskarx/thinkgemma-4bgoogle/gemma-3-4b-it1from transformers import AutoTokenizer, Gemma3ForConditionalGeneration, TextStreamer
2import torch
3
4# Load model and tokenizer
5model_id = "xsanskarx/thinkygemma-4b"
6model = Gemma3ForConditionalGeneration.from_pretrained(model_id, device_map="auto").eval()
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8
9def ask_model(prompt: str, max_tokens=8192, temperature=0.7):
10 """
11 Function to ask a question to the model and stream the response.
12 """
13 messages = [
14 {"role": "system", "content": "You are an expert math problem solver, think and reason inside <think> tags, enclose all reasoning in <think> tags, verifying logic step by step and then return your final structured answer"},
15 {"role": "user", "content": prompt}
16 ]
17
18 formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False)
19 inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
20
21 streamer = TextStreamer(tokenizer, skip_special_tokens=True)
22 with torch.inference_mode():
23 model.generate(**inputs, max_new_tokens=max_tokens, do_sample=True, temperature=temperature, streamer=streamer)
24
25# Example usage
26ask_model("do 2+2")