Views
No views yet
google/gemma-3-270m-it, designed to perform various text classification tasks in Dhivehi including sentiment analysis, topic classification, intent recognition, and opinion mining.google/gemma-3-270m-it1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2import json
3
4model_path = "alakxender/gemma-3-270m-dhivehi-text-classifier"
5
6model = AutoModelForCausalLM.from_pretrained(
7 model_path,
8 torch_dtype="auto",
9 device_map="auto",
10 attn_implementation="eager"
11)
12tokenizer = AutoTokenizer.from_pretrained(model_path)
13pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)1def analyze_sentiment(dhivehi_text: str, model, tokenizer, pipe, max_new_tokens: int = 128, temperature: float = 0.1, top_p: float = 0.2, top_k: int = 5, do_sample: bool = False):
2 """
3 Analyze sentiment of Dhivehi text
4
5 Args:
6 dhivehi_text: Input Dhivehi text
7 model: Loaded model
8 tokenizer: Loaded tokenizer
9 pipe: Loaded pipeline
10 max_new_tokens: Maximum tokens to generate
11 temperature: Sampling temperature
12 top_p: Top-p sampling parameter
13 top_k: Top-k sampling parameter
14 do_sample: Whether to use sampling
15
16 Returns:
17 Dictionary with sentiment analysis result
18 """
19 instruction = f"Analyze the sentiment of this Dhivehi text: {dhivehi_text}"
20 return _generate_classification_response(instruction, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
21
22def classify_topic(dhivehi_text: str, model, tokenizer, pipe, max_new_tokens: int = 128, temperature: float = 0.1, top_p: float = 0.2, top_k: int = 5, do_sample: bool = False):
23 """
24 Classify topic of Dhivehi text
25
26 Args:
27 dhivehi_text: Input Dhivehi text
28 model: Loaded model
29 tokenizer: Loaded tokenizer
30 pipe: Loaded pipeline
31 max_new_tokens: Maximum tokens to generate
32 temperature: Sampling temperature
33 top_p: Top-p sampling parameter
34 top_k: Top-k sampling parameter
35 do_sample: Whether to use sampling
36
37 Returns:
38 Dictionary with topic classification result
39 """
40 instruction = f"Determine the topic of this Dhivehi text: {dhivehi_text}"
41 return _generate_classification_response(instruction, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
42
43def identify_intent(dhivehi_text: str, model, tokenizer, pipe, max_new_tokens: int = 128, temperature: float = 0.1, top_p: float = 0.2, top_k: int = 5, do_sample: bool = False):
44 """
45 Identify intent in Dhivehi text
46
47 Args:
48 dhivehi_text: Input Dhivehi text
49 model: Loaded model
50 tokenizer: Loaded tokenizer
51 pipe: Loaded pipeline
52 max_new_tokens: Maximum tokens to generate
53 temperature: Sampling temperature
54 top_p: Top-p sampling parameter
55 top_k: Top-k sampling parameter
56 do_sample: Whether to use sampling
57
58 Returns:
59 Dictionary with intent identification result
60 """
61 instruction = f"Identify the intent behind this Dhivehi text: {dhivehi_text}"
62 return _generate_classification_response(instruction, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
63
64def classify_opinion(dhivehi_text: str, model, tokenizer, pipe, max_new_tokens: int = 128, temperature: float = 0.1, top_p: float = 0.2, top_k: int = 5, do_sample: bool = False):
65 """
66 Classify opinion in Dhivehi text
67
68 Args:
69 dhivehi_text: Input Dhivehi text
70 model: Loaded model
71 tokenizer: Loaded tokenizer
72 pipe: Loaded pipeline
73 max_new_tokens: Maximum tokens to generate
74 temperature: Sampling temperature
75 top_p: Top-p sampling parameter
76 top_k: Top-k sampling parameter
77 do_sample: Whether to use sampling
78
79 Returns:
80 Dictionary with opinion classification result
81 """
82 instruction = f"Classify the opinion expressed in this Dhivehi text: {dhivehi_text}"
83 return _generate_classification_response(instruction, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
84
85def analyze_all(dhivehi_text: str, model, tokenizer, pipe, max_new_tokens: int = 128, temperature: float = 0.1, top_p: float = 0.2, top_k: int = 5, do_sample: bool = False):
86 """
87 Run all analysis tasks on the input text
88
89 Args:
90 dhivehi_text: Input Dhivehi text
91 model: Loaded model
92 tokenizer: Loaded tokenizer
93 pipe: Loaded pipeline
94 max_new_tokens: Maximum tokens to generate
95 temperature: Sampling temperature
96 top_p: Top-p sampling parameter
97 top_k: Top-k sampling parameter
98 do_sample: Whether to use sampling
99
100 Returns:
101 Dictionary with all analysis results
102 """
103 if pipe is None or model is None or tokenizer is None:
104 return {"error": "Please load a classification model first!"}
105
106 try:
107 opinion_result = classify_opinion(dhivehi_text, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
108 sentiment_result = analyze_sentiment(dhivehi_text, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
109 intent_result = identify_intent(dhivehi_text, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
110 topic_result = classify_topic(dhivehi_text, model, tokenizer, pipe, max_new_tokens, temperature, top_p, top_k, do_sample)
111
112 # Extract the actual values from nested results
113 results = {
114 "opinion": opinion_result.get("opinion", "unknown") if isinstance(opinion_result, dict) else opinion_result,
115 "sentiment": sentiment_result.get("sentiment", "unknown") if isinstance(sentiment_result, dict) else sentiment_result,
116 "intent": intent_result.get("intent", "unknown") if isinstance(intent_result, dict) else intent_result,
117 "topic": topic_result.get("topic", "unknown") if isinstance(topic_result, dict) else topic_result
118 }
119
120 return results
121 except Exception as e:
122 return {"error": f"Error in complete analysis: {str(e)}"}
123
124def _generate_classification_response(instruction: str, model, tokenizer, pipe, max_new_tokens: int, temperature: float, top_p: float, top_k: int, do_sample: bool):
125 """
126 Helper function to generate classification response
127
128 Args:
129 instruction: The instruction for the model
130 model: Loaded model
131 tokenizer: Loaded tokenizer
132 pipe: Loaded pipeline
133 max_new_tokens: Maximum tokens to generate
134 temperature: Sampling temperature
135 top_p: Top-p sampling parameter
136 top_k: Top-k sampling parameter
137 do_sample: Whether to use sampling
138
139 Returns:
140 Dictionary with classification result
141 """
142 if pipe is None or model is None or tokenizer is None:
143 return {"error": "Please load a classification model first!"}
144
145 try:
146 messages = [{"role": "user", "content": instruction}]
147 prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
148
149 # Generation parameters
150 if do_sample:
151 gen_kwargs = {
152 "max_new_tokens": max_new_tokens,
153 "temperature": temperature,
154 "top_p": top_p,
155 "top_k": top_k,
156 "do_sample": True,
157 "disable_compile": True,
158 "pad_token_id": tokenizer.eos_token_id
159 }
160 else:
161 gen_kwargs = {
162 "max_new_tokens": max_new_tokens,
163 "disable_compile": True,
164 "pad_token_id": tokenizer.eos_token_id
165 }
166
167 outputs = pipe(prompt, **gen_kwargs)
168
169 # Extract only the generated part
170 response = outputs[0]['generated_text'][len(prompt):].strip()
171
172 # Try to parse JSON response
173 try:
174 import json
175 result = json.loads(response)
176 return result
177 except json.JSONDecodeError:
178 return {"raw_response": response, "error": "Failed to parse JSON"}
179
180 except Exception as e:
181 return {"error": f"Error in classification: {str(e)}"}1# Load your model, tokenizer, and pipeline first
2# model, tokenizer, pipe = load_your_model()
3
4# Example text for classification
5dhivehi_text = "ދިވެހިރާއްޖެއަކީ އިންޑިޔާ ކަނޑުގައި އޮންނަ ޖަޒީރާ ޤައުމެކެވެ."
6
7# Run individual classifications
8sentiment_result = analyze_sentiment(dhivehi_text, model, tokenizer, pipe)
9topic_result = classify_topic(dhivehi_text, model, tokenizer, pipe)
10intent_result = identify_intent(dhivehi_text, model, tokenizer, pipe)
11opinion_result = classify_opinion(dhivehi_text, model, tokenizer, pipe)
12
13# Or run all analyses at once
14all_results = analyze_all(dhivehi_text, model, tokenizer, pipe)
15
16print(f"Sentiment: {sentiment_result}")
17print(f"Topic: {topic_result}")
18print(f"Intent: {intent_result}")
19print(f"Opinion: {opinion_result}")
20print(f"Complete Analysis: {all_results}")
21
22"""
23# Response:
24Sentiment: {'sentiment': 'Positive'}
25Topic: {'topic': 'Politics'}
26Intent: {'intent': 'Identification'}
27Opinion: {'opinion': 'Neutral'}
28Complete Analysis: {'opinion': 'Neutral', 'sentiment': 'Neutral', 'intent': 'Statement', 'topic': 'International'
29"""do_sample=False for consistent, deterministic classification resultsmax_new_tokens modest (64-128) for focused classification outputs{"sentiment": "positive/negative/neutral"}{"topic": "news/politics/sports/etc"}{"intent": "question/statement/request/etc"}{"opinion": "support/oppose/neutral"}