ShadowWolf1999 commited on
Commit
9400fb4
·
verified ·
1 Parent(s): bcf3ce4

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +31 -14
  2. app.py +239 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,14 +1,31 @@
1
- ---
2
- title: AI Deep Persona Profiling System
3
- emoji:
4
- colorFrom: indigo
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 5.37.0
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
- short_description: AI深度人物画像系统
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AI深度人物画像系统
3
+ emoji: 🕵️‍♂️
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 4.28.3
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # AI深度人物画像系统
13
+
14
+ 这是一个基于Gradio的前端界面,用于与后端的“AI深度人物画像系统 API”进行交互。
15
+
16
+ ## 🚀 功能
17
+
18
+ - **输入手机号**: 启动一次人物画像分析会话。
19
+ - **查看初始报告**: 系统将自动分析第一条最相关的数据线索,并生成初始画像。
20
+ - **迭代分析**: 如果对当前画像不满意,可以点击“与事实不符, 换一条”按钮,系统将按顺序分析下一条数据线索,并更新画像报告。
21
+ - **状态反馈**: 界面会实时显示分析状态、找到的线索总数以及当前正在展示第几条线索。
22
+
23
+ ## 🛠️ 技术栈
24
+
25
+ - **前端**: [Gradio](https://www.gradio.app/)
26
+ - **后端通信**: Python `requests` 库
27
+ - **API**: 本应用依赖一个独立的后端API服务来执行实际的分析任务。
28
+
29
+ ## ⚠️ 注意
30
+
31
+ 本应用本身不执行任何AI分析,它仅仅是一个用户界面。所有的数据查询和AI分析都由后端API完成。确保后端API服务正在运行并且可以从该Hugging Face Space公开访问。
app.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import re
4
+
5
+ # --- 配置 ---
6
+ # !!! 重要: 请将此 URL 替换为您的 API 服务器的实际地址
7
+ API_BASE_URL = "http://134.175.222.87:5006" # 这是一个示例,请务必修改
8
+ START_ANALYSIS_ENDPOINT = f"{API_BASE_URL}/start_analysis_session"
9
+ ANALYZE_NEXT_ENDPOINT = f"{API_BASE_URL}/analyze_next"
10
+
11
+
12
+ # --- 核心逻辑函数 ---
13
+
14
+ def format_report(analysis_result):
15
+ """将API返回的JSON格式化为美观的Markdown文本"""
16
+ if not analysis_result or "ai_analysis" not in analysis_result:
17
+ return "### ❌ 错误\n收到的分析结果格式不正确。"
18
+
19
+ analysis = analysis_result["ai_analysis"]
20
+
21
+ def create_tag_list(items):
22
+ if not items:
23
+ return "_暂无推断_"
24
+ return " ".join([f"`{item}`" for item in items])
25
+
26
+ assessment = analysis.get("评估体系", {})
27
+ confidence = assessment.get("画像置信度", {})
28
+ identity_level = assessment.get("身份明确度", "未知")
29
+ badge_map = {"低": "🟢", "中": "🟡", "高": "🔴"}
30
+ identity_badge = f"{badge_map.get(identity_level, '⚪️')} {identity_level}"
31
+
32
+ report_md = f"""
33
+ ## 深度人物画像报告
34
+ > {analysis.get('核心摘要', 'AI未能生成摘要。')}
35
+ ---
36
+ #### 推断画像
37
+ - **职业角色**: {create_tag_list(analysis.get("推断画像", {}).get("职业角色"))}
38
+ - **能力/技能**: {create_tag_list(analysis.get("推断画像", {}).get("能力技能"))}
39
+ - **兴趣爱好**: {create_tag_list(analysis.get("推断画像", {}).get("兴趣爱好"))}
40
+ ---
41
+ #### 关联网络
42
+ - **关联人物**: {create_tag_list(analysis.get("关联网络", {}).get("关联人物"))}
43
+ - **关联组织**: {create_tag_list(analysis.get("关联网络", {}).get("关联组织"))}
44
+ ---
45
+ #### 行为模式推断
46
+ {analysis.get('行为模式推断', '暂无明确的行为模式推断。')}
47
+ ---
48
+ #### 评估体系
49
+ - **身份明确度**: {identity_badge}
50
+ - **画像总置信度**: **{confidence.get('总分', 0) * 100:.0f}%**
51
+ - `职业置信度: {confidence.get('职业置信度', 0) * 100:.0f}%`
52
+ - `爱好置信度: {confidence.get('爱好置信度', 0) * 100:.0f}%`
53
+ - `关联网络置信度: {confidence.get('关联网络置信度', 0) * 100:.0f}%`
54
+ """
55
+ return report_md
56
+
57
+ def start_analysis(phone):
58
+ """处理“生成画像”按钮点击事件"""
59
+ if not phone or not re.match(r"^\d{7,15}$", phone):
60
+ # 返回值对应 outputs 列表
61
+ return (
62
+ "号码格式不正确哦~", # status_message
63
+ gr.update(visible=False), # report_area
64
+ gr.update(value=""), # report_output
65
+ gr.update(visible=False), # feedback_area
66
+ gr.update(interactive=False), # correct_btn
67
+ gr.update(interactive=False), # incorrect_btn
68
+ None, 0, 0 # states
69
+ )
70
+
71
+ status_message = "正在启动分析会话,请稍候..."
72
+ # 初始加载状态
73
+ yield (
74
+ status_message,
75
+ gr.update(visible=False),
76
+ gr.update(value=""),
77
+ gr.update(visible=False),
78
+ gr.update(interactive=False),
79
+ gr.update(interactive=False),
80
+ None, 0, 0
81
+ )
82
+
83
+ try:
84
+ response = requests.post(START_ANALYSIS_ENDPOINT, data={"phone": phone}, timeout=20)
85
+ response.raise_for_status()
86
+ data = response.json()
87
+
88
+ if not data.get("success"):
89
+ raise Exception(data.get("message", "API返回错误但未提供消息"))
90
+
91
+ total_count = data.get("total_count", 0)
92
+ if total_count == 0:
93
+ yield (
94
+ "恭喜!您开启了隐身魔法呦👉👉", gr.update(visible=False), "", gr.update(visible=False),
95
+ gr.update(interactive=False), gr.update(interactive=False), [], 0, 0
96
+ )
97
+ return
98
+
99
+ references = data.get("data_references", [])
100
+ analysis_result = data.get("analysis_result")
101
+ report_md = format_report(analysis_result)
102
+ status_message = f"发现 {total_count} 条线索,正在展示第 1 号情报..."
103
+ next_btn_interactive = total_count > 1
104
+
105
+ # 成功获取数据后的最终状态
106
+ yield (
107
+ status_message,
108
+ gr.update(visible=True), # report_area
109
+ gr.update(value=report_md), # report_output
110
+ gr.update(visible=True), # feedback_area
111
+ gr.update(interactive=True), # correct_btn
112
+ gr.update(interactive=next_btn_interactive), # incorrect_btn
113
+ references, 1, total_count
114
+ )
115
+
116
+ except (requests.exceptions.RequestException, Exception) as e:
117
+ error_message = f"发生错误: {e}"
118
+ yield (
119
+ error_message, gr.update(visible=False), "", gr.update(visible=False),
120
+ gr.update(interactive=False), gr.update(interactive=False), None, 0, 0
121
+ )
122
+
123
+ def analyze_next_clue(references, current_index, total_count):
124
+ """处理“换一条”按钮点击事件"""
125
+ if not references or current_index >= total_count:
126
+ yield (
127
+ "所有线索已分析完毕,没有更多信息了。", # status_message
128
+ gr.update(), # report_output (保持不变)
129
+ gr.update(interactive=False), # correct_btn (可设为True或False,这里设为False)
130
+ gr.update(interactive=False), # incorrect_btn
131
+ references, current_index, total_count
132
+ )
133
+ return
134
+
135
+ next_list_index = current_index
136
+ reference_to_analyze = references[next_list_index]
137
+ display_index = next_list_index + 1
138
+
139
+ status_message = f"正在分析第 {display_index} / {total_count} 号情报..."
140
+
141
+ # 加载时禁用按钮
142
+ yield (
143
+ status_message, gr.update(),
144
+ gr.update(interactive=False), gr.update(interactive=False),
145
+ references, current_index, total_count
146
+ )
147
+
148
+ try:
149
+ response = requests.post(ANALYZE_NEXT_ENDPOINT, json={"reference": reference_to_analyze}, timeout=20)
150
+ response.raise_for_status()
151
+ data = response.json()
152
+
153
+ if not data.get("success"):
154
+ raise Exception(data.get("message", "API返回分析失败"))
155
+
156
+ analysis_result = data.get("analysis_result")
157
+ report_md = format_report(analysis_result)
158
+ new_current_index = current_index + 1
159
+
160
+ status_message = f"第 {new_current_index} 号情报画像构建完毕!"
161
+ next_btn_interactive = new_current_index < total_count
162
+
163
+ yield (
164
+ status_message,
165
+ gr.update(value=report_md),
166
+ gr.update(interactive=True),
167
+ gr.update(interactive=next_btn_interactive),
168
+ references, new_current_index, total_count
169
+ )
170
+
171
+ except (requests.exceptions.RequestException, Exception) as e:
172
+ error_message = f"分析出错了: {e}"
173
+ yield (
174
+ error_message, gr.update(),
175
+ gr.update(interactive=True), gr.update(interactive=True), # 恢复按钮交互
176
+ references, current_index, total_count
177
+ )
178
+
179
+ def confirm_analysis():
180
+ """处理“画像准确”按钮点击事件"""
181
+ return (
182
+ "感谢您的确认!AI酱很开心。🎉", # status_message
183
+ gr.update(interactive=False), # correct_btn
184
+ gr.update(interactive=False) # incorrect_btn
185
+ )
186
+
187
+ # --- Gradio 界面构建 ---
188
+ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
189
+ # 状态管理
190
+ state_references = gr.State([])
191
+ state_current_index = gr.State(0)
192
+ state_total_count = gr.State(0)
193
+
194
+ gr.Markdown("# AI深度人物画像系统")
195
+ gr.Markdown("本系统利用AI对公开的互联网数据进行深度分析,构建推理性的人物画像...")
196
+
197
+ with gr.Row():
198
+ phone_input = gr.Textbox(label="手机号", placeholder="输入11位数字...", scale=3)
199
+ start_button = gr.Button("生成画像", variant="primary", scale=1)
200
+
201
+ status_message = gr.Textbox(label="状态", value="AI分析引擎待命中...", interactive=False)
202
+
203
+ with gr.Column(visible=False) as report_area:
204
+ report_output = gr.Markdown()
205
+
206
+ with gr.Row(visible=False) as feedback_area:
207
+ correct_btn = gr.Button("✅ 画像准确")
208
+ incorrect_btn = gr.Button("❌ 与事实不符, 换一条")
209
+
210
+ # --- 事件绑定 ---
211
+ # **核心改动**: 将所有需要更新的组件都列在outputs中
212
+ start_button.click(
213
+ fn=start_analysis,
214
+ inputs=[phone_input],
215
+ outputs=[
216
+ status_message, report_area, report_output, feedback_area,
217
+ correct_btn, incorrect_btn,
218
+ state_references, state_current_index, state_total_count
219
+ ]
220
+ )
221
+
222
+ incorrect_btn.click(
223
+ fn=analyze_next_clue,
224
+ inputs=[state_references, state_current_index, state_total_count],
225
+ outputs=[
226
+ status_message, report_output,
227
+ correct_btn, incorrect_btn,
228
+ state_references, state_current_index, state_total_count
229
+ ]
230
+ )
231
+
232
+ correct_btn.click(
233
+ fn=confirm_analysis,
234
+ inputs=[],
235
+ outputs=[status_message, correct_btn, incorrect_btn]
236
+ )
237
+
238
+ if __name__ == "__main__":
239
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ requests