Views
No views yet
1import torch
2from peft import PeftModel
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5# Load the base model and tokenizer
6base_model_id = "google/gemma-4-E2B-it"
7peft_model_id = "YOUR_HUGGINGFACE_USERNAME/gemma_4_e2b_it_absa_finetuned" # Replace with your HF ID
8
9tokenizer = AutoTokenizer.from_pretrained(base_model_id)
10base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.bfloat16, device_map="auto")
11
12# Load the fine-tuned LoRA adapter
13model = PeftModel.from_pretrained(base_model, peft_model_id)
14
15system_prompt = """You are an expert Aspect-Based Sentiment Analysis (ABSA) assistant for Turkish e-commerce.
16Analyze reviews and extract aspects with sentiments (positive, negative, neutral).
17Format: aspect|sentiment; aspect|sentiment
18Provide ONLY the tags."""
19
20review = "İndirimden 50 liraya kaptım fiyatına göre iyi ama kargo tam 2 haftada geldi."
21
22messages = [
23 {"role": "user", "content": f"{system_prompt}\n\nReview: {review}"}
24]
25
26inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to("cuda")
27outputs = model.generate(inputs, max_new_tokens=50)
28
29print(tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True))
30# Expected Output: price_value|positive; product_quality|neutral; shipping|negative