File size: 2,346 Bytes
b0e9cd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# config.py - Configuraci贸n del Space

import os

class Config:
    """Configuraci贸n centralizada para el Space"""

    # Modelo
    MODEL_NAME = "meta-llama/Llama-3.2-3B-Instruct"
    DEVICE = "cuda" if os.environ.get("SPACES_GPU") else "cpu"

    # Tokens y autenticaci贸n
    HF_TOKEN = os.environ.get("HF_TOKEN")

    # L铆mites de generaci贸n
    MAX_TOKENS_LIMIT = 1024
    MIN_TOKENS_LIMIT = 50
    DEFAULT_MAX_TOKENS = 512

    # Temperatura
    MAX_TEMPERATURE = 2.0
    MIN_TEMPERATURE = 0.1
    DEFAULT_TEMPERATURE = 0.7

    # Cola y concurrencia
    MAX_QUEUE_SIZE = 10
    QUEUE_TIMEOUT = 300  # 5 minutos

    # Context length
    MAX_CONTEXT_LENGTH = 2048

    # Interface
    CHAT_HEIGHT = 500
    DEFAULT_SYSTEM_PROMPT = "Eres un asistente de IA 煤til y amigable. Responde de manera clara y concisa."

    # API
    API_TIMEOUT = 300
    ENABLE_API_LOGGING = True

    @classmethod
    def validate(cls):
        """Validar configuraci贸n"""
        errors = []

        if not cls.HF_TOKEN:
            errors.append("HF_TOKEN no configurado en variables de entorno")

        if cls.MAX_TOKENS_LIMIT < cls.MIN_TOKENS_LIMIT:
            errors.append("MAX_TOKENS_LIMIT debe ser mayor que MIN_TOKENS_LIMIT")

        if cls.MAX_TEMPERATURE < cls.MIN_TEMPERATURE:
            errors.append("MAX_TEMPERATURE debe ser mayor que MIN_TEMPERATURE")

        return errors

    @classmethod
    def get_model_config(cls):
        """Configuraci贸n espec铆fica del modelo"""
        return {
            "torch_dtype": "float16" if cls.DEVICE == "cuda" else "float32",
            "device_map": "auto" if cls.DEVICE == "cuda" else None,
            "trust_remote_code": True,
            "token": cls.HF_TOKEN
        }

    @classmethod
    def get_generation_config(cls, max_tokens=None, temperature=None):
        """Configuraci贸n de generaci贸n"""
        return {
            "max_new_tokens": max_tokens or cls.DEFAULT_MAX_TOKENS,
            "temperature": temperature or cls.DEFAULT_TEMPERATURE,
            "do_sample": True,
            "repetition_penalty": 1.1,
            "top_p": 0.9,
            "top_k": 50
        }

# Validar configuraci贸n al importar
config_errors = Config.validate()
if config_errors:
    print("鈿狅笍 Errores de configuraci贸n:")
    for error in config_errors:
        print(f"  - {error}")