File size: 10,156 Bytes
43ab10a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3c998f
 
 
 
 
43ab10a
 
 
 
 
 
 
 
 
 
 
 
 
c3c998f
 
 
 
 
 
 
43ab10a
c3c998f
 
 
 
 
43ab10a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Wrapper para ejecutar Llama 2 (u otros modelos de HF) LOCALMENTE en el Space.
Usa transformers + pipeline para inferencia en CPU/GPU.
Compatible con la clase Agent (método generate_simple).
"""

import os
import time
import torch
from functools import lru_cache
from typing import List, Dict, Optional
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

try:
    from transformers import BitsAndBytesConfig
    BITSANDBYTES_AVAILABLE = True
except ImportError:
    BITSANDBYTES_AVAILABLE = False
    print("⚠️ bitsandbytes no disponible, no se puede usar quantización 8-bit")


class LocalHFModel:
    """
    Modelo de HuggingFace cargado localmente en memoria.
    
    Ventajas:
    - ⚡ Más rápido (sin latencia de red)
    - 🔒 Sin rate limits
    - 💾 Control total sobre parámetros
    
    Desventajas:
    - 🧠 Usa RAM del Space (~7-14GB según modelo)
    - ⏳ Carga inicial lenta (30-60s)
    """
    
    def __init__(
        self,
        model_id: str = "meta-llama/Llama-2-7b-chat-hf",
        max_new_tokens: int = 256,
        temperature: float = 0.0,
        device: str = "auto",
        load_in_8bit: bool = True,
    ):
        """
        Inicializa modelo local.
        
        Args:
            model_id: ID del modelo en HuggingFace Hub
            max_new_tokens: Tokens máximos a generar
            temperature: 0.0 = determinístico, >0 = creativo
            device: "auto", "cpu", "cuda"
            load_in_8bit: True = ~7GB RAM, False = ~14GB RAM
        """
        self.model_id = model_id
        self.max_new_tokens = max_new_tokens
        self.temperature = temperature
        self.device = device
        self.load_in_8bit = load_in_8bit
        
        # HF Token (necesario para modelos gated como Llama)
        self.hf_token = os.getenv("HF_TOKEN")
        if not self.hf_token:
            raise ValueError(
                "❌ HF_TOKEN no configurado.\n"
                "Necesario para descargar modelos de HuggingFace.\n"
                "Configúralo en Settings → Repository secrets"
            )
        
        print(f"🦙 Cargando {model_id} localmente...")
        print(f"   📍 Device: {device}")
        print(f"   💾 8-bit quantization: {load_in_8bit}")
        print(f"   🎯 Max tokens: {max_new_tokens}")
        print(f"   🌡️ Temperature: {temperature}")
        
        start_time = time.time()
        
        # Configurar quantización
        quantization_config = None
        if load_in_8bit and BITSANDBYTES_AVAILABLE:
            try:
                quantization_config = BitsAndBytesConfig(
                    load_in_8bit=True,
                    llm_int8_threshold=6.0,
                )
                print("   ✅ Quantización 8-bit configurada")
            except Exception as e:
                print(f"   ⚠️ Error en 8-bit, usando float16: {e}")
                load_in_8bit = False
        elif load_in_8bit and not BITSANDBYTES_AVAILABLE:
            print("   ⚠️ bitsandbytes no instalado, usando float16")
            load_in_8bit = False
        
        # Cargar tokenizer
        print("   📦 Cargando tokenizer...")
        self.tokenizer = AutoTokenizer.from_pretrained(
            model_id,
            token=self.hf_token,
            trust_remote_code=True
        )
        
        # Configurar pad_token si no existe
        if self.tokenizer.pad_token is None:
            self.tokenizer.pad_token = self.tokenizer.eos_token
        
        # Cargar modelo
        print("   🧠 Cargando modelo (30-60s)...")
        try:
            self.model = AutoModelForCausalLM.from_pretrained(
                model_id,
                token=self.hf_token,
                torch_dtype=torch.float16 if not load_in_8bit else torch.float32,
                device_map=device,
                quantization_config=quantization_config,
                low_cpu_mem_usage=True,  # Importante para 16GB RAM
                trust_remote_code=True
            )
        except Exception as e:
            print(f"   ❌ Error cargando modelo: {e}")
            print("   ℹ️ Verifica que HF_TOKEN tenga acceso al modelo")
            raise
        
        # Crear pipeline
        self.pipe = pipeline(
            "text-generation",
            model=self.model,
            tokenizer=self.tokenizer,
            max_new_tokens=max_new_tokens,
            temperature=temperature if temperature > 0 else 0.01,  # 0.0 causa problemas
            do_sample=temperature > 0,
            top_p=0.95 if temperature > 0 else 1.0,
            repetition_penalty=1.15,
            return_full_text=False,  # Solo nueva generación
        )
        
        load_time = time.time() - start_time
        print(f"   ✅ Modelo cargado en {load_time:.1f}s")
        
        # Info de memoria
        if torch.cuda.is_available():
            mem_allocated = torch.cuda.memory_allocated() / 1024**3
            print(f"   📊 GPU Memory: {mem_allocated:.2f} GB")
        else:
            print("   📊 Running on CPU")
    
    def _format_llama_prompt(self, messages: List[Dict[str, str]]) -> str:
        """
        Formatea mensajes al formato correcto según el modelo.
        
        Soporta:
        - Llama 2: <s>[INST] <<SYS>>...
        - Zephyr: <|system|>...<|user|>...<|assistant|>
        """
        system_msg = ""
        user_msg = ""
        
        for msg in messages:
            role = msg.get("role", "user")
            content = msg.get("content", "")
            
            if role == "system":
                system_msg = content
            elif role == "user":
                user_msg = content
        
        # Detectar formato según model_id
        if "zephyr" in self.model_id.lower():
            # Formato Zephyr
            if system_msg:
                prompt = f"<|system|>\n{system_msg}</s>\n<|user|>\n{user_msg}</s>\n<|assistant|>\n"
            else:
                prompt = f"<|user|>\n{user_msg}</s>\n<|assistant|>\n"
        else:
            # Formato Llama 2 (default)
            if system_msg:
                prompt = f"<s>[INST] <<SYS>>\n{system_msg}\n<</SYS>>\n\n{user_msg} [/INST]"
            else:
                prompt = f"<s>[INST] {user_msg} [/INST]"
        
        return prompt
    
    def __call__(
        self,
        messages: List[Dict[str, str]],
        max_new_tokens: Optional[int] = None,
        temperature: Optional[float] = None,
        **kwargs
    ) -> str:
        """
        Genera respuesta.
        
        Args:
            messages: [{"role": "user", "content": "..."}]
            max_new_tokens: Override de tokens
            temperature: Override de temperatura
            
        Returns:
            Texto generado
        """
        try:
            # Formatear prompt
            prompt = self._format_llama_prompt(messages)
            
            # Override parámetros
            gen_kwargs = {}
            if max_new_tokens:
                gen_kwargs["max_new_tokens"] = max_new_tokens
            if temperature is not None:
                gen_kwargs["temperature"] = temperature if temperature > 0 else 0.01
                gen_kwargs["do_sample"] = temperature > 0
            
            # Generar
            start_time = time.time()
            result = self.pipe(prompt, **gen_kwargs)
            gen_time = time.time() - start_time
            
            # Extraer texto
            if isinstance(result, list) and len(result) > 0:
                generated_text = result[0].get("generated_text", "")
            else:
                generated_text = str(result)
            
            print(f"   ⚡ Generado en {gen_time:.2f}s ({len(generated_text)} chars)")
            
            return generated_text.strip()
            
        except Exception as e:
            error_msg = f"ERROR: {str(e)}"
            print(f"   ❌ {error_msg}")
            return error_msg
    
    def generate_simple(
        self,
        prompt: str,
        system: Optional[str] = None,
        **kwargs
    ) -> str:
        """
        Interfaz simplificada compatible con Agent.
        
        Args:
            prompt: Texto del usuario
            system: Prompt de sistema (opcional)
            
        Returns:
            Respuesta generada
        """
        messages = []
        if system:
            messages.append({"role": "system", "content": system})
        messages.append({"role": "user", "content": prompt})
        
        return self(messages, **kwargs)


@lru_cache(maxsize=1)  # Solo 1 modelo en cache (usa mucha RAM)
def get_local_model(
    model_id: str = "meta-llama/Llama-2-7b-chat-hf",
    load_in_8bit: bool = True,
    max_new_tokens: int = 256,
    temperature: float = 0.0,
) -> LocalHFModel:
    """
    Factory con cache para modelo local.
    
    IMPORTANTE: maxsize=1 porque cada modelo usa ~7-14GB RAM.
    """
    return LocalHFModel(
        model_id=model_id,
        load_in_8bit=load_in_8bit,
        max_new_tokens=max_new_tokens,
        temperature=temperature
    )


# Alias para compatibilidad con app.py
def get_model(model_id: str = "meta-llama/Llama-2-7b-chat-hf", **kwargs) -> LocalHFModel:
    """
    Factory principal para obtener modelo local.
    
    Args:
        model_id: Modelo de HuggingFace
        **kwargs: Parámetros adicionales (load_in_8bit, max_new_tokens, etc.)
    
    Returns:
        LocalHFModel listo para usar
    """
    # Obtener parámetros con defaults
    load_in_8bit = kwargs.pop("load_in_8bit", True)
    max_new_tokens = kwargs.pop("max_new_tokens", 256)
    temperature = kwargs.pop("temperature", 0.0)
    
    return get_local_model(
        model_id=model_id,
        load_in_8bit=load_in_8bit,
        max_new_tokens=max_new_tokens,
        temperature=temperature
    )


if __name__ == "__main__":
    # Test
    print("=== Test de Modelo Local ===")
    
    model = get_model(load_in_8bit=True)
    
    response = model.generate_simple(
        "What is 2+2?",
        system="You are a helpful math assistant."
    )
    
    print(f"\n📝 Respuesta: {response}")