app.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. import os
  2. import pymysql
  3. import requests
  4. import json
  5. import re
  6. import threading
  7. import urllib3
  8. from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify, Response, stream_with_context
  9. from werkzeug.utils import secure_filename
  10. from oss_utils import upload_to_oss
  11. from ocr_utils import extract_page_number
  12. import time
  13. from datetime import datetime
  14. # Suppress InsecureRequestWarning
  15. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  16. app = Flask(__name__)
  17. app.secret_key = 'genealogy_secret_key'
  18. app.config['UPLOAD_FOLDER'] = 'uploads'
  19. os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
  20. # 数据库配置
  21. DB_CONFIG = {
  22. "host": "rm-f8ze60yirdj8786u2.mysql.rds.aliyuncs.com",
  23. "port": 3306,
  24. "user": "root",
  25. "password": "csqz@20255",
  26. "db": "csqz-client",
  27. "charset": "utf8mb4",
  28. "cursorclass": pymysql.cursors.DictCursor
  29. }
  30. def get_db_connection():
  31. return pymysql.connect(**DB_CONFIG)
  32. def format_timestamp(ts):
  33. if not ts: return '未知'
  34. try:
  35. # 兼容秒和毫秒
  36. if ts > 10000000000: # 超过2286年的秒数,通常认为是毫秒
  37. ts = ts / 1000
  38. return time.strftime('%Y-%m-%d', time.localtime(ts))
  39. except:
  40. return '未知'
  41. def manual_simplify(text):
  42. """
  43. Simple fallback for common Traditional to Simplified conversion
  44. if AI fails to convert specific characters.
  45. """
  46. if not text: return text
  47. mapping = {
  48. '學': '学', '國': '国', '萬': '万', '寶': '宝', '興': '兴',
  49. '華': '华', '會': '会', '葉': '叶', '藝': '艺', '號': '号',
  50. '處': '处', '見': '见', '視': '视', '言': '言', '語': '语',
  51. '貝': '贝', '車': '车', '長': '长', '門': '门', '韋': '韦',
  52. '頁': '页', '風': '风', '飛': '飞', '食': '食', '馬': '马',
  53. '魚': '鱼', '鳥': '鸟', '麥': '麦', '黃': '黄', '齊': '齐',
  54. '齒': '齿', '龍': '龙', '龜': '龟', '壽': '寿', '榮': '荣',
  55. '愛': '爱', '慶': '庆', '衛': '卫', '賢': '贤', '義': '义',
  56. '禮': '礼', '樂': '乐', '靈': '灵', '滅': '灭', '氣': '气',
  57. '智': '智', '信': '信', '仁': '仁', '勇': '勇', '嚴': '严',
  58. '銳': '锐', '優': '优', '楊': '杨', '吳': '吴', '銀': '银'
  59. }
  60. result = ""
  61. for char in text:
  62. result += mapping.get(char, char)
  63. return result
  64. def clean_name(name):
  65. """
  66. Clean name according to Liu family genealogy rules:
  67. 1. If name is '学公' or '留学公', keep 'Gong' (exception).
  68. 2. Otherwise, if name ends with '公', remove '公'.
  69. 3. If name does not start with '留', prepend '留'.
  70. """
  71. if not name: return name
  72. name = name.strip()
  73. # Pre-process: Ensure Simplified Chinese for specific chars
  74. name = manual_simplify(name)
  75. # 1. Check exceptions (names that SHOULD keep 'Gong')
  76. exceptions = ['学公', '留学公']
  77. if name in exceptions:
  78. if not name.startswith('留'):
  79. name = '留' + name
  80. return name
  81. # 2. General Rule: Remove 'Gong' suffix
  82. if name.endswith('公'):
  83. name = name[:-1]
  84. # 3. Ensure 'Liu' surname
  85. if not name.startswith('留'):
  86. name = '留' + name
  87. return name
  88. def process_ai_task(record_id, image_url):
  89. """Background task to process image with AI and store result."""
  90. print(f"[AI Task] Starting task for record {record_id}...")
  91. conn = get_db_connection()
  92. try:
  93. with conn.cursor() as cursor:
  94. cursor.execute("UPDATE genealogy_records SET ai_status = 1 WHERE id = %s", (record_id,))
  95. conn.commit()
  96. print(f"[AI Task] Status updated to 'Processing' for record {record_id}")
  97. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  98. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  99. prompt = """
  100. 请分析这张家谱图片,提取其中关于人物的信息。
  101. 请务必将繁体字转换为简体字(original_name 字段除外)。
  102. 特别注意:'name' 字段必须是纯简体中文,不能包含繁体字(例如:'學'应转换为'学','劉'应转换为'刘','萬'应转换为'万')。
  103. 请提取以下字段(如果存在):
  104. - original_name: 原始姓名(严格保持图片上的繁体字,不做任何修改或转换)
  105. - name: 简体姓名(必须转换为简体中文,去除不需要的敬称)
  106. - sex: 性别(男/女)
  107. - birthday: 出生日期(尝试转换为YYYY-MM-DD格式,如果无法确定年份可只填月日)
  108. - father_name: 父亲姓名
  109. - spouse_name: 配偶姓名
  110. - generation: 第几世/代数
  111. - name_word: 字辈(例如名字为“学勤公”,“学”为字辈;提取名字中的字辈信息)
  112. - education: 学历/功名
  113. - title: 官职/称号
  114. 请严格以JSON列表格式返回,不要包含Markdown代码块标记(如 ```json ... ```),直接返回JSON数组。
  115. 如果包含多个人物,请都提取出来。
  116. Do not output any reasoning or explanation, just the JSON.
  117. """
  118. payload = {
  119. "model": "doubao-seed-1-8-251228",
  120. "stream": True, # Streaming for robust handling
  121. "input": [
  122. {
  123. "role": "user",
  124. "content": [
  125. {"type": "input_image", "image_url": image_url},
  126. {"type": "input_text", "text": prompt}
  127. ]
  128. }
  129. ]
  130. }
  131. headers = {
  132. "Authorization": f"Bearer {api_key}",
  133. "Content-Type": "application/json"
  134. }
  135. max_retries = 3
  136. last_exception = None
  137. for attempt in range(max_retries):
  138. try:
  139. print(f"[AI Task] Attempt {attempt+1}/{max_retries} connecting to API for record {record_id}...")
  140. response = requests.post(
  141. api_url,
  142. json=payload,
  143. headers=headers,
  144. timeout=1200,
  145. stream=True,
  146. verify=False,
  147. proxies={"http": None, "https": None}
  148. )
  149. if response.status_code == 200:
  150. print(f"[AI Task] Connection established for record {record_id}, receiving stream...")
  151. full_content = ""
  152. for line in response.iter_lines():
  153. if not line: continue
  154. line_str = line.decode('utf-8')
  155. # Debug: Print full line to understand event flow
  156. print(f"[AI Task Debug] Raw Line: {line_str[:500]}") # Truncate very long lines
  157. if line_str.startswith('data: '):
  158. json_str = line_str[6:]
  159. if json_str.strip() == '[DONE]':
  160. print("[AI Task Debug] Received [DONE]")
  161. break
  162. try:
  163. chunk = json.loads(json_str)
  164. chunk_type = chunk.get('type')
  165. # Standard OpenAI format (choices)
  166. if 'choices' in chunk and len(chunk['choices']) > 0:
  167. delta = chunk['choices'][0].get('delta', {})
  168. if 'content' in delta:
  169. full_content += delta['content']
  170. # Doubao/Volcengine specific formats (delta)
  171. elif chunk_type == 'response.text.delta':
  172. full_content += chunk.get('delta', '')
  173. # Check response.completed if empty
  174. elif chunk_type == 'response.completed' and not full_content:
  175. output = chunk.get('response', {}).get('output', [])
  176. for item in output:
  177. # Also extract from reasoning if it contains JSON-like text
  178. if item.get('type') == 'reasoning':
  179. summary = item.get('summary', [])
  180. for sum_item in summary:
  181. if sum_item.get('type') == 'summary_text':
  182. full_content += sum_item.get('text', '')
  183. elif item.get('type') == 'message':
  184. content = item.get('content')
  185. if isinstance(content, str):
  186. full_content += content
  187. elif isinstance(content, list):
  188. for part in content:
  189. if isinstance(part, dict) and part.get('type') == 'text':
  190. full_content += part.get('text', '')
  191. # Fallback: output_item.added
  192. elif chunk_type == 'response.output_item.added':
  193. item = chunk.get('item', {})
  194. if item.get('role') == 'assistant':
  195. content_field = item.get('content', [])
  196. if isinstance(content_field, str):
  197. full_content += content_field
  198. elif isinstance(content_field, list):
  199. for part in content_field:
  200. if isinstance(part, dict) and part.get('type') == 'text':
  201. full_content += part.get('text', '')
  202. except Exception as e:
  203. print(f"[AI Task] Chunk parse error: {e}")
  204. else:
  205. # Fallback for non-SSE
  206. try:
  207. chunk = json.loads(line_str)
  208. if 'choices' in chunk and len(chunk['choices']) > 0:
  209. content = chunk['choices'][0]['message']['content']
  210. full_content += content
  211. except:
  212. pass
  213. print(f"[AI Task] Stream finished. Content length: {len(full_content)}")
  214. if len(full_content) == 0:
  215. print(f"[AI Task] WARNING: No content received from AI stream.")
  216. # Continue to JSON parse to fail gracefully
  217. # Clean JSON
  218. try:
  219. # 1. Try finding [...] array
  220. start = full_content.find('[')
  221. end = full_content.rfind(']')
  222. # 2. If not found, try finding {...} object and wrap it
  223. is_single_object = False
  224. if start == -1 or end == -1 or end <= start:
  225. start = full_content.find('{')
  226. end = full_content.rfind('}')
  227. is_single_object = True
  228. if start != -1 and end != -1 and end > start:
  229. content_clean = full_content[start:end+1]
  230. else:
  231. # Fallback to regex or raw
  232. content_clean = re.sub(r'^```json\s*', '', full_content)
  233. content_clean = re.sub(r'```$', '', content_clean)
  234. parsed = json.loads(content_clean)
  235. # Normalize single object to list
  236. if is_single_object and isinstance(parsed, dict):
  237. parsed = [parsed]
  238. content_clean = json.dumps(parsed, ensure_ascii=False)
  239. elif isinstance(parsed, dict) and not isinstance(parsed, list):
  240. # Just in case json.loads parsed a dict even if we looked for []
  241. parsed = [parsed]
  242. content_clean = json.dumps(parsed, ensure_ascii=False)
  243. # Clean names in parsed content
  244. if isinstance(parsed, list):
  245. for person in parsed:
  246. # Process Name: 'name' is Simplified from AI, 'original_name' is Traditional/Raw from AI
  247. simplified_name = person.get('name', '')
  248. original_name = person.get('original_name', '')
  249. # Apply clean logic to simplified name(本人生效:拼接“留”姓、处理“公”)
  250. cleaned_simplified = clean_name(simplified_name)
  251. person['simplified_name'] = cleaned_simplified
  252. # Store raw name in 'name' field (as requested)
  253. if original_name:
  254. person['name'] = original_name
  255. else:
  256. # Fallback: if no original_name returned, use the uncleaned name as 'name'
  257. # or keep existing logic. But user wants raw in 'name'.
  258. # If AI didn't return original_name, 'name' is likely simplified.
  259. pass # Keep 'name' as is (which is Simplified) if original_name missing
  260. # Father name:同族,需要按“留”姓规则清洗
  261. if 'father_name' in person and person['father_name']:
  262. person['father_name'] = clean_name(person['father_name'])
  263. # Spouse name:只做繁转简,不拼接“留”姓,也不去“公”
  264. if 'spouse_name' in person and person['spouse_name']:
  265. person['spouse_name'] = manual_simplify(person['spouse_name'])
  266. # Re-serialize
  267. content_clean = json.dumps(parsed, ensure_ascii=False)
  268. with conn.cursor() as cursor:
  269. cursor.execute("UPDATE genealogy_records SET ai_status = 2, ai_content = %s WHERE id = %s", (content_clean, record_id))
  270. conn.commit()
  271. print(f"[AI Task] SUCCESS: Record {record_id} processed and saved.")
  272. return # Success
  273. except json.JSONDecodeError:
  274. print(f"[AI Task] WARNING: JSON Parse Error for record {record_id}. Saving raw content.")
  275. with conn.cursor() as cursor:
  276. cursor.execute("UPDATE genealogy_records SET ai_status = 3, ai_content = %s WHERE id = %s", (f"JSON Parse Error. Raw: {full_content}", record_id))
  277. conn.commit()
  278. return # Success (technically API worked, just bad content)
  279. else:
  280. print(f"[AI Task] API Error {response.status_code} for record {record_id}: {response.text[:100]}...")
  281. if response.status_code >= 500:
  282. raise Exception(f"Server Error {response.status_code}")
  283. with conn.cursor() as cursor:
  284. cursor.execute("UPDATE genealogy_records SET ai_status = 3, ai_content = %s WHERE id = %s", (f"API Error: {response.text}", record_id))
  285. conn.commit()
  286. return
  287. except Exception as e:
  288. print(f"[AI Task] Attempt {attempt+1} failed for record {record_id}: {e}")
  289. last_exception = e
  290. if attempt < max_retries - 1:
  291. wait_time = 2 * (attempt + 1)
  292. print(f"[AI Task] Waiting {wait_time}s before retry...")
  293. time.sleep(wait_time)
  294. raise last_exception or Exception("Unknown error")
  295. except Exception as e:
  296. print(f"[AI Task] FINAL FAILURE for record {record_id}: {e}")
  297. try:
  298. with conn.cursor() as cursor:
  299. cursor.execute("UPDATE genealogy_records SET ai_status = 3, ai_content = %s WHERE id = %s", (f"Max Retries Exceeded. Error: {str(e)}", record_id))
  300. conn.commit()
  301. except:
  302. pass
  303. finally:
  304. conn.close()
  305. print(f"[AI Task] Task finished for record {record_id}")
  306. @app.route('/manager/')
  307. def index():
  308. if 'user_id' not in session:
  309. return redirect(url_for('login'))
  310. conn = get_db_connection()
  311. try:
  312. with conn.cursor() as cursor:
  313. # 获取家谱图片记录 (上传管理)
  314. cursor.execute("SELECT * FROM genealogy_records ORDER BY upload_time DESC")
  315. records = cursor.fetchall()
  316. finally:
  317. conn.close()
  318. return render_template('index.html', records=records)
  319. @app.route('/manager/members')
  320. def members():
  321. if 'user_id' not in session:
  322. return redirect(url_for('login'))
  323. search_name = request.args.get('name', '').strip()
  324. page = request.args.get('page', 1, type=int)
  325. per_page = 10
  326. offset = (page - 1) * per_page
  327. conn = get_db_connection()
  328. try:
  329. with conn.cursor() as cursor:
  330. # 1. Get total count
  331. if search_name:
  332. cursor.execute("SELECT COUNT(*) as count FROM family_member_info WHERE name LIKE %s", (f"%{search_name}%",))
  333. else:
  334. cursor.execute("SELECT COUNT(*) as count FROM family_member_info")
  335. result = cursor.fetchone()
  336. total = result['count'] if result else 0
  337. total_pages = (total + per_page - 1) // per_page
  338. # 2. Get paginated results, ordered by modified_time DESC (or create_time if modified is null/same)
  339. # Using COALESCE to ensure sort works even if modified_time is NULL
  340. order_clause = "ORDER BY COALESCE(modified_time, create_time) DESC"
  341. if search_name:
  342. sql = f"SELECT * FROM family_member_info WHERE name LIKE %s {order_clause} LIMIT %s OFFSET %s"
  343. cursor.execute(sql, (f"%{search_name}%", per_page, offset))
  344. else:
  345. sql = f"SELECT * FROM family_member_info {order_clause} LIMIT %s OFFSET %s"
  346. cursor.execute(sql, (per_page, offset))
  347. members = cursor.fetchall()
  348. # 格式化日期
  349. for m in members:
  350. m['birthday_str'] = format_timestamp(m.get('birthday'))
  351. # 格式化创建时间 (针对 TIMESTAMP 字段)
  352. if m.get('create_time'):
  353. m['create_time_str'] = m['create_time'].strftime('%Y-%m-%d')
  354. if m.get('modified_time'):
  355. m['modified_time_str'] = m['modified_time'].strftime('%Y-%m-%d %H:%M')
  356. finally:
  357. conn.close()
  358. return render_template('members.html', members=members, search_name=search_name, page=page, total_pages=total_pages, total=total)
  359. @app.route('/manager/tree')
  360. def tree():
  361. if 'user_id' not in session:
  362. return redirect(url_for('login'))
  363. return render_template('tree.html')
  364. @app.route('/manager/api/tree_data')
  365. def tree_data():
  366. if 'user_id' not in session:
  367. return jsonify({"error": "Unauthorized"}), 401
  368. conn = get_db_connection()
  369. try:
  370. with conn.cursor() as cursor:
  371. # 获取所有成员
  372. cursor.execute("SELECT id, name, simplified_name, sex FROM family_member_info")
  373. members = cursor.fetchall()
  374. # 获取所有关系 (1:父子 2:母子 10:夫妻 11:兄弟 12:姐妹)
  375. cursor.execute("SELECT parent_mid, child_mid, relation_type FROM family_relation_info")
  376. relations = cursor.fetchall()
  377. return jsonify({"members": members, "relations": relations})
  378. finally:
  379. conn.close()
  380. @app.route('/manager/api/save_relation', methods=['POST'])
  381. def save_relation():
  382. if 'user_id' not in session:
  383. return jsonify({"success": False, "message": "Unauthorized"}), 401
  384. data = request.json
  385. source_mid = data.get('source_mid') # The member being dragged
  386. target_mid = data.get('target_mid') # The member being dropped onto
  387. rel_type = int(data.get('relation_type'))
  388. sub_rel_type = int(data.get('sub_relation_type', 0))
  389. if not source_mid or not target_mid or not rel_type:
  390. return jsonify({"success": False, "message": "参数不完整"}), 400
  391. conn = get_db_connection()
  392. try:
  393. with conn.cursor() as cursor:
  394. # 简单处理:如果是父子/母子关系
  395. # target_mid 是父辈,source_mid 是子辈
  396. parent_mid = target_mid
  397. child_mid = source_mid
  398. gen_diff = 1
  399. if rel_type == 10: # 夫妻
  400. # 夫妻关系中,我们通常把关联人设为 parent_mid
  401. parent_mid = target_mid
  402. child_mid = source_mid
  403. gen_diff = 0
  404. elif rel_type in [11, 12]: # 兄弟姐妹
  405. # 这里逻辑上比较复杂,通常兄弟姐妹有共同父母。
  406. # 简化处理:暂时存为同级关系 (gen_diff=0)
  407. parent_mid = target_mid
  408. child_mid = source_mid
  409. gen_diff = 0
  410. # 删除旧关系
  411. cursor.execute("DELETE FROM family_relation_info WHERE source_mid = %s", (source_mid,))
  412. # 插入新关系
  413. sql = """
  414. INSERT INTO family_relation_info
  415. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff)
  416. VALUES (%s, %s, %s, %s, %s, %s)
  417. """
  418. cursor.execute(sql, (parent_mid, child_mid, rel_type, sub_rel_type, source_mid, gen_diff))
  419. conn.commit()
  420. return jsonify({"success": True, "message": "关系已保存"})
  421. except Exception as e:
  422. return jsonify({"success": False, "message": str(e)}), 500
  423. finally:
  424. conn.close()
  425. @app.route('/manager/api/check_relations', methods=['POST'])
  426. def check_relations():
  427. if 'user_id' not in session:
  428. return jsonify({"success": False, "message": "Unauthorized"}), 401
  429. data = request.json
  430. people = data.get('people', [])
  431. if not people:
  432. return jsonify({"success": False, "matches": {}})
  433. conn = get_db_connection()
  434. matches = {}
  435. try:
  436. with conn.cursor() as cursor:
  437. # Collect all father names and spouse names to query
  438. names_to_check = set()
  439. for p in people:
  440. if p.get('father_name'): names_to_check.add(p['father_name'])
  441. if p.get('spouse_name'): names_to_check.add(p['spouse_name'])
  442. if not names_to_check:
  443. return jsonify({"success": True, "matches": {}})
  444. # Query DB
  445. format_strings = ','.join(['%s'] * len(names_to_check))
  446. if names_to_check:
  447. sql = "SELECT id, name, simplified_name, sex, birthday FROM family_member_info WHERE name IN (%s) OR simplified_name IN (%s)" % (format_strings, format_strings)
  448. cursor.execute(sql, tuple(names_to_check) * 2)
  449. results = cursor.fetchall()
  450. else:
  451. results = []
  452. # Organize by name
  453. db_map = {} # name -> [list of members]
  454. for r in results:
  455. # Add under 'name' (Traditional/Old Simplified)
  456. if r['name'] not in db_map: db_map[r['name']] = []
  457. db_map[r['name']].append(r)
  458. # Add under 'simplified_name' if exists
  459. if r.get('simplified_name'):
  460. sname = r['simplified_name']
  461. if sname not in db_map: db_map[sname] = []
  462. # Avoid duplicates if simplified_name is same as name?
  463. # The list might contain same object reference, which is fine.
  464. if sname != r['name']:
  465. db_map[sname].append(r)
  466. # Build matches for each input person
  467. for index, p in enumerate(people):
  468. p_match = {}
  469. # Check Father
  470. fname = p.get('father_name')
  471. if fname and fname in db_map:
  472. candidates = db_map[fname]
  473. # Filter: Father should be Male usually, and older than child (if birthday available)
  474. valid_fathers = [c for c in candidates if c['sex'] == 1]
  475. if valid_fathers:
  476. p_match['father'] = valid_fathers # Return all candidates
  477. # Check Spouse
  478. sname = p.get('spouse_name')
  479. if sname and sname in db_map:
  480. candidates = db_map[sname]
  481. # Filter: Spouse usually opposite sex
  482. target_sex = 1 if p.get('sex') == '女' else 2
  483. valid_spouses = [c for c in candidates if c['sex'] == target_sex]
  484. if valid_spouses:
  485. p_match['spouse'] = valid_spouses
  486. if p_match:
  487. matches[index] = p_match
  488. return jsonify({"success": True, "matches": matches})
  489. finally:
  490. conn.close()
  491. @app.route('/manager/add_member', methods=['GET', 'POST'])
  492. def add_member():
  493. if 'user_id' not in session:
  494. return redirect(url_for('login'))
  495. conn = get_db_connection()
  496. try:
  497. # Check for source_record_id (from GET or POST)
  498. source_record_id = request.args.get('record_id') or request.form.get('source_record_id')
  499. prefilled_content = None
  500. source_oss_url = None
  501. if source_record_id:
  502. with conn.cursor() as cursor:
  503. cursor.execute("SELECT oss_url, ai_content, ai_status FROM genealogy_records WHERE id = %s", (source_record_id,))
  504. rec = cursor.fetchone()
  505. if rec:
  506. source_oss_url = rec['oss_url']
  507. # Check ai_status (2 = success)
  508. if rec['ai_status'] == 2 and rec['ai_content']:
  509. prefilled_content = rec['ai_content']
  510. if request.method == 'POST':
  511. # 处理生日转换为 Unix 时间戳
  512. birthday_str = request.form.get('birthday')
  513. birthday_ts = 0
  514. if birthday_str:
  515. try:
  516. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  517. except ValueError:
  518. birthday_ts = 0
  519. # 关系数据
  520. related_mid = request.form.get('related_mid')
  521. relation_type = request.form.get('relation_type')
  522. sub_relation_type = request.form.get('sub_relation_type', 0)
  523. # 年龄校验逻辑
  524. if related_mid and relation_type in ['1', '2']: # 1:父子 2:母子
  525. with conn.cursor() as cursor:
  526. cursor.execute("SELECT name, birthday FROM family_member_info WHERE id = %s", (related_mid,))
  527. parent = cursor.fetchone()
  528. if parent and parent['birthday'] > 0 and birthday_ts > 0:
  529. if birthday_ts < parent['birthday']:
  530. error_msg = f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。"
  531. flash(error_msg)
  532. # Re-fetch data for rendering
  533. cursor.execute("SELECT id, name FROM family_member_info ORDER BY name")
  534. all_members = cursor.fetchall()
  535. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  536. images = cursor.fetchall()
  537. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  538. return jsonify({
  539. "success": False,
  540. "message": error_msg
  541. }), 400
  542. return render_template('add_member.html', all_members=all_members, images=images,
  543. prefilled_content=prefilled_content, source_oss_url=source_oss_url, source_record_id=source_record_id)
  544. # 获取表单数据
  545. data = {
  546. 'name': request.form['name'],
  547. 'simplified_name': request.form.get('simplified_name'),
  548. 'former_name': request.form.get('former_name'),
  549. 'childhood_name': request.form.get('childhood_name'),
  550. 'name_word': request.form.get('name_word'),
  551. 'name_word_generation': request.form.get('name_word_generation'),
  552. 'name_title': request.form.get('name_title'),
  553. 'sex': request.form['sex'],
  554. 'birthday': birthday_ts,
  555. 'is_pass_away': request.form.get('is_pass_away', 0),
  556. 'marital_status': request.form.get('marital_status', 0),
  557. 'birth_place': request.form.get('birth_place'),
  558. 'branch_family_hall': request.form.get('branch_family_hall'),
  559. 'cluster_place': request.form.get('cluster_place'),
  560. 'nation': request.form.get('nation'),
  561. 'residential_address': request.form.get('residential_address'),
  562. 'phone': request.form.get('phone'),
  563. 'mail': request.form.get('mail'),
  564. 'wechat_account': request.form.get('wechat_account'),
  565. 'id_number': request.form.get('id_number'),
  566. 'occupation': request.form.get('occupation'),
  567. 'educational': request.form.get('educational'),
  568. 'blood_type': request.form.get('blood_type'),
  569. 'religion': request.form.get('religion'),
  570. 'hobbies': request.form.get('hobbies'),
  571. 'personal_achievements': request.form.get('personal_achievements'),
  572. 'family_rank': request.form.get('family_rank'),
  573. 'tags': request.form.get('tags'),
  574. 'notes': request.form.get('notes'),
  575. 'source_record_id': request.form.get('source_record_id') or None # Save source record ID
  576. }
  577. # ... (rest of logic) ...
  578. with conn.cursor() as cursor:
  579. fields = ", ".join(data.keys())
  580. placeholders = ", ".join(["%s"] * len(data))
  581. sql = f"INSERT INTO family_member_info ({fields}) VALUES ({placeholders})"
  582. cursor.execute(sql, list(data.values()))
  583. member_id = cursor.lastrowid
  584. # 录入关系
  585. if related_mid and relation_type:
  586. rel_type = int(relation_type)
  587. parent_mid = int(related_mid)
  588. child_mid = member_id
  589. gen_diff = 1 if rel_type in [1, 2] else 0
  590. sql_relation = """
  591. INSERT INTO family_relation_info
  592. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff)
  593. VALUES (%s, %s, %s, %s, %s, %s)
  594. """
  595. cursor.execute(sql_relation, (parent_mid, child_mid, rel_type, sub_relation_type, member_id, gen_diff))
  596. # Update AI Record Status if applicable
  597. source_record_id = data.get('source_record_id')
  598. source_index = request.form.get('source_index')
  599. if source_record_id and source_index and source_index.isdigit():
  600. try:
  601. idx = int(source_index)
  602. cursor.execute("SELECT ai_content FROM genealogy_records WHERE id = %s FOR UPDATE", (source_record_id,))
  603. rec = cursor.fetchone()
  604. if rec and rec['ai_content']:
  605. import json
  606. content = json.loads(rec['ai_content'])
  607. # Ensure content is a list (it might be a dict if single object, though we try to normalize)
  608. if isinstance(content, dict):
  609. content = [content]
  610. if isinstance(content, list):
  611. updated = False
  612. if 0 <= idx < len(content):
  613. if not content[idx].get('is_imported'): # Avoid redundant updates
  614. content[idx]['is_imported'] = True
  615. content[idx]['imported_member_id'] = member_id
  616. updated = True
  617. if updated:
  618. new_content = json.dumps(content, ensure_ascii=False)
  619. cursor.execute("UPDATE genealogy_records SET ai_content = %s WHERE id = %s", (new_content, source_record_id))
  620. except Exception as e:
  621. print(f"Error updating AI content status: {e}")
  622. conn.commit()
  623. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  624. return jsonify({"success": True, "message": "成员录入成功", "member_id": member_id})
  625. flash('成员录入成功')
  626. return redirect(url_for('members'))
  627. with conn.cursor() as cursor:
  628. cursor.execute("SELECT id, name FROM family_member_info ORDER BY name")
  629. all_members = cursor.fetchall()
  630. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  631. images = cursor.fetchall()
  632. except Exception as e:
  633. flash(f'发生错误: {e}')
  634. all_members = []
  635. images = []
  636. finally:
  637. conn.close()
  638. return render_template('add_member.html', all_members=all_members, images=images,
  639. prefilled_content=prefilled_content, source_oss_url=source_oss_url, source_record_id=source_record_id)
  640. @app.route('/manager/edit_member/<int:member_id>', methods=['GET', 'POST'])
  641. def edit_member(member_id):
  642. if 'user_id' not in session:
  643. return redirect(url_for('login'))
  644. conn = get_db_connection()
  645. try:
  646. if request.method == 'POST':
  647. birthday_str = request.form.get('birthday')
  648. birthday_ts = 0
  649. if birthday_str:
  650. try:
  651. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  652. except ValueError:
  653. birthday_ts = 0
  654. # 关系数据
  655. related_mid = request.form.get('related_mid')
  656. relation_type = request.form.get('relation_type')
  657. sub_relation_type = request.form.get('sub_relation_type', 0)
  658. # 年龄校验逻辑
  659. if related_mid and relation_type in ['1', '2']:
  660. with conn.cursor() as cursor:
  661. cursor.execute("SELECT name, birthday FROM family_member_info WHERE id = %s", (related_mid,))
  662. parent = cursor.fetchone()
  663. if parent and parent['birthday'] > 0 and birthday_ts > 0:
  664. if birthday_ts < parent['birthday']:
  665. flash(f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。")
  666. # 重新加载编辑页所需数据
  667. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  668. member = cursor.fetchone()
  669. member['birthday_date'] = birthday_str # 保持用户输入
  670. cursor.execute("SELECT id, name FROM family_member_info WHERE id != %s ORDER BY name", (member_id,))
  671. all_members = cursor.fetchall()
  672. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  673. images = cursor.fetchall()
  674. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  675. return jsonify({
  676. "success": False,
  677. "message": f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。"
  678. }), 400
  679. return render_template('add_member.html', member=member, images=images, all_members=all_members)
  680. data = {
  681. 'name': request.form['name'],
  682. 'simplified_name': request.form.get('simplified_name'),
  683. 'former_name': request.form.get('former_name'),
  684. 'childhood_name': request.form.get('childhood_name'),
  685. 'name_word': request.form.get('name_word'),
  686. 'name_word_generation': request.form.get('name_word_generation'),
  687. 'name_title': request.form.get('name_title'),
  688. 'sex': request.form['sex'],
  689. 'birthday': birthday_ts,
  690. 'is_pass_away': request.form.get('is_pass_away', 0),
  691. 'marital_status': request.form.get('marital_status', 0),
  692. 'birth_place': request.form.get('birth_place'),
  693. 'branch_family_hall': request.form.get('branch_family_hall'),
  694. 'cluster_place': request.form.get('cluster_place'),
  695. 'nation': request.form.get('nation'),
  696. 'residential_address': request.form.get('residential_address'),
  697. 'phone': request.form.get('phone'),
  698. 'mail': request.form.get('mail'),
  699. 'wechat_account': request.form.get('wechat_account'),
  700. 'id_number': request.form.get('id_number'),
  701. 'occupation': request.form.get('occupation'),
  702. 'educational': request.form.get('educational'),
  703. 'blood_type': request.form.get('blood_type'),
  704. 'religion': request.form.get('religion'),
  705. 'hobbies': request.form.get('hobbies'),
  706. 'personal_achievements': request.form.get('personal_achievements'),
  707. 'family_rank': request.form.get('family_rank'),
  708. 'tags': request.form.get('tags'),
  709. 'notes': request.form.get('notes'),
  710. 'source_record_id': request.form.get('source_record_id') or None
  711. }
  712. # 关系数据
  713. related_mid = request.form.get('related_mid')
  714. relation_type = request.form.get('relation_type')
  715. sub_relation_type = request.form.get('sub_relation_type', 0)
  716. with conn.cursor() as cursor:
  717. update_parts = [f"{k} = %s" for k in data.keys()]
  718. sql = f"UPDATE family_member_info SET {', '.join(update_parts)} WHERE id = %s"
  719. cursor.execute(sql, list(data.values()) + [member_id])
  720. # 更新关系
  721. if related_mid and relation_type:
  722. rel_type = int(relation_type)
  723. cursor.execute("DELETE FROM family_relation_info WHERE source_mid = %s", (member_id,))
  724. parent_mid = int(related_mid)
  725. child_mid = member_id
  726. gen_diff = 1 if rel_type in [1, 2] else 0
  727. sql_relation = """
  728. INSERT INTO family_relation_info
  729. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff)
  730. VALUES (%s, %s, %s, %s, %s, %s)
  731. """
  732. cursor.execute(sql_relation, (parent_mid, child_mid, rel_type, sub_relation_type, member_id, gen_diff))
  733. # Update AI Record Status if applicable
  734. source_record_id = data.get('source_record_id')
  735. source_index = request.form.get('source_index')
  736. if source_record_id and source_index and source_index.isdigit():
  737. try:
  738. idx = int(source_index)
  739. cursor.execute("SELECT ai_content FROM genealogy_records WHERE id = %s FOR UPDATE", (source_record_id,))
  740. rec = cursor.fetchone()
  741. if rec and rec['ai_content']:
  742. import json
  743. content = json.loads(rec['ai_content'])
  744. if isinstance(content, dict):
  745. content = [content]
  746. if isinstance(content, list):
  747. updated = False
  748. if 0 <= idx < len(content):
  749. if not content[idx].get('is_imported'): # Avoid redundant updates
  750. content[idx]['is_imported'] = True
  751. content[idx]['imported_member_id'] = member_id
  752. updated = True
  753. if updated:
  754. new_content = json.dumps(content, ensure_ascii=False)
  755. cursor.execute("UPDATE genealogy_records SET ai_content = %s WHERE id = %s", (new_content, source_record_id))
  756. except Exception as e:
  757. print(f"Error updating AI content status: {e}")
  758. conn.commit()
  759. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  760. return jsonify({"success": True, "message": "成员信息更新成功"})
  761. flash('成员信息更新成功')
  762. return redirect(url_for('members'))
  763. with conn.cursor() as cursor:
  764. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  765. member = cursor.fetchone()
  766. if not member:
  767. flash('成员不存在')
  768. return redirect(url_for('members'))
  769. # 格式化日期供显示
  770. if member.get('birthday'):
  771. member['birthday_date'] = format_timestamp(member['birthday'])
  772. # 获取现有关系
  773. cursor.execute("SELECT * FROM family_relation_info WHERE source_mid = %s LIMIT 1", (member_id,))
  774. current_relation = cursor.fetchone()
  775. cursor.execute("SELECT id, name FROM family_member_info WHERE id != %s ORDER BY name", (member_id,))
  776. all_members = cursor.fetchall()
  777. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  778. images = cursor.fetchall()
  779. finally:
  780. conn.close()
  781. return render_template('add_member.html', member=member, images=images, all_members=all_members, current_relation=current_relation)
  782. @app.route('/manager/member_detail/<int:member_id>')
  783. def member_detail(member_id):
  784. if 'user_id' not in session:
  785. return redirect(url_for('login'))
  786. conn = get_db_connection()
  787. try:
  788. with conn.cursor() as cursor:
  789. # Join with genealogy_records to get source image info
  790. sql = """
  791. SELECT m.*, r.oss_url as source_image_url, r.page_number as source_page
  792. FROM family_member_info m
  793. LEFT JOIN genealogy_records r ON m.source_record_id = r.id
  794. WHERE m.id = %s
  795. """
  796. cursor.execute(sql, (member_id,))
  797. member = cursor.fetchone()
  798. if not member:
  799. flash('成员不存在')
  800. return redirect(url_for('members'))
  801. member['birthday_str'] = format_timestamp(member.get('birthday'))
  802. # 获取关系
  803. cursor.execute("""
  804. SELECT m.id, m.name, r.relation_type
  805. FROM family_relation_info r
  806. JOIN family_member_info m ON r.parent_mid = m.id
  807. WHERE r.child_mid = %s
  808. """, (member_id,))
  809. parents = cursor.fetchall()
  810. cursor.execute("""
  811. SELECT m.id, m.name, r.relation_type
  812. FROM family_relation_info r
  813. JOIN family_member_info m ON r.child_mid = m.id
  814. WHERE r.parent_mid = %s
  815. """, (member_id,))
  816. children = cursor.fetchall()
  817. finally:
  818. conn.close()
  819. return render_template('member_detail.html', member=member, parents=parents, children=children)
  820. @app.route('/manager/delete_member/<int:member_id>', methods=['POST'])
  821. def delete_member(member_id):
  822. if 'user_id' not in session:
  823. return jsonify({"success": False, "message": "Unauthorized"}), 401
  824. conn = get_db_connection()
  825. try:
  826. with conn.cursor() as cursor:
  827. # 1. 删除关系表中关联该成员的所有记录
  828. cursor.execute("DELETE FROM family_relation_info WHERE parent_mid = %s OR child_mid = %s OR source_mid = %s",
  829. (member_id, member_id, member_id))
  830. # 2. 删除成员本身
  831. cursor.execute("DELETE FROM family_member_info WHERE id = %s", (member_id,))
  832. conn.commit()
  833. flash('成员及其关系已成功删除')
  834. return redirect(url_for('members'))
  835. except Exception as e:
  836. conn.rollback()
  837. flash(f'删除失败: {e}')
  838. return redirect(url_for('members'))
  839. finally:
  840. conn.close()
  841. @app.route('/manager/login', methods=['GET', 'POST'])
  842. def login():
  843. if request.method == 'POST':
  844. username = request.form['username']
  845. password = request.form['password']
  846. try:
  847. conn = get_db_connection()
  848. try:
  849. with conn.cursor() as cursor:
  850. cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password))
  851. user = cursor.fetchone()
  852. if user:
  853. session['user_id'] = user['id']
  854. session['username'] = user['username']
  855. return redirect(url_for('index'))
  856. else:
  857. flash('用户名或密码错误')
  858. finally:
  859. conn.close()
  860. except Exception as e:
  861. flash(f'数据库连接错误: {str(e)}')
  862. print(f'Login error: {str(e)}')
  863. return render_template('login.html')
  864. @app.route('/manager/logout')
  865. def logout():
  866. session.clear()
  867. return redirect(url_for('login'))
  868. import requests
  869. import json
  870. import re
  871. @app.route('/manager/api/recognize_image', methods=['POST'])
  872. def recognize_image():
  873. if 'user_id' not in session:
  874. return jsonify({"success": False, "message": "Unauthorized"}), 401
  875. data = request.json
  876. image_url = data.get('image_url')
  877. if not image_url:
  878. return jsonify({"success": False, "message": "No image URL provided"}), 400
  879. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  880. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  881. prompt = """
  882. 请分析这张家谱图片,提取其中关于人物的信息。
  883. 请务必将繁体字转换为简体字(original_name 字段除外)。
  884. 特别注意:'name' 字段必须是纯简体中文,不能包含繁体字(例如:'學'应转换为'学','劉'应转换为'刘','萬'应转换为'万')。
  885. 请提取以下字段(如果存在):
  886. - original_name: 原始姓名(严格保持图片上的繁体字,不做任何修改或转换)
  887. - name: 简体姓名(必须转换为简体中文,去除不需要的敬称)
  888. - sex: 性别(男/女)
  889. - birthday: 出生日期(尝试转换为YYYY-MM-DD格式,如果无法确定年份可只填月日)
  890. - father_name: 父亲姓名
  891. - spouse_name: 配偶姓名
  892. - generation: 第几世/代数
  893. - name_word: 字辈(例如名字为“学勤公”,“学”为字辈;提取名字中的字辈信息)
  894. - education: 学历/功名
  895. - title: 官职/称号
  896. 请严格以JSON列表格式返回,不要包含Markdown代码块标记(如 ```json ... ```),直接返回JSON数组。
  897. 如果包含多个人物,请都提取出来。
  898. """
  899. payload = {
  900. "model": "doubao-seed-1-8-251228",
  901. "stream": True,
  902. "input": [
  903. {
  904. "role": "user",
  905. "content": [
  906. {
  907. "type": "input_image",
  908. "image_url": image_url
  909. },
  910. {
  911. "type": "input_text",
  912. "text": prompt
  913. }
  914. ]
  915. }
  916. ]
  917. }
  918. headers = {
  919. "Authorization": f"Bearer {api_key}",
  920. "Content-Type": "application/json"
  921. }
  922. def generate():
  923. yield "正在连接 AI 服务...\n"
  924. try:
  925. # 使用 stream=True, timeout=120
  926. # 增加 verify=False 以防 SSL 问题(开发环境)
  927. # 增加 proxies=None 以防本地代理干扰
  928. with requests.post(
  929. api_url,
  930. json=payload,
  931. headers=headers,
  932. stream=True,
  933. timeout=1200,
  934. verify=False,
  935. proxies={"http": None, "https": None}
  936. ) as r:
  937. if r.status_code != 200:
  938. yield f"Error: API returned status code {r.status_code}. Response: {r.text}"
  939. return
  940. yield "连接成功,正在等待 AI 响应...\n"
  941. full_reasoning = ""
  942. json_started = False
  943. for line in r.iter_lines():
  944. if line:
  945. line_str = line.decode('utf-8')
  946. if line_str.startswith('data: '):
  947. json_str = line_str[6:]
  948. if json_str.strip() == '[DONE]':
  949. break
  950. try:
  951. chunk = json.loads(json_str)
  952. # 处理 standard OpenAI choices format (content)
  953. if 'choices' in chunk and len(chunk['choices']) > 0:
  954. delta = chunk['choices'][0].get('delta', {})
  955. if 'content' in delta:
  956. if not json_started:
  957. yield "|||JSON_START|||"
  958. json_started = True
  959. yield delta['content']
  960. # 处理 standard OpenAI choices format (reasoning_content) if any
  961. if 'reasoning_content' in delta:
  962. yield f"\n[推理]: {delta['reasoning_content']}"
  963. # 处理 Doubao/Volcano specific formats
  964. # Type: response.reasoning_summary_text.delta
  965. if chunk.get('type') == 'response.reasoning_summary_text.delta':
  966. if 'delta' in chunk:
  967. yield chunk['delta']
  968. # Type: response.text.delta
  969. if chunk.get('type') == 'response.text.delta':
  970. if 'delta' in chunk:
  971. if not json_started:
  972. yield "|||JSON_START|||"
  973. json_started = True
  974. yield chunk['delta']
  975. # Type: response.output_item.added (May contain initial content or status)
  976. # Type: response.reasoning_summary_part.added
  977. except Exception as e:
  978. print(f"Chunk parse error: {e}")
  979. else:
  980. # 尝试直接解析非 data: 开头的行
  981. try:
  982. chunk = json.loads(line_str)
  983. if 'choices' in chunk and len(chunk['choices']) > 0:
  984. content = chunk['choices'][0]['message']['content']
  985. yield content
  986. except:
  987. pass
  988. except Exception as e:
  989. yield f"\n[Error: {str(e)}]"
  990. return Response(stream_with_context(generate()), mimetype='text/plain')
  991. @app.route('/manager/api/start_analysis/<int:record_id>', methods=['POST'])
  992. def start_analysis(record_id):
  993. if 'user_id' not in session:
  994. return jsonify({"success": False, "message": "Unauthorized"}), 401
  995. conn = get_db_connection()
  996. try:
  997. with conn.cursor() as cursor:
  998. # Check if record exists
  999. cursor.execute("SELECT oss_url, ai_status FROM genealogy_records WHERE id = %s", (record_id,))
  1000. record = cursor.fetchone()
  1001. if not record:
  1002. return jsonify({"success": False, "message": "Record not found"}), 404
  1003. # Update status to processing (1)
  1004. cursor.execute("UPDATE genealogy_records SET ai_status = 1 WHERE id = %s", (record_id,))
  1005. conn.commit()
  1006. # Start background task
  1007. threading.Thread(target=process_ai_task, args=(record_id, record['oss_url'])).start()
  1008. return jsonify({"success": True, "message": "Analysis started"})
  1009. except Exception as e:
  1010. return jsonify({"success": False, "message": str(e)}), 500
  1011. finally:
  1012. conn.close()
  1013. @app.route('/manager/upload', methods=['GET', 'POST'])
  1014. def upload():
  1015. if 'user_id' not in session:
  1016. return redirect(url_for('login'))
  1017. # 获取建议页码 (当前最大页码 + 1)
  1018. conn = get_db_connection()
  1019. suggested_page = 1
  1020. try:
  1021. with conn.cursor() as cursor:
  1022. cursor.execute("SELECT MAX(page_number) as max_p FROM genealogy_records")
  1023. result = cursor.fetchone()
  1024. if result and result['max_p']:
  1025. suggested_page = result['max_p'] + 1
  1026. finally:
  1027. conn.close()
  1028. if request.method == 'POST':
  1029. if 'file' not in request.files:
  1030. flash('未选择文件')
  1031. return redirect(request.url)
  1032. file = request.files['file']
  1033. if file.filename == '':
  1034. flash('未选择文件')
  1035. return redirect(request.url)
  1036. # 允许用户在上传时直接指定页码,或者由 OCR 识别
  1037. manual_page = request.form.get('manual_page')
  1038. if file:
  1039. filename = secure_filename(file.filename)
  1040. file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  1041. file.save(file_path)
  1042. # 1. 尝试 OCR 提取页码
  1043. page_num = extract_page_number(file_path)
  1044. # 如果 OCR 没识别到,且用户也没手动输入,则跳转到“补录页码”页面
  1045. if not page_num and not manual_page:
  1046. # 先把文件传到 OSS,拿到 URL,方便后续补录
  1047. oss_url = upload_to_oss(file_path)
  1048. if oss_url:
  1049. # 暂时存入 session 或者跳转带参数
  1050. return render_template('confirm_page.html',
  1051. filename=filename,
  1052. oss_url=oss_url,
  1053. suggested_page=suggested_page)
  1054. else:
  1055. flash('上传 OSS 失败')
  1056. return redirect(url_for('upload'))
  1057. # 使用识别到的或手动输入的页码
  1058. final_page = manual_page if manual_page else page_num
  1059. oss_url = upload_to_oss(file_path)
  1060. if oss_url:
  1061. conn = get_db_connection()
  1062. try:
  1063. with conn.cursor() as cursor:
  1064. sql = "INSERT INTO genealogy_records (file_name, oss_url, page_number, ai_status) VALUES (%s, %s, %s, 1)"
  1065. cursor.execute(sql, (filename, oss_url, final_page))
  1066. record_id = cursor.lastrowid
  1067. conn.commit()
  1068. # Start AI Task
  1069. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  1070. flash(f'上传成功!页码:{final_page},已加入解析队列')
  1071. except Exception as e:
  1072. flash(f'数据库错误:{e}')
  1073. finally:
  1074. conn.close()
  1075. return redirect(url_for('index'))
  1076. else:
  1077. flash('上传 OSS 失败')
  1078. return render_template('upload.html', suggested_page=suggested_page)
  1079. @app.route('/manager/save_upload', methods=['POST'])
  1080. def save_upload():
  1081. if 'user_id' not in session: return redirect(url_for('login'))
  1082. filename = request.form.get('filename')
  1083. oss_url = request.form.get('oss_url')
  1084. page_number = request.form.get('page_number')
  1085. if not oss_url or not page_number:
  1086. flash('页码不能为空')
  1087. return redirect(url_for('upload'))
  1088. conn = get_db_connection()
  1089. try:
  1090. with conn.cursor() as cursor:
  1091. sql = "INSERT INTO genealogy_records (file_name, oss_url, page_number, ai_status) VALUES (%s, %s, %s, 1)"
  1092. cursor.execute(sql, (filename, oss_url, page_number))
  1093. record_id = cursor.lastrowid
  1094. conn.commit()
  1095. # Start AI Task
  1096. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  1097. flash(f'记录已保存,页码:{page_number},已加入解析队列')
  1098. except Exception as e:
  1099. flash(f'保存失败: {e}')
  1100. finally:
  1101. conn.close()
  1102. return redirect(url_for('index'))
  1103. @app.route('/manager/delete_upload/<int:record_id>', methods=['POST'])
  1104. def delete_upload(record_id):
  1105. if 'user_id' not in session:
  1106. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1107. conn = get_db_connection()
  1108. try:
  1109. with conn.cursor() as cursor:
  1110. # 删除记录
  1111. cursor.execute("DELETE FROM genealogy_records WHERE id = %s", (record_id,))
  1112. conn.commit()
  1113. flash('文件记录已成功删除')
  1114. return redirect(url_for('index'))
  1115. except Exception as e:
  1116. conn.rollback()
  1117. flash(f'删除失败: {e}')
  1118. return redirect(url_for('index'))
  1119. finally:
  1120. conn.close()
  1121. if __name__ == '__main__':
  1122. app.run(debug=True, port=5001)