Download skill/scripts/domain_router.py from cpuai/ahp-development-workstation: direct link, hf CLI and curl.
- Browser
- Download file 6.19 kB
-
https://huggingface.co/spaces/cpuai/ahp-development-workstation/resolve/main/skill/scripts/domain_router.py
- Command line
-
hf download hf://spaces/cpuai/ahp-development-workstation/skill/scripts/domain_router.py
-
curl -L -o domain_router.py https://huggingface.co/spaces/cpuai/ahp-development-workstation/resolve/main/skill/scripts/domain_router.py
6.19 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """科普动画自动专业路由:优先按机制选解释结构与播音员,不能替代科学核对。 | |
| Usage: python scripts/domain_router.py --title "植物为什么需要水?" --audience elementary --output routing.json | |
| Only Python standard library is required. Works for new science topics, not only the three tests. | |
| """ | |
| import argparse,json,re | |
| from pathlib import Path | |
| PRESETS={ | |
| "astronomy":{"keywords":["白天","黑夜","昼夜","自转","地球","日食","月食","行星","太阳系","星球"],"structure":["生活中观察到的天象","明确观察坐标系和光源","解释天体相对运动","地点随时间变化","对照容易混淆的运动","实际验证与例外"],"diagram":"光源固定 + 天体转动 + 遮光分界 + 地面点轨迹;严格标示参考系与时间比例", "truth":["illumination_direction","rotation_vs_revolution","reference_frame","polar_exception"],"verification":["光源方向/昼夜面一致","旋转与公转有明显区分","时间数字有条件","极昼极夜例外不误导"]}, | |
| "biology":{"keywords":["植物","根","叶","光合作用","蒸腾","细胞","森林","生长","水分","花朵"],"structure":["植物外在现象","剖面显示器官","物质运输路线","细胞层面作用","能量/代谢过程","缺乏与过量的对照","安全观察实验"],"diagram":"根毛吸水→木质部上行→叶片光合与气孔蒸腾;不能把能量当物质移动", "truth":["root_uptake","xylem_upward","photosynthesis_inputs","stomata_transpiration","overwatering_risks"],"verification":["器官方向与箭头一致","糖碳源不是来自土壤","蒸腾水蒸气不可见","适量浇水而非越多越好"]}, | |
| "physics":{"keywords":["烧开","沸腾","气泡","泡泡","冰","汽化","蒸发","温度","热","压力","声音","光速","摩擦"],"structure":["物理现象与反直觉提问","关键变量/条件","微观或剖面示意","过程前后状态变化","排除常见错误解释","条件改变的反事实对照","安全观察"],"diagram":"真实过程方向、数量/时间变化,量纲和环境条件;微观图与日常画面分层", "truth":["early_dissolved_gases","boiling_water_vapor","phase_not_decomposition","boiling_pressure_condition","visible_mist_condensation"],"verification":["早期小泡和持续沸腾大泡区别","泡中是水蒸气并非氢氧","100摄氏度有标准大气压条件","白雾是冷凝水滴","安全禁止孩子独自靠近沸水"]}, | |
| "chemistry":{"keywords":["化学","酸碱","生锈","燃烧","氧化","溶解","分子","反应"],"structure":["观测现象","反应前材料与条件","微观粒子模型","变化前后对照","守恒与反例","安全演示"],"diagram":"粒子图必须守恒,标注配平条件;不可无依据混用微观与宏观颜色", "truth":["balanced_reaction","atom_conservation","safety"],"verification":["原子数量前后相同","无危险操作模仿","反应条件被明确说明"]}, | |
| "geography":{"keywords":["台风","降雨","地震","河流","山脉","海洋","季风","气候","天气"],"structure":["地点与观测现象","空间尺度与地图","形成机制的多因素","时间变化","地域差异","防灾行动"],"diagram":"地图有方向与比例定义;危险天气以权威监测为准", "truth":["scale_and_orientation","multi_factor_causality","risk_safety"],"verification":["地图方位正确","因果不归因于单一因素","防灾建议合规"]}, | |
| "general":{"keywords":[],"structure":["观察现象","提出明确问题","分解核心变量","逐步演示机制","核对反例/限制条件","实验/观察与总结"],"diagram":"先声明模型与假设,不允许空占位图或假装精确仿真", "truth":["core_mechanism","conditions","observable_evidence"],"verification":["科学因果与箭头一致","有条件及例外","科普提问能让孩子复述机制"]} | |
| } | |
| CAST={"child":{"speaker":"zh-CN-YunxiaNeural","rate":"+5%"},"warm_narrator":{"speaker":"zh-CN-XiaoxiaoNeural","rate":"+4%"},"expert":{"speaker":"zh-CN-YunyangNeural","rate":"+0%"},"warm_child":{"speaker":"zh-CN-XiaoyiNeural","rate":"+5%"},"energetic":{"speaker":"zh-CN-YunxiNeural","rate":"+5%"}} | |
| def route(title,preferred=None,audience="小学3-6年级"): | |
| if preferred and preferred not in PRESETS:raise ValueError(f"Unknown domain: {preferred}") | |
| scores={k:sum(4 if len(w)>1 else 1 for w in v["keywords"] if w in title) for k,v in PRESETS.items() if k!="general"} | |
| # Prioritize diagnostic core phenomenon over generic occurrence of water, light, etc. | |
| if any(k in title for k in ("沸腾","烧开","泡泡","气泡")):pred="physics" | |
| elif any(k in title for k in ("白天","黑夜","昼夜","自转")):pred="astronomy" | |
| elif any(k in title for k in ("植物","根","叶片","蒸腾")):pred="biology" | |
| else:pred=max(scores,key=scores.get) if any(scores.values()) else "general" | |
| domain=preferred or pred | |
| recipe=PRESETS[domain] | |
| return {"schema_version":"1.0.0","title":title,"audience":audience,"domain":domain,"confidence":"keyword heuristic; subject expert reviews","teaching_structure":recipe["structure"],"diagram_mode":recipe["diagram"],"required_mechanism_tags":recipe["truth"],"science_review_questions":recipe["verification"],"voices":{"narrator":CAST["warm_narrator"],"question":CAST["warm_child" if domain=="biology" else "child"],"expert":CAST["expert"]},"video":{"aspect_ratio":"9:16","width":1080,"height":1920,"fps":30,"estimated_seconds":"60-100"},"mandatory_gates":["authoritative_sources_opened","claims_to_scene_map","diagram_truth_check","actual_edge_tts_no_mock","caption_gap_45ms","hyperframes_zero_errors","keyframe_science_review","ffprobe_h264_aac","full_decode","release_zip_integrity"],"manual_science_review_required":True} | |
| if __name__=='__main__': | |
| p=argparse.ArgumentParser();p.add_argument('--title',required=True);p.add_argument('--domain',choices=PRESETS);p.add_argument('--audience',default='小学3–6年级');p.add_argument('--output');a=p.parse_args() | |
| r=route(a.title,a.domain,a.audience);o=json.dumps(r,ensure_ascii=False,indent=2) | |
| if a.output:Path(a.output).write_text(o,encoding='utf-8') | |
| print(o) | |