RehanKingggg commited on
Commit
6a68b5c
·
verified ·
1 Parent(s): 98a101b

Upload brello_ei_0.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. brello_ei_0.py +242 -0
brello_ei_0.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Brello EI 0 - Emotional Intelligence AI Model
3
+ Created by Epic Systems | Engineered by Rehan Temkar
4
+
5
+ A locally-run emotional intelligence AI model based on Llama 3.2 3B,
6
+ designed to provide empathetic, emotionally-aware responses.
7
+ """
8
+
9
+ import torch
10
+ from transformers import (
11
+ AutoModelForCausalLM,
12
+ AutoTokenizer,
13
+ GenerationConfig,
14
+ BitsAndBytesConfig
15
+ )
16
+ from typing import Optional, Dict, Any, List
17
+ import logging
18
+ import os
19
+
20
+ logger = logging.getLogger(__name__)
21
+
22
+ class BrelloEI0:
23
+ """
24
+ Brello EI 0 - Emotional Intelligence AI Model
25
+
26
+ A locally-run AI model designed to provide emotionally intelligent,
27
+ empathetic responses with natural conversation flow.
28
+ """
29
+
30
+ def __init__(
31
+ self,
32
+ model_path: str = "microsoft/DialoGPT-medium",
33
+ device: Optional[str] = None,
34
+ load_in_4bit: bool = False,
35
+ load_in_8bit: bool = False,
36
+ torch_dtype: Optional[torch.dtype] = None,
37
+ **kwargs
38
+ ):
39
+ """
40
+ Initialize Brello EI 0 model
41
+
42
+ Args:
43
+ model_path: Path to Llama 3.2 3B model
44
+ device: Device to load model on ('cuda', 'cpu', etc.)
45
+ load_in_4bit: Whether to load model in 4-bit quantization
46
+ load_in_8bit: Whether to load model in 8-bit quantization
47
+ torch_dtype: Torch data type for model weights
48
+ """
49
+ self.model_path = model_path
50
+ self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
51
+ self.model = None
52
+ self.tokenizer = None
53
+ self.config = {
54
+ "max_length": 4096,
55
+ "temperature": 0.7,
56
+ "top_p": 0.9,
57
+ "repetition_penalty": 1.1,
58
+ "do_sample": True,
59
+ "min_length": 30,
60
+ "max_new_tokens": 256,
61
+ "no_repeat_ngram_size": 3
62
+ }
63
+
64
+ # Quantization config for memory efficiency
65
+ self.quantization_config = None
66
+ if load_in_4bit:
67
+ self.quantization_config = BitsAndBytesConfig(
68
+ load_in_4bit=True,
69
+ bnb_4bit_compute_dtype=torch.float16,
70
+ bnb_4bit_use_double_quant=True,
71
+ bnb_4bit_quant_type="nf4"
72
+ )
73
+ elif load_in_8bit:
74
+ self.quantization_config = BitsAndBytesConfig(load_in_8bit=True)
75
+
76
+ self.torch_dtype = torch_dtype or torch.float16 if self.device == "cuda" else torch.float32
77
+
78
+ logger.info(f"Initializing Brello EI 0 model: {model_path}")
79
+ self.load_model()
80
+
81
+ def load_model(self):
82
+ """Load the Brello EI 0 model and tokenizer"""
83
+ try:
84
+ logger.info(f"Loading Brello EI 0 model: {self.model_path}")
85
+
86
+ # Load tokenizer
87
+ self.tokenizer = AutoTokenizer.from_pretrained(
88
+ self.model_path,
89
+ trust_remote_code=True,
90
+ padding_side="left"
91
+ )
92
+
93
+ # Set padding token
94
+ if self.tokenizer.pad_token is None:
95
+ self.tokenizer.pad_token = self.tokenizer.eos_token
96
+
97
+ # Load model
98
+ model_kwargs = {
99
+ "torch_dtype": self.torch_dtype,
100
+ "device_map": "auto" if self.device == "cuda" else None,
101
+ "trust_remote_code": True
102
+ }
103
+
104
+ if self.quantization_config:
105
+ model_kwargs["quantization_config"] = self.quantization_config
106
+
107
+ self.model = AutoModelForCausalLM.from_pretrained(
108
+ self.model_path,
109
+ **model_kwargs
110
+ )
111
+
112
+ # Move to device if not using device_map
113
+ if self.device != "cuda" or self.quantization_config is None:
114
+ self.model = self.model.to(self.device)
115
+
116
+ logger.info("✅ Brello EI 0 model loaded successfully")
117
+
118
+ except Exception as e:
119
+ logger.error(f"❌ Failed to load Brello EI 0 model: {e}")
120
+ raise
121
+
122
+ def apply_emotional_intelligence_prompt(self, user_input: str) -> str:
123
+ """
124
+ Apply emotional intelligence prompt template for Brello EI 0
125
+
126
+ Args:
127
+ user_input: User's message
128
+
129
+ Returns:
130
+ Formatted conversation string with emotional intelligence focus
131
+ """
132
+ # Format the conversation with emotional intelligence focus
133
+ prompt = f"""<|system|>
134
+ You are Brello EI 0, an emotionally intelligent AI created by Epic Systems and engineered by Rehan Temkar. You provide empathetic, understanding responses that show emotional awareness and genuine care for the user's feelings and experiences. You are part of the Brello AI family, designed to bring emotional intelligence to AI conversations.
135
+ </s>
136
+ <|user|>
137
+ {user_input}
138
+ </s>
139
+ <|assistant|>"""
140
+ return prompt
141
+
142
+ def generate_response(
143
+ self,
144
+ user_input: str,
145
+ max_length: Optional[int] = None,
146
+ temperature: Optional[float] = None,
147
+ top_p: Optional[float] = None,
148
+ **kwargs
149
+ ) -> str:
150
+ """
151
+ Generate emotionally intelligent response
152
+
153
+ Args:
154
+ user_input: User's message
155
+ max_length: Maximum response length
156
+ temperature: Sampling temperature
157
+ top_p: Top-p sampling parameter
158
+ **kwargs: Additional generation parameters
159
+
160
+ Returns:
161
+ Generated emotionally intelligent response
162
+ """
163
+ if self.model is None or self.tokenizer is None:
164
+ raise ValueError("Model not loaded. Call load_model() first.")
165
+
166
+ # Apply emotional intelligence prompt template
167
+ formatted_input = self.apply_emotional_intelligence_prompt(user_input)
168
+
169
+ # Tokenize input
170
+ inputs = self.tokenizer.encode(formatted_input, return_tensors="pt")
171
+ if hasattr(self.model, 'device'):
172
+ inputs = inputs.to(self.model.device)
173
+
174
+ # Generation parameters - optimized for emotional intelligence
175
+ gen_params = {
176
+ "max_length": max_length or self.config["max_length"],
177
+ "temperature": temperature or self.config["temperature"],
178
+ "top_p": top_p or self.config["top_p"],
179
+ "do_sample": self.config["do_sample"],
180
+ "pad_token_id": self.tokenizer.eos_token_id,
181
+ "eos_token_id": self.tokenizer.eos_token_id,
182
+ "repetition_penalty": self.config["repetition_penalty"],
183
+ "length_penalty": 1.0,
184
+ "no_repeat_ngram_size": self.config["no_repeat_ngram_size"],
185
+ "min_length": self.config["min_length"],
186
+ "max_new_tokens": self.config["max_new_tokens"],
187
+ **kwargs
188
+ }
189
+
190
+ # Generate response
191
+ with torch.no_grad():
192
+ outputs = self.model.generate(
193
+ inputs,
194
+ **gen_params
195
+ )
196
+
197
+ # Decode response
198
+ response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
199
+
200
+ # Extract only the assistant's response
201
+ if "<|assistant|>" in response:
202
+ response = response.split("<|assistant|>")[-1].strip()
203
+
204
+ # Clean up the response
205
+ response = response.strip()
206
+
207
+ # Ensure response shows emotional intelligence
208
+ if len(response) < 20:
209
+ response = f"I understand how you might be feeling. {response} It's important to acknowledge our emotions and experiences."
210
+
211
+ return response
212
+
213
+ def chat(self, message: str, maintain_history: bool = False) -> str:
214
+ """
215
+ Simple chat interface
216
+
217
+ Args:
218
+ message: User message
219
+ maintain_history: Whether to maintain conversation history
220
+
221
+ Returns:
222
+ Model response
223
+ """
224
+ return self.generate_response(message)
225
+
226
+ def __call__(self, text: str, **kwargs) -> str:
227
+ """Convenience method for generating responses"""
228
+ return self.generate_response(text, **kwargs)
229
+
230
+ # Convenience function for quick usage
231
+ def load_brello_ei_0(model_path: str = "microsoft/DialoGPT-medium", **kwargs) -> BrelloEI0:
232
+ """
233
+ Load Brello EI 0 model
234
+
235
+ Args:
236
+ model_path: Path to Llama 3.2 3B model
237
+ **kwargs: Additional model parameters
238
+
239
+ Returns:
240
+ BrelloEI0 instance
241
+ """
242
+ return BrelloEI0(model_path=model_path, **kwargs)