LH-Tech-AI commited on
Commit
af0d029
·
verified ·
1 Parent(s): ee2639e

Create use.py

Browse files
Files changed (1) hide show
  1. use.py +46 -0
use.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import tiktoken
3
+ import os
4
+ from model import GPTConfig, GPT
5
+
6
+ out_dir = 'out'
7
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
8
+ ckpt_path = os.path.join(out_dir, 'ckpt.pt')
9
+ checkpoint = torch.load(ckpt_path, map_location=device)
10
+ gptconf = GPTConfig(**checkpoint['model_args'])
11
+ model = GPT(gptconf)
12
+ state_dict = checkpoint['model']
13
+ unwanted_prefix = '_orig_mod.'
14
+ for k,v in list(state_dict.items()):
15
+ if k.startswith(unwanted_prefix):
16
+ state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
17
+ model.load_state_dict(state_dict)
18
+ model.to(device)
19
+ model.eval()
20
+
21
+ enc = tiktoken.get_encoding("gpt2")
22
+ EOS_TOKEN_ID = 50256
23
+
24
+ def ask_gpt(prompt, max_new_tokens=150, temperature=0.7, top_k=25):
25
+ start_ids = enc.encode(prompt)
26
+ x = torch.tensor(start_ids, dtype=torch.long, device=device)[None, ...]
27
+
28
+ with torch.no_grad():
29
+ y = model.generate(x, max_new_tokens, temperature=temperature, top_k=top_k)
30
+
31
+ full_ids = y[0].tolist()
32
+ new_ids = full_ids[len(start_ids):]
33
+
34
+ response = enc.decode(new_ids)
35
+ response = response.split('<|endoftext|>')[0]
36
+ return response
37
+
38
+ print("--- Crest Completion Chat started ---")
39
+ while True:
40
+ user_input = input("\nYour Prompt: ")
41
+ if user_input.lower() in ['exit', 'quit']: break
42
+
43
+ antwort_rest = ask_gpt(user_input)
44
+
45
+ print(f"\nCrest Completion: {user_input}{antwort_rest}")
46
+ print("-" * 30)