Sakalti commited on
Commit
be4b518
·
verified ·
1 Parent(s): 56f6207

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -4,10 +4,13 @@ from llama_cpp import Llama
4
  import requests
5
  from tqdm import tqdm
6
 
7
- # ご自身のモデルURLと保存先パス
8
  MODEL_URL = "https://huggingface.co/mradermacher/Saka-14B-GGUF/resolve/main/Saka-14B.Q4_K_M.gguf"
9
  MODEL_PATH = "models/Saka-14B.Q4_K_M.gguf"
10
 
 
 
 
11
  def download_model(url=MODEL_URL, path=MODEL_PATH):
12
  os.makedirs(os.path.dirname(path), exist_ok=True)
13
  if os.path.exists(path):
@@ -28,36 +31,49 @@ def download_model(url=MODEL_URL, path=MODEL_PATH):
28
  bar.update(size)
29
  print("モデルのダウンロードが完了しました。")
30
 
31
- # モデルのダウンロード(初回のみ実行)
32
  download_model()
33
 
34
  # モデルロード
35
  llm = Llama(model_path=MODEL_PATH)
36
 
37
- def generate_response(prompt, temperature, top_p, max_tokens):
 
 
 
 
 
 
 
 
 
 
 
38
  response = llm.create_completion(
39
  prompt=prompt,
40
  temperature=temperature,
41
  top_p=top_p,
42
  max_tokens=max_tokens,
43
- stop=["\n\n"]
44
  )
45
- return response.choices[0].text.strip()
46
 
47
  def chat_interface(user_input, history, temperature, top_p, max_tokens):
48
- if history is None:
49
  history = []
50
- history.append(("ユーザー", user_input))
51
- prompt = ""
52
- for speaker, text in history:
53
- prompt += f"{speaker}: {text}\n"
54
- prompt += "AI: "
55
- response = generate_response(prompt, temperature, top_p, max_tokens)
56
- history.append(("AI", response))
57
- return history, history
 
 
58
 
59
  with gr.Blocks() as demo:
60
- gr.Markdown("# Saka-14B GGUF 日本語チャット")
61
  chatbot = gr.Chatbot()
62
  user_input = gr.Textbox(placeholder="質問をどうぞ", label="あなたの入力")
63
 
 
4
  import requests
5
  from tqdm import tqdm
6
 
7
+ # モデル情報
8
  MODEL_URL = "https://huggingface.co/mradermacher/Saka-14B-GGUF/resolve/main/Saka-14B.Q4_K_M.gguf"
9
  MODEL_PATH = "models/Saka-14B.Q4_K_M.gguf"
10
 
11
+ # システムプロンプト(自由に変更してください)
12
+ SYSTEM_PROMPT = "あなたは丁寧で知的な日本語AIアシスタントです。ユーザーの質問にわかりやすく答えてください。"
13
+
14
  def download_model(url=MODEL_URL, path=MODEL_PATH):
15
  os.makedirs(os.path.dirname(path), exist_ok=True)
16
  if os.path.exists(path):
 
31
  bar.update(size)
32
  print("モデルのダウンロードが完了しました。")
33
 
34
+ # モデルダウンロード
35
  download_model()
36
 
37
  # モデルロード
38
  llm = Llama(model_path=MODEL_PATH)
39
 
40
+ def build_prompt(messages):
41
+ prompt = f"<|system|>\n{SYSTEM_PROMPT}\n"
42
+ for msg in messages:
43
+ if msg["role"] == "user":
44
+ prompt += f"<|user|>\n{msg['content']}\n"
45
+ elif msg["role"] == "assistant":
46
+ prompt += f"<|assistant|>\n{msg['content']}\n"
47
+ prompt += "<|assistant|>\n"
48
+ return prompt
49
+
50
+ def generate_response(messages, temperature, top_p, max_tokens):
51
+ prompt = build_prompt(messages)
52
  response = llm.create_completion(
53
  prompt=prompt,
54
  temperature=temperature,
55
  top_p=top_p,
56
  max_tokens=max_tokens,
57
+ stop=["<|user|>", "<|system|>", "<|assistant|>"]
58
  )
59
+ return response["choices"][0]["text"].strip()
60
 
61
  def chat_interface(user_input, history, temperature, top_p, max_tokens):
62
+ if history is None or len(history) == 0:
63
  history = []
64
+ history.append({"role": "user", "content": user_input})
65
+ response = generate_response(history, temperature, top_p, max_tokens)
66
+ history.append({"role": "assistant", "content": response})
67
+
68
+ chat_display = []
69
+ for msg in history:
70
+ role = "ユーザー" if msg["role"] == "user" else "AI"
71
+ chat_display.append((role, msg["content"]))
72
+
73
+ return chat_display, history
74
 
75
  with gr.Blocks() as demo:
76
+ gr.Markdown("# Saka-14B GGUF 日本語チャット(システムプロンプト+履歴対応)")
77
  chatbot = gr.Chatbot()
78
  user_input = gr.Textbox(placeholder="質問をどうぞ", label="あなたの入力")
79