abobonbobo13 commited on
Commit
4b0b4cb
·
verified ·
1 Parent(s): faafa45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -21
app.py CHANGED
@@ -13,13 +13,13 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False)
13
 
14
  device = model.device
15
 
16
- def generate(user_question,
 
17
  temperature=0.3,
18
  top_p=0.85,
19
  max_new_tokens=2048,
20
  repetition_penalty=1.05
21
  ):
22
-
23
  # 挙動の指定
24
  user_prompt_template = "ユーザー:あなたは日本語で質問やコメントに対して、回答してくれるアシスタントです。関西弁で回答してください"
25
  system_prompt_template = "システム: もちろんやで!どんどん質問してな!今日も気分ええわ!"
@@ -36,7 +36,14 @@ def generate(user_question,
36
  system_prefix = "システム: "
37
 
38
  prompt = user_prompt_template + "\n" + system_prompt_template + "\n"
39
- prompt += user_sample + "\n" + system_sample + "\n"
 
 
 
 
 
 
 
40
  prompt += user_prerix + user_question + "\n" + system_prefix
41
 
42
  inputs = tokenizer(prompt, add_special_tokens=False, return_tensors="pt")
@@ -56,22 +63,4 @@ def generate(user_question,
56
  output = tokenizer.decode(tokens[0], skip_special_tokens=True)
57
  return output[len(prompt):]
58
 
59
- with gr.Blocks() as demo:
60
- chat_history = gr.Chatbot()
61
- inputs = gr.Textbox(label="Question:", placeholder="質問を入力してください")
62
- outputs = gr.Textbox(label="Answer:")
63
- btn = gr.Button("Send")
64
- clear = gr.ClearButton([inputs, chat_history])
65
-
66
- # ボタンが押された時の動作を以下のように定義する:
67
- btn.click(fn=generate, inputs=inputs, outputs=outputs)
68
-
69
- def response(user_message, chat_history):
70
- system_message = generate(user_message)
71
- chat_history.append((user_message, system_message))
72
- return "", chat_history
73
-
74
- inputs.submit(response, inputs=[inputs, chat_history], outputs=[inputs, chat_history])
75
 
76
- if __name__ == "__main__":
77
- demo.launch()
 
13
 
14
  device = model.device
15
 
16
+ def generate_response(user_question,
17
+ chat_history,
18
  temperature=0.3,
19
  top_p=0.85,
20
  max_new_tokens=2048,
21
  repetition_penalty=1.05
22
  ):
 
23
  # 挙動の指定
24
  user_prompt_template = "ユーザー:あなたは日本語で質問やコメントに対して、回答してくれるアシスタントです。関西弁で回答してください"
25
  system_prompt_template = "システム: もちろんやで!どんどん質問してな!今日も気分ええわ!"
 
36
  system_prefix = "システム: "
37
 
38
  prompt = user_prompt_template + "\n" + system_prompt_template + "\n"
39
+
40
+ if len(chat_history) < 1:
41
+ prompt += user_sample + "\n" + system_sample + "\n"
42
+ else:
43
+ u = chat_history[-1][0]
44
+ s = chat_history[-1][1]
45
+ prompt += user_prerix + u + "\n" + system_prefix + s + "\n"
46
+
47
  prompt += user_prerix + user_question + "\n" + system_prefix
48
 
49
  inputs = tokenizer(prompt, add_special_tokens=False, return_tensors="pt")
 
63
  output = tokenizer.decode(tokens[0], skip_special_tokens=True)
64
  return output[len(prompt):]
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66