AurelioAguirre commited on
Commit
dd4ba61
·
1 Parent(s): f35f208

Changed model

Browse files
app/config.yaml CHANGED
@@ -10,7 +10,7 @@ model:
10
  temperature: 0.7
11
  repetition_penalty: 1.1
12
  defaults:
13
- model_name: "Qwen/Qwen2.5-Coder-3B-Instruct"
14
 
15
  folders:
16
  models: "models"
 
10
  temperature: 0.7
11
  repetition_penalty: 1.1
12
  defaults:
13
+ model_name: "huihui-ai/Qwen2.5-Coder-32B-Instruct-abliterated"
14
 
15
  folders:
16
  models: "models"
client/__init__.py DELETED
File without changes
client/client.py DELETED
@@ -1,275 +0,0 @@
1
- import requests
2
- import json
3
- import sseclient
4
- import sys
5
- from pathlib import Path
6
- import yaml
7
- from typing import Optional
8
- import os
9
-
10
- from litgpt.scripts.convert_hf_checkpoint import convert_hf_checkpoint
11
- from litgpt.scripts.download import download_from_hub
12
-
13
- DEFAULT_CONFIG = {
14
- 'server': {'url': 'http://localhost:7860'},
15
- 'model': {
16
- 'name': 'Qwen2.5-Coder-7B-Instruct',
17
- 'download_location': 'huihui-ai/Qwen2.5-Coder-7B-Instruct-abliterated',
18
- 'folder_path': 'huihui-ai/Qwen2.5-Coder-7B-Instruct-abliterated',
19
- 'model_filename': 'model.safetensors'
20
- }
21
- }
22
-
23
- def get_project_root(config: dict) -> Path:
24
- client_dir = Path(__file__).parent
25
- return (client_dir / config['project']['root_dir']).resolve()
26
-
27
- def get_checkpoints_dir(config: dict) -> Path:
28
- root = get_project_root(config)
29
- return root / config['project']['checkpoints_dir']
30
-
31
- class LLMClient:
32
- def __init__(self, config: dict):
33
- self.config = config
34
- self.base_url = config['server']['url'].rstrip('/')
35
- self.session = requests.Session()
36
- self.checkpoints_dir = get_checkpoints_dir(config)
37
-
38
- def download_model(
39
- self,
40
- repo_id: Optional[str] = None,
41
- access_token: Optional[str] = os.getenv("HF_TOKEN"),
42
- ) -> None:
43
- repo_id = repo_id or self.config['model']['folder_path']
44
-
45
- print(f"\nDownloading model from: {repo_id}")
46
- download_from_hub(
47
- repo_id=repo_id,
48
- model_name=self.config['model']['name'],
49
- access_token=access_token,
50
- tokenizer_only=False,
51
- checkpoint_dir=self.checkpoints_dir
52
- )
53
-
54
- def convert_model(
55
- self,
56
- folder_path: Optional[str] = None,
57
- model_name: Optional[str] = None,
58
- ) -> None:
59
- """Convert downloaded model to LitGPT format."""
60
- folder_path = folder_path or self.config['model']['folder_path']
61
- model_name = model_name or self.config['model']['name']
62
-
63
- model_dir = self.checkpoints_dir / folder_path
64
- print(f"\nConverting model in: {model_dir}")
65
- print(f"Using model name: {model_name}")
66
-
67
- try:
68
- convert_hf_checkpoint(
69
- checkpoint_dir=model_dir,
70
- model_name=model_name
71
- )
72
- print("Conversion complete!")
73
- except ValueError as e:
74
- if "is not a supported config name" in str(e):
75
- print(f"\nNote: Model '{model_name}' isn't in LitGPT's predefined configs.")
76
- print("You may need to use the model's safetensors files directly.")
77
- raise
78
-
79
- def initialize_model(
80
- self,
81
- folder_path: Optional[str] = None,
82
- mode: Optional[str] = None,
83
- **kwargs
84
- ) -> dict:
85
- """Initialize a converted model using the standard initialize endpoint."""
86
- url = f"{self.base_url}/initialize"
87
-
88
- folder_path = folder_path or self.config['model']['folder_path']
89
- mode = mode or self.config['hardware']['mode']
90
-
91
- # Debug prints
92
- print(f"\nDebug - Attempting to initialize model with:")
93
- print(f"Model path: {folder_path}")
94
- print(f"Mode: {mode}")
95
-
96
- payload = {
97
- "model_path": folder_path, # This is what the regular initialize endpoint expects
98
- "mode": mode,
99
- "precision": self.config['hardware'].get('precision'),
100
- "quantize": self.config['hardware'].get('quantize'),
101
- "gpu_count": self.config['hardware'].get('gpu_count', 'auto'),
102
- **kwargs
103
- }
104
-
105
- response = self.session.post(url, json=payload)
106
- response.raise_for_status()
107
- return response.json()
108
-
109
- def generate_stream(
110
- self,
111
- prompt: str,
112
- max_new_tokens: Optional[int] = None,
113
- temperature: Optional[float] = None,
114
- top_k: Optional[int] = None,
115
- top_p: Optional[float] = None
116
- ):
117
- url = f"{self.base_url}/generate/stream"
118
-
119
- gen_config = self.config.get('generation', {})
120
- payload = {
121
- "prompt": prompt,
122
- "max_new_tokens": max_new_tokens or gen_config.get('max_new_tokens', 50),
123
- "temperature": temperature or gen_config.get('temperature', 1.0),
124
- "top_k": top_k or gen_config.get('top_k'),
125
- "top_p": top_p or gen_config.get('top_p', 1.0)
126
- }
127
-
128
- response = self.session.post(url, json=payload, stream=True)
129
- response.raise_for_status()
130
-
131
- client = sseclient.SSEClient(response)
132
- for event in client.events():
133
- yield json.loads(event.data)
134
-
135
- def clear_screen():
136
- os.system('cls' if os.name == 'nt' else 'clear')
137
-
138
- def load_config(config_path: str = "client_config.yaml") -> dict:
139
- try:
140
- with open(config_path, 'r') as f:
141
- config = yaml.safe_load(f)
142
- return config
143
- except Exception as e:
144
- print(f"Warning: Could not load config file: {str(e)}")
145
- print("Using default configuration.")
146
- return DEFAULT_CONFIG
147
-
148
-
149
-
150
- def main():
151
- config = load_config()
152
- client = LLMClient(config)
153
-
154
- while True:
155
- clear_screen()
156
- print("\nLLM Engine Client")
157
- print("================")
158
- print(f"Server: {client.base_url}")
159
- print(f"Current Model: {config['model']['name']}")
160
- print("\nOptions:")
161
- print("1. Download Model")
162
- print("2. Convert Model")
163
- print("3. Initialize Model")
164
- print("4. Generate Text (Streaming)")
165
- print("5. Exit")
166
-
167
- choice = input("\nEnter your choice (1-5): ").strip()
168
-
169
- if choice == "1":
170
- try:
171
- print("\nDownload Model")
172
- print("==============")
173
- print(f"Default location: {config['model']['download_location']}")
174
- if input("\nUse default? (Y/n): ").lower() != 'n':
175
- repo_id = config['model']['download_location']
176
- else:
177
- repo_id = input("Enter download location: ").strip()
178
-
179
- access_token = input("Enter HF access token (or press Enter to use HF_TOKEN env var): ").strip() or None
180
- client.download_model(repo_id=repo_id, access_token=access_token)
181
- print("\nModel downloaded successfully!")
182
- input("\nPress Enter to continue...")
183
-
184
- except Exception as e:
185
- print(f"\nError: {str(e)}")
186
- input("\nPress Enter to continue...")
187
-
188
- elif choice == "2":
189
- try:
190
- print("\nConvert Model")
191
- print("=============")
192
- print(f"Default folder path: {config['model']['folder_path']}")
193
- print(f"Default model name: {config['model']['name']}")
194
- if input("\nUse defaults? (Y/n): ").lower() != 'n':
195
- folder_path = config['model']['folder_path']
196
- model_name = config['model']['name']
197
- else:
198
- folder_path = input("Enter folder path: ").strip()
199
- model_name = input("Enter model name: ").strip()
200
-
201
- client.convert_model(
202
- folder_path=folder_path,
203
- model_name=model_name
204
- )
205
- print("\nModel converted successfully!")
206
- input("\nPress Enter to continue...")
207
-
208
- except Exception as e:
209
- print(f"\nError: {str(e)}")
210
- input("\nPress Enter to continue...")
211
-
212
- elif choice == "3":
213
- try:
214
- print("\nInitialize Model")
215
- print("================")
216
- print(f"Default folder path: {config['model']['folder_path']}")
217
- if input("\nUse defaults? (Y/n): ").lower() != 'n':
218
- result = client.initialize_model()
219
- else:
220
- folder_path = input("Enter model folder path: ").strip()
221
- mode = input("Enter mode (cpu/gpu): ").strip()
222
- result = client.initialize_model(
223
- folder_path=folder_path,
224
- mode=mode
225
- )
226
- print("\nSuccess! Model initialized.")
227
- print(json.dumps(result, indent=2))
228
- input("\nPress Enter to continue...")
229
-
230
- except Exception as e:
231
- print(f"\nError: {str(e)}")
232
- input("\nPress Enter to continue...")
233
-
234
- elif choice == "4":
235
- try:
236
- print("\nGenerate Text (Streaming)")
237
- print("========================")
238
- prompt = input("Enter your prompt: ").strip()
239
-
240
- print("\nGenerating (Ctrl+C to stop)...")
241
- print("\nResponse:")
242
- try:
243
- for chunk in client.generate_stream(prompt=prompt):
244
- if "error" in chunk:
245
- print(f"\nError: {chunk['error']}")
246
- break
247
-
248
- token = chunk.get("token", "")
249
- is_finished = chunk.get("metadata", {}).get("is_finished", False)
250
-
251
- if is_finished:
252
- print("\n[Generation Complete]")
253
- break
254
-
255
- print(token, end="", flush=True)
256
-
257
- except KeyboardInterrupt:
258
- print("\n\n[Generation Stopped]")
259
-
260
- input("\nPress Enter to continue...")
261
-
262
- except Exception as e:
263
- print(f"\nError: {str(e)}")
264
- input("\nPress Enter to continue...")
265
-
266
- elif choice == "5":
267
- print("\nGoodbye!")
268
- break
269
-
270
- else:
271
- print("\nInvalid choice. Please try again.")
272
- input("\nPress Enter to continue...")
273
-
274
- if __name__ == "__main__":
275
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
client/client_config.yaml DELETED
@@ -1,33 +0,0 @@
1
- # Project Configuration
2
- project:
3
- root_dir: ".."
4
- checkpoints_dir: "checkpoints"
5
-
6
- # Server Configuration
7
- server:
8
- url: "http://localhost:7860"
9
-
10
- # Model Configuration
11
- model:
12
- name: "Llama-3.2-3B"
13
- download_location: "huihui-ai/Llama-3.2-3B-Instruct-abliterated"
14
- folder_path: "huihui-ai/Llama-3.2-3B-Instruct-abliterated"
15
- model_filename: "lit_model.pth"
16
- config_filename: "config.json"
17
- tokenizer_filename: "tokenizer.json"
18
-
19
- # Hardware Configuration
20
- hardware:
21
- mode: "gpu"
22
- precision: "16-true"
23
- # Precision Options: "32-true", "16-mixed", "16-true", "bf16-mixed", "bf16-true"
24
- quantize: "bnb.int8"
25
- # Quantization Options: "bnb.nf4", "bnb.nf4-dq", "bnb.fp4", "bnb.fp4-dq", "bnb.int8"
26
- gpu_count: "auto"
27
-
28
- # Generation Parameters
29
- generation:
30
- max_new_tokens: 500
31
- temperature: 1.0
32
- top_k: null
33
- top_p: 1.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main/hf_downloader.py DELETED
@@ -1,97 +0,0 @@
1
- import os
2
- import argparse
3
- from transformers import AutoTokenizer, AutoModel
4
- from huggingface_hub import login, HfApi
5
- import logging
6
- from tqdm import tqdm
7
-
8
- # Set up logging
9
- logging.basicConfig(
10
- level=logging.INFO,
11
- format='%(asctime)s - %(levelname)s - %(message)s'
12
- )
13
- logger = logging.getLogger(__name__)
14
-
15
- def setup_auth(token):
16
- """Setup Hugging Face authentication"""
17
- try:
18
- login(token)
19
- logger.info("Successfully authenticated with Hugging Face")
20
- except Exception as e:
21
- logger.error(f"Authentication failed: {str(e)}")
22
- raise
23
-
24
- def list_models(pattern=None):
25
- """List available models matching the pattern"""
26
- try:
27
- api = HfApi()
28
- models = api.list_models(pattern=pattern, full=True)
29
- return [(model.modelId, model.downloads) for model in models]
30
- except Exception as e:
31
- logger.error(f"Failed to list models: {str(e)}")
32
- raise
33
-
34
- def download_model(model_name, output_dir):
35
- """Download model and tokenizer"""
36
- try:
37
- logger.info(f"Downloading model: {model_name}")
38
-
39
- # Create output directory if it doesn't exist
40
- os.makedirs(output_dir, exist_ok=True)
41
-
42
- # Download tokenizer
43
- logger.info("Downloading tokenizer...")
44
- tokenizer = AutoTokenizer.from_pretrained(model_name)
45
- tokenizer.save_pretrained(os.path.join(output_dir, model_name))
46
-
47
- # Download model
48
- logger.info("Downloading model...")
49
- model = AutoModel.from_pretrained(model_name)
50
- model.save_pretrained(os.path.join(output_dir, model_name))
51
-
52
- logger.info(f"Successfully downloaded {model_name} to {output_dir}")
53
- return True
54
- except Exception as e:
55
- logger.error(f"Failed to download model {model_name}: {str(e)}")
56
- raise
57
-
58
- def main():
59
- parser = argparse.ArgumentParser(description='Download models from Hugging Face')
60
- parser.add_argument('--token', type=str, help='Hugging Face API token')
61
- parser.add_argument('--model', type=str, help='Model name to download')
62
- parser.add_argument('--output', type=str, default='./models',
63
- help='Output directory for downloaded models')
64
- parser.add_argument('--search', type=str, help='Search pattern for models')
65
- parser.add_argument('--list', action='store_true',
66
- help='List available models matching the search pattern')
67
-
68
- args = parser.parse_args()
69
-
70
- try:
71
- # Setup authentication if token provided
72
- if args.token:
73
- setup_auth(args.token)
74
-
75
- # List models if requested
76
- if args.list:
77
- logger.info(f"Searching for models matching: {args.search}")
78
- models = list_models(args.search)
79
- print("\nAvailable models:")
80
- for model_id, downloads in sorted(models, key=lambda x: x[1], reverse=True):
81
- print(f"- {model_id} (Downloads: {downloads:,})")
82
- return
83
-
84
- # Download specific model
85
- if args.model:
86
- download_model(args.model, args.output)
87
- else:
88
- logger.error("Please specify a model to download using --model")
89
- return
90
-
91
- except KeyboardInterrupt:
92
- logger.info("\nOperation cancelled by user")
93
- except Exception as e:
94
- logger.error(f"An error occurred: {str(e)}")
95
-
96
- if __name__ == "__main__":
97
- main()