fdbw commited on
Commit
727e1c1
·
verified ·
1 Parent(s): 3f12b26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+
4
+ st.set_page_config(page_title="家族亲戚关系网查询系统", layout="wide")
5
+ st.title("🌳 家族亲戚关系网查询系统")
6
+
7
+ @st.cache_data
8
+ def load_data(file_path):
9
+ data = []
10
+ try:
11
+ with open(file_path, 'r', encoding='utf-8') as f:
12
+ for line in f:
13
+ line = line.strip()
14
+ if not line: continue
15
+ try:
16
+ item = json.loads(line)
17
+ # 严格统一字段:n, s, f, m, sp, info
18
+ clean_item = {
19
+ "n": str(item.get("n", "未知")),
20
+ "s": str(item.get("s", "未知")),
21
+ "f": str(item.get("f", "")),
22
+ "m": str(item.get("m", "")),
23
+ "sp": item.get("sp", []) if isinstance(item.get("sp"), list) else [item.get("sp")] if item.get("sp") else [],
24
+ "info": str(item.get("info", ""))
25
+ }
26
+ data.append(clean_item)
27
+ except: continue
28
+ except Exception as e:
29
+ st.error(f"加载失败: {e}")
30
+ return data
31
+
32
+ class FamilySystem:
33
+ def __init__(self, data):
34
+ self.data = data
35
+ self.nodes = {n['n']: n for n in data}
36
+
37
+ def get_siblings(self, name):
38
+ node = self.nodes.get(name)
39
+ if not node or not node['f']: return []
40
+ # 同父即是亲兄妹
41
+ return [n for n in self.data if n['f'] == node['f'] and n['n'] != name]
42
+
43
+ def get_children(self, name):
44
+ return [n for n in self.data if n['f'] == name or n['m'] == name]
45
+
46
+ def build_chain(self, ancestor, target):
47
+ """生成路径:A(父亲)→B(父亲)→C"""
48
+ if ancestor == target: return ancestor
49
+ curr = self.nodes.get(target)
50
+ if not curr: return target
51
+
52
+ parent = curr['f'] if curr['f'] in self.nodes else curr['m'] if curr['m'] in self.nodes else None
53
+ if not parent: return target
54
+
55
+ rel = "父亲" if curr['f'] == parent else "母亲"
56
+ if parent == ancestor:
57
+ return f"{ancestor}({rel})→{target}"
58
+
59
+ return f"{self.build_chain(ancestor, parent)}({rel})→{target}"
60
+
61
+ # 加载数据
62
+ family_data = load_data("family_data.jsonl")
63
+
64
+ if family_data:
65
+ tree = FamilySystem(family_data)
66
+ name_list = [n['n'] for n in family_data]
67
+
68
+ query_name = st.sidebar.selectbox("🔎 输入姓名开始查询", name_list)
69
+
70
+ if query_name:
71
+ node = tree.nodes[query_name]
72
+ st.header(f"人物核心档案:{query_name}")
73
+
74
+ t1, t2, t3 = st.tabs(["🏛️ 家族溯源 (长辈)", "👫 同辈关系", "👶 后代脉络 (下三代)"])
75
+
76
+ with t1:
77
+ st.subheader("上一辈及溯源")
78
+ f, m = node['f'], node['m']
79
+ if f or m:
80
+ st.write(f"直接长辈:父【{f or '无'}】 母【{m or '无'}】")
81
+ # 递归显示上三辈逻辑可在此扩展
82
+ else:
83
+ st.info("已达到该支脉已知最高辈分")
84
+
85
+ with t2:
86
+ st.subheader("同辈亲属 (亲/堂/表/姻)")
87
+ sibs = tree.get_siblings(query_name)
88
+ if sibs:
89
+ st.write("**亲兄妹:**")
90
+ for s in sibs: st.write(f"• {s['n']} ({'男' if s['s']=='男' else '女'})")
91
+ else:
92
+ st.write("无亲兄妹记录")
93
+
94
+ with t3:
95
+ st.subheader("后代按家庭组织显示")
96
+ children = tree.get_children(query_name)
97
+ for c in children:
98
+ with st.expander(f"子/女:{c['n']}", expanded=True):
99
+ st.write(f"配偶:{', '.join(c['sp']) if c['sp'] else '无'}")
100
+ # 下二代
101
+ grandchildren = tree.get_children(c['n'])
102
+ for gc in grandchildren:
103
+ st.success(f"孙辈:{gc['n']}")
104
+ st.code(f"路径:{tree.build_chain(query_name, gc['n'])}")
105
+ # 下三代(曾孙)
106
+ great_gc = tree.get_children(gc['n'])
107
+ for ggc in great_gc:
108
+ st.warning(f"曾孙辈:{ggc['n']}")
109
+ st.code(f"路径:{tree.build_chain(query_name, ggc['n'])}")