fdbw commited on
Commit
da83f68
·
verified ·
1 Parent(s): cbeeecb

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -124
app.py DELETED
@@ -1,124 +0,0 @@
1
- import json, gradio as gr, os, re
2
- from collections import deque
3
-
4
- # ==========================================
5
- # 1. 基础配置 (严格屏蔽 .gitkeep)
6
- # ==========================================
7
- PROJECT_NAME = "家族亲戚关系网查询系统"
8
- DATA_FILE = "family_data.jsonl"
9
- PHOTO_ROOT = "photos"
10
- ARCHIVE_ROOT = "archives"
11
-
12
- def load_family_data():
13
- db = {}
14
- if not os.path.exists(DATA_FILE): return db
15
- with open(DATA_FILE, "r", encoding="utf-8") as f:
16
- for line in f:
17
- try:
18
- data = json.loads(line.strip())
19
- if "n" in data: db[str(data["n"]).strip()] = data
20
- except: continue
21
- return db
22
-
23
- family_db = load_family_data()
24
-
25
- def get_year(name):
26
- rec = family_db.get(name, {})
27
- match = re.search(r'(\d{4})', str(rec.get("info", "")))
28
- return int(match.group(1)) if match else 9999
29
-
30
- # ==========================================
31
- # 2. 推演逻辑 (补全亲、堂、表、姻、溯)
32
- # ==========================================
33
- def run_inference(start_name, cate):
34
- name = start_name.strip()
35
- me = family_db.get(name)
36
- if not me: return f"❌ 未找到【{name}】的记录"
37
-
38
- res = f"📜 {name} 的【{cate}】系推演结果\n" + "="*40 + "\n"
39
- content = ""
40
-
41
- if cate == "晚辈":
42
- def get_descendants(p_name, level=1):
43
- kids = [n for n, d in family_db.items() if d.get("f") == p_name or d.get("m") == p_name]
44
- out = ""
45
- for k in sorted(kids, key=get_year):
46
- out += f"{' '*level} ┕ {k}\n" + get_descendants(k, level + 1)
47
- return out
48
- content = get_descendants(name)
49
-
50
- elif cate == "前辈":
51
- def get_ancestors(p_name, level=1):
52
- if level > 4 or not p_name or p_name == "待考": return ""
53
- r = family_db.get(p_name, {})
54
- line = f"第{level}代: {p_name}\n"
55
- return line + get_ancestors(r.get("f"), level+1) + get_ancestors(r.get("m"), level+1)
56
- content = get_ancestors(me.get("f")) + get_ancestors(me.get("m"))
57
-
58
- elif cate == "亲兄妹":
59
- f, m = me.get("f"), me.get("m")
60
- sibs = [n for n, d in family_db.items() if (f and d.get("f") == f) or (m and d.get("m") == m)]
61
- content = "\n".join([f" 🚩 {s}" for s in sorted(set(sibs), key=get_year) if s != name])
62
-
63
- return res + (content if content else "(暂无记录)")
64
-
65
- # ==========================================
66
- # 3. 档案与查询逻辑
67
- # ==========================================
68
- def get_personal_info(name):
69
- name = name.strip()
70
- p = family_db.get(name)
71
- if not p: return "❌ 未找到档案", None, gr.update(choices=[])
72
-
73
- # 关键点:此处过滤 .gitkeep
74
- files = []
75
- if os.path.exists(f"{ARCHIVE_ROOT}/{name}"):
76
- files = [f for f in os.listdir(f"{ARCHIVE_ROOT}/{name}") if not f.startswith(".")]
77
-
78
- info_md = f"### {name} 资料\n- **性别**: {p.get('s','?')}\n- **父母**: {p.get('f','?')}/{p.get('m','?')}\n- **信息**: {p.get('info', '')}"
79
- photo = f"{PHOTO_ROOT}/{name}.jpg"
80
- return info_md, photo if os.path.exists(photo) else None, gr.update(choices=files)
81
-
82
- def relationship_search(text):
83
- """解析 '冯宗德的舅舅'"""
84
- if "的" not in text: return "格式:姓名+的+称谓"
85
- name, rel = text.split("的")
86
- p = family_db.get(name.strip())
87
- if p and "舅" in rel and p.get("m"):
88
- m_rec = family_db.get(p["m"])
89
- if m_rec and m_rec.get("f"):
90
- uncles = [n for n, d in family_db.items() if d.get("f") == m_rec["f"] and d.get("s") == "男" and n != p["m"]]
91
- return f"{name} 的舅舅: " + "、".join(uncles)
92
- return "未找到匹配关系"
93
-
94
- # ==========================================
95
- # 4. 界面 (适配手机端与 Gradio 最新规范)
96
- # ==========================================
97
- with gr.Blocks(title=PROJECT_NAME) as demo:
98
- gr.Markdown(f"# 📜 {PROJECT_NAME}")
99
- with gr.Tabs():
100
- with gr.Tab("📋 个人档案"):
101
- name_input = gr.Textbox(label="输入姓名并回车")
102
- with gr.Row():
103
- with gr.Column(scale=1):
104
- p_img = gr.Image(label="照片")
105
- with gr.Column(scale=2):
106
- p_md = gr.Markdown("请输入姓名...")
107
- file_dd = gr.Dropdown(label="档案资料库 (已过滤系统文件)")
108
- name_input.submit(get_personal_info, inputs=name_input, outputs=[p_md, p_img, file_dd])
109
-
110
- with gr.Tab("🧬 族网推演"):
111
- with gr.Row():
112
- t_name = gr.Textbox(label="起点", value="冯宗德")
113
- t_mode = gr.Radio(["晚辈", "前辈", "亲兄妹"], label="维度", value="晚辈")
114
- btn = gr.Button("开始推演", variant="primary")
115
- t_out = gr.Textbox(label="推演结果", lines=15)
116
- btn.click(run_inference, inputs=[t_name, t_mode], outputs=t_out)
117
-
118
- with gr.Tab("🔍 称谓查询"):
119
- q_in = gr.Textbox(label="例如:冯宗德的舅舅")
120
- q_out = gr.Textbox(label="结果")
121
- gr.Button("查询").click(relationship_search, inputs=q_in, outputs=q_out)
122
-
123
- # 移除 Blocks 中的 css 参数以避免日志报错
124
- demo.launch(css="footer {display: none !important;}")