Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# Check GPU availability
5print(f"CUDA available: {torch.cuda.is_available()}")
6print(f"GPU device count: {torch.cuda.device_count()}")
7if torch.cuda.is_available():
8 print(f"Current GPU: {torch.cuda.get_device_name(0)}")
9
10# Set device
11device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12print(f"Using device: {device}")
13
14# Load model and tokenizer
15print("Loading tokenizer...")
16tokenizer = AutoTokenizer.from_pretrained("ritvik77/dauji-ai-sales-crm-consultant_v.0.01")
17
18print("Loading model...")
19model = AutoModelForCausalLM.from_pretrained(
20 "ritvik77/dauji-ai-sales-crm-consultant_v.0.01",
21 dtype=torch.float16, # Use half precision to save GPU memory (updated from torch_dtype)
22 device_map="auto" # Automatically distribute model across available GPUs
23)
24
25# Alternative manual GPU placement (use this if device_map="auto" doesn't work)
26# model = model.to(device)
27
28print(f"Model device: {next(model.parameters()).device}")
29
30# Consultation prompt template
31def dauji_consultation(question):
32 prompt = '''Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
33
34### Instruction:
35You are an expert Sales & CRM Consultant for Dauji.ai, the always-on AI Sales Agent platform with deep CRM integration. Focus on measurable business outcomes, CRM optimization, and sales acceleration strategies.
36
37### Input:
38{}
39
40### Response:
41'''.format(question)
42
43 # Tokenize and move inputs to GPU
44 inputs = tokenizer(prompt, return_tensors="pt").to(device)
45
46 # Generate with GPU
47 with torch.no_grad(): # Save memory during inference
48 outputs = model.generate(
49 **inputs,
50 max_new_tokens=400,
51 temperature=0.3, # Lower temperature for more focused responses
52 do_sample=True,
53 top_k=50,
54 top_p=0.95,
55 pad_token_id=tokenizer.eos_token_id,
56 eos_token_id=tokenizer.eos_token_id,
57 repetition_penalty=1.15,
58 no_repeat_ngram_size=3, # Prevent 3-gram repetition
59 early_stopping=True
60 )
61
62 # Decode only the generated part (excluding input prompt)
63 input_length = inputs.input_ids.shape[1]
64 generated_tokens = outputs[0][input_length:]
65 response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
66
67 return response.strip()
68
69# Alternative function with better prompt engineering
70def dauji_consultation_v2(question):
71 prompt = f"""You are Dauji.ai's expert Sales & CRM consultant. Answer the following question with specific, actionable advice about Dauji.ai's capabilities.
72
73Question: {question}
74
75Answer:"""
76
77 inputs = tokenizer(prompt, return_tensors="pt").to(device)
78
79 with torch.no_grad():
80 outputs = model.generate(
81 **inputs,
82 max_new_tokens=300,
83 temperature=0.2, # Very low temperature for consistency
84 do_sample=False, # Use greedy decoding for more predictable outputs
85 pad_token_id=tokenizer.eos_token_id,
86 eos_token_id=tokenizer.eos_token_id,
87 repetition_penalty=1.2
88 )
89
90 input_length = inputs.input_ids.shape[1]
91 generated_tokens = outputs[0][input_length:]
92 response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
93
94 return response.strip()
95
96# Debug function to check model behavior
97def debug_model_response(question):
98 prompt = f"Question: {question}\nAnswer:"
99
100 print(f"Input prompt: {prompt}")
101 print("-" * 50)
102
103 inputs = tokenizer(prompt, return_tensors="pt").to(device)
104 print(f"Input token IDs: {inputs.input_ids[0][:20]}...") # First 20 tokens
105 print(f"Input length: {inputs.input_ids.shape[1]} tokens")
106
107 with torch.no_grad():
108 outputs = model.generate(
109 **inputs,
110 max_new_tokens=100,
111 temperature=0.1,
112 do_sample=False,
113 pad_token_id=tokenizer.eos_token_id
114 )
115
116 full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
117 print(f"Full response: {full_response}")
118
119 return full_response
120
121# Check GPU memory usage
122if torch.cuda.is_available():
123 print(f"GPU memory allocated: {torch.cuda.memory_allocated(0) / 1024**3:.2f} GB")
124 print(f"GPU memory reserved: {torch.cuda.memory_reserved(0) / 1024**3:.2f} GB")
125
126# Example usage
127print("\nGenerating response...")
128response = dauji_consultation("How can Dauji.ai improve our CRM conversion rates?")
129print("\nResponse:")
130print(response)
131
132# Check GPU memory usage after inference
133if torch.cuda.is_available():
134 print(f"\nGPU memory allocated after inference: {torch.cuda.memory_allocated(0) / 1024**3:.2f} GB")
135 print(f"GPU memory reserved after inference: {torch.cuda.memory_reserved(0) / 1024**3:.2f} GB")@misc{dauji_ai_consultant_2024,
title={Dauji.ai Sales & CRM Consultant - Gemma 2B},
author={Your Name},
year={2024},
howpublished={Hugging Face Model Hub},
url={https://huggingface.co/ritvik77/dauji-ai-sales-crm-consultant_v.0.01}
}