ikun520 commited on
Commit
c81ad68
·
verified ·
1 Parent(s): 3446394

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -45
app.py CHANGED
@@ -1,64 +1,114 @@
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
8
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  stream=True,
 
34
  temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
41
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
- respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
 
 
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ from openai import OpenAI
2
+ from docx import Document
3
+ import numpy as np
4
+ import faiss
5
+ from sentence_transformers import SentenceTransformer
6
+ import os
7
  import gradio as gr
 
8
 
9
+ # 配置参数
10
+ WORD_DOC_PATH = "知识库.docx" # Word文档路径
11
+ VECTOR_INDEX_PATH = "faiss_index.index" # 向量索引保存路径
12
+ TEXT_DATA_PATH = "text_data.npy" # 文本数据保存路径
13
+ EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2" # 嵌入模型
14
 
15
+ # 初始化模型和客户端
16
+ client = OpenAI(
17
+ base_url='https://api-inference.modelscope.cn/v1/',
18
+ api_key='7ed44f86-e2c6-4b85-9c4a-26eacfc2e5ee',
19
+ )
20
+ embedder = SentenceTransformer(EMBEDDING_MODEL)
21
 
22
+ def process_word_document():
23
+ """处理Word文档并分块(保持原有实现不变)"""
24
+ doc = Document(WORD_DOC_PATH)
25
+ chunks = []
26
+ current_chunk = []
27
+ chunk_size = 300
 
 
 
28
 
29
+ for para in doc.paragraphs:
30
+ text = para.text.strip()
31
+ if text:
32
+ words = text.split()
33
+ current_chunk.extend(words)
34
 
35
+ while len(current_chunk) > chunk_size:
36
+ chunks.append(" ".join(current_chunk[:chunk_size]))
37
+ current_chunk = current_chunk[chunk_size:]
38
 
39
+ if current_chunk:
40
+ chunks.append(" ".join(current_chunk))
41
 
42
+ return chunks
43
+
44
+ def create_vector_store():
45
+ """创建并保存向量存储(保持原有实现不变)"""
46
+ if os.path.exists(VECTOR_INDEX_PATH):
47
+ return
48
+
49
+ chunks = process_word_document()
50
+ embeddings = embedder.encode(chunks, convert_to_tensor=False)
51
+ embeddings = np.array(embeddings).astype('float32')
52
+
53
+ dimension = embeddings.shape[1]
54
+ index = faiss.IndexFlatL2(dimension)
55
+ index.add(embeddings)
56
+
57
+ faiss.write_index(index, VECTOR_INDEX_PATH)
58
+ np.save(TEXT_DATA_PATH, np.array(chunks))
59
+
60
+ def search_knowledge(query, top_k=3):
61
+ """知识检索(保持原有实现不变)"""
62
+ index = faiss.read_index(VECTOR_INDEX_PATH)
63
+ text_data = np.load(TEXT_DATA_PATH, allow_pickle=True)
64
+
65
+ query_embedding = embedder.encode([query], convert_to_tensor=False)
66
+ query_embedding = np.array(query_embedding).astype('float32')
67
+
68
+ distances, indices = index.search(query_embedding, top_k)
69
+ return "\n".join([text_data[i] for i in indices[0]])
70
+
71
+ def respond(message, history, max_tokens, temperature, top_p):
72
+ """Gradio响应函数"""
73
+ # 检索相关知识
74
+ context = search_knowledge(message)
75
+
76
+ # 构建对话消息
77
+ messages = [
78
+ {"role": "system", "content": f"基于以下知识回答问题,如果不知道就说不知道:\n{context}"},
79
+ {"role": "user", "content": message}
80
+ ]
81
+
82
+ # 流式生成响应
83
+ full_response = ""
84
+ response = client.chat.completions.create(
85
+ model='deepseek-ai/DeepSeek-R1',
86
+ messages=messages,
87
  stream=True,
88
+ max_tokens=max_tokens,
89
  temperature=temperature,
90
+ top_p=top_p
91
+ )
 
92
 
93
+ for chunk in response:
94
+ content = chunk.choices[0].delta.content or ""
95
+ full_response += content
96
+ yield full_response
97
 
98
+ # 初始化向量存储
99
+ create_vector_store()
100
 
101
+ # 创建Gradio界面
 
 
102
  demo = gr.ChatInterface(
103
+ fn=respond,
104
  additional_inputs=[
105
+ gr.Slider(512, 2048, value=512, step=1, label="最大Token数"),
106
+ gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="温度参数"),
107
+ gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p采样"),
 
 
 
 
 
 
 
108
  ],
109
+ title="制度文档问答系统",
110
+ description="输入关于广西警察学院制度的问题进行问答"
111
  )
112
 
 
113
  if __name__ == "__main__":
114
+ demo.launch()