ikun520 commited on
Commit
254aa75
·
verified ·
1 Parent(s): 682b8aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -25
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from openai import OpenAI
2
  from docx import Document
3
  import numpy as np
@@ -20,7 +21,7 @@ client = OpenAI(
20
  embedder = SentenceTransformer(EMBEDDING_MODEL)
21
 
22
  def process_word_document():
23
- """处理Word文档并分块(保持原有实现不变)"""
24
  doc = Document(WORD_DOC_PATH)
25
  chunks = []
26
  current_chunk = []
@@ -42,7 +43,7 @@ def process_word_document():
42
  return chunks
43
 
44
  def create_vector_store():
45
- """创建并保存向量存储(保持原有实现不变)"""
46
  if os.path.exists(VECTOR_INDEX_PATH):
47
  return
48
 
@@ -58,7 +59,7 @@ def create_vector_store():
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
 
@@ -68,15 +69,15 @@ def search_knowledge(query, top_k=3):
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
  # 流式生成响应
@@ -90,34 +91,23 @@ def respond(message, history, max_tokens, temperature, top_p):
90
  top_p=top_p
91
  )
92
 
 
93
  for chunk in response:
94
- # 从当前分块中获取推理内容和回答内容
95
  reasoning_chunk = chunk.choices[0].delta.reasoning_content or ""
96
  answer_chunk = chunk.choices[0].delta.content or ""
97
 
98
- # 累加推理内容到全局变量
99
  if reasoning_chunk:
100
- full_reasoning += reasoning_chunk
101
- # 当前分块是推理内容,更新并返回完整的推理内容
102
- yield full_reasoning + "\n\n=== 最终答案 ===\n" + full_response # 提前构建完整输出
103
-
104
- # 累加回答内容到全局变量
105
  elif answer_chunk:
106
- full_response += answer_chunk
107
-
108
- # 如果还没有进入答案输出阶段
109
  if not done_reasoning:
110
- done_reasoning = True # 标记推理阶段结束
111
- # 立即更新并返回,带上推理内容和“最终答案”提示
112
- yield full_reasoning + "\n\n=== 最终答案 ===\n" + full_response
113
 
114
- # 如果已经在答案输出阶段,则继续累加答案内容并返回更新
115
- else:
116
- yield full_reasoning + "\n\n=== 最终答案 ===\n" + full_response
117
- yield full_response, full_reasoning
118
 
119
- # Finally, yield the complete response and reasoning after all chunks are processed
120
- yield full_response, full_reasoning
121
  # 初始化向量存储
122
  create_vector_store()
123
 
@@ -125,10 +115,12 @@ create_vector_store()
125
  demo = gr.ChatInterface(
126
  fn=respond,
127
  additional_inputs=[
 
128
  gr.Slider(512, 2048, value=512, step=1, label="最大Token数"),
129
  gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="温度参数"),
130
  gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p采样"),
131
  ],
 
132
  title="制度文档问答系统",
133
  description="输入关于广西警察学院制度的问题进行问答"
134
  )
 
1
+ # coding: utf-8
2
  from openai import OpenAI
3
  from docx import Document
4
  import numpy as np
 
21
  embedder = SentenceTransformer(EMBEDDING_MODEL)
22
 
23
  def process_word_document():
24
+ """处理Word文档并分块"""
25
  doc = Document(WORD_DOC_PATH)
26
  chunks = []
27
  current_chunk = []
 
43
  return chunks
44
 
45
  def create_vector_store():
46
+ """创建并保存向量存储"""
47
  if os.path.exists(VECTOR_INDEX_PATH):
48
  return
49
 
 
59
  np.save(TEXT_DATA_PATH, np.array(chunks))
60
 
61
  def search_knowledge(query, top_k=3):
62
+ """知识检索"""
63
  index = faiss.read_index(VECTOR_INDEX_PATH)
64
  text_data = np.load(TEXT_DATA_PATH, allow_pickle=True)
65
 
 
69
  distances, indices = index.search(query_embedding, top_k)
70
  return "\n".join([text_data[i] for i in indices[0]])
71
 
72
+ def respond(message, history, max_tokens, temperature, top_p, user_input):
73
  """Gradio响应函数"""
74
  # 检索相关知识
75
+ context = search_knowledge(user_input)
76
 
77
  # 构建对话消息
78
  messages = [
79
  {"role": "system", "content": f"基于以下知识回答问题,如果不知道就说不知道:\n{context}"},
80
+ {"role": "user", "content": user_input}
81
  ]
82
 
83
  # 流式生成响应
 
91
  top_p=top_p
92
  )
93
 
94
+ done_reasoning = False
95
  for chunk in response:
 
96
  reasoning_chunk = chunk.choices[0].delta.reasoning_content or ""
97
  answer_chunk = chunk.choices[0].delta.content or ""
98
 
 
99
  if reasoning_chunk:
100
+ full_response += reasoning_chunk
101
+ print(reasoning_chunk, end='', flush=True)
 
 
 
102
  elif answer_chunk:
 
 
 
103
  if not done_reasoning:
104
+ print('\n\n=== 最终答案 ===\n')
105
+ done_reasoning = True
106
+ print(answer_chunk, end='', flush=True)
107
 
108
+ print("\n" + "="*50)
109
+ return full_response
 
 
110
 
 
 
111
  # 初始化向量存储
112
  create_vector_store()
113
 
 
115
  demo = gr.ChatInterface(
116
  fn=respond,
117
  additional_inputs=[
118
+ gr.Textbox(label="用户提问"),
119
  gr.Slider(512, 2048, value=512, step=1, label="最大Token数"),
120
  gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="温度参数"),
121
  gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p采样"),
122
  ],
123
+ theme=gr.themes.Soft(),
124
  title="制度文档问答系统",
125
  description="输入关于广西警察学院制度的问题进行问答"
126
  )