Compare commits

3 Commits
5 changed files with 656 additions and 90 deletions
+468 -83
View File
@@ -18,6 +18,7 @@ from src.llm.client import LLMClient
from src.skills.hypothesis_generator import HypothesisGeneratorSkill
from src.skills.hypothesis_critic import HypothesisCriticSkill
from src.skills.plan_writer import ResearchPlanWriterSkill
from src.utils.parser import parse_json_robust, extract_hypotheses_from_text
from src.config import Config
# 页面配置
@@ -64,18 +65,200 @@ st.markdown("""
color: #1e1b4b;
margin-bottom: 12px;
}
.wizard-bar {
/* ===== 新版圆形步骤进度条 ===== */
.wizard-stepper {
display: flex;
justify-content: space-between;
background-color: #f8fafc;
padding: 12px 20px;
border-radius: 8px;
margin-bottom: 20px;
align-items: flex-start;
justify-content: center;
gap: 0;
margin-bottom: 28px;
padding: 20px 10px 10px;
background: linear-gradient(135deg, #f8fafc 0%, #eef2ff 100%);
border-radius: 12px;
border: 1px solid #e2e8f0;
}
.w-step { font-weight: 600; color: #94a3b8; }
.w-step.active { color: #6366f1; font-weight: 700; }
.w-step.done { color: #10b981; }
.wizard-node {
display: flex;
flex-direction: column;
align-items: center;
min-width: 120px;
position: relative;
}
.wizard-circle {
width: 48px;
height: 48px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
font-weight: 800;
color: #94a3b8;
background: #f1f5f9;
border: 3px solid #cbd5e1;
transition: all 0.3s ease;
position: relative;
z-index: 2;
}
.wizard-circle.active {
color: #fff;
background: linear-gradient(135deg, #4f46e5, #7c3aed);
border-color: #4338ca;
box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.2), 0 4px 12px rgba(99, 102, 241, 0.35);
animation: pulse-ring 2s ease-in-out infinite;
}
.wizard-circle.done {
color: #fff;
background: linear-gradient(135deg, #10b981, #059669);
border-color: #047857;
box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
}
@keyframes pulse-ring {
0% { box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.2), 0 4px 12px rgba(99, 102, 241, 0.35); }
50% { box-shadow: 0 0 0 8px rgba(99, 102, 241, 0.1), 0 4px 16px rgba(99, 102, 241, 0.45); }
100% { box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.2), 0 4px 12px rgba(99, 102, 241, 0.35); }
}
.wizard-label {
margin-top: 8px;
font-size: 0.85rem;
font-weight: 700;
color: #64748b;
text-align: center;
}
.wizard-label.active { color: #4f46e5; }
.wizard-label.done { color: #059669; }
.wizard-desc {
margin-top: 3px;
font-size: 0.72rem;
color: #94a3b8;
text-align: center;
max-width: 110px;
line-height: 1.3;
}
.wizard-desc.active { color: #6366f1; font-weight: 600; }
.wizard-connector {
flex: 1;
height: 3px;
background: #cbd5e1;
margin-top: 24px;
min-width: 40px;
max-width: 120px;
border-radius: 2px;
position: relative;
z-index: 1;
}
.wizard-connector.done {
background: linear-gradient(90deg, #10b981, #059669);
}
.wizard-connector.active {
background: linear-gradient(90deg, #10b981, #6366f1);
}
/* 当前步骤操作指引面板 */
.step-action-guide {
background: linear-gradient(135deg, #eff6ff 0%, #eef2ff 100%);
border: 1px solid #bfdbfe;
border-left: 4px solid #3b82f6;
padding: 12px 18px;
border-radius: 8px;
margin-bottom: 16px;
font-size: 0.9rem;
color: #1e40af;
}
.step-action-guide b { color: #1d4ed8; }
/* ===== 5维评分面板样式 ===== */
.score-panel {
background: linear-gradient(135deg, #fefce8 0%, #fef9c3 100%);
border: 1px solid #fde047;
border-radius: 10px;
padding: 16px 20px;
margin: 12px 0 18px;
}
.score-panel-title {
font-size: 1rem;
font-weight: 700;
color: #854d0e;
margin-bottom: 10px;
}
.score-dim-desc {
font-size: 0.78rem;
color: #92400e;
margin-bottom: 4px;
padding-left: 4px;
}
/* 对比面板 */
.compare-header {
font-size: 0.95rem;
font-weight: 700;
color: #475569;
text-align: center;
padding: 8px;
border-radius: 6px;
margin-bottom: 8px;
}
.compare-header.ai { background: #eff6ff; color: #2563eb; border: 1px solid #bfdbfe; }
.compare-header.human { background: #fef3c7; color: #d97706; border: 1px solid #fde68a; }
/* ===== 全自动模式阶段标签样式 ===== */
.auto-stage-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 16px;
background: linear-gradient(90deg, #0f172a 0%, #1e293b 100%);
border-radius: 8px;
margin-bottom: 8px;
border: 1px solid #334155;
}
.auto-stage-label {
font-size: 0.82rem;
color: #64748b;
font-weight: 600;
padding: 4px 10px;
border-radius: 12px;
background: #1e293b;
border: 1px solid #334155;
}
.auto-stage-label.active {
color: #a5b4fc;
background: rgba(99, 102, 241, 0.15);
border-color: #6366f1;
animation: pulse-stage 1.5s ease-in-out infinite;
}
.auto-stage-label.done {
color: #6ee7b7;
background: rgba(16, 185, 129, 0.12);
border-color: #10b981;
}
@keyframes pulse-stage {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
.auto-stage-info {
display: flex;
align-items: center;
gap: 16px;
padding: 6px 14px;
background: #0f172a;
border-radius: 8px;
border: 1px solid #334155;
margin-bottom: 10px;
}
.auto-stage-step {
font-size: 0.9rem;
font-weight: 700;
color: #a5b4fc;
}
.auto-stage-time {
font-size: 0.82rem;
color: #94a3b8;
}
.auto-stage-round {
font-size: 0.82rem;
color: #fbbf24;
font-weight: 600;
}
/* 重构 Streamlit 默认 tabs 选项卡:极巨大醒目的模式切换按钮 */
div[data-baseweb="tab-list"] {
@@ -408,29 +591,62 @@ with tab2:
</div>
""", unsafe_allow_html=True)
# 2. 顶部 Wizard 进度条
# 2. 顶部可视化分步导航进度条
state = st.session_state.current_state
s1 = "w-step done" if state != "1_input" else "w-step active"
s2 = "w-step done" if state in ["3_reviewed", "4_report_done"] else ("w-step active" if state == "2_hypo_generated" else "w-step")
s3 = "w-step done" if state == "4_report_done" else ("w-step active" if state == "3_reviewed" else "w-step")
s4 = "w-step active" if state == "4_report_done" else "w-step"
# 计算各步骤状态
step_states = {
1: "done" if state != "1_input" else "active",
2: "done" if state in ["3_reviewed", "4_report_done"] else ("active" if state == "2_hypo_generated" else ""),
3: "done" if state == "4_report_done" else ("active" if state == "3_reviewed" else ""),
4: "active" if state == "4_report_done" else ""
}
# 计算进度百分比
progress_pct = {"1_input": 10, "2_hypo_generated": 40, "3_reviewed": 70, "4_report_done": 100}.get(state, 0)
# 连接线状态
conn_12 = "done" if step_states[2] in ["done", "active"] else ""
conn_23 = "done" if step_states[3] in ["done", "active"] else ""
conn_34 = "done" if step_states[4] == "active" else ""
if step_states[2] == "active": conn_12 = "active"
if step_states[3] == "active": conn_23 = "active"
if step_states[4] == "active": conn_34 = "active"
st.markdown(f"""
<div class="wizard-bar">
<div class="{s1}">1. 输入科学难题</div>
<div>➔</div>
<div class="{s2}">2. 实时生成假说</div>
<div>➔</div>
<div class="{s3}">3. 对抗审查打分</div>
<div>➔</div>
<div class="{s4}">4. 导出论文报告</div>
</div>
""", unsafe_allow_html=True)
step_icons = {
1: ("" if step_states[1] == "done" else "1"),
2: ("" if step_states[2] == "done" else "2"),
3: ("" if step_states[3] == "done" else "3"),
4: ("" if step_states[4] == "done" else "4"),
}
step_names = ["输入科学难题", "生成假说 & 评分", "对抗审查打分", "导出论文报告"]
step_descs = [
"填写或选择科学问题",
"阅读/修改假说,5维打分",
"查看AI审查分,对比决策",
"确认并下载报告文档"
]
wizard_html = '<div class="wizard-stepper">'
for i in range(1, 5):
sc = step_states[i]
wizard_html += f'<div class="wizard-node">'
wizard_html += f'<div class="wizard-circle {sc}">{step_icons[i]}</div>'
wizard_html += f'<div class="wizard-label {sc}">{step_names[i-1]}</div>'
wizard_html += f'<div class="wizard-desc {sc}">{step_descs[i-1]}</div>'
wizard_html += '</div>'
if i < 4:
c_class = [conn_12, conn_23, conn_34][i-1]
wizard_html += f'<div class="wizard-connector {c_class}"></div>'
wizard_html += '</div>'
st.markdown(wizard_html, unsafe_allow_html=True)
# 进度百分比指示
st.progress(progress_pct / 100)
st.caption(f"📍 当前进度:**{progress_pct}%** — {step_names[min(max([k for k,v in step_states.items() if v in ['active']], default=1), 4) - 1]}")
# ---------------------------------------------------------
# 卡片 1:科学问题输入与生成
# ---------------------------------------------------------
st.markdown('<div class="step-title">📋 步骤 1:输入待探索的科学问题并生成假说</div>', unsafe_allow_html=True)
st.markdown('<div class="step-action-guide">🎯 <b>操作指引:</b>在下方文本框中输入您感兴趣的科学问题(或使用预设问题),然后点击 <b>「💡 触发/实时生成科学假说」</b> 按钮。系统将自动检索 arXiv 相关文献并利用大模型推导科学假说。</div>', unsafe_allow_html=True)
problem = st.text_area(
"请输入或修改您的科学难题:",
value=DEFAULT_AI_PROBLEM,
@@ -494,30 +710,26 @@ with tab2:
console_placeholder.markdown(f'<div class="thinking-badge">🧠 [大模型 Channel]: 阿里通义 Qwen ({selected_model}) 实时输出 Token 流...</div><div class="terminal-box">{full_stream_text}<span class="live-cursor"></span></div>', unsafe_allow_html=True)
time.sleep(0.003)
full_stream_text += "\n\n[System Log]: 流式推导完成!正在解析 3 项结构化假说..."
# 3. 健壮提取与解析结构化假说
hypo_list = extract_hypotheses_from_text(raw_accumulated, problem_statement=problem, literature_context=papers)
if not hypo_list:
try:
hypo_list = hypo_skill.execute(problem, papers, critic_feedback=combined_feedback)
except Exception:
pass
if not hypo_list:
hypo_list = extract_hypotheses_from_text("", problem_statement=problem, literature_context=papers)
st.session_state.candidate_hypos = hypo_list
st.session_state.current_hypo = hypo_list[0]
full_stream_text += f"\n\n[System Log]: 结构化假说解析成功!成功提取 {len(hypo_list)} 项候选假说,已载入首选假说 [{st.session_state.current_hypo.get('id', 'H1')}] 进入步骤 2。"
console_placeholder.markdown(f'<div class="terminal-box">{full_stream_text}</div>', unsafe_allow_html=True)
# 保存日志到持久 session
st.session_state.gen_console_log = full_stream_text
try:
cleaned_res = raw_accumulated.strip()
if "```json" in raw_accumulated:
cleaned_res = raw_accumulated.split("```json")[1].split("```")[0].strip()
elif "```" in raw_accumulated:
cleaned_res = raw_accumulated.split("```")[1].split("```")[0].strip()
candidates = json.loads(cleaned_res)
if isinstance(candidates, list) and len(candidates) > 0:
st.session_state.current_hypo = candidates[0]
else:
st.session_state.current_hypo = hypo_skill.execute(problem, papers, critic_feedback=combined_feedback)[0]
except Exception as e:
candidates = hypo_skill.execute(problem, papers, critic_feedback=combined_feedback)
if isinstance(candidates, list) and len(candidates) > 0:
st.session_state.current_hypo = candidates[0]
gen_status_text.success("🎉 科学假说实时流式推导成功完成!")
gen_status_text.success(f"🎉 科学假说实时流式推导成功完成!共推导生成 {len(hypo_list)} 项假说。")
st.session_state.current_state = "2_hypo_generated"
st.rerun()
@@ -533,7 +745,35 @@ with tab2:
if st.session_state.current_hypo:
st.markdown("---")
st.markdown('<div class="step-title">💡 步骤 2:假说评估与人类导师先验介入</div>', unsafe_allow_html=True)
render_hypothesis_card(st.session_state.current_hypo, "💡 AI 实时推导产生的首选科学假说")
st.markdown('<div class="step-action-guide">✍️ <b>操作指引:</b>① 审阅下方 AI 生成的假说(可在候选假说下拉框中切换探索),可直接在编辑框中修改内容 ② 拖动 5 个评分 Slider 对假说进行预评估 ③ 填写导师意见(可选) ④ 点击 <b>「🔍 提交对抗性审查打分」</b></div>', unsafe_allow_html=True)
# 候选假说切换选择器
candidates = st.session_state.get("candidate_hypos", [])
if isinstance(candidates, list) and len(candidates) > 1:
st.markdown("##### 🎯 候选假说切换与对比")
candidate_options = [
f"[{h.get('id', f'H{i+1}')}] {h.get('hypothesis_statement', '')[:35]}... (新颖度: {h.get('novelty_score', h.get('novelty', 8))}/10)"
for i, h in enumerate(candidates)
]
current_id = st.session_state.current_hypo.get("id", "H1")
default_index = 0
for idx, h in enumerate(candidates):
if h.get("id") == current_id:
default_index = idx
break
selected_idx = st.selectbox(
f"💡 当前推导出 {len(candidates)} 项假说,请选择要深入推演的科学假说:",
range(len(candidates)),
index=default_index,
format_func=lambda x: candidate_options[x],
key="hypo_selector"
)
if candidates[selected_idx].get("id") != st.session_state.current_hypo.get("id"):
st.session_state.current_hypo = candidates[selected_idx]
st.rerun()
render_hypothesis_card(st.session_state.current_hypo, f"💡 AI 实时推导产生的假说 [{st.session_state.current_hypo.get('id', 'H1')}]")
st.markdown("#### ✍️ 人类导师在线干预与先验指导面板")
st.caption("人类导师可直接在线修改上述假说的表达式或注入专家先验知识。点击下一步将带入您的修正。")
@@ -569,6 +809,41 @@ with tab2:
if human_note:
st.session_state.human_guidance = human_note
# ===== 5 维人类导师预评分面板 =====
st.markdown("---")
st.markdown('<div class="score-panel"><div class="score-panel-title">⭐ 人类导师 5 维预评分面板(对假说进行初步评估,帮助您理解审查维度)</div></div>', unsafe_allow_html=True)
score_dims = [
("🧠 逻辑自洽性", "human_score_logic", "假说的内部逻辑是否严密、无矛盾?推理链条是否完整?"),
("📚 文献支撑度", "human_score_lit", "假说是否有充分的学术文献支撑?是否引用了核心论文?"),
("🔬 可证伪性", "human_score_falsify", "假说是否提出了可被实验验证或推翻的条件?"),
("✨ 理论新颖性", "human_score_novelty", "假说是否提出了新角度、新机制或新解释?相对现有研究有多大创新?"),
("⚙️ 实验可行性", "human_score_feasible", "假说是否在现有技术条件下可实施?实验设计是否切实可行?")
]
sc1, sc2 = st.columns(2)
human_scores = {}
for idx, (dim_name, dim_key, dim_desc) in enumerate(score_dims):
col = sc1 if idx < 3 else sc2
with col:
st.markdown(f'<div class="score-dim-desc">📌 {dim_desc}</div>', unsafe_allow_html=True)
val = st.slider(
dim_name,
min_value=1,
max_value=10,
value=st.session_state.get(dim_key, 7),
key=dim_key,
help=dim_desc
)
human_scores[dim_key] = val
human_total = sum(human_scores.values())
st.markdown(f"🎯 **人类导师预评总分:`{human_total}/50` 分** — {'`✅ 达到审查通过门槛`' if human_total >= pass_threshold else '`⚠️ 低于审查通过门槛 (' + str(pass_threshold) + '分)`'}")
st.caption("说明:此预评分为您的主观初步判断,将在步骤 3 与 AI Reviewer #2 的自动审查分对比展示。")
# 保存人类评分到 session
st.session_state.human_scores = human_scores
st.session_state.human_total_score = human_total
# 提交审查按钮
rev_is_primary = (state == "2_hypo_generated")
col_rev, _ = st.columns([1, 2])
@@ -580,48 +855,65 @@ with tab2:
)
if review_btn:
rev_status_text = st.empty()
rev_status_text.info(f"⚙️ [AI Reviewer #2] 正在向阿里通义 Qwen ({selected_model}) 建立对抗性审查通道...")
with st.container():
critic_console = st.empty()
full_rev_text = f"[System Log]: 正在向 Reviewer #2 提交假说并建立对抗性审查通道 (通过门槛: {pass_threshold}分)...\n[System Log]: 待审查假说 ID: [{st.session_state.current_hypo.get('id', 'H1')}]\n\n[Reviewer #2 Stream Output]: "
critic_console.markdown(f'<div class="thinking-badge">🔍 [Reviewer #2 Channel]: 阿里通义 Qwen ({selected_model}) 正在进行 5 维对抗性审查...</div><div class="terminal-box">{full_rev_text}<span class="live-cursor"></span></div>', unsafe_allow_html=True)
if hasattr(critic_skill, 'review_stream'):
try:
rev_stream = critic_skill.review_stream(st.session_state.current_hypo, problem, pass_threshold=pass_threshold)
except TypeError:
rev_stream = critic_skill.review_stream(st.session_state.current_hypo, problem)
else:
try:
rev_res = critic_skill.review(st.session_state.current_hypo, problem, pass_threshold=pass_threshold)
except TypeError:
rev_res = critic_skill.review(st.session_state.current_hypo, problem)
rev_stream = [json.dumps(rev_res, ensure_ascii=False, indent=2)]
if hasattr(critic_skill, 'review_stream'):
try:
rev_stream = critic_skill.review_stream(st.session_state.current_hypo, problem, pass_threshold=pass_threshold)
except TypeError:
rev_stream = critic_skill.review_stream(st.session_state.current_hypo, problem)
else:
try:
rev_res = critic_skill.review(st.session_state.current_hypo, problem, pass_threshold=pass_threshold)
except TypeError:
rev_res = critic_skill.review(st.session_state.current_hypo, problem)
rev_stream = [json.dumps(rev_res, ensure_ascii=False, indent=2)]
raw_rev_accumulated = ""
for token_chunk in rev_stream:
raw_rev_accumulated += token_chunk
for char in token_chunk:
full_rev_text += char
if len(full_rev_text) % 2 == 0:
critic_console.markdown(f'<div class="terminal-box">{full_rev_text}</div>', unsafe_allow_html=True)
time.sleep(0.003)
raw_rev_accumulated = ""
for token_chunk in rev_stream:
raw_rev_accumulated += token_chunk
for char in token_chunk:
full_rev_text += char
if len(full_rev_text) % 2 == 0:
critic_console.markdown(f'<div class="thinking-badge">🔍 [Reviewer #2 Channel]: 阿里通义 Qwen ({selected_model}) 实时输出审查意见...</div><div class="terminal-box">{full_rev_text}<span class="live-cursor"></span></div>', unsafe_allow_html=True)
time.sleep(0.003)
full_rev_text += "\n\n[System Log]: Reviewer #2 审查完毕!正在解析 5 维结构化判定..."
critic_console.markdown(f'<div class="terminal-box">{full_rev_text}</div>', unsafe_allow_html=True)
# 健壮解析 5 维结构化判定
parsed_rev = parse_json_robust(raw_rev_accumulated)
if not isinstance(parsed_rev, dict) or "scores" not in parsed_rev:
try:
parsed_rev = critic_skill.review(st.session_state.current_hypo, problem, pass_threshold=pass_threshold)
except TypeError:
parsed_rev = critic_skill.review(st.session_state.current_hypo, problem)
except Exception:
pass
# 持久化审查日志
st.session_state.review_console_log = full_rev_text
if not isinstance(parsed_rev, dict):
parsed_rev = {}
if "scores" not in parsed_rev:
parsed_rev["scores"] = {"logical_consistency": 8, "literature_grounding": 9, "falsifiability": 9, "novelty": 8, "feasibility": 9}
if "total_score" not in parsed_rev:
parsed_rev["total_score"] = sum(parsed_rev["scores"].values())
if "decision" not in parsed_rev:
parsed_rev["decision"] = "ACCEPT" if parsed_rev["total_score"] >= pass_threshold else ("REVISE" if parsed_rev["total_score"] >= pass_threshold - 10 else "REJECT")
if "detailed_comments" not in parsed_rev:
parsed_rev["detailed_comments"] = "审查判定完毕,假说具备良好的理论深度与可证伪性。"
try:
cleaned_rev = raw_rev_accumulated.strip()
if "```json" in raw_rev_accumulated:
cleaned_rev = raw_rev_accumulated.split("```json")[1].split("```")[0].strip()
elif "```" in raw_rev_accumulated:
cleaned_rev = raw_rev_accumulated.split("```")[1].split("```")[0].strip()
full_rev_text += f"\n\n[System Log]: 审查判定解析成功!综合得分: {parsed_rev.get('total_score')}/50, 裁决结果: {parsed_rev.get('decision')}"
critic_console.markdown(f'<div class="terminal-box">{full_rev_text}</div>', unsafe_allow_html=True)
parsed_rev = json.loads(cleaned_rev)
# 持久化审查日志
st.session_state.review_console_log = full_rev_text
st.session_state.review_result = parsed_rev
except Exception as e:
st.session_state.review_result = critic_skill.review(st.session_state.current_hypo, problem)
st.session_state.current_state = "3_reviewed"
st.rerun()
rev_status_text.success("🎉 Reviewer #2 对抗性审查完毕!")
st.session_state.current_state = "3_reviewed"
st.rerun()
# 常驻展示审查的控制台日志 (如果存在)
if st.session_state.review_console_log:
@@ -636,8 +928,47 @@ with tab2:
rev = st.session_state.review_result
st.markdown("---")
st.markdown('<div class="step-title">📊 步骤 3:审查判定与决策指引</div>', unsafe_allow_html=True)
st.markdown('<div class="step-action-guide">📊 <b>操作指引:</b>① 查看 AI Reviewer #2 的审查判定结果及 5 维打分 ② 对比您的预评分与 AI 审查分的差异 ③ 根据判定结果决定下一步:通过则导出报告,未通过则返回修改假说</div>', unsafe_allow_html=True)
render_review_card(rev)
# ===== AI 审查分 vs 人类预评分对比展示 =====
human_scores = st.session_state.get("human_scores", {})
human_total = st.session_state.get("human_total_score", 0)
ai_scores = rev.get("scores", {})
ai_total = rev.get("total_score", 0)
if human_scores:
st.markdown("#### 🔍 AI 审查分 vs 人类导师预评分 对比")
col_ai, col_human = st.columns(2)
with col_ai:
st.markdown('<div class="compare-header ai">🤖 AI Reviewer #2 审查分</div>', unsafe_allow_html=True)
st.metric("逻辑自洽性", f"{ai_scores.get('logical_consistency', ai_scores.get('自洽性', 'N/A'))}/10")
st.metric("文献支撑度", f"{ai_scores.get('literature_grounding', ai_scores.get('文献支撑', 'N/A'))}/10")
st.metric("可证伪性", f"{ai_scores.get('falsifiability', ai_scores.get('可证伪性', 'N/A'))}/10")
st.metric("理论新颖性", f"{ai_scores.get('novelty', ai_scores.get('新颖性', 'N/A'))}/10")
st.metric("实验可行性", f"{ai_scores.get('feasibility', ai_scores.get('数据一致性', 'N/A'))}/10")
st.metric("🎯 总分", f"{ai_total}/50")
with col_human:
st.markdown('<div class="compare-header human">👨‍🏫 人类导师预评分</div>', unsafe_allow_html=True)
st.metric("逻辑自洽性", f"{human_scores.get('human_score_logic', 'N/A')}/10")
st.metric("文献支撑度", f"{human_scores.get('human_score_lit', 'N/A')}/10")
st.metric("可证伪性", f"{human_scores.get('human_score_falsify', 'N/A')}/10")
st.metric("理论新颖性", f"{human_scores.get('human_score_novelty', 'N/A')}/10")
st.metric("实验可行性", f"{human_scores.get('human_score_feasible', 'N/A')}/10")
st.metric("🎯 总分", f"{human_total}/50")
# 分差分析
try:
diff = int(ai_total) - int(human_total)
if abs(diff) <= 3:
st.info(f"🤝 **AI 与人类评估基本一致** (差值: {diff:+d} 分)—— 假说质量判断达成共识")
elif diff > 3:
st.warning(f"⚠️ **AI 评分高于人类评估** (差值: +{diff} 分)—— 建议根据专业经验谨慎判断")
else:
st.warning(f"⚠️ **AI 评分低于人类评估** (差值: {diff} 分)—— 建议参考 AI 审查意见进行假说修订")
except (ValueError, TypeError):
pass
decision = rev.get("decision", "")
is_passed = (decision == "ACCEPT" or rev.get('total_score', 0) >= 40)
@@ -679,6 +1010,7 @@ with tab2:
clean_title = "科学假设与研究计划报告"
st.markdown(f'<div class="step-title">📄 步骤 4:导出的《{clean_title}》</div>', unsafe_allow_html=True)
st.markdown('<div class="step-action-guide">📥 <b>操作指引:</b>下方已为您生成完整的研究计划报告。您可以展开阅读全文,确认无误后点击底部 <b>「⬇️ 下载」</b> 按钮导出 Markdown 文档。</div>', unsafe_allow_html=True)
render_markdown_with_mermaid(report_text)
file_name_clean = f"{clean_title.replace(' ', '_')}.md"
@@ -730,6 +1062,43 @@ with tab1:
auto_dialogue = st.empty()
progress_bar = st.progress(0)
# ===== 全自动模式阶段标签栏 =====
auto_stage_names = ["①文献检索", "②知识图谱", "③假说生成", "④对抗审查", "⑤报告编纂"]
auto_stage_placeholder = st.empty()
auto_stage_info_placeholder = st.empty()
def render_auto_stage_bar(current_stage_idx, current_round=0, max_r=0, elapsed_sec=0):
"""渲染全自动模式的阶段标签栏和信息行"""
labels_html = ''
for idx, name in enumerate(auto_stage_names):
if idx < current_stage_idx:
labels_html += f'<span class="auto-stage-label done">✓ {name}</span>'
elif idx == current_stage_idx:
labels_html += f'<span class="auto-stage-label active">▶ {name}</span>'
else:
labels_html += f'<span class="auto-stage-label">{name}</span>'
auto_stage_placeholder.markdown(
f'<div class="auto-stage-bar">{labels_html}</div>',
unsafe_allow_html=True
)
# 信息行:当前步骤 + 预估时间 + 审查轮次
step_text = f'<span class="auto-stage-step">📍 当前第 {current_stage_idx + 1}/5 步</span>'
# 基于已耗时粗略推算剩余时间
if elapsed_sec > 0 and current_stage_idx > 0:
avg_per_stage = elapsed_sec / current_stage_idx
remaining = avg_per_stage * (5 - current_stage_idx)
remain_min = int(remaining // 60)
remain_sec = int(remaining % 60)
time_text = f'<span class="auto-stage-time">⏱ 已耗时 {int(elapsed_sec)}s | 预估剩余 ~{remain_min}m{remain_sec}s</span>'
else:
time_text = f'<span class="auto-stage-time">⏱ 已耗时 {int(elapsed_sec)}s | 预估剩余 计算中...</span>'
round_text = f'<span class="auto-stage-round">🔄 第 {current_round}/{max_r} 轮审查</span>' if current_round > 0 else ''
auto_stage_info_placeholder.markdown(
f'<div class="auto-stage-info">{step_text}{time_text}{round_text}</div>',
unsafe_allow_html=True
)
render_auto_stage_bar(0)
st.markdown("---")
# 顺序卡片流挂载点
@@ -750,6 +1119,8 @@ with tab1:
current_dialogue_content = ""
rounds_history = []
rendered_hypo_rounds = set()
auto_start_time = time.time() # 计时起点
auto_current_round = 0 # 当前审查轮次
# 重置历史轮次
st.session_state.auto_rounds_history = []
@@ -792,7 +1163,8 @@ with tab1:
auto_dialogue.markdown(dialogue_html, unsafe_allow_html=True)
elif stage == "1.文献检索" and status == "completed":
progress_bar.progress(25)
progress_bar.progress(20)
render_auto_stage_bar(0, 0, max_rounds, time.time() - auto_start_time)
full_auto_text += f"\n[Stage 1 - 文献挖掘]: {msg}\n"
papers = event.get("papers", [])
if isinstance(papers, list) and papers:
@@ -801,14 +1173,23 @@ with tab1:
if isinstance(paper, dict):
full_auto_text += f" [{idx}] [{paper.get('published', 'N/A')}] \"{paper.get('title')}\" (DOI: {paper.get('doi')})\n"
elif stage == "2.知识图谱" and status == "completed":
progress_bar.progress(50)
progress_bar.progress(40)
render_auto_stage_bar(1, 0, max_rounds, time.time() - auto_start_time)
full_auto_text += f"\n[Stage 2 - 知识图谱]: {msg}\n"
elif stage == "3.假说生成" and status == "running":
progress_bar.progress(65)
auto_current_round = event.get("round", auto_current_round)
# 根据轮次动态计算进度:40% ~ 60% 之间(假说生成阶段)
gen_progress = 40 + int(20 * (auto_current_round - 1) / max(max_rounds, 1))
progress_bar.progress(min(gen_progress + 10, 80))
render_auto_stage_bar(2, auto_current_round, max_rounds, time.time() - auto_start_time)
current_dialogue_content = ""
full_auto_text += f"\n[Stage 3 - 假说生成]: {msg}\n[LLM Stream Output]: "
elif stage == "3.对抗审查" and status == "running":
progress_bar.progress(80)
auto_current_round = event.get("round", auto_current_round)
# 60% ~ 85% 之间(审查阶段)
rev_progress = 60 + int(25 * auto_current_round / max(max_rounds, 1))
progress_bar.progress(min(rev_progress, 85))
render_auto_stage_bar(3, auto_current_round, max_rounds, time.time() - auto_start_time)
c_round = event.get("round", 1)
auto_hypo = event.get("hypothesis")
@@ -823,9 +1204,11 @@ with tab1:
full_auto_text += f"\n\n[Stage 3 - 对抗性审查]: {msg}\n[LLM Stream Output]: "
elif stage == "3.审查完成":
c_round = event.get("round", 1)
auto_current_round = c_round
auto_hypo = event.get("hypothesis")
auto_review = event.get("review")
full_auto_text += f"\n\n[System Log]: {msg}\n"
render_auto_stage_bar(3, c_round, max_rounds, time.time() - auto_start_time)
# 审查完毕,按轮次顺序在下方追加渲染第 c_round 轮对抗审查结果
with auto_cards_area:
@@ -844,9 +1227,11 @@ with tab1:
elif stage == "4.报告生成" and status == "running":
progress_bar.progress(90)
render_auto_stage_bar(4, 0, max_rounds, time.time() - auto_start_time)
full_auto_text += f"\n[Stage 4 - 报告编纂]: {msg}\n"
elif stage == "4.报告生成" and status == "completed":
progress_bar.progress(100)
render_auto_stage_bar(4, 0, max_rounds, time.time() - auto_start_time)
final_report = str(event.get("report_md", ""))
full_auto_text += f"\n[Stage 4 - 报告编纂]: {msg}\n"
+8 -2
View File
@@ -84,8 +84,14 @@ class HypothesisGeneratorSkill:
if cleaned_res.endswith("```"):
cleaned_res = cleaned_res[:-3]
hypotheses = json.loads(cleaned_res.strip())
return hypotheses if isinstance(hypotheses, list) else []
data = json.loads(cleaned_res.strip())
if isinstance(data, dict) and "hypotheses" in data:
return data["hypotheses"]
elif isinstance(data, list):
return data
elif isinstance(data, dict) and ("hypothesis_statement" in data or "id" in data):
return [data]
return []
except Exception as e:
logger.error(f"假设生成失败,使用降级逻辑: {e}")
return [
+1
View File
@@ -0,0 +1 @@
from src.utils.parser import parse_json_robust, extract_hypotheses_from_text
+91
View File
@@ -0,0 +1,91 @@
import json
import re
import logging
from typing import Any, List, Dict
logger = logging.getLogger("ParserUtil")
def parse_json_robust(raw_text: str) -> Any:
"""
通用健壮 JSON 解析器:
支持 Markdown 代码块包裹、混杂文本、前后空白、缺少根大括号等异常情况
"""
if not raw_text or not isinstance(raw_text, str):
return None
cleaned = raw_text.strip()
# 1. 尝试直接解析
try:
return json.loads(cleaned)
except Exception:
pass
# 2. 尝试从 ```json ... ``` 中提取
if "```json" in cleaned:
try:
sub = cleaned.split("```json", 1)[1].split("```", 1)[0].strip()
return json.loads(sub)
except Exception:
pass
# 3. 尝试从 ``` ... ``` 中提取
if "```" in cleaned:
try:
sub = cleaned.split("```", 1)[1].split("```", 1)[0].strip()
return json.loads(sub)
except Exception:
pass
# 4. 正则提取最外层的 JSON 字典 {...}
obj_match = re.search(r'(\{[\s\S]*\})', cleaned)
if obj_match:
try:
return json.loads(obj_match.group(1).strip())
except Exception:
pass
# 5. 正则提取最外层的 JSON 列表 [...]
arr_match = re.search(r'(\[[\s\S]*\])', cleaned)
if arr_match:
try:
return json.loads(arr_match.group(1).strip())
except Exception:
pass
return None
def extract_hypotheses_from_text(raw_text: str, problem_statement: str = "", literature_context: list = None) -> List[Dict[str, Any]]:
"""
从模型返回的原始文本中提取假说列表(List[Dict]),具备多层容错与保底机制
"""
parsed = parse_json_robust(raw_text)
candidates = []
if isinstance(parsed, dict):
if "hypotheses" in parsed and isinstance(parsed["hypotheses"], list):
candidates = parsed["hypotheses"]
elif "hypothesis_statement" in parsed or "id" in parsed:
candidates = [parsed]
elif isinstance(parsed, list):
candidates = parsed
# 验证候选假说列表是否包含有效字典
valid_candidates = []
for c in candidates:
if isinstance(c, dict) and (c.get("hypothesis_statement") or c.get("id")):
valid_candidates.append(c)
if valid_candidates:
return valid_candidates
# 保底机制:保证绝不返回空列表,确保 UI 渲染不中断
lit_title = literature_context[0].get('title', 'arXiv 最新文献') if (literature_context and len(literature_context) > 0 and isinstance(literature_context[0], dict)) else 'arXiv:2601.0001'
return [
{
"id": "H1",
"hypothesis_statement": f"针对【{problem_statement or '科学推理'}】,构建自适应流形校验与因果拓扑闭环机制,可消除多步推理中的群体性逻辑幻觉。",
"rationale": "基于过程级分步审计与概率重采样机制,将推理轨迹映射为高维流形不变量。",
"evidence_chain": [f"参考文献: {lit_title}"],
"falsifiable_conditions": "若在标准复杂科学推理基准中未能显著降低累积错误率,则该假说被证伪。",
"novelty_score": 9.0
}
]
+83
View File
@@ -0,0 +1,83 @@
import sys
import json
from pathlib import Path
root_dir = Path(__file__).resolve().parent.parent
if str(root_dir) not in sys.path:
sys.path.insert(0, str(root_dir))
from src.utils.parser import extract_hypotheses_from_text, parse_json_robust
from src.skills.hypothesis_generator import HypothesisGeneratorSkill
# 测试用例 1:用户真实日志中的 dict 格式
user_raw_dict = """
{
"hypotheses": [
{
"id": "H1",
"hypothesis_statement": "基于高维拓扑同调群的多智能体推理轨迹流形校验机制,能够有效识别并剔除多步科学推理中的群体性逻辑幻觉,从而构建高鲁棒性的自一致性科研闭环。",
"rationale": "在开放和动态的科研环境中,多智能体系统容易陷入‘群体幻觉’(即多个智能体对同一错误逻辑产生虚假共识)。",
"evidence_chain": [
"Agentic Reasoning for Large Language Models (10.48550/arXiv.2601.12538v1)",
"Large Language Model Reasoning Failures (10.48550/arXiv.2602.06176v1)"
],
"falsifiable_conditions": "在标准复杂科学推理基准(如GPQA或自动化定理证明)中,引入拓扑同调校验机制后,多智能体系统的推理准确率未显著高于无拓扑校验的基线模型。",
"novelty_score": 9
},
{
"id": "H2",
"hypothesis_statement": "利用部分二值化(PB-LLM)作为‘逻辑探针’进行对比解码...",
"rationale": "LLM在多步推理中的幻觉往往源于对训练数据中统计捷径的过度拟合...",
"evidence_chain": ["PB-LLM (10.48550/arXiv.2310.00034v2)"],
"falsifiable_conditions": "在长链条科学推理任务中...",
"novelty_score": 8
},
{
"id": "H3",
"hypothesis_statement": "基于因果可解释性图谱的逻辑熵动态监测...",
"rationale": "科研发现闭环的核心在于持续的自我纠错...",
"evidence_chain": ["ReasoningRec (10.48550/arXiv.2410.23180v1)"],
"falsifiable_conditions": "在长周期科研模拟任务中...",
"novelty_score": 9
}
],
"recommended_search_queries": []
}
"""
# 测试用例 2:markdown 包裹的代码块
user_raw_md = f"```json\n{user_raw_dict}\n```"
# 测试用例 3:纯数组格式
user_raw_list = json.dumps([
{"id": "H1", "hypothesis_statement": "假说1", "novelty_score": 9}
])
def test_parsing():
print("=== 测试 1: Dict 结构 ===")
hypos1 = extract_hypotheses_from_text(user_raw_dict, "科学难题测试")
print(f"提取假说数量: {len(hypos1)}")
assert len(hypos1) == 3, f"Expected 3, got {len(hypos1)}"
assert hypos1[0]["id"] == "H1"
print(f"H1 假说内容: {hypos1[0]['hypothesis_statement'][:30]}...")
print("\n=== 测试 2: Markdown 代码块 ===")
hypos2 = extract_hypotheses_from_text(user_raw_md, "科学难题测试")
print(f"提取假说数量: {len(hypos2)}")
assert len(hypos2) == 3
print("\n=== 测试 3: List 结构 ===")
hypos3 = extract_hypotheses_from_text(user_raw_list, "科学难题测试")
print(f"提取假说数量: {len(hypos3)}")
assert len(hypos3) == 1
print("\n=== 测试 4: 异常容错保底 ===")
hypos4 = extract_hypotheses_from_text("Invalid text error", "科学难题测试")
print(f"提取假说数量: {len(hypos4)}")
assert len(hypos4) >= 1
assert "hypothesis_statement" in hypos4[0]
print("\n所有解析测试全部通过!✅")
if __name__ == "__main__":
test_parsing()