mkshing commited on
Commit
0bb16c0
•
1 Parent(s): f81cc8f

first commit

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +71 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Rinna Japanese Gpt Neox 3.6b Instruction Sft
3
  emoji: 👀
4
  colorFrom: gray
5
  colorTo: yellow
 
1
  ---
2
+ title: Chat with `rinna/japanese-gpt-neox-3.6b-instruction-sft`
3
  emoji: 👀
4
  colorFrom: gray
5
  colorTo: yellow
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import itertools
2
+
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ import gradio as gr
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ print(f"device: {device}")
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained("rinna/japanese-gpt-neox-3.6b-instruction-sft", use_fast=False)
11
+ model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt-neox-3.6b-instruction-sft", device_map="auto", torch_dtype=torch.float16)
12
+ model = model.to(device)
13
+
14
+
15
+ @torch.no_grad()
16
+ def inference_func(prompt, max_new_tokens=128, temperature=0.7):
17
+ token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
18
+ output_ids = model.generate(
19
+ token_ids.to(model.device),
20
+ do_sample=True,
21
+ max_new_tokens=max_new_tokens,
22
+ temperature=temperature,
23
+ pad_token_id=tokenizer.pad_token_id,
24
+ bos_token_id=tokenizer.bos_token_id,
25
+ eos_token_id=tokenizer.eos_token_id
26
+ )
27
+ output = tokenizer.decode(output_ids.tolist()[0][token_ids.size(1):], skip_special_tokens=True)
28
+ output = output.replace("<NL>", "\n")
29
+ return output
30
+
31
+
32
+ def make_prompt(message, chat_history, max_context_size: int = 10):
33
+ contexts = chat_history + [[message, ""]]
34
+ contexts = list(itertools.chain.from_iterable(contexts))
35
+ if max_context_size > 0:
36
+ context_size = max_context_size - 1
37
+ else:
38
+ context_size = 100000
39
+ contexts = contexts[-context_size:]
40
+ prompt = []
41
+ for idx, context in enumerate(reversed(contexts)):
42
+ if idx % 2 == 0:
43
+ prompt = [f"システム: {context}"] + prompt
44
+ else:
45
+ prompt = [f"ユーザー: {context}"] + prompt
46
+ prompt = "<NL>".join(prompt)
47
+ return prompt
48
+
49
+ def interact_func(message, chat_history, max_context_size, max_new_tokens, temperature):
50
+ prompt = make_prompt(message, chat_history, max_context_size)
51
+ print(f"prompt: {prompt}")
52
+ generated = inference_func(prompt, max_new_tokens, temperature)
53
+ print(f"generated: {generated}")
54
+ chat_history.append((message, generated))
55
+ return "", chat_history
56
+
57
+
58
+ with gr.Blocks() as demo:
59
+ with gr.Accordion("Configs", open=False):
60
+ # max_context_size = the number of turns * 2
61
+ max_context_size = gr.Number(value=10, label="max_context_size", precision=0)
62
+ max_new_tokens = gr.Number(value=128, label="max_new_tokens", precision=0)
63
+ temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.1, label="temperature")
64
+ chatbot = gr.Chatbot()
65
+ msg = gr.Textbox()
66
+ clear = gr.Button("Clear")
67
+ msg.submit(interact_func, [msg, chatbot, max_context_size, max_new_tokens, temperature], [msg, chatbot])
68
+ clear.click(lambda: None, None, chatbot, queue=False)
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ sentencepiece
3
+ gradio
4
+ accelerate>=0.12.0
5
+ bitsandbytes>=0.31.5