Views
No views yet
Accuracy: 95.51%
F1 Score (Weighted): 0.96
F1 Score (Macro): 0.88
Training Loss: 0.3pip install transformers peft torch1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4import json
5
6# Load model
7base_model = AutoModelForCausalLM.from_pretrained(
8 "google/functiongemma-270m-it",
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12model = PeftModel.from_pretrained(base_model, "ovinduG/functiongemma-domain-classifier")
13tokenizer = AutoTokenizer.from_pretrained("ovinduG/functiongemma-domain-classifier")
14
15# Classify a query
16def classify(text):
17 # Define function schema
18 function_def = {
19 "type": "function",
20 "function": {
21 "name": "classify_query_domain",
22 "description": "Classify query into domains",
23 "parameters": {
24 "type": "object",
25 "properties": {
26 "primary_domain": {"type": "string"},
27 "primary_confidence": {"type": "number"},
28 "is_multi_domain": {"type": "boolean"},
29 "secondary_domains": {"type": "array"}
30 }
31 }
32 }
33 }
34
35 messages = [
36 {"role": "developer", "content": "You are a model that can do function calling"},
37 {"role": "user", "content": text}
38 ]
39
40 inputs = tokenizer.apply_chat_template(
41 messages,
42 tools=[function_def],
43 add_generation_prompt=True,
44 return_dict=True,
45 return_tensors="pt"
46 ).to(model.device)
47
48 with torch.no_grad():
49 outputs = model.generate(
50 **inputs,
51 max_new_tokens=150,
52 do_sample=False,
53 pad_token_id=tokenizer.eos_token_id
54 )
55
56 response = tokenizer.decode(
57 outputs[0][inputs["input_ids"].shape[-1]:],
58 skip_special_tokens=True
59 )
60
61 # Parse function call
62 if "{" in response:
63 start = response.find("{")")
64 end = response.rfind("}") + 1
65 return json.loads(response[start:end])
66
67 return {"error": "Failed to parse response"}
68
69# Example
70result = classify("Write a Python function to calculate fibonacci numbers")
71print(json.dumps(result, indent=2))1{
2 "primary_domain": "coding",
3 "primary_confidence": 0.95,
4 "is_multi_domain": false,
5 "secondary_domains": []
6}1result = classify("Build an ML model to predict customer churn and create REST API endpoints")
2print(json.dumps(result, indent=2))1{
2 "primary_domain": "data_analysis",
3 "primary_confidence": 0.85,
4 "is_multi_domain": true,
5 "secondary_domains": [
6 {
7 "domain": "api_generation",
8 "confidence": 0.75
9 }
10 ]
11}1# LoRA Configuration
2r = 32
3lora_alpha = 64
4lora_dropout = 0.05
5target_modules = ['q_proj', 'v_proj', 'k_proj', 'o_proj', 'gate_proj', 'up_proj', 'down_proj']
6
7# Training Configuration
8num_epochs = 5
9batch_size = 4
10gradient_accumulation_steps = 8
11learning_rate = 0.0003
12max_length = 1024
13optimizer = "adamw_8bit" # Memory optimized| Domain | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| ambiguous | 0.98 | 1.00 | 0.99 | 45 |
| api_generation | 0.98 | 1.00 | 0.99 | 45 |
| business | 0.98 | 0.93 | 0.95 | 44 |
| coding | 0.98 | 0.96 | 0.97 | 48 |
| creative_content | 0.90 | 1.00 | 0.95 | 45 |
| data_analysis | 0.96 | 0.98 | 0.97 | 46 |
| education | 0.98 | 0.96 | 0.97 | 45 |
| general_knowledge | 0.76 | 0.84 | 0.80 | 45 |
| law | 0.98 | 0.94 | 0.96 | 49 |
| literature | 1.00 | 0.93 | 0.97 | 45 |
| mathematics | 1.00 | 1.00 | 1.00 | 47 |
| medicine | 0.98 | 0.89 | 0.93 | 46 |
| science | 1.00 | 0.98 | 0.99 | 47 |
| sensitive | 0.92 | 1.00 | 0.96 | 45 |
| technology | 1.00 | 0.93 | 0.97 | 46 |
google/functiongemma1license: gemma
2base_model: google/functiongemma
3tags:
4 - text-classification
5 - domain-classification
6 - gemma
7 - functiongemma