Raiff1982 commited on
Commit
e24fc1e
·
verified ·
1 Parent(s): a5da0f9

Upload 5 files

Browse files
core/__pycache__/model_manager.cpython-313.pyc ADDED
Binary file (6.44 kB). View file
 
core/__pycache__/setup.cpython-313.pyc ADDED
Binary file (3.47 kB). View file
 
core/model_manager.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import logging
4
+ from typing import Optional, Dict, Any
5
+ import torch
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+ class ModelManager:
11
+ def __init__(self):
12
+ """Initialize the model manager."""
13
+ self.current_model = None
14
+ self.current_tokenizer = None
15
+ self.current_model_name = None
16
+ self.load_model()
17
+
18
+ def load_model(self, model_name: Optional[str] = None) -> bool:
19
+ """
20
+ Load the language model, trying different models in order of preference.
21
+
22
+ Args:
23
+ model_name: Optional specific model to load
24
+
25
+ Returns:
26
+ bool: True if any model was loaded successfully
27
+ """
28
+ models_to_try = [
29
+ model_name
30
+ ] if model_name else [
31
+ "mistralai/Mistral-7B-Instruct-v0.2", # Best balance of capability/size
32
+ "microsoft/phi-2", # Fallback
33
+ "gpt2" # Last resort
34
+ ]
35
+
36
+ for model_id in models_to_try:
37
+ try:
38
+ logger.info(f"Loading {model_id}")
39
+ self.current_tokenizer = AutoTokenizer.from_pretrained(model_id)
40
+ self.current_model = AutoModelForCausalLM.from_pretrained(
41
+ model_id,
42
+ device_map="auto",
43
+ torch_dtype=torch.float16, # Use half precision
44
+ load_in_8bit=True
45
+ )
46
+ self.current_model_name = model_id
47
+ self.current_model.eval()
48
+ logger.info(f"Successfully loaded {model_id}")
49
+ return True
50
+ except Exception as e:
51
+ logger.warning(f"Failed to load {model_id}: {e}")
52
+ continue
53
+
54
+ return False
55
+ torch_dtype=getattr(torch, self.config.get('torch_dtype', 'float32'))
56
+ )
57
+
58
+ self.current_model.eval()
59
+ self.current_model_name = model_name
60
+
61
+ logger.info(f"Successfully loaded model {model_name}")
62
+ return True
63
+
64
+ except Exception as e:
65
+ logger.error(f"Error loading model {model_name}: {e}")
66
+ return False
67
+
68
+ def get_current_model(self) -> tuple:
69
+ """Get currently loaded model and tokenizer."""
70
+ return self.current_model, self.current_tokenizer
71
+
72
+ def is_model_loaded(self) -> bool:
73
+ """Check if a model is currently loaded."""
74
+ return self.current_model is not None and self.current_tokenizer is not None
core/new_model_manager.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import torch
3
+ from typing import Optional, Tuple
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ logger = logging.getLogger(__name__)
7
+
8
+ class ModelManager:
9
+ def __init__(self):
10
+ """Initialize the model manager."""
11
+ self.current_model = None
12
+ self.current_tokenizer = None
13
+ self.current_model_name = None
14
+ self.load_model()
15
+
16
+ def load_model(self, model_name: Optional[str] = None) -> bool:
17
+ """
18
+ Load the language model, trying different models in order of preference.
19
+
20
+ Args:
21
+ model_name: Optional specific model to load
22
+
23
+ Returns:
24
+ bool: True if any model was loaded successfully
25
+ """
26
+ models_to_try = [
27
+ model_name
28
+ ] if model_name else [
29
+ "mistralai/Mistral-7B-Instruct-v0.2", # Best balance of capability/size
30
+ "microsoft/phi-2", # Fallback
31
+ "gpt2" # Last resort
32
+ ]
33
+
34
+ for model_id in models_to_try:
35
+ try:
36
+ logger.info(f"Loading {model_id}")
37
+ self.current_tokenizer = AutoTokenizer.from_pretrained(model_id)
38
+ self.current_model = AutoModelForCausalLM.from_pretrained(
39
+ model_id,
40
+ device_map="auto",
41
+ torch_dtype=torch.float16, # Use half precision
42
+ load_in_8bit=True
43
+ )
44
+ self.current_model_name = model_id
45
+ self.current_model.eval()
46
+ logger.info(f"Successfully loaded {model_id}")
47
+ return True
48
+ except Exception as e:
49
+ logger.warning(f"Failed to load {model_id}: {e}")
50
+ continue
51
+
52
+ return False
53
+
54
+ def get_current_model(self) -> Tuple[Optional[AutoModelForCausalLM], Optional[AutoTokenizer]]:
55
+ """Get currently loaded model and tokenizer."""
56
+ return self.current_model, self.current_tokenizer
57
+
58
+ def is_model_loaded(self) -> bool:
59
+ """Check if a model is currently loaded."""
60
+ return self.current_model is not None and self.current_tokenizer is not None
core/setup.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import json
4
+ import torch
5
+ import logging
6
+ from pathlib import Path
7
+
8
+ def setup_environment():
9
+ """Set up the environment for Codette with modern language models."""
10
+
11
+ # Configure logging
12
+ logging.basicConfig(
13
+ level=logging.INFO,
14
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
15
+ )
16
+ logger = logging.getLogger(__name__)
17
+
18
+ # Create necessary directories
19
+ config_dir = Path(__file__).parent.parent / 'config'
20
+ config_dir.mkdir(exist_ok=True)
21
+
22
+ # Check system requirements
23
+ logger.info("Checking system requirements...")
24
+
25
+ # Check Python version
26
+ python_version = sys.version_info
27
+ if python_version.major < 3 or (python_version.major == 3 and python_version.minor < 8):
28
+ logger.error("Python 3.8 or higher is required")
29
+ return False
30
+
31
+ # Check CUDA availability
32
+ cuda_available = torch.cuda.is_available()
33
+ if cuda_available:
34
+ gpu_count = torch.cuda.device_count()
35
+ gpu_name = torch.cuda.get_device_name(0) if gpu_count > 0 else "Unknown"
36
+ gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3 if gpu_count > 0 else 0
37
+
38
+ logger.info(f"CUDA is available with {gpu_count} device(s)")
39
+ logger.info(f"GPU: {gpu_name} with {gpu_memory:.1f}GB memory")
40
+ else:
41
+ logger.warning("CUDA is not available - running in CPU mode will be very slow")
42
+
43
+ # Create or update environment configuration
44
+ env_config = {
45
+ 'cuda_available': cuda_available,
46
+ 'gpu_count': gpu_count if cuda_available else 0,
47
+ 'gpu_memory': gpu_memory if cuda_available else 0,
48
+ 'python_version': f"{python_version.major}.{python_version.minor}.{python_version.micro}",
49
+ 'torch_version': torch.__version__
50
+ }
51
+
52
+ with open(config_dir / 'environment.json', 'w') as f:
53
+ json.dump(env_config, f, indent=2)
54
+
55
+ # Set environment variables
56
+ os.environ['TRANSFORMERS_CACHE'] = str(Path.home() / '.cache' / 'huggingface')
57
+ os.environ['TORCH_HOME'] = str(Path.home() / '.cache' / 'torch')
58
+
59
+ # Optimize for inference
60
+ if cuda_available:
61
+ torch.backends.cudnn.benchmark = True
62
+
63
+ logger.info("Environment setup complete")
64
+ return True
65
+
66
+ if __name__ == '__main__':
67
+ setup_environment()