KIRA111B commited on
Commit
c5cf99a
·
verified ·
1 Parent(s): 816e8cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py (最终稳定版 - 使用 gr.Blocks)
2
  import gradio as gr
3
  from langchain.prompts import PromptTemplate
4
  from langchain_community.embeddings import HuggingFaceBgeEmbeddings
@@ -12,18 +12,21 @@ import time
12
  # --- 1. 配置 (保持不变) ---
13
  VECTOR_STORE_PATH = "vector_store"
14
  EMBEDDING_MODEL = "BAAI/bge-large-zh-v1.5"
 
15
  GGUF_MODEL_REPO = "TheBloke/CapybaraHermes-2.5-Mistral-7B-GGUF"
 
16
  GGUF_MODEL_FILE = "capybarahermes-2.5-mistral-7b.Q4_K_M.gguf"
17
 
18
  # --- 2. 加载RAG管道 (保持不变) ---
19
  def load_rag_chain():
20
- # ... (这部分代码和之前完全一样,无需修改) ...
21
  print("开始加载RAG管道...")
22
  embeddings = HuggingFaceBgeEmbeddings(model_name=EMBEDDING_MODEL, model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': True})
23
  if not os.path.exists(VECTOR_STORE_PATH): raise FileNotFoundError(f"错误:向量数据库 '{VECTOR_STORE_PATH}' 不存在!")
24
  vector_store = FAISS.load_local(VECTOR_STORE_PATH, embeddings, allow_dangerous_deserialization=True)
25
  model_path = hf_hub_download(repo_id=GGUF_MODEL_REPO, filename=GGUF_MODEL_FILE, local_dir="models")
26
  llm = LlamaCpp(model_path=model_path, n_gpu_layers=0, n_batch=512, n_ctx=4096, f16_kv=True, verbose=False)
 
 
27
  prompt_template = """<|im_start|>system
28
  You are a helpful assistant named "粤小智". Answer the user's question in Chinese based on the provided "Context".
29
  If the context is not sufficient, just say: "抱歉,关于您的问题,我的知识库暂时没有相关信息。". Do not make up answers.
@@ -41,10 +44,9 @@ Question:
41
  print("✅ RAG管道加载完毕!")
42
  return qa_chain
43
 
44
- # --- 3. Gradio应用逻辑 (修改以适配gr.Blocks) ---
45
  RAG_CHAIN = load_rag_chain()
46
 
47
- # history是Gradio自动管理的,格式为[ [user_msg1, bot_msg1], [user_msg2, bot_msg2], ... ]
48
  def user(user_message, history):
49
  # 将用户消息添加到聊天记录中,并返回一个空的输入框
50
  return "", history + [[user_message, None]]
@@ -58,7 +60,7 @@ def bot(history):
58
  result = RAG_CHAIN.invoke({"query": user_message})
59
  bot_message = result.get('result', "处理出错").strip()
60
 
61
- # 我们模拟打字效果,让体验更好
62
  history[-1][1] = ""
63
  for character in bot_message:
64
  history[-1][1] += character
@@ -90,6 +92,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}")
90
  bot, chatbot, chatbot
91
  )
92
 
93
- # 使用最简单的启动方式,但加入queue()来处理打字效果
94
  demo.queue()
95
  demo.launch()
 
1
+ # app.py (最终确认版 - 使用 gr.Blocks)
2
  import gradio as gr
3
  from langchain.prompts import PromptTemplate
4
  from langchain_community.embeddings import HuggingFaceBgeEmbeddings
 
12
  # --- 1. 配置 (保持不变) ---
13
  VECTOR_STORE_PATH = "vector_store"
14
  EMBEDDING_MODEL = "BAAI/bge-large-zh-v1.5"
15
+ # 切换到 CapybaraHermes-2.5-Mistral-7B 模型
16
  GGUF_MODEL_REPO = "TheBloke/CapybaraHermes-2.5-Mistral-7B-GGUF"
17
+ # 我们选择一个大小适中的4位量化版本
18
  GGUF_MODEL_FILE = "capybarahermes-2.5-mistral-7b.Q4_K_M.gguf"
19
 
20
  # --- 2. 加载RAG管道 (保持不变) ---
21
  def load_rag_chain():
 
22
  print("开始加载RAG管道...")
23
  embeddings = HuggingFaceBgeEmbeddings(model_name=EMBEDDING_MODEL, model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': True})
24
  if not os.path.exists(VECTOR_STORE_PATH): raise FileNotFoundError(f"错误:向量数据库 '{VECTOR_STORE_PATH}' 不存在!")
25
  vector_store = FAISS.load_local(VECTOR_STORE_PATH, embeddings, allow_dangerous_deserialization=True)
26
  model_path = hf_hub_download(repo_id=GGUF_MODEL_REPO, filename=GGUF_MODEL_FILE, local_dir="models")
27
  llm = LlamaCpp(model_path=model_path, n_gpu_layers=0, n_batch=512, n_ctx=4096, f16_kv=True, verbose=False)
28
+
29
+ # 使用为Mistral模型优化的Prompt模板
30
  prompt_template = """<|im_start|>system
31
  You are a helpful assistant named "粤小智". Answer the user's question in Chinese based on the provided "Context".
32
  If the context is not sufficient, just say: "抱歉,关于您的问题,我的知识库暂时没有相关信息。". Do not make up answers.
 
44
  print("✅ RAG管道加载完毕!")
45
  return qa_chain
46
 
47
+ # --- 3. Gradio应用逻辑 (适配gr.Blocks) ---
48
  RAG_CHAIN = load_rag_chain()
49
 
 
50
  def user(user_message, history):
51
  # 将用户消息添加到聊天记录中,并返回一个空的输入框
52
  return "", history + [[user_message, None]]
 
60
  result = RAG_CHAIN.invoke({"query": user_message})
61
  bot_message = result.get('result', "处理出错").strip()
62
 
63
+ # 模拟打字效果
64
  history[-1][1] = ""
65
  for character in bot_message:
66
  history[-1][1] += character
 
92
  bot, chatbot, chatbot
93
  )
94
 
95
+ # 使用queue()来处理流式(打字效果)输出
96
  demo.queue()
97
  demo.launch()