fdbw commited on
Commit
40078e1
·
verified ·
1 Parent(s): d3d206b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ import os
4
+ import re
5
+
6
+ # ==========================================
7
+ # 1. 全局配置与数据加载
8
+ # ==========================================
9
+ PROJECT_NAME = "冯氏亲戚关系网"
10
+ ARCHIVE_ROOT = "archives"
11
+ PHOTO_ROOT = "photos"
12
+
13
+ def load_family_data():
14
+ db = {}
15
+ if os.path.exists("family_data.jsonl"):
16
+ with open("family_data.jsonl", "r", encoding="utf-8") as f:
17
+ for line in f:
18
+ try:
19
+ data = json.loads(line.strip())
20
+ if "n" in data:
21
+ db[data["n"].strip()] = data
22
+ except: continue
23
+ return db
24
+
25
+ # 预加载数据
26
+ family_db = load_family_data()
27
+
28
+ # ==========================================
29
+ # 2. 核心功能函数
30
+ # ==========================================
31
+
32
+ def handle_profile_search(name):
33
+ """处理个人资料查询:返回照片、文件列表和初始提示"""
34
+ name = name.strip()
35
+ if not name:
36
+ return None, gr.update(choices=[], value=None), "请输入姓名"
37
+
38
+ # 查找照片
39
+ photo = None
40
+ for ext in ['.jpg', '.png', '.jpeg']:
41
+ p = os.path.join(PHOTO_ROOT, f"{name}{ext}")
42
+ if os.path.exists(p):
43
+ photo = p
44
+ break
45
+
46
+ # 查找档案
47
+ path = os.path.join(ARCHIVE_ROOT, name)
48
+ choices = []
49
+ if os.path.exists(path):
50
+ choices = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
51
+
52
+ if not choices:
53
+ return photo, gr.update(choices=[], value=None), "未找到相关档案文件"
54
+
55
+ return photo, gr.update(choices=choices, value=None), "已加载档案列表,请选择文件查看内容"
56
+
57
+ def handle_file_read(name, filename):
58
+ """读取选中的档案内容"""
59
+ if not name or not filename:
60
+ return ""
61
+ try:
62
+ fpath = os.path.join(ARCHIVE_ROOT, name.strip(), filename)
63
+ with open(fpath, 'r', encoding='utf-8') as f:
64
+ return f.read()
65
+ except Exception as e:
66
+ return f"读取失败: {str(e)}"
67
+
68
+ def resolve_query(name, rel):
69
+ """查亲戚逻辑占位"""
70
+ if not name: return "请输入姓名"
71
+ return f"正在【{PROJECT_NAME}】中查询 {name} 的 {rel}..."
72
+
73
+ # ==========================================
74
+ # 3. 构建界面
75
+ # ==========================================
76
+
77
+ with gr.Blocks(title=PROJECT_NAME) as demo:
78
+ gr.Markdown(f"# 👨‍👩‍👧‍👦 {PROJECT_NAME}")
79
+
80
+ with gr.Tabs():
81
+ # 选项卡:查亲戚
82
+ with gr.Tab("🔍 查亲戚"):
83
+ with gr.Row():
84
+ name_in = gr.Textbox(label="姓名")
85
+ rel_in = gr.Textbox(label="关系链")
86
+ btn_q = gr.Button("查询", variant="primary")
87
+ res_out = gr.Markdown()
88
+ btn_q.click(resolve_query, [name_in, rel_in], res_out)
89
+
90
+ # 选项卡:个人资料
91
+ with gr.Tab("📋 个人资料"):
92
+ with gr.Row():
93
+ with gr.Column(scale=1):
94
+ p_name = gr.Textbox(label="输入姓名并回车", placeholder="例如:冯乔福")
95
+ p_avatar = gr.Image(label="照片", height=300)
96
+ with gr.Column(scale=2):
97
+ f_select = gr.Dropdown(label="档案文件列表", choices=[])
98
+ f_content = gr.Textbox(label="预览内容", lines=15)
99
+
100
+ # 事件绑定(确保每个组件只绑定一次)
101
+ p_name.submit(
102
+ fn=handle_profile_search,
103
+ inputs=p_name,
104
+ outputs=[p_avatar, f_select, f_content]
105
+ )
106
+
107
+ f_select.change(
108
+ fn=handle_file_read,
109
+ inputs=[p_name, f_select],
110
+ outputs=f_content
111
+ )
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch()