|
import torch |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
|
|
# 1. Detect device (GPU or CPU) |
|
DEVICE = 0 if torch.cuda.is_available() else -1 |
|
|
|
# 2. Load tokenizer & model from Hugging Face Hub |
|
MODEL_NAME = "kshitijthakkar/loggenix_general" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True) |
|
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, trust_remote_code=True) |
|
|
|
# 3. Set up a text-generation pipeline (optional but convenient) |
|
generator = pipeline( |
|
"text-generation", |
|
model=model, |
|
tokenizer=tokenizer, |
|
device=DEVICE, # GPU if available, else CPU |
|
framework="pt" |
|
) |
|
|
|
# 4. Simple generate function |
|
def generate_text(prompt: str, max_length: int = 100, num_return_sequences: int = 1): |
|
output = generator( |
|
prompt, |
|
max_length=max_length, |
|
num_return_sequences=num_return_sequences, |
|
do_sample=True, # enables sampling |
|
top_k=50, # top-k sampling |
|
top_p=0.95, # nucleus sampling |
|
temperature=0.7 # adjust creativity |
|
) |
|
return [item['generated_text'] for item in output] |
|
|
|
# 5. Example usage |
|
if __name__ == "__main__": |
|
prompt = input("Enter your prompt: ") |
|
outputs = generate_text(prompt, max_length=512) |
|
for i, text in enumerate(outputs, 3): |
|
print(f"----- Generated Sequence {i} -----") |
|
print(text) |
|
print() |
|
|
|
|