Spaces:
Sleeping
Sleeping
增强AI助手功能:添加学习中心支持聊天记录上传和知识文档投喂
Browse files- README.md +20 -1
- app.py +149 -24
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -12,4 +12,23 @@ hf_oauth_scopes:
|
|
| 12 |
- inference-api
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
- inference-api
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# 我的AI助手
|
| 16 |
+
|
| 17 |
+
一个支持持续学习的中文聊天机器人,具备以下功能:
|
| 18 |
+
|
| 19 |
+
## 主要特性
|
| 20 |
+
- 🗣️ 中文对话交流
|
| 21 |
+
- 📚 知识学习能力
|
| 22 |
+
- 📝 聊天记录上传学习
|
| 23 |
+
- 📄 知识文档投喂学习
|
| 24 |
+
- 🔄 对话历史记录
|
| 25 |
+
|
| 26 |
+
## 使用说明
|
| 27 |
+
1. 在"聊天"标签页中与AI助手进行对话
|
| 28 |
+
2. 在"学习中心"标签页中上传聊天记录或知识文档
|
| 29 |
+
3. AI助手会根据上传的材料不断改进回答质量
|
| 30 |
+
|
| 31 |
+
## 技术细节
|
| 32 |
+
- 使用 THUDM/chatglm3-6b 模型
|
| 33 |
+
- 基于 Gradio 构建界面
|
| 34 |
+
- 支持 Hugging Face OAuth 认证
|
app.py
CHANGED
|
@@ -1,7 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
|
@@ -14,6 +45,14 @@ def respond(
|
|
| 14 |
"""
|
| 15 |
更多关于 `huggingface_hub` 推理API的信息,请查看文档: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# 使用一个更适合中文的模型
|
| 18 |
client = InferenceClient(token=hf_token.token, model="THUDM/chatglm3-6b")
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -37,36 +76,122 @@ def respond(
|
|
| 37 |
|
| 38 |
response += token
|
| 39 |
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
"""
|
| 43 |
有关如何自定义聊天界面的信息,请查看gradio文档: https://www.gradio.app/docs/chatinterface
|
| 44 |
"""
|
| 45 |
-
chatbot = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
type="messages",
|
| 48 |
-
title="我的AI助手",
|
| 49 |
-
description="与AI助手进行中文对话",
|
| 50 |
-
# 移除示例以避免需要登录用户进行缓存
|
| 51 |
-
# examples=[["你好"], ["你能帮我做什么?"], ["今天天气怎么样?"]],
|
| 52 |
-
additional_inputs=[
|
| 53 |
-
gr.Textbox(value="你是一个友好的中文聊天机器人。", label="系统消息"),
|
| 54 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="最大新令牌数"),
|
| 55 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="温度"),
|
| 56 |
-
gr.Slider(
|
| 57 |
-
minimum=0.1,
|
| 58 |
-
maximum=1.0,
|
| 59 |
-
value=0.95,
|
| 60 |
-
step=0.05,
|
| 61 |
-
label="Top-p (核采样)",
|
| 62 |
-
),
|
| 63 |
-
],
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
with gr.
|
| 68 |
-
gr.
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
|
| 8 |
+
# 存储对话历史的文件
|
| 9 |
+
HISTORY_FILE = "conversation_history.json"
|
| 10 |
+
KNOWLEDGE_FILE = "knowledge_base.txt"
|
| 11 |
+
|
| 12 |
+
def load_history():
|
| 13 |
+
"""加载对话历史"""
|
| 14 |
+
if os.path.exists(HISTORY_FILE):
|
| 15 |
+
with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
|
| 16 |
+
return json.load(f)
|
| 17 |
+
return []
|
| 18 |
+
|
| 19 |
+
def save_history(history):
|
| 20 |
+
"""保存对话历史"""
|
| 21 |
+
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
|
| 22 |
+
json.dump(history, f, ensure_ascii=False, indent=2)
|
| 23 |
+
|
| 24 |
+
def load_knowledge():
|
| 25 |
+
"""加载知识库"""
|
| 26 |
+
if os.path.exists(KNOWLEDGE_FILE):
|
| 27 |
+
with open(KNOWLEDGE_FILE, 'r', encoding='utf-8') as f:
|
| 28 |
+
return f.read()
|
| 29 |
+
return ""
|
| 30 |
+
|
| 31 |
+
def save_knowledge(content):
|
| 32 |
+
"""保存知识库"""
|
| 33 |
+
with open(KNOWLEDGE_FILE, 'w', encoding='utf-8') as f:
|
| 34 |
+
f.write(content)
|
| 35 |
+
|
| 36 |
def respond(
|
| 37 |
message,
|
| 38 |
history: list[dict[str, str]],
|
|
|
|
| 45 |
"""
|
| 46 |
更多关于 `huggingface_hub` 推理API的信息,请查看文档: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 47 |
"""
|
| 48 |
+
# 保存对话历史
|
| 49 |
+
conversation_data = {
|
| 50 |
+
"timestamp": datetime.now().isoformat(),
|
| 51 |
+
"message": message,
|
| 52 |
+
"history": history,
|
| 53 |
+
"response": ""
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
# 使用一个更适合中文的模型
|
| 57 |
client = InferenceClient(token=hf_token.token, model="THUDM/chatglm3-6b")
|
| 58 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 76 |
|
| 77 |
response += token
|
| 78 |
yield response
|
| 79 |
+
|
| 80 |
+
# 保存响应到历史记录
|
| 81 |
+
conversation_data["response"] = response
|
| 82 |
+
history_list = load_history()
|
| 83 |
+
history_list.append(conversation_data)
|
| 84 |
+
save_history(history_list)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def upload_chat_history(file):
|
| 88 |
+
"""处理上传的聊天记录"""
|
| 89 |
+
if file is None:
|
| 90 |
+
return "请上传文件"
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
# 读取上传的文件
|
| 94 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
| 95 |
+
content = f.read()
|
| 96 |
+
|
| 97 |
+
# 保存到历史记录
|
| 98 |
+
history_list = load_history()
|
| 99 |
+
history_entry = {
|
| 100 |
+
"timestamp": datetime.now().isoformat(),
|
| 101 |
+
"uploaded_chat": content,
|
| 102 |
+
"type": "uploaded_history"
|
| 103 |
+
}
|
| 104 |
+
history_list.append(history_entry)
|
| 105 |
+
save_history(history_list)
|
| 106 |
+
|
| 107 |
+
return f"成功上传聊天记录,共{len(content)}个字符"
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return f"上传失败: {str(e)}"
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def upload_knowledge_document(file):
|
| 113 |
+
"""处理上传的知识文档"""
|
| 114 |
+
if file is None:
|
| 115 |
+
return "请上传文件"
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
# 读取上传的文件
|
| 119 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
| 120 |
+
content = f.read()
|
| 121 |
+
|
| 122 |
+
# 追加到知识库
|
| 123 |
+
existing_knowledge = load_knowledge()
|
| 124 |
+
updated_knowledge = existing_knowledge + "\n\n" + content
|
| 125 |
+
save_knowledge(updated_knowledge)
|
| 126 |
+
|
| 127 |
+
return f"成功添加知识文档,共{len(content)}个字符"
|
| 128 |
+
except Exception as e:
|
| 129 |
+
return f"上传失败: {str(e)}"
|
| 130 |
|
| 131 |
|
| 132 |
"""
|
| 133 |
有关如何自定义聊天界面的信息,请查看gradio文档: https://www.gradio.app/docs/chatinterface
|
| 134 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
with gr.Blocks() as demo:
|
| 136 |
+
with gr.Tab("聊天"):
|
| 137 |
+
chatbot = gr.ChatInterface(
|
| 138 |
+
respond,
|
| 139 |
+
type="messages",
|
| 140 |
+
title="我的AI助手",
|
| 141 |
+
description="与AI助手进行中文对话",
|
| 142 |
+
additional_inputs=[
|
| 143 |
+
gr.Textbox(value="你是一个友好的中文聊天机器人。", label="系统消息"),
|
| 144 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="最大新令牌数"),
|
| 145 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="温度"),
|
| 146 |
+
gr.Slider(
|
| 147 |
+
minimum=0.1,
|
| 148 |
+
maximum=1.0,
|
| 149 |
+
value=0.95,
|
| 150 |
+
step=0.05,
|
| 151 |
+
label="Top-p (核采样)",
|
| 152 |
+
),
|
| 153 |
+
],
|
| 154 |
+
)
|
| 155 |
+
with gr.Sidebar():
|
| 156 |
+
gr.LoginButton()
|
| 157 |
+
|
| 158 |
+
with gr.Tab("学习中心"):
|
| 159 |
+
gr.Markdown("## 上传学习材料")
|
| 160 |
+
|
| 161 |
+
with gr.Row():
|
| 162 |
+
with gr.Column():
|
| 163 |
+
chat_file = gr.File(label="上传聊天记录", file_types=[".txt", ".json"])
|
| 164 |
+
upload_chat_btn = gr.Button("处理聊天记录")
|
| 165 |
+
chat_output = gr.Textbox(label="处理结果")
|
| 166 |
+
|
| 167 |
+
with gr.Column():
|
| 168 |
+
knowledge_file = gr.File(label="上传知识文档", file_types=[".txt", ".md", ".pdf"])
|
| 169 |
+
upload_knowledge_btn = gr.Button("处理知识文档")
|
| 170 |
+
knowledge_output = gr.Textbox(label="处理结果")
|
| 171 |
+
|
| 172 |
+
upload_chat_btn.click(
|
| 173 |
+
upload_chat_history,
|
| 174 |
+
inputs=[chat_file],
|
| 175 |
+
outputs=[chat_output]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
upload_knowledge_btn.click(
|
| 179 |
+
upload_knowledge_document,
|
| 180 |
+
inputs=[knowledge_file],
|
| 181 |
+
outputs=[knowledge_output]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
gr.Markdown("## 当前知识状态")
|
| 185 |
+
knowledge_display = gr.Textbox(label="知识库内容", max_lines=10)
|
| 186 |
+
refresh_btn = gr.Button("刷新知识库")
|
| 187 |
+
|
| 188 |
+
def refresh_knowledge():
|
| 189 |
+
return load_knowledge()
|
| 190 |
+
|
| 191 |
+
refresh_btn.click(
|
| 192 |
+
refresh_knowledge,
|
| 193 |
+
outputs=[knowledge_display]
|
| 194 |
+
)
|
| 195 |
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.42.0
|
| 2 |
+
huggingface_hub>=0.22.2
|