Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,53 +4,34 @@ from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
|
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain_community.document_loaders import TextLoader
|
| 6 |
from langchain_text_splitters import CharacterTextSplitter
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
# 1.
|
| 10 |
-
# 确保你在 Settings -> Secrets 中设置了 HF_TOKEN
|
| 11 |
llm = HuggingFaceEndpoint(
|
| 12 |
repo_id="Qwen/Qwen2.5-7B-Instruct",
|
| 13 |
-
huggingfacehub_api_token=os.getenv("HF_TOKEN")
|
| 14 |
-
timeout=300
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# 2.
|
| 18 |
-
# 如果 knowledge.txt 不存在,先创建一个简单的,防止报错
|
| 19 |
if not os.path.exists("knowledge.txt"):
|
| 20 |
with open("knowledge.txt", "w", encoding="utf-8") as f:
|
| 21 |
-
f.write("
|
| 22 |
|
| 23 |
-
# 加载并切分文档
|
| 24 |
loader = TextLoader("knowledge.txt", encoding="utf-8")
|
| 25 |
-
|
| 26 |
-
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 27 |
-
docs = text_splitter.split_documents(documents)
|
| 28 |
-
|
| 29 |
-
# 3. 创建向量检索系统 (使用中文优化的 Embedding 模型)
|
| 30 |
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
|
| 31 |
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 35 |
-
llm=llm,
|
| 36 |
-
retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
def
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return response["result"]
|
| 45 |
except Exception as e:
|
| 46 |
-
return f"
|
| 47 |
-
|
| 48 |
-
# 启动 Gradio 界面
|
| 49 |
-
demo = gr.ChatInterface(
|
| 50 |
-
chat_fn,
|
| 51 |
-
title="我的全能私有大脑",
|
| 52 |
-
description="基于 Qwen 2.5 + RAG 技术。它会先查阅你的 knowledge.txt 再回答。"
|
| 53 |
-
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
demo.launch()
|
|
|
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain_community.document_loaders import TextLoader
|
| 6 |
from langchain_text_splitters import CharacterTextSplitter
|
| 7 |
+
# 修改这里:使用更稳定的导入路径
|
| 8 |
+
from langchain.chains.retrieval_qa.base import RetrievalQA
|
| 9 |
|
| 10 |
+
# 1. 引擎初始化
|
|
|
|
| 11 |
llm = HuggingFaceEndpoint(
|
| 12 |
repo_id="Qwen/Qwen2.5-7B-Instruct",
|
| 13 |
+
huggingfacehub_api_token=os.getenv("HF_TOKEN")
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# 2. 知识库加载
|
|
|
|
| 17 |
if not os.path.exists("knowledge.txt"):
|
| 18 |
with open("knowledge.txt", "w", encoding="utf-8") as f:
|
| 19 |
+
f.write("这里是你的私有大脑知识库。")
|
| 20 |
|
|
|
|
| 21 |
loader = TextLoader("knowledge.txt", encoding="utf-8")
|
| 22 |
+
docs = CharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(loader.load())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
|
| 24 |
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 25 |
|
| 26 |
+
# 3. 构建问答链
|
| 27 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever())
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# 4. 界面逻辑
|
| 30 |
+
def chat(msg, history):
|
| 31 |
try:
|
| 32 |
+
res = qa_chain.invoke({"query": msg})
|
| 33 |
+
return res["result"]
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
+
return f"运行出错:{str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
gr.ChatInterface(chat).launch()
|
|
|