Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from http.server import HTTPServer, BaseHTTPRequestHandler
|
|
| 6 |
from urllib.parse import parse_qs
|
| 7 |
|
| 8 |
# ==========================================
|
| 9 |
-
# 1. 核心
|
| 10 |
# ==========================================
|
| 11 |
class FengKinshipEngine:
|
| 12 |
def __init__(self, data_path='family_data.jsonl'):
|
|
@@ -18,7 +18,6 @@ class FengKinshipEngine:
|
|
| 18 |
try:
|
| 19 |
d = json.loads(line)
|
| 20 |
info_text = str(d.get("info", ""))
|
| 21 |
-
# 提取年份
|
| 22 |
year = 9999
|
| 23 |
year_match = re.search(r'(\d{4})', info_text)
|
| 24 |
if year_match: year = int(year_match.group(1))
|
|
@@ -27,8 +26,8 @@ class FengKinshipEngine:
|
|
| 27 |
"n": d.get("n"), "f": d.get("f"), "m": d.get("m"),
|
| 28 |
"g": d.get("g"), "sp": d.get("sp", []), "year": year,
|
| 29 |
"info": info_text,
|
| 30 |
-
"has_media": any(k in info_text for k in ['照片', 'http', '.jpg'
|
| 31 |
-
"has_ach": any(k in info_text for k in ['成就', '事迹', '简介'
|
| 32 |
})
|
| 33 |
except: continue
|
| 34 |
|
|
@@ -39,17 +38,22 @@ class FengKinshipEngine:
|
|
| 39 |
try: t_gen = int(target.get("g"))
|
| 40 |
except: t_gen = 0
|
| 41 |
|
|
|
|
| 42 |
res = {"target": target, "groups": {
|
| 43 |
-
"【始祖/先辈】": [], "【直系父母】": [], "【姻亲/配偶】":
|
| 44 |
"【同辈-兄姐】": [], "【同辈-弟妹】": [], "【直系晚辈】": []
|
| 45 |
}}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
for m in self.members:
|
| 48 |
if m["n"] == target_name: continue
|
| 49 |
try: m_gen = int(m.get("g"))
|
| 50 |
except: m_gen = 0
|
| 51 |
|
| 52 |
-
# 逻辑
|
| 53 |
if m_gen == 0 or (t_gen - m_gen >= 2) or (target["year"] - m["year"] > 60):
|
| 54 |
res["groups"]["【始祖/先辈】"].append(m)
|
| 55 |
elif m["n"] in [target["f"], target["m"]]:
|
|
@@ -66,48 +70,27 @@ class FengKinshipEngine:
|
|
| 66 |
engine = FengKinshipEngine()
|
| 67 |
|
| 68 |
# ==========================================
|
| 69 |
-
# 2.
|
| 70 |
-
# ==========================================
|
| 71 |
-
def run_auto_audit():
|
| 72 |
-
print("\n" + "="*50)
|
| 73 |
-
print("冯氏亲戚关系网 (Feng's Kinship Network) 自动审计报告")
|
| 74 |
-
print("="*50)
|
| 75 |
-
for test_name in ["杨知秋", "冯达尊"]:
|
| 76 |
-
res = engine.analyze(test_name)
|
| 77 |
-
if res:
|
| 78 |
-
print(f"\n对象: {res['target']['n']} (第{res['target']['g']}代)")
|
| 79 |
-
print(f"背景: {res['target']['info']}")
|
| 80 |
-
for title, members in res["groups"].items():
|
| 81 |
-
if members:
|
| 82 |
-
m_str = "、".join([m['n'] + ("[成就]" if m['has_ach'] else "") for m in (members if isinstance(members, list) else [])])
|
| 83 |
-
if not m_str: m_str = str(members)
|
| 84 |
-
print(f"{title}: {m_str}")
|
| 85 |
-
print("\n" + "="*50)
|
| 86 |
-
print("Web服务器已启动! 请访问端口 7860 进行实测。")
|
| 87 |
-
print("="*50 + "\n")
|
| 88 |
-
|
| 89 |
-
# ==========================================
|
| 90 |
-
# 3. 界面模板与服务器逻辑
|
| 91 |
# ==========================================
|
| 92 |
HTML_TPL = """
|
| 93 |
<!DOCTYPE html>
|
| 94 |
<html>
|
| 95 |
<head>
|
| 96 |
-
<meta charset="utf-8">
|
|
|
|
| 97 |
<style>
|
| 98 |
body {{ font-family: sans-serif; background: #f0f2f5; padding: 20px; }}
|
| 99 |
.card {{ max-width: 800px; margin: auto; background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); }}
|
| 100 |
-
.
|
| 101 |
-
.tag
|
| 102 |
-
.res-block {{ border-left: 4px solid #1a73e8; padding-left: 15px; margin: 15px 0; }}
|
| 103 |
</style>
|
| 104 |
</head>
|
| 105 |
<body>
|
| 106 |
<div class="card">
|
| 107 |
-
<h1>冯氏亲戚关系网实测</h1>
|
| 108 |
<form method="POST" style="display:flex;margin-bottom:20px;">
|
| 109 |
-
<input type="text" name="n" placeholder="输入姓名(如:杨知秋)" style="flex:1;padding:10px;" required>
|
| 110 |
-
<button type="submit" style="padding:10px 20px;background:#1a73e8;color:white;border:none;">立即推理</button>
|
| 111 |
</form>
|
| 112 |
{content}
|
| 113 |
</div>
|
|
@@ -118,7 +101,7 @@ HTML_TPL = """
|
|
| 118 |
class RequestHandler(BaseHTTPRequestHandler):
|
| 119 |
def do_GET(self):
|
| 120 |
self.send_response(200); self.send_header('Content-type','text/html; charset=utf-8'); self.end_headers()
|
| 121 |
-
self.wfile.write(HTML_TPL.format(content="<p>输入姓名
|
| 122 |
|
| 123 |
def do_POST(self):
|
| 124 |
length = int(self.headers['Content-Length']); data = self.rfile.read(length).decode('utf-8')
|
|
@@ -126,14 +109,14 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|
| 126 |
content = ""
|
| 127 |
if not res: content = f"<p style='color:red;'>未找到 '{name}'</p>"
|
| 128 |
else:
|
| 129 |
-
content = f"<h2>{res['target']['n']} (第{res['target']['g']}代)</h2>"
|
| 130 |
for title, members in res["groups"].items():
|
| 131 |
if members:
|
| 132 |
-
|
| 133 |
-
content += f"<div class='res-block'><b>{title}:</b> {
|
| 134 |
self.send_response(200); self.send_header('Content-type','text/html; charset=utf-8'); self.end_headers()
|
| 135 |
self.wfile.write(HTML_TPL.format(content=content).encode('utf-8'))
|
| 136 |
|
| 137 |
if __name__ == '__main__':
|
| 138 |
-
|
| 139 |
HTTPServer(('0.0.0.0', 7860), RequestHandler).serve_forever()
|
|
|
|
| 6 |
from urllib.parse import parse_qs
|
| 7 |
|
| 8 |
# ==========================================
|
| 9 |
+
# 1. 核心引擎 (逻辑推理 + 结构修复)
|
| 10 |
# ==========================================
|
| 11 |
class FengKinshipEngine:
|
| 12 |
def __init__(self, data_path='family_data.jsonl'):
|
|
|
|
| 18 |
try:
|
| 19 |
d = json.loads(line)
|
| 20 |
info_text = str(d.get("info", ""))
|
|
|
|
| 21 |
year = 9999
|
| 22 |
year_match = re.search(r'(\d{4})', info_text)
|
| 23 |
if year_match: year = int(year_match.group(1))
|
|
|
|
| 26 |
"n": d.get("n"), "f": d.get("f"), "m": d.get("m"),
|
| 27 |
"g": d.get("g"), "sp": d.get("sp", []), "year": year,
|
| 28 |
"info": info_text,
|
| 29 |
+
"has_media": any(k in info_text for k in ['照片', 'http', '.jpg']),
|
| 30 |
+
"has_ach": any(k in info_text for k in ['成就', '事迹', '简介'])
|
| 31 |
})
|
| 32 |
except: continue
|
| 33 |
|
|
|
|
| 38 |
try: t_gen = int(target.get("g"))
|
| 39 |
except: t_gen = 0
|
| 40 |
|
| 41 |
+
# 统一输出格式为字典列表,避免类型错误
|
| 42 |
res = {"target": target, "groups": {
|
| 43 |
+
"【始祖/先辈】": [], "【直系父母】": [], "【姻亲/配偶】": [],
|
| 44 |
"【同辈-兄姐】": [], "【同辈-弟妹】": [], "【直系晚辈】": []
|
| 45 |
}}
|
| 46 |
|
| 47 |
+
# 特殊处理配偶 (字符串转对象)
|
| 48 |
+
for sp_name in target.get("sp", []):
|
| 49 |
+
res["groups"]["【姻亲/配偶】"].append({"n": sp_name, "has_media": False, "has_ach": False})
|
| 50 |
+
|
| 51 |
for m in self.members:
|
| 52 |
if m["n"] == target_name: continue
|
| 53 |
try: m_gen = int(m.get("g"))
|
| 54 |
except: m_gen = 0
|
| 55 |
|
| 56 |
+
# 逻辑判定
|
| 57 |
if m_gen == 0 or (t_gen - m_gen >= 2) or (target["year"] - m["year"] > 60):
|
| 58 |
res["groups"]["【始祖/先辈】"].append(m)
|
| 59 |
elif m["n"] in [target["f"], target["m"]]:
|
|
|
|
| 70 |
engine = FengKinshipEngine()
|
| 71 |
|
| 72 |
# ==========================================
|
| 73 |
+
# 2. 界面显示逻辑
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# ==========================================
|
| 75 |
HTML_TPL = """
|
| 76 |
<!DOCTYPE html>
|
| 77 |
<html>
|
| 78 |
<head>
|
| 79 |
+
<meta charset="utf-8">
|
| 80 |
+
<title>冯氏亲戚关系网</title>
|
| 81 |
<style>
|
| 82 |
body {{ font-family: sans-serif; background: #f0f2f5; padding: 20px; }}
|
| 83 |
.card {{ max-width: 800px; margin: auto; background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); }}
|
| 84 |
+
.res-block {{ border-left: 4px solid #1a73e8; padding-left: 15px; margin: 15px 0; line-height: 1.6; }}
|
| 85 |
+
.tag {{ font-size: 0.7em; padding: 2px 5px; border-radius: 3px; margin-left: 5px; color: white; background: #e67e22; }}
|
|
|
|
| 86 |
</style>
|
| 87 |
</head>
|
| 88 |
<body>
|
| 89 |
<div class="card">
|
| 90 |
+
<h1>冯氏亲戚关系网 (实测版)</h1>
|
| 91 |
<form method="POST" style="display:flex;margin-bottom:20px;">
|
| 92 |
+
<input type="text" name="n" placeholder="输入姓名实测 (如: 杨知秋)" style="flex:1;padding:10px;" required>
|
| 93 |
+
<button type="submit" style="padding:10px 20px;background:#1a73e8;color:white;border:none;cursor:pointer;">立即推理</button>
|
| 94 |
</form>
|
| 95 |
{content}
|
| 96 |
</div>
|
|
|
|
| 101 |
class RequestHandler(BaseHTTPRequestHandler):
|
| 102 |
def do_GET(self):
|
| 103 |
self.send_response(200); self.send_header('Content-type','text/html; charset=utf-8'); self.end_headers()
|
| 104 |
+
self.wfile.write(HTML_TPL.format(content="<p>系统已修复,请输入姓名进行逻辑实测。</p>").encode('utf-8'))
|
| 105 |
|
| 106 |
def do_POST(self):
|
| 107 |
length = int(self.headers['Content-Length']); data = self.rfile.read(length).decode('utf-8')
|
|
|
|
| 109 |
content = ""
|
| 110 |
if not res: content = f"<p style='color:red;'>未找到 '{name}'</p>"
|
| 111 |
else:
|
| 112 |
+
content = f"<h2>{res['target']['n']} <small>(第{res['target']['g']}代)</small></h2><p><b>背景:</b> {res['target']['info']}</p>"
|
| 113 |
for title, members in res["groups"].items():
|
| 114 |
if members:
|
| 115 |
+
m_str = "、".join([m['n'] + (f"<span class='tag'>成就</span>" if m.get('has_ach') else "") for m in members])
|
| 116 |
+
content += f"<div class='res-block'><b>{title}:</b> {m_str}</div>"
|
| 117 |
self.send_response(200); self.send_header('Content-type','text/html; charset=utf-8'); self.end_headers()
|
| 118 |
self.wfile.write(HTML_TPL.format(content=content).encode('utf-8'))
|
| 119 |
|
| 120 |
if __name__ == '__main__':
|
| 121 |
+
print("冯氏亲戚关系网系统启动... 端口: 7860")
|
| 122 |
HTTPServer(('0.0.0.0', 7860), RequestHandler).serve_forever()
|