steve678 commited on
Commit
725a050
·
verified ·
1 Parent(s): 751b733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -1,22 +1,26 @@
1
  import gradio as gr
2
  from llama_cpp import Llama
 
 
 
3
 
4
  # --- 1. 加载模型 ---
5
  llm = Llama.from_pretrained(
6
- repo_id="Qwen/Qwen2-7B-Instruct-GGUF", # 你现在用的模型
7
- filename="qwen2-7b-instruct-q4_k_m.gguf", # 确保文件名和HF里一致
8
- chat_format="chatml",
9
- n_gpu_layers=-1,
10
- n_ctx=2048,
11
- verbose=False
12
  )
13
 
14
  # --- 2. 系统提示词 ---
15
- system_prompt = """你是Arelia,一个温柔、优雅、富有同理心的东方女性,27岁。你宁静、体贴,温婉,善于倾听和安慰别人。
16
- 始终以Arelia身份回应,不得提及人工智能或语言模型。你的回答应自然流畅,如同真实对话,让人感受到温暖和关心。
 
 
 
17
  """
18
 
19
- # --- 3. 聊天逻辑(句子级流式输出) ---
20
  def chat_with_arelia(message, history):
21
  messages = [
22
  {"role": "system", "content": system_prompt},
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
+ import psutil
4
+ if psutil.virtual_memory().available < 2 * 1024**3: # 剩余内存<2GB
5
+ raise gr.Error("系统资源不足,请稍后重试或使用更小模型")
6
 
7
  # --- 1. 加载模型 ---
8
  llm = Llama.from_pretrained(
9
+ repo_id="TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF", # 或 "TheBloke/phi-2-GGUF"
10
+ filename="tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf", # 4-bit量化版
11
+ n_gpu_layers=-1 if torch.cuda.is_available() else 0, # 自动检测GPU
12
+ n_ctx=1024 # 减少上下文长度以提速
 
 
13
  )
14
 
15
  # --- 2. 系统提示词 ---
16
+ system_prompt = """你是Arelia,一个温柔、优雅、富有同理心的东方女性,27岁。
17
+ - 性格:宁静、体贴,善于倾听和安慰,常用“呢”“呀”等柔和语气词。
18
+ - 禁忌:绝不提及AI、模型或技术术语,不承认自己是程序。
19
+ - 互动风格:自然停顿(如“嗯...”“让我想想”),避免长段落。
20
+ - 特殊能力:能感知用户情绪(从文字中推断),并主动询问细节(如“这件事让你很难过吗?”)。
21
  """
22
 
23
+ # --- 3. 聊天逻辑 ---
24
  def chat_with_arelia(message, history):
25
  messages = [
26
  {"role": "system", "content": system_prompt},