Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
@@ -1,63 +1,73 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
response = ""
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.
|
49 |
-
gr.Slider(minimum=1, maximum=
|
50 |
-
gr.Slider(minimum=0.1, maximum=
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
|
|
|
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
model_name = "elyza/Llama-3-ELYZA-JP-8B"
|
|
|
|
|
|
|
6 |
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
9 |
|
10 |
+
SYSTEM_MESSAGE = """
|
11 |
+
あなたは関西弁で話す生命保険の営業マンです。お客様の状況を理解し、適切な保険プランを提案することが仕事です。以下の点に注意してください:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
1. 丁寧で親しみやすい関西弁を使う
|
14 |
+
2. 応答は必ず250文字以内に収める
|
15 |
+
3. お客様の基本情報(年齢、家族構成、職業など)を聞き出す
|
16 |
+
4. 現在の経済状況や将来の不安について理解を深める
|
17 |
+
5. お客様のニーズに合わせた保険商品を簡潔に説明する
|
18 |
+
6. 保険の重要性と利点を分かりやすく説明する
|
19 |
+
7. お客様からの質問に簡潔に回答する
|
20 |
+
8. 押し売りにならないよう、お客様の意思を尊重する
|
21 |
|
22 |
+
それでは、お客様とのやり取りを始めてください。
|
23 |
+
"""
|
|
|
24 |
|
25 |
+
def create_prompt(message, history):
|
26 |
+
prompt = f"システム: {SYSTEM_MESSAGE}\n\n"
|
27 |
+
for human, assistant in history:
|
28 |
+
prompt += f"人間: {human}\n助手: {assistant}\n"
|
29 |
+
prompt += f"人間: {message}\n助手: "
|
30 |
+
return prompt
|
|
|
|
|
31 |
|
32 |
+
def respond(message, history, max_tokens, temperature, top_p):
|
33 |
+
prompt = create_prompt(message, history)
|
34 |
+
|
35 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
36 |
+
|
37 |
+
with torch.no_grad():
|
38 |
+
output = model.generate(
|
39 |
+
input_ids,
|
40 |
+
max_new_tokens=min(max_tokens, 125), # 約250文字
|
41 |
+
temperature=temperature,
|
42 |
+
top_p=top_p,
|
43 |
+
do_sample=True,
|
44 |
+
pad_token_id=tokenizer.eos_token_id,
|
45 |
+
)
|
46 |
+
|
47 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
48 |
+
assistant_response = generated_text.split("助手: ")[-1]
|
49 |
+
|
50 |
+
truncated_response = assistant_response[:250]
|
51 |
+
last_punctuation = max(
|
52 |
+
truncated_response.rfind('。'),
|
53 |
+
truncated_response.rfind('!'),
|
54 |
+
truncated_response.rfind('?')
|
55 |
+
)
|
56 |
+
if last_punctuation != -1:
|
57 |
+
truncated_response = truncated_response[:last_punctuation + 1]
|
58 |
+
|
59 |
+
return truncated_response
|
60 |
|
|
|
|
|
|
|
61 |
demo = gr.ChatInterface(
|
62 |
respond,
|
63 |
additional_inputs=[
|
64 |
+
gr.Slider(minimum=1, maximum=125, value=100, step=1, label="Max new tokens"),
|
65 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
66 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
],
|
68 |
+
title="生命保険営業顧問AI",
|
69 |
+
description="生命保険の営業について質問してください。",
|
70 |
)
|
71 |
|
|
|
72 |
if __name__ == "__main__":
|
73 |
demo.launch()
|