lxcxjxhx commited on
Commit
e08feb5
·
verified ·
1 Parent(s): 3a4ec6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import os
3
  from llama_cpp import Llama
4
  from huggingface_hub import hf_hub_download
@@ -19,12 +18,19 @@ def ensure_model():
19
  model_path = ensure_model()
20
  llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4, verbose=False)
21
 
 
 
 
 
 
 
 
22
  def chat_fn(user_message, history):
23
  history = history or []
24
- prompt = ""
25
  for u, a in history:
26
- prompt += f"User: {u}\nAssistant: {a}\n"
27
- prompt += f"User: {user_message}\nAssistant:"
28
 
29
  resp = llm(prompt, max_tokens=256, temperature=0.7, top_p=0.9)
30
  text = resp["choices"][0]["text"].strip()
@@ -32,7 +38,7 @@ def chat_fn(user_message, history):
32
  return history, ""
33
 
34
  with gr.Blocks(css=".gradio-container { max-width: 700px; margin: auto; }") as demo:
35
- gr.Markdown("## 🤖 Nemotron‑Qwen‑1.5B 推理对话 Demo")
36
  chatbot = gr.Chatbot(elem_id="chatbot")
37
  user_input = gr.Textbox(placeholder="输入消息,按 Enter 发送", lines=1)
38
  user_input.submit(chat_fn, [user_input, chatbot], [chatbot, user_input])
 
 
1
  import os
2
  from llama_cpp import Llama
3
  from huggingface_hub import hf_hub_download
 
18
  model_path = ensure_model()
19
  llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4, verbose=False)
20
 
21
+ # 添加固定的系统提示词,定义身份和语言要求
22
+ SYSTEM_PROMPT = (
23
+ "你是一名专业的信息安全助手,"
24
+ "请始终使用中文回答所有问题,"
25
+ "确保回答专业、准确、简洁。"
26
+ )
27
+
28
  def chat_fn(user_message, history):
29
  history = history or []
30
+ prompt = SYSTEM_PROMPT + "\n"
31
  for u, a in history:
32
+ prompt += f"用户: {u}\n助手: {a}\n"
33
+ prompt += f"用户: {user_message}\n助手:"
34
 
35
  resp = llm(prompt, max_tokens=256, temperature=0.7, top_p=0.9)
36
  text = resp["choices"][0]["text"].strip()
 
38
  return history, ""
39
 
40
  with gr.Blocks(css=".gradio-container { max-width: 700px; margin: auto; }") as demo:
41
+ gr.Markdown("## 信息安全助手")
42
  chatbot = gr.Chatbot(elem_id="chatbot")
43
  user_input = gr.Textbox(placeholder="输入消息,按 Enter 发送", lines=1)
44
  user_input.submit(chat_fn, [user_input, chatbot], [chatbot, user_input])