hcy276 commited on
Commit
7508267
·
verified ·
1 Parent(s): 0ea9daa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -116
app.py CHANGED
@@ -1,117 +1,117 @@
1
- import gradio as gr
2
- from utils.embedding import ChatEmbedder
3
- from utils.retrieval import ChatQABot
4
- import json
5
-
6
- class ChatAnalyzerApp:
7
- def __init__(self):
8
- self.embedder = ChatEmbedder()
9
- self.qa_bot = ChatQABot()
10
- self.setup_data()
11
-
12
- def setup_data(self):
13
- """初始化数据和索引"""
14
- try:
15
- count = self.embedder.load_chat_data('chat_log.json')
16
- index_size = self.embedder.build_index()
17
- print(f"✓ 加载了 {count} 条聊天记录,构建了 {index_size} 维索引")
18
- return True
19
- except Exception as e:
20
- print(f"× 数据加载失败: {e}")
21
- return False
22
-
23
- def query_chat(self, question: str, top_k: int = 3):
24
- """处理查询"""
25
- # 1. 检索相关文档
26
- results = self.embedder.search(question, top_k)
27
-
28
- if not results:
29
- return "未找到相关聊天记录。", ""
30
-
31
- # 2. 组合检索到的上下文
32
- context = "\n".join([f"{i+1}. {r['content']} (相关度: {r['score']:.3f})"
33
- for i, r in enumerate(results)])
34
-
35
- # 3. 生成答案
36
- answer = self.qa_bot.generate_answer(question, context)
37
-
38
- return answer, context
39
-
40
- def main():
41
- app = ChatAnalyzerApp()
42
-
43
- if not app.setup_data():
44
- return
45
-
46
- # 创建Gradio界面
47
- with gr.Blocks(title="群聊记录分析助手", theme=gr.themes.Soft()) as demo:
48
- gr.Markdown("# 📱 群聊记录分析与问答系统")
49
- gr.Markdown("基于RAG技术,智能分析群聊记录并回答问题")
50
-
51
- with gr.Row():
52
- with gr.Column(scale=2):
53
- question_input = gr.Textbox(
54
- label="输入您的问题",
55
- placeholder="例如:关于新版UI设计,最终的结论是什么?",
56
- lines=2
57
- )
58
-
59
- top_k_slider = gr.Slider(
60
- minimum=1,
61
- maximum=5,
62
- value=3,
63
- step=1,
64
- label="检索结果数量"
65
- )
66
-
67
- submit_btn = gr.Button("🔍 搜索", variant="primary")
68
-
69
- with gr.Column(scale=3):
70
- answer_output = gr.Textbox(
71
- label="答案",
72
- lines=4,
73
- interactive=False
74
- )
75
-
76
- context_output = gr.Textbox(
77
- label="检索到的相关聊天记录",
78
- lines=6,
79
- interactive=False
80
- )
81
-
82
- # 示例问题
83
- examples = gr.Examples(
84
- examples=[
85
- "关于新版UI设计,最终的结论是什么?",
86
- "谁负责联系市场部?",
87
- "市场部的联系方式是什么?",
88
- "讨论过哪些设计方案?"
89
- ],
90
- inputs=[question_input]
91
- )
92
-
93
- gr.Markdown("### 功能说明")
94
- gr.Markdown("""
95
- 1. 系统基于模拟的飞书/企微群聊记录构建知识库
96
- 2. 使用中文嵌入模型检索相关聊天片段
97
- 3. 使用大语言模型生成准确答案
98
- 4. 支持调整检索结果数量以优化答案质量
99
- """)
100
-
101
- # 绑定事件
102
- submit_btn.click(
103
- fn=app.query_chat,
104
- inputs=[question_input, top_k_slider],
105
- outputs=[answer_output, context_output]
106
- )
107
-
108
- question_input.submit(
109
- fn=app.query_chat,
110
- inputs=[question_input, top_k_slider],
111
- outputs=[answer_output, context_output]
112
- )
113
-
114
- demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
115
-
116
- if __name__ == "__main__":
117
  main()
 
1
+ import gradio as gr
2
+ from utils.embedding import ChatEmbedder
3
+ from utils.retrieval import ChatQABot
4
+ import json
5
+
6
+ class ChatAnalyzerApp:
7
+ def __init__(self):
8
+ self.embedder = ChatEmbedder()
9
+ self.qa_bot = ChatQABot()
10
+ self.setup_data()
11
+
12
+ def setup_data(self):
13
+ """初始化数据和索引"""
14
+ try:
15
+ count = self.embedder.load_chat_data('chat_log.json')
16
+ index_size = self.embedder.build_index()
17
+ print(f"✓ 加载了 {count} 条聊天记录,构建了 {index_size} 维索引")
18
+ return True
19
+ except Exception as e:
20
+ print(f"× 数据加载失败: {e}")
21
+ return False
22
+
23
+ def query_chat(self, question: str, top_k: int = 3):
24
+ """处理查询"""
25
+ # 1. 检索相关文档
26
+ results = self.embedder.search(question, top_k)
27
+
28
+ if not results:
29
+ return "未找到相关聊天记录。", ""
30
+
31
+ # 2. 组合检索到的上下文
32
+ context = "\n".join([f"{i+1}. {r['content']} (相关度: {r['score']:.3f})"
33
+ for i, r in enumerate(results)])
34
+
35
+ # 3. 生成答案
36
+ answer = self.qa_bot.generate_answer(question, context)
37
+
38
+ return answer, context
39
+
40
+ def main():
41
+ app = ChatAnalyzerApp()
42
+
43
+ if not app.setup_data():
44
+ return
45
+
46
+ # 创建Gradio界面
47
+ with gr.Blocks(title="群聊记录分析助手", theme=gr.themes.Soft()) as demo:
48
+ gr.Markdown("# 📱 群聊记录分析与问答系统")
49
+ gr.Markdown("基于RAG技术,智能分析群聊记录并回答问题")
50
+
51
+ with gr.Row():
52
+ with gr.Column(scale=2):
53
+ question_input = gr.Textbox(
54
+ label="输入您的问题",
55
+ placeholder="例如:关于新版UI设计,最终的结论是什么?",
56
+ lines=2
57
+ )
58
+
59
+ top_k_slider = gr.Slider(
60
+ minimum=1,
61
+ maximum=5,
62
+ value=3,
63
+ step=1,
64
+ label="检索结果数量"
65
+ )
66
+
67
+ submit_btn = gr.Button("🔍 搜索", variant="primary")
68
+
69
+ with gr.Column(scale=3):
70
+ answer_output = gr.Textbox(
71
+ label="答案",
72
+ lines=4,
73
+ interactive=False
74
+ )
75
+
76
+ context_output = gr.Textbox(
77
+ label="检索到的相关聊天记录",
78
+ lines=6,
79
+ interactive=False
80
+ )
81
+
82
+ # 示例问题
83
+ examples = gr.Examples(
84
+ examples=[
85
+ "关于新版UI设计,最终的结论是什么?",
86
+ "谁负责联系市场部?",
87
+ "市场部的联系方式是什么?",
88
+ "讨论过哪些设计方案?"
89
+ ],
90
+ inputs=[question_input]
91
+ )
92
+
93
+ gr.Markdown("### 功能说明")
94
+ gr.Markdown("""
95
+ 1. 系统基于模拟的飞书/企微群聊记录构建知识库
96
+ 2. 使用中文嵌入模型检索相关聊天片段
97
+ 3. 使用大语言模型生成准确答案
98
+ 4. 支持调整检索结果数量以优化答案质量
99
+ """)
100
+
101
+ # 绑定事件
102
+ submit_btn.click(
103
+ fn=app.query_chat,
104
+ inputs=[question_input, top_k_slider],
105
+ outputs=[answer_output, context_output]
106
+ )
107
+
108
+ question_input.submit(
109
+ fn=app.query_chat,
110
+ inputs=[question_input, top_k_slider],
111
+ outputs=[answer_output, context_output]
112
+ )
113
+
114
+ demo.launch()
115
+
116
+ if __name__ == "__main__":
117
  main()