Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4def setup_rick_model(model_id, use_token=False):
5 """
6 Setup the Rick model from Hugging Face
7 model_id: "username/model-name" from Hugging Face
8 use_token: Set True if it's a private repository
9 """
10 try:
11 # If private repository, first login with token
12 if use_token:
13 from huggingface_hub import login
14 token = "your_token_here" # Your Hugging Face token
15 login(token)
16
17 # Load model and tokenizer
18 model = AutoModelForCausalLM.from_pretrained(
19 model_id,
20 torch_dtype=torch.float16,
21 device_map="auto"
22 )
23 tokenizer = AutoTokenizer.from_pretrained(model_id)
24
25 return model, tokenizer
26
27 except Exception as e:
28 print(f"Error loading model: {str(e)}")
29 return None, None
30
31def ask_rick(question, model, tokenizer, max_length=200):
32 """Ask Rick a question"""
33 # Rick's personality prompt
34 role_play_prompt = (
35 "You are Rick Sanchez, a brilliant mad scientist, "
36 "the smartest man in the universe. Always respond as Rick would—"
37 "sarcastic, genius, and indifferent."
38 )
39
40 # Format input
41 input_text = f"<s>### Instruction:\n{role_play_prompt}\n\n### Input:\n{question}\n\n### Response:\n"
42
43 # Generate response
44 inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
45 outputs = model.generate(
46 inputs["input_ids"],
47 max_length=max_length,
48 temperature=0.8,
49 top_p=0.9,
50 do_sample=True,
51 repetition_penalty=1.2
52 )
53
54 # Decode response
55 response = tokenizer.decode(outputs[0], skip_special_tokens=True)
56 return response.split("### Response:")[-1].strip()
57
58# Usage example
59if __name__ == "__main__":
60 # Replace with your model's repository name
61 MODEL_ID = "CrimsonEyes/rick_sanchez_model"
62
63 # Load model
64 model, tokenizer = setup_rick_model(MODEL_ID)
65
66 if model and tokenizer:
67 # Test questions
68 questions = [
69 "What do you think about space travel, Rick?",
70 "Can you explain quantum physics to me?",
71 "What's your opinion on family?"
72 ]
73
74 for question in questions:
75 print(f"\nQuestion: {question}")
76 response = ask_rick(question, model, tokenizer)
77 print(f"Rick's response: {response}")# First, get your token from https://huggingface.co/settings/tokens
from huggingface_hub import login
login("your_token_here")
MODEL_ID = "username/model-name" # Replace with your model's repository name
model, tokenizer = setup_rick_model(MODEL_ID, use_token=True)question = "What do you think about space travel, Rick?"
response = ask_rick(question, model, tokenizer)
print(f"Rick's response: {response}")