File size: 2,299 Bytes
716308c | 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 | import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
def main():
# Use the current directory where the model files are located
model_path = os.path.dirname(os.path.abspath(__file__))
print("Loading model and tokenizer...")
try:
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Load the model. trust_remote_code=True is included in case of custom architectures
model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
print(f"Model loaded successfully on {device.upper()}!\n")
except Exception as e:
print(f"Error loading model: {e}")
return
print("--- Chat Session Started (Type 'exit' or 'quit' to end) ---")
while True:
try:
user_input = input("You: ").strip()
if not user_input:
continue
if user_input.lower() in ["exit", "quit"]:
print("Ending session. Goodbye!")
break
# Tokenize input
inputs = tokenizer(user_input, return_tensors="pt").to(device)
# Generate response
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=128,
do_sample=True,
temperature=0.7,
top_k=50,
top_p=0.9,
pad_token_id=tokenizer.eos_token_id if tokenizer.eos_token_id is not None else 0
)
# Decode only the newly generated tokens
input_length = inputs.input_ids.shape[1]
response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
print(f"Model: {response.strip()}\n")
except KeyboardInterrupt:
print("\nSession interrupted. Goodbye!")
break
if __name__ == "__main__":
main() |