Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,12 @@ Multi-portal UI integrated: Parent (with Interactive Scale/Assessment), Doctor,
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
|
|
|
| 9 |
import logging
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
_backend_dir = os.path.join(os.path.dirname(__file__), "backend")
|
| 12 |
sys.path.insert(0, _backend_dir)
|
| 13 |
_data_dir = os.path.join(os.path.dirname(__file__), "data")
|
|
@@ -25,10 +29,30 @@ def _zerogpu_placeholder():
|
|
| 25 |
# 模拟 12D 评估量表题库数据 (Assessment Scale Item Bank)
|
| 26 |
# -------------------------------------------------------------------
|
| 27 |
SCALE_ITEMS = [
|
| 28 |
-
{
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
]
|
| 33 |
|
| 34 |
# -------------------------------------------------------------------
|
|
@@ -38,48 +62,70 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 38 |
gr.Markdown(
|
| 39 |
"""
|
| 40 |
# 💡 12D AI Assessment Engine V2.2
|
| 41 |
-
**LearningOS
|
| 42 |
|
| 43 |
---
|
| 44 |
"""
|
| 45 |
)
|
| 46 |
-
|
| 47 |
with gr.Tabs():
|
| 48 |
# ==================== 1. 👶 家长端 Portal (含评估量表) ====================
|
| 49 |
with gr.Tab("👶 家长端 (Parent Portal)"):
|
| 50 |
gr.Markdown("### 👶 家长工作台 — 儿童档案与 12D 认知能力评估量表")
|
| 51 |
-
|
| 52 |
with gr.Row():
|
| 53 |
# 左侧:档案管理
|
| 54 |
with gr.Column(scale=1):
|
| 55 |
p_name = gr.Textbox(label="孩子姓名/昵称", value="小敏")
|
| 56 |
p_age = gr.Number(label="年龄", value=7, precision=0)
|
| 57 |
-
p_gender = gr.Dropdown(
|
|
|
|
|
|
|
| 58 |
p_create_btn = gr.Button("➕ 建立儿童档案", variant="primary")
|
| 59 |
p_create_output = gr.JSON(label="档案创建结果")
|
| 60 |
-
|
| 61 |
# 右侧:量表测评答题区
|
| 62 |
with gr.Column(scale=2):
|
| 63 |
p_child_id = gr.Textbox(label="儿童 ID", value="child_demo_888")
|
| 64 |
-
p_start_assess_btn = gr.Button(
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
# 评估量表交互区域
|
| 67 |
with gr.Group(visible=False) as scale_group:
|
| 68 |
gr.Markdown("#### 📋 12D AI 认知能力快速筛查量表")
|
| 69 |
-
q1_radio = gr.Radio(
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
p_assess_output = gr.JSON(label="评估 Session & 答题结果")
|
| 77 |
p_report_output = gr.JSON(label="📊 智能生成 12D 认知测评报告")
|
| 78 |
|
| 79 |
-
#逻辑响应
|
| 80 |
def _parent_create_child(name, age, gender):
|
| 81 |
-
return {
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def _parent_start_assess(child_id):
|
| 84 |
if not child_id:
|
| 85 |
return gr.update(visible=False), {"error": "请输入有效的儿童 ID"}
|
|
@@ -88,17 +134,18 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 88 |
"session_id": "sess_12d_999",
|
| 89 |
"child_id": child_id,
|
| 90 |
"total_items": len(SCALE_ITEMS),
|
| 91 |
-
"message": "量表加载成功,请在下方完成评估量表勾选。"
|
| 92 |
}
|
| 93 |
return gr.update(visible=True), session_data
|
| 94 |
|
| 95 |
def _parent_submit_scale(child_id, a1, a2, a3, a4):
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
s1 = score_map.get(a1, 70)
|
| 103 |
s2 = score_map.get(a2, 70)
|
| 104 |
s3 = score_map.get(a3, 70)
|
|
@@ -112,18 +159,56 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 112 |
"专注力": s1,
|
| 113 |
"工作记忆": s2,
|
| 114 |
"抑制控制": s3,
|
| 115 |
-
"空间想象": s4
|
| 116 |
},
|
| 117 |
-
"strengths": [
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
-
|
| 122 |
-
return {"status": "completed", "submitted_answers": {"q1": a1, "q2": a2, "q3": a3, "q4": a4}}, report_data
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# ==================== 2. 👨⚕️ 医生端 Portal ====================
|
| 129 |
with gr.Tab("👨⚕️ 医生端 (Doctor Portal)"):
|
|
@@ -133,12 +218,16 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 133 |
d_child_id = gr.Textbox(label="患者/儿童 ID", value="child_demo_888")
|
| 134 |
d_adhd_score = gr.Slider(0, 10, value=2, label="ADHD 倾向评估分 (0-10)")
|
| 135 |
d_asd_score = gr.Slider(0, 10, value=1, label="ASD 倾向评估分 (0-10)")
|
| 136 |
-
d_notes = gr.TextArea(
|
|
|
|
|
|
|
| 137 |
d_submit_btn = gr.Button("💾 提交临床诊断报告", variant="primary")
|
| 138 |
d_submit_output = gr.JSON(label="诊断录入结果")
|
| 139 |
|
| 140 |
with gr.Column():
|
| 141 |
-
d_twin_btn = gr.Button(
|
|
|
|
|
|
|
| 142 |
d_twin_output = gr.JSON(label="深度多维认知矩阵")
|
| 143 |
|
| 144 |
def _doctor_submit_diag(child_id, adhd, asd, notes):
|
|
@@ -148,7 +237,7 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 148 |
"adhd_risk_index": adhd / 10.0,
|
| 149 |
"asd_risk_index": asd / 10.0,
|
| 150 |
"clinical_notes": notes or "未填写评语",
|
| 151 |
-
"message": "临床数据已保存至云端电子病历系统"
|
| 152 |
}
|
| 153 |
|
| 154 |
def _doctor_get_twin(child_id):
|
|
@@ -158,13 +247,19 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 158 |
"working_memory": 92,
|
| 159 |
"inhibitory_control": 78,
|
| 160 |
"cognitive_flexibility": 85,
|
| 161 |
-
"processing_speed": 88
|
| 162 |
},
|
| 163 |
-
"neurological_risk": "Low Risk"
|
| 164 |
}
|
| 165 |
|
| 166 |
-
d_submit_btn.click(
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
# ==================== 3. 🏫 学校端 Portal ====================
|
| 170 |
with gr.Tab("🏫 学校端 (School Portal)"):
|
|
@@ -178,11 +273,18 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 178 |
|
| 179 |
with gr.Column():
|
| 180 |
s_class_id = gr.Textbox(label="班级 ID", value="class_302")
|
| 181 |
-
s_analytics_btn = gr.Button(
|
|
|
|
|
|
|
| 182 |
s_analytics_output = gr.JSON(label="班级报告与预警名单")
|
| 183 |
|
| 184 |
def _school_batch_import(school, cls):
|
| 185 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
def _school_get_analytics(cls_id):
|
| 188 |
return {
|
|
@@ -190,11 +292,19 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 190 |
"total_students": 45,
|
| 191 |
"class_average_score": 81.5,
|
| 192 |
"dimension_averages": {"逻辑": 83, "记忆": 80, "专注": 77},
|
| 193 |
-
"attention_warning_list": ["学生_03", "学生_17", "学生_29"]
|
| 194 |
}
|
| 195 |
|
| 196 |
-
s_batch_btn.click(
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# ==================== 4. ⚙️ 系统与 API 状态 ====================
|
| 200 |
with gr.Tab("⚙️ 系统与 API 文档"):
|
|
@@ -214,6 +324,9 @@ with gr.Blocks(title="12D AI Assessment Engine V2.2", theme=gr.themes.Soft()) as
|
|
| 214 |
|
| 215 |
check_btn.click(fn=check_status, outputs=status_box)
|
| 216 |
|
|
|
|
|
|
|
|
|
|
| 217 |
demo.launch(
|
| 218 |
server_name="0.0.0.0",
|
| 219 |
server_port=7860,
|
|
@@ -224,7 +337,7 @@ demo.launch(
|
|
| 224 |
)
|
| 225 |
|
| 226 |
# -------------------------------------------------------------------
|
| 227 |
-
# FastAPI 路由注入与后台逻辑
|
| 228 |
# -------------------------------------------------------------------
|
| 229 |
from app.api.v1 import tenant, assessment, report, analytics, auth, child
|
| 230 |
from app.api.v1.endpoints import billing_audit_demo, offline_sync, compliance
|
|
@@ -249,18 +362,31 @@ _static_dir = os.path.join(os.path.dirname(__file__), "backend", "static")
|
|
| 249 |
os.makedirs(_static_dir, exist_ok=True)
|
| 250 |
app.mount("/wasm-static", StaticFiles(directory=_static_dir), name="wasm-static")
|
| 251 |
|
|
|
|
| 252 |
@app.get("/health")
|
| 253 |
def health_check():
|
| 254 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
@app.get("/api")
|
| 257 |
def api_info():
|
| 258 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
# -------------------------------------------------------------------
|
| 261 |
# 数据库与 Seed 初始化逻辑
|
| 262 |
# -------------------------------------------------------------------
|
| 263 |
-
logging.basicConfig(
|
|
|
|
|
|
|
| 264 |
_logger = logging.getLogger("main")
|
| 265 |
|
| 266 |
try:
|
|
@@ -268,21 +394,27 @@ try:
|
|
| 268 |
from app.models.base import Base
|
| 269 |
from app.models.auth import User, Role, Permission, Organization
|
| 270 |
from app.models.child import Child, Guardian
|
| 271 |
-
from app.models.assessment import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
from app.models.report import AssessmentReport
|
| 273 |
from app.models.tenant import Tenant
|
| 274 |
from app.core.audit_logger import AuditLog
|
|
|
|
| 275 |
Base.metadata.create_all(bind=engine)
|
| 276 |
_logger.info("Database tables created/verified.")
|
| 277 |
from scripts.seed_database import seed_data
|
|
|
|
| 278 |
seed_data()
|
| 279 |
_logger.info("Demo data seeded successfully.")
|
| 280 |
except Exception as e:
|
| 281 |
_logger.error(f"Startup initialization error: {e}")
|
| 282 |
|
|
|
|
| 283 |
if __name__ == "__main__":
|
| 284 |
try:
|
| 285 |
-
import time
|
| 286 |
while True:
|
| 287 |
time.sleep(3600)
|
| 288 |
except KeyboardInterrupt:
|
|
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
| 9 |
+
import time
|
| 10 |
import logging
|
| 11 |
|
| 12 |
+
# -------------------------------------------------------------------
|
| 13 |
+
# 环境路径与目录初始化
|
| 14 |
+
# -------------------------------------------------------------------
|
| 15 |
_backend_dir = os.path.join(os.path.dirname(__file__), "backend")
|
| 16 |
sys.path.insert(0, _backend_dir)
|
| 17 |
_data_dir = os.path.join(os.path.dirname(__file__), "data")
|
|
|
|
| 29 |
# 模拟 12D 评估量表题库数据 (Assessment Scale Item Bank)
|
| 30 |
# -------------------------------------------------------------------
|
| 31 |
SCALE_ITEMS = [
|
| 32 |
+
{
|
| 33 |
+
"id": "q1",
|
| 34 |
+
"dimension": "专注力 (Attention)",
|
| 35 |
+
"question": "1. 孩子在完成日常作业或游戏任务时,容易被无关刺激干扰吗?",
|
| 36 |
+
"options": ["从不", "偶有", "经常", "总是"],
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"id": "q2",
|
| 40 |
+
"dimension": "工作记忆 (Working Memory)",
|
| 41 |
+
"question": "2. 给孩子连续下达3个简单口头指令(如:关灯、拿书、坐下),能否准确执行?",
|
| 42 |
+
"options": ["完全不能", "能完成1项", "能完成2项", "全部准确完成"],
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"id": "q3",
|
| 46 |
+
"dimension": "抑制控制 (Inhibitory Control)",
|
| 47 |
+
"question": "3. 在需要轮流等待或遵守规则的场景下,孩子能否控制冲动?",
|
| 48 |
+
"options": ["极难控制", "需要提醒", "基本可以", "完全没问题"],
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"id": "q4",
|
| 52 |
+
"dimension": "空间想象 (Spatial Cognition)",
|
| 53 |
+
"question": "4. 孩子拼图或进行积木搭建时,对空间位置变换的感知表现如何?",
|
| 54 |
+
"options": ["较困难", "一般", "良好", "极优秀"],
|
| 55 |
+
},
|
| 56 |
]
|
| 57 |
|
| 58 |
# -------------------------------------------------------------------
|
|
|
|
| 62 |
gr.Markdown(
|
| 63 |
"""
|
| 64 |
# 💡 12D AI Assessment Engine V2.2
|
| 65 |
+
**LearningOS V2.0 Enterprise Edition** — 智能化儿童认知能力多端评估与诊断平台
|
| 66 |
|
| 67 |
---
|
| 68 |
"""
|
| 69 |
)
|
| 70 |
+
|
| 71 |
with gr.Tabs():
|
| 72 |
# ==================== 1. 👶 家长端 Portal (含评估量表) ====================
|
| 73 |
with gr.Tab("👶 家长端 (Parent Portal)"):
|
| 74 |
gr.Markdown("### 👶 家长工作台 — 儿童档案与 12D 认知能力评估量表")
|
| 75 |
+
|
| 76 |
with gr.Row():
|
| 77 |
# 左侧:档案管理
|
| 78 |
with gr.Column(scale=1):
|
| 79 |
p_name = gr.Textbox(label="孩子姓名/昵称", value="小敏")
|
| 80 |
p_age = gr.Number(label="年龄", value=7, precision=0)
|
| 81 |
+
p_gender = gr.Dropdown(
|
| 82 |
+
label="性别", choices=["男", "女", "其他"], value="男"
|
| 83 |
+
)
|
| 84 |
p_create_btn = gr.Button("➕ 建立儿童档案", variant="primary")
|
| 85 |
p_create_output = gr.JSON(label="档案创建结果")
|
| 86 |
+
|
| 87 |
# 右侧:量表测评答题区
|
| 88 |
with gr.Column(scale=2):
|
| 89 |
p_child_id = gr.Textbox(label="儿童 ID", value="child_demo_888")
|
| 90 |
+
p_start_assess_btn = gr.Button(
|
| 91 |
+
"🚀 加载/发起 12D 评估量表", variant="secondary"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
# 评估量表交互区域
|
| 95 |
with gr.Group(visible=False) as scale_group:
|
| 96 |
gr.Markdown("#### 📋 12D AI 认知能力快速筛查量表")
|
| 97 |
+
q1_radio = gr.Radio(
|
| 98 |
+
choices=SCALE_ITEMS[0]["options"],
|
| 99 |
+
label=f"【{SCALE_ITEMS[0]['dimension']}】 {SCALE_ITEMS[0]['question']}",
|
| 100 |
+
)
|
| 101 |
+
q2_radio = gr.Radio(
|
| 102 |
+
choices=SCALE_ITEMS[1]["options"],
|
| 103 |
+
label=f"【{SCALE_ITEMS[1]['dimension']}】 {SCALE_ITEMS[1]['question']}",
|
| 104 |
+
)
|
| 105 |
+
q3_radio = gr.Radio(
|
| 106 |
+
choices=SCALE_ITEMS[2]["options"],
|
| 107 |
+
label=f"【{SCALE_ITEMS[2]['dimension']}】 {SCALE_ITEMS[2]['question']}",
|
| 108 |
+
)
|
| 109 |
+
q4_radio = gr.Radio(
|
| 110 |
+
choices=SCALE_ITEMS[3]["options"],
|
| 111 |
+
label=f"【{SCALE_ITEMS[3]['dimension']}】 {SCALE_ITEMS[3]['question']}",
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
p_submit_answers_btn = gr.Button(
|
| 115 |
+
"📤 提交量表答案并计算分析", variant="primary"
|
| 116 |
+
)
|
| 117 |
|
| 118 |
p_assess_output = gr.JSON(label="评估 Session & 答题结果")
|
| 119 |
p_report_output = gr.JSON(label="📊 智能生成 12D 认知测评报告")
|
| 120 |
|
| 121 |
+
# 逻辑响应函数
|
| 122 |
def _parent_create_child(name, age, gender):
|
| 123 |
+
return {
|
| 124 |
+
"status": "success",
|
| 125 |
+
"message": f"成功为 {name} ({age}岁, {gender}) 建立档案",
|
| 126 |
+
"child_id": f"child_{name}_888",
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
def _parent_start_assess(child_id):
|
| 130 |
if not child_id:
|
| 131 |
return gr.update(visible=False), {"error": "请输入有效的儿童 ID"}
|
|
|
|
| 134 |
"session_id": "sess_12d_999",
|
| 135 |
"child_id": child_id,
|
| 136 |
"total_items": len(SCALE_ITEMS),
|
| 137 |
+
"message": "量表加载成功,请在下方完成评估量表勾选。",
|
| 138 |
}
|
| 139 |
return gr.update(visible=True), session_data
|
| 140 |
|
| 141 |
def _parent_submit_scale(child_id, a1, a2, a3, a4):
|
| 142 |
+
score_map = {
|
| 143 |
+
"从不": 100, "偶有": 80, "经常": 60, "总是": 40,
|
| 144 |
+
"完全不能": 40, "能完成1项": 60, "能完成2项": 80, "全部准确完成": 100,
|
| 145 |
+
"极难控制": 40, "需要提醒": 60, "基本可以": 80, "完全没问题": 100,
|
| 146 |
+
"较困难": 40, "一般": 60, "良好": 80, "极优秀": 100,
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
s1 = score_map.get(a1, 70)
|
| 150 |
s2 = score_map.get(a2, 70)
|
| 151 |
s3 = score_map.get(a3, 70)
|
|
|
|
| 159 |
"专注力": s1,
|
| 160 |
"工作记忆": s2,
|
| 161 |
"抑制控制": s3,
|
| 162 |
+
"空间想象": s4,
|
| 163 |
},
|
| 164 |
+
"strengths": [
|
| 165 |
+
k
|
| 166 |
+
for k, v in {
|
| 167 |
+
"专注力": s1,
|
| 168 |
+
"工作记忆": s2,
|
| 169 |
+
"抑制控制": s3,
|
| 170 |
+
"空间想象": s4,
|
| 171 |
+
}.items()
|
| 172 |
+
if v >= 80
|
| 173 |
+
],
|
| 174 |
+
"weaknesses": [
|
| 175 |
+
k
|
| 176 |
+
for k, v in {
|
| 177 |
+
"专注力": s1,
|
| 178 |
+
"工作记忆": s2,
|
| 179 |
+
"抑制控制": s3,
|
| 180 |
+
"空间想象": s4,
|
| 181 |
+
}.items()
|
| 182 |
+
if v < 70
|
| 183 |
+
],
|
| 184 |
+
"ai_suggestions": f"综合得分 {avg_score} 分。"
|
| 185 |
+
+ (
|
| 186 |
+
"建议多安排专注力训练游戏及户外有氧运动。"
|
| 187 |
+
if avg_score < 80
|
| 188 |
+
else "各项能力表现优异,保持当前引导策略!"
|
| 189 |
+
),
|
| 190 |
}
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
return {
|
| 193 |
+
"status": "completed",
|
| 194 |
+
"submitted_answers": {"q1": a1, "q2": a2, "q3": a3, "q4": a4},
|
| 195 |
+
}, report_data
|
| 196 |
+
|
| 197 |
+
p_create_btn.click(
|
| 198 |
+
_parent_create_child,
|
| 199 |
+
inputs=[p_name, p_age, p_gender],
|
| 200 |
+
outputs=p_create_output,
|
| 201 |
+
)
|
| 202 |
+
p_start_assess_btn.click(
|
| 203 |
+
_parent_start_assess,
|
| 204 |
+
inputs=[p_child_id],
|
| 205 |
+
outputs=[scale_group, p_assess_output],
|
| 206 |
+
)
|
| 207 |
+
p_submit_answers_btn.click(
|
| 208 |
+
_parent_submit_scale,
|
| 209 |
+
inputs=[p_child_id, q1_radio, q2_radio, q3_radio, q4_radio],
|
| 210 |
+
outputs=[p_assess_output, p_report_output],
|
| 211 |
+
)
|
| 212 |
|
| 213 |
# ==================== 2. 👨⚕️ 医生端 Portal ====================
|
| 214 |
with gr.Tab("👨⚕️ 医生端 (Doctor Portal)"):
|
|
|
|
| 218 |
d_child_id = gr.Textbox(label="患者/儿童 ID", value="child_demo_888")
|
| 219 |
d_adhd_score = gr.Slider(0, 10, value=2, label="ADHD 倾向评估分 (0-10)")
|
| 220 |
d_asd_score = gr.Slider(0, 10, value=1, label="ASD 倾向评估分 (0-10)")
|
| 221 |
+
d_notes = gr.TextArea(
|
| 222 |
+
label="临床诊断意见", placeholder="录入医生专业评语或干预方案..."
|
| 223 |
+
)
|
| 224 |
d_submit_btn = gr.Button("💾 提交临床诊断报告", variant="primary")
|
| 225 |
d_submit_output = gr.JSON(label="诊断录入结果")
|
| 226 |
|
| 227 |
with gr.Column():
|
| 228 |
+
d_twin_btn = gr.Button(
|
| 229 |
+
"🧬 提取临床级数字孪生 (Diagnostic Twin)", variant="secondary"
|
| 230 |
+
)
|
| 231 |
d_twin_output = gr.JSON(label="深度多维认知矩阵")
|
| 232 |
|
| 233 |
def _doctor_submit_diag(child_id, adhd, asd, notes):
|
|
|
|
| 237 |
"adhd_risk_index": adhd / 10.0,
|
| 238 |
"asd_risk_index": asd / 10.0,
|
| 239 |
"clinical_notes": notes or "未填写评语",
|
| 240 |
+
"message": "临床数据已保存至云端电子病历系统",
|
| 241 |
}
|
| 242 |
|
| 243 |
def _doctor_get_twin(child_id):
|
|
|
|
| 247 |
"working_memory": 92,
|
| 248 |
"inhibitory_control": 78,
|
| 249 |
"cognitive_flexibility": 85,
|
| 250 |
+
"processing_speed": 88,
|
| 251 |
},
|
| 252 |
+
"neurological_risk": "Low Risk",
|
| 253 |
}
|
| 254 |
|
| 255 |
+
d_submit_btn.click(
|
| 256 |
+
_doctor_submit_diag,
|
| 257 |
+
inputs=[d_child_id, d_adhd_score, d_asd_score, d_notes],
|
| 258 |
+
outputs=d_submit_output,
|
| 259 |
+
)
|
| 260 |
+
d_twin_btn.click(
|
| 261 |
+
_doctor_get_twin, inputs=[d_child_id], outputs=d_twin_output
|
| 262 |
+
)
|
| 263 |
|
| 264 |
# ==================== 3. 🏫 学校端 Portal ====================
|
| 265 |
with gr.Tab("🏫 学校端 (School Portal)"):
|
|
|
|
| 273 |
|
| 274 |
with gr.Column():
|
| 275 |
s_class_id = gr.Textbox(label="班级 ID", value="class_302")
|
| 276 |
+
s_analytics_btn = gr.Button(
|
| 277 |
+
"📈 生成班级群体认知分析", variant="secondary"
|
| 278 |
+
)
|
| 279 |
s_analytics_output = gr.JSON(label="班级报告与预警名单")
|
| 280 |
|
| 281 |
def _school_batch_import(school, cls):
|
| 282 |
+
return {
|
| 283 |
+
"status": "success",
|
| 284 |
+
"school": school,
|
| 285 |
+
"class": cls,
|
| 286 |
+
"imported_students": 45,
|
| 287 |
+
}
|
| 288 |
|
| 289 |
def _school_get_analytics(cls_id):
|
| 290 |
return {
|
|
|
|
| 292 |
"total_students": 45,
|
| 293 |
"class_average_score": 81.5,
|
| 294 |
"dimension_averages": {"逻辑": 83, "记忆": 80, "专注": 77},
|
| 295 |
+
"attention_warning_list": ["学生_03", "学生_17", "学生_29"],
|
| 296 |
}
|
| 297 |
|
| 298 |
+
s_batch_btn.click(
|
| 299 |
+
_school_batch_import,
|
| 300 |
+
inputs=[s_school_name, s_class_name],
|
| 301 |
+
outputs=s_batch_output,
|
| 302 |
+
)
|
| 303 |
+
s_analytics_btn.click(
|
| 304 |
+
_school_get_analytics,
|
| 305 |
+
inputs=[s_class_id],
|
| 306 |
+
outputs=s_analytics_output,
|
| 307 |
+
)
|
| 308 |
|
| 309 |
# ==================== 4. ⚙️ 系统与 API 状态 ====================
|
| 310 |
with gr.Tab("⚙️ 系统与 API 文档"):
|
|
|
|
| 324 |
|
| 325 |
check_btn.click(fn=check_status, outputs=status_box)
|
| 326 |
|
| 327 |
+
# -------------------------------------------------------------------
|
| 328 |
+
# 启动 Gradio 前端服务器
|
| 329 |
+
# -------------------------------------------------------------------
|
| 330 |
demo.launch(
|
| 331 |
server_name="0.0.0.0",
|
| 332 |
server_port=7860,
|
|
|
|
| 337 |
)
|
| 338 |
|
| 339 |
# -------------------------------------------------------------------
|
| 340 |
+
# FastAPI 路由注入与后台逻辑
|
| 341 |
# -------------------------------------------------------------------
|
| 342 |
from app.api.v1 import tenant, assessment, report, analytics, auth, child
|
| 343 |
from app.api.v1.endpoints import billing_audit_demo, offline_sync, compliance
|
|
|
|
| 362 |
os.makedirs(_static_dir, exist_ok=True)
|
| 363 |
app.mount("/wasm-static", StaticFiles(directory=_static_dir), name="wasm-static")
|
| 364 |
|
| 365 |
+
|
| 366 |
@app.get("/health")
|
| 367 |
def health_check():
|
| 368 |
+
return {
|
| 369 |
+
"status": "healthy",
|
| 370 |
+
"engine": "12D AI Assessment Engine V2.2",
|
| 371 |
+
"version": settings.APP_VERSION,
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
|
| 375 |
@app.get("/api")
|
| 376 |
def api_info():
|
| 377 |
+
return {
|
| 378 |
+
"service": "12D AI Assessment Engine V2.2",
|
| 379 |
+
"docs": "/docs",
|
| 380 |
+
"health": "/health",
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
|
| 384 |
# -------------------------------------------------------------------
|
| 385 |
# 数据库与 Seed 初始化逻辑
|
| 386 |
# -------------------------------------------------------------------
|
| 387 |
+
logging.basicConfig(
|
| 388 |
+
level=logging.INFO, format="%(asctime)s | %(levelname)s | %(name)s | %(message)s"
|
| 389 |
+
)
|
| 390 |
_logger = logging.getLogger("main")
|
| 391 |
|
| 392 |
try:
|
|
|
|
| 394 |
from app.models.base import Base
|
| 395 |
from app.models.auth import User, Role, Permission, Organization
|
| 396 |
from app.models.child import Child, Guardian
|
| 397 |
+
from app.models.assessment import (
|
| 398 |
+
AssessmentItem,
|
| 399 |
+
AssessmentSession,
|
| 400 |
+
AssessmentResponse,
|
| 401 |
+
)
|
| 402 |
from app.models.report import AssessmentReport
|
| 403 |
from app.models.tenant import Tenant
|
| 404 |
from app.core.audit_logger import AuditLog
|
| 405 |
+
|
| 406 |
Base.metadata.create_all(bind=engine)
|
| 407 |
_logger.info("Database tables created/verified.")
|
| 408 |
from scripts.seed_database import seed_data
|
| 409 |
+
|
| 410 |
seed_data()
|
| 411 |
_logger.info("Demo data seeded successfully.")
|
| 412 |
except Exception as e:
|
| 413 |
_logger.error(f"Startup initialization error: {e}")
|
| 414 |
|
| 415 |
+
# 主进程保持运行状态
|
| 416 |
if __name__ == "__main__":
|
| 417 |
try:
|
|
|
|
| 418 |
while True:
|
| 419 |
time.sleep(3600)
|
| 420 |
except KeyboardInterrupt:
|