Views
No views yet
meta-llama/Llama-3.1-8B-Instruct model fine-tuned for the Turkish sentiment analysis task using the winvoker/turkish-sentiment-analysis-dataset dataset and the QLoRA (4-bit) method.ceofast/llama3.1-8b-instruct-turkish-sentiment-qloraLABEL_0 (negative), LABEL_1 (neutral), LABEL_2 (positive)transformers, peft, accelerate, bitsandbytes, and torch libraries installed.1import torch
2from peft import PeftModel
3from transformers import AutoTokenizer, AutoModelForSequenceClassification, BitsAndBytesConfig
4
5# Base model ID
6base_model_id = "meta-llama/Llama-3.1-8B-Instruct"
7# QLoRA adapter ID (this repository)
8adapter_id = "ceofast/llama3.1-8b-instruct-turkish-sentiment-qlora"
9# Labels
10labels = ["negative", "neutral", "positive"]
11
12# 4-bit quantization configuration
13bnb_config = BitsAndBytesConfig(
14 load_in_4bit=True,
15 bnb_4bit_quant_type="nf4",
16 bnb_4bit_compute_dtype=torch.bfloat16 # Or float16 depending on your GPU
17)
18
19# Load the base model in 4-bit
20base_model = AutoModelForSequenceClassification.from_pretrained(
21 base_model_id,
22 num_labels=len(labels),
23 quantization_config=bnb_config,
24 device_map="auto", # Load model to appropriate device (GPU/CPU)
25 trust_remote_code=True, # If required by the base model
26 # Add your HF Token here or ensure you are logged in
27 # token="YOUR_HF_TOKEN"
28 # Suppress classification head mismatch warning
29 ignore_mismatched_sizes=True
30)
31
32# Load the tokenizer
33tokenizer = AutoTokenizer.from_pretrained(base_model_id)
34# Set PAD token for Llama
35if tokenizer.pad_token is None:
36 tokenizer.pad_token = tokenizer.eos_token
37base_model.config.pad_token_id = tokenizer.pad_token_id
38
39# Load the PEFT adapter and merge it with the base model
40# Note: For inference, merging is often not necessary, you can directly use the PeftModel
41model = PeftModel.from_pretrained(base_model, adapter_id)
42model.eval() # Set the model to evaluation mode
43
44# Inference function
45def predict_sentiment(text):
46 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
47 # Move inputs to the same device as the model
48 inputs = {k: v.to(model.device) for k, v in inputs.items()}
49
50 with torch.no_grad():
51 outputs = model(**inputs)
52 logits = outputs.logits
53 predictions = torch.argmax(logits, dim=-1)
54 return labels[predictions.item()]
55
56# Example usage
57text1 = "Bu film tek kelimeyle muhteşemdi!" # Keeping original Turkish examples
58text2 = "Kargo çok geç geldi ve ürün hasarlıydı."
59text3 = "Hava bugün güneşli."
60text4 = "This restaurant is fantastic!" # Added an English example
61
62print(f"'{text1}' -> Sentiment: {predict_sentiment(text1)}")
63print(f"'{text2}' -> Sentiment: {predict_sentiment(text2)}")
64print(f"'{text3}' -> Sentiment: {predict_sentiment(text3)}")
65print(f"'{text4}' -> Sentiment: {predict_sentiment(text4)}") # Note: Model is trained on Turkishlanguage: tr to language: en in the YAML front matter.widget and the "How to Use" section, while keeping the Turkish ones.checkpoint-3000 which was likely the last successfully saved one.)