Views
No views yet
google/gemma-4-E4B-it, fine-tuned specifically for Autonomous Multi-Hop Reasoning and Deep Web Research. It was developed as part of a Hackathon hosted by lablab.ai and sponsored by AMD.google/gemma-4-E4B-it (Multimodal)K=16, significantly accelerating reward convergence.AutoProcessor alongside AutoModelForCausalLM.1import torch
2from transformers import AutoProcessor, AutoModelForCausalLM
3
4model_id = "Phonsiri/Gemma-4-E4B-it-PARL"
5
6# Load the processor and the model
7processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
8model = AutoModelForCausalLM.from_pretrained(
9 model_id,
10 torch_dtype=torch.bfloat16,
11 device_map="auto",
12 trust_remote_code=True
13)
14
15# Example Chat Template with Native Thinking Enabled
16messages = [
17 {"role": "system", "content": "<|think|> You are a highly capable autonomous research agent."},
18 {"role": "user", "content": "Write a detailed report on the evolution of AMD's ROCm ecosystem."}
19]
20
21text = processor.apply_chat_template(
22 messages,
23 tokenize=False,
24 add_generation_prompt=True,
25 enable_thinking=True
26)
27
28inputs = processor(text=text, return_tensors="pt").to(model.device)
29
30# Generate response
31with torch.no_grad():
32 outputs = model.generate(**inputs, max_new_tokens=4096, temperature=0.7)
33
34input_len = inputs["input_ids"].shape[-1]
35response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
36
37print(response)1import torch
2import requests
3from PIL import Image
4from transformers import AutoProcessor, AutoModelForCausalLM, TextStreamer # นำเข้า TextStreamer
5
6model_id = "Phonsiri/Gemma-4-E4B-it-PARL"
7
8# 1. โหลด Processor และ Model
9processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
10model = AutoModelForCausalLM.from_pretrained(
11 model_id,
12 torch_dtype=torch.bfloat16,
13 device_map="auto",
14 trust_remote_code=True
15)
16
17# 2. โหลดรูปภาพ
18image_url = "https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg"
19image = Image.open(requests.get(image_url, stream=True).raw).convert("RGB")
20
21# 3. เตรียม Chat Template
22messages = [
23 {
24 "role": "system",
25 "content": [{"type": "text", "text": "<|think|> You are a highly capable autonomous research agent."}]
26 },
27 {
28 "role": "user",
29 "content": [
30 {"type": "image"},
31 {"type": "text", "text": "Analyze this image in detail"}
32 ]
33 }
34]
35
36text = processor.apply_chat_template(
37 messages,
38 tokenize=False,
39 add_generation_prompt=True,
40 enable_thinking=True
41)
42
43inputs = processor(
44 text=text,
45 images=image,
46 return_tensors="pt"
47).to(model.device)
48
49# 4. ตั้งค่า Streamer
50# skip_prompt=True เพื่อซ่อนข้อความส่วนที่เป็นคำถาม (Input) ไม่ให้พิมพ์ซ้ำออกมา
51# skip_special_tokens=False เพื่อให้เห็น Tag พิเศษต่างๆ (เช่น <|think|>) ระหว่างสตรีม
52streamer = TextStreamer(processor, skip_prompt=True, skip_special_tokens=False)
53
54print("\n--- กำลังสร้างคำตอบ (Streaming) ---\n")
55
56# 5. Generate response (ส่ง streamer เข้าไปในฟังก์ชัน)
57with torch.no_grad():
58 outputs = model.generate(
59 **inputs,
60 max_new_tokens=4096,
61 temperature=0.7,
62 streamer=streamer # เพิ่มบรรทัดนี้
63 )
64
65# หมายเหตุ: TextStreamer จะทำการ print ข้อความออกทางหน้าจอให้โดยอัตโนมัติ
66# ดังนั้นคุณไม่จำเป็นต้องใช้ processor.decode() แล้ว print ออกมาอีกรอบในตอนท้าย