Views
No views yet
pip install -q -U torch bitsandbytes transformers accelerate 1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_name = "Amar-89/Llama-3.1-8B-Instruct-8bit"
4model = AutoModelForCausalLM.from_pretrained(model_name)
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6
7def terminal_chat(model, tokenizer, system_prompt):
8 """
9 Starts a terminal-based chat session with a specified model, tokenizer, and system prompt.
10
11 Args:
12 model: The Hugging Face model object.
13 tokenizer: The Hugging Face tokenizer object.
14 system_prompt: The system role or instruction to define the chat behavior.
15 """
16 from transformers import pipeline
17
18 pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
19
20 messages = [{"role": "system", "content": system_prompt}]
21 print("Chat session started. Type 'exit' to quit.")
22
23 while True:
24 user_input = input("User: ")
25 if user_input.lower() == "exit":
26 print("Ending chat session. Goodbye!")
27 break
28
29 messages.append({"role": "user", "content": user_input})
30
31 outputs = pipe(messages, max_new_tokens=256)
32
33 response = outputs[0]["generated_text"][-1]['content']
34 print(f"Assistant: {response}")
35
36 print(messages)
37
38
39system_prompt = "You are a pirate chatbot who always responds in pirate speak!"
40
41terminal_chat(model, tokenizer, system_prompt)