File size: 10,236 Bytes
9426882 |
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
"""
Helion-V1.5-XL Usage Examples
Demonstrates various use cases and configurations
"""
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
# Initialize model and tokenizer
MODEL_NAME = "DeepXR/Helion-V1.5-XL"
def load_model(quantization="none"):
"""Load model with optional quantization"""
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
if quantization == "4bit":
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4"
)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
quantization_config=quantization_config,
device_map="auto",
trust_remote_code=True
)
else:
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
)
return model, tokenizer
# Example 1: Simple Text Generation
def example_simple_generation():
"""Basic text generation example"""
print("\n" + "="*80)
print("EXAMPLE 1: Simple Text Generation")
print("="*80)
model, tokenizer = load_model()
prompt = "Explain the concept of neural networks in simple terms:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.7,
top_p=0.9,
do_sample=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\nPrompt: {prompt}")
print(f"\nResponse: {response[len(prompt):]}")
# Example 2: Chat Conversation
def example_chat_conversation():
"""Multi-turn conversation example"""
print("\n" + "="*80)
print("EXAMPLE 2: Chat Conversation")
print("="*80)
model, tokenizer = load_model()
conversation = [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "What are the main benefits of renewable energy?"},
]
prompt = tokenizer.apply_chat_template(
conversation,
tokenize=False,
add_generation_prompt=True
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.7)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\nConversation:\n{response}")
# Example 3: Code Generation
def example_code_generation():
"""Code generation example"""
print("\n" + "="*80)
print("EXAMPLE 3: Code Generation")
print("="*80)
model, tokenizer = load_model()
prompt = """Write a Python function that finds the longest palindromic substring:
def longest_palindrome(s: str) -> str:"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.2, # Lower temperature for code
top_p=0.95,
do_sample=True
)
code = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\nGenerated Code:\n{code}")
# Example 4: Structured Output (JSON)
def example_structured_output():
"""Generate structured JSON output"""
print("\n" + "="*80)
print("EXAMPLE 4: Structured JSON Output")
print("="*80)
model, tokenizer = load_model()
prompt = """Generate a JSON object describing a fictional book:
{
"title": "The Last Algorithm",
"author": """
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.4,
top_p=0.9
)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\nGenerated JSON:\n{result}")
# Example 5: Batch Processing
def example_batch_processing():
"""Process multiple prompts in batch"""
print("\n" + "="*80)
print("EXAMPLE 5: Batch Processing")
print("="*80)
model, tokenizer = load_model()
prompts = [
"List three benefits of exercise:",
"What is quantum computing?",
"Explain photosynthesis briefly:"
]
inputs = tokenizer(
prompts,
return_tensors="pt",
padding=True,
truncation=True
).to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=128,
temperature=0.7,
do_sample=True
)
for i, output in enumerate(outputs):
response = tokenizer.decode(output, skip_special_tokens=True)
print(f"\nPrompt {i+1}: {prompts[i]}")
print(f"Response: {response[len(prompts[i]):]}\n")
# Example 6: Creative Writing
def example_creative_writing():
"""Creative writing with higher temperature"""
print("\n" + "="*80)
print("EXAMPLE 6: Creative Writing")
print("="*80)
model, tokenizer = load_model()
prompt = "Write the opening paragraph of a science fiction story:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.9, # Higher for creativity
top_p=0.95,
top_k=100,
repetition_penalty=1.15,
do_sample=True
)
story = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\n{story}")
# Example 7: Using Pipeline API
def example_pipeline_api():
"""Use the transformers pipeline API"""
print("\n" + "="*80)
print("EXAMPLE 7: Pipeline API")
print("="*80)
generator = pipeline(
"text-generation",
model=MODEL_NAME,
torch_dtype=torch.bfloat16,
device_map="auto"
)
results = generator(
"The future of artificial intelligence is",
max_new_tokens=200,
temperature=0.7,
top_p=0.9,
num_return_sequences=1
)
print(f"\nGenerated text:\n{results[0]['generated_text']}")
# Example 8: Streaming Generation
def example_streaming_generation():
"""Generate text with streaming (token by token)"""
print("\n" + "="*80)
print("EXAMPLE 8: Streaming Generation")
print("="*80)
from transformers import TextIteratorStreamer
from threading import Thread
model, tokenizer = load_model()
prompt = "Explain machine learning in three sentences:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
generation_kwargs = dict(
**inputs,
max_new_tokens=256,
temperature=0.7,
streamer=streamer
)
thread = Thread(target=model.generate, kwargs=generation_kwargs)
thread.start()
print(f"\nPrompt: {prompt}\n\nResponse (streaming): ", end="")
for new_text in streamer:
print(new_text, end="", flush=True)
print("\n")
thread.join()
# Example 9: Few-Shot Learning
def example_few_shot():
"""Few-shot learning example"""
print("\n" + "="*80)
print("EXAMPLE 9: Few-Shot Learning")
print("="*80)
model, tokenizer = load_model()
prompt = """Translate English to French:
English: Hello, how are you?
French: Bonjour, comment allez-vous?
English: What is your name?
French: Comment vous appelez-vous?
English: I love programming.
French:"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=50, temperature=0.3)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\n{result}")
# Example 10: Custom Generation Parameters
def example_custom_parameters():
"""Advanced generation parameter tuning"""
print("\n" + "="*80)
print("EXAMPLE 10: Custom Generation Parameters")
print("="*80)
model, tokenizer = load_model()
prompt = "Write a haiku about technology:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# Multiple generations with different parameters
configs = [
{"name": "Conservative", "temperature": 0.3, "top_p": 0.9, "top_k": 30},
{"name": "Balanced", "temperature": 0.7, "top_p": 0.9, "top_k": 50},
{"name": "Creative", "temperature": 1.0, "top_p": 0.95, "top_k": 100},
]
for config in configs:
outputs = model.generate(
**inputs,
max_new_tokens=128,
temperature=config["temperature"],
top_p=config["top_p"],
top_k=config["top_k"],
do_sample=True
)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"\n{config['name']} (temp={config['temperature']}):")
print(result[len(prompt):])
def main():
"""Run all examples"""
print("\n" + "="*80)
print("HELION-V1.5-XL USAGE EXAMPLES")
print("="*80)
examples = [
("Simple Generation", example_simple_generation),
("Chat Conversation", example_chat_conversation),
("Code Generation", example_code_generation),
("Structured Output", example_structured_output),
("Batch Processing", example_batch_processing),
("Creative Writing", example_creative_writing),
("Pipeline API", example_pipeline_api),
("Streaming Generation", example_streaming_generation),
("Few-Shot Learning", example_few_shot),
("Custom Parameters", example_custom_parameters),
]
print("\nAvailable examples:")
for i, (name, _) in enumerate(examples, 1):
print(f" {i}. {name}")
print("\nRun individual examples or all examples.")
print("Example: python example_usage.py")
# Uncomment to run specific examples
# example_simple_generation()
# example_chat_conversation()
# example_code_generation()
if __name__ == "__main__":
main() |