| |
|
| | import torch |
| | import torch.nn.functional as F |
| | import tiktoken |
| | import os |
| |
|
| | |
| | |
| | |
| | model_path = "/content/yagiz_gpt_full_packaged.pt" |
| | device = 'cuda' if torch.cuda.is_available() else 'cpu' |
| | block_size = 512 |
| |
|
| | |
| | |
| | |
| | print(f"Device: {device}") |
| |
|
| | if not os.path.exists(model_path): |
| | raise FileNotFoundError(f"ERROR: File {model_path} not found. Please make sure the model is packaged correctly.") |
| |
|
| | print(f"Loading {model_path}...") |
| |
|
| | |
| | try: |
| | model = torch.jit.load(model_path, map_location=device) |
| | model.eval() |
| | print("Model loaded successfully!") |
| | except Exception as e: |
| | print(f"Failed to load the model: {e}") |
| | exit() |
| |
|
| | |
| | |
| | |
| | |
| | try: |
| | enc = tiktoken.get_encoding("gpt2") |
| | except: |
| | print("Tiktoken library missing. Installing...") |
| | os.system("pip install tiktoken") |
| | import tiktoken |
| | enc = tiktoken.get_encoding("gpt2") |
| |
|
| | |
| | encode = lambda s: enc.encode(s, allowed_special={"<|endoftext|>"}) |
| | decode = lambda l: enc.decode(l) |
| |
|
| | |
| | |
| | |
| | def generate_response(prompt, max_new_tokens=100): |
| | |
| | idx = torch.tensor([encode(prompt)], dtype=torch.long, device=device) |
| | |
| | |
| | for _ in range(max_new_tokens): |
| | |
| | idx_cond = idx if idx.size(1) <= block_size else idx[:, -block_size:] |
| | |
| | |
| | |
| | logits = model(idx_cond) |
| | |
| | |
| | logits = logits[:, -1, :] |
| | |
| | |
| | probs = F.softmax(logits, dim=-1) |
| | |
| | |
| | idx_next = torch.multinomial(probs, num_samples=1) |
| | |
| | |
| | idx = torch.cat((idx, idx_next), dim=1) |
| |
|
| | |
| | return decode(idx[0].tolist()) |
| |
|
| | |
| | |
| | |
| | print("\n" + "="*40) |
| | print("YAGIZ GPT (FULL PACKAGED) - READY") |
| | print("Type 'q' and press Enter to exit.") |
| | print("="*40 + "\n") |
| |
|
| | while True: |
| | user_input = input("Ask a question: ") |
| | if user_input.lower() == 'q': |
| | print("Exiting...") |
| | break |
| | |
| | |
| | prompt = f"Question: {user_input}\nAnswer:" |
| | |
| | print(">> Model is thinking...") |
| | try: |
| | response = generate_response(prompt) |
| | |
| | |
| | |
| | if "Answer:" in response: |
| | answer_only = response.split("Answer:")[-1].strip() |
| | else: |
| | answer_only = response |
| | |
| | print(f"\nAnswer: {answer_only}\n") |
| | print("-" * 30) |
| | |
| | except Exception as e: |
| | print(f"An error occurred: {e}") |
| |
|