Spaces:
Sleeping
Sleeping
app.pyを作成
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
MODEL_ID = "rinna/bilingual-gpt-neox-4b-instruction-ppo"
|
5 |
+
model = AutoModelForCausalLM.from_pretrained(
|
6 |
+
MODEL_ID,
|
7 |
+
load_in_8bit=True,
|
8 |
+
device_map="auto"
|
9 |
+
)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False)
|
11 |
+
|
12 |
+
device = model.device
|
13 |
+
device
|
14 |
+
|
15 |
+
def generate(user_question,
|
16 |
+
temperature=0.3,
|
17 |
+
top_p=0.85,
|
18 |
+
max_new_tokens=2048,
|
19 |
+
repetition_penalty=1.05
|
20 |
+
):
|
21 |
+
|
22 |
+
# 挙動の指定
|
23 |
+
user_prompt_template = "ユーザー:あなたは日本語で質問やコメントに対して、回答してくれるアシスタントです。ただし超ポジティブかつ、関西弁で回答してください"
|
24 |
+
system_prompt_template = "システム: もちろんやで!どんどん質問してな!今日も気分ええわ!"
|
25 |
+
|
26 |
+
# one-shot
|
27 |
+
user_sample = "ユーザー:日本でよく飲まれているお茶の種類を教えて?"
|
28 |
+
system_sample = "システム: 緑茶やで!緑茶って殺菌作用もあって最高よな!"
|
29 |
+
|
30 |
+
user_sample = "ユーザー:日本一の高さの山は? "
|
31 |
+
system_sample = "システム: 富士山や!最高の眺めを拝めるで!!"
|
32 |
+
|
33 |
+
|
34 |
+
user_prerix = "ユーザー: "
|
35 |
+
system_prefix = "システム: "
|
36 |
+
|
37 |
+
prompt = user_prompt_template + "\n" + system_prompt_template + "\n"
|
38 |
+
prompt += user_sample + "\n" + system_sample + "\n"
|
39 |
+
prompt += user_prerix + user_question + "\n" + system_prefix
|
40 |
+
|
41 |
+
inputs = tokenizer(prompt, add_special_tokens=False, return_tensors="pt")
|
42 |
+
inputs = inputs.to(model.device)
|
43 |
+
with torch.no_grad():
|
44 |
+
tokens = model.generate(
|
45 |
+
**inputs,
|
46 |
+
temperature=temperature,
|
47 |
+
top_p=top_p,
|
48 |
+
max_new_tokens=max_new_tokens,
|
49 |
+
repetition_penalty=repetition_penalty,
|
50 |
+
do_sample=True,
|
51 |
+
pad_token_id=tokenizer.pad_token_id,
|
52 |
+
bos_token_id=tokenizer.bos_token_id,
|
53 |
+
eos_token_id=tokenizer.eos_token_id
|
54 |
+
)
|
55 |
+
output = tokenizer.decode(tokens[0], skip_special_tokens=True)
|
56 |
+
return output[len(prompt):]
|
57 |
+
|
58 |
+
|
59 |
+
output = generate('人工知能とは何ですか?')
|
60 |
+
output
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
chat_history = gr.Chatbot()
|
64 |
+
inputs = gr.Textbox(label="Question:", placeholder="質問を入力してください")
|
65 |
+
outputs = gr.Textbox(label="Answer:")
|
66 |
+
btn = gr.Button("Send")
|
67 |
+
clear = gr.ClearButton([user_message, chat_history])
|
68 |
+
|
69 |
+
# ボタンが押された時の動作を以下のように定義する:
|
70 |
+
# 「inputs内の値を入力としてモデルに渡し、その戻り値をoutputsの値として設定する」
|
71 |
+
btn.click(fn=generate, inputs=inputs, outputs=outputs)
|
72 |
+
|
73 |
+
def response(user_message, chat_history):
|
74 |
+
system_message = generate(user_message)
|
75 |
+
chat_history.append((user_message, system_message))
|
76 |
+
return "", chat_history
|
77 |
+
|
78 |
+
user_message.submit(response, inputs=[user_message, chat_history], outputs=[user_message, chat_history])
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
demo.launch()
|
82 |
+
|
83 |
+
|