GuXiaopeng commited on
Commit
a8135f1
·
verified ·
1 Parent(s): b157f30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -16
app.py CHANGED
@@ -22,11 +22,22 @@ from utils.docx_builder import text_to_docx
22
  from core.pipeline_agent import SSciPipelineAgent
23
 
24
  # ==========================================
25
- # 0. 核心环境与内置浏览器阻断
26
  # ==========================================
27
  os.environ["STREAMLIT_SERVER_ENABLE_CORS"] = "false"
28
  os.environ["STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION"] = "false"
29
 
 
 
 
 
 
 
 
 
 
 
 
30
  WECHAT_BLOCKER_JS = """
31
  <script>
32
  try {
@@ -79,15 +90,33 @@ with tab_wizard:
79
  with tab_clone:
80
  st.markdown("### 🧬 高级克隆:骨架与风格复刻")
81
  context_differences = st.text_area("1. 研究背景与核心差异(Context & Differences)", placeholder="简述您的新研究与参考论文在数据、案例或背景上的核心区别...", disabled=st.session_state.is_running)
82
- uploaded_reference_clone = st.file_uploader("2. 上传范例参考论文 (必填,支持 PDF/DOCX,系统将解构其行文风格)", type=["pdf", "doc", "docx"], key="clone_ref", disabled=st.session_state.is_running)
83
- sections_to_clone = st.multiselect("3. 🎯 请勾选希望模仿参考论文的哪些部分:",
84
- ["Introduction", "Literature Review", "Methodology", "Results", "Discussion"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  disabled=st.session_state.is_running)
86
 
87
- uploaded_data_clone = None
88
- if any("Methodology" in sec or "Results" in sec for sec in sections_to_clone):
89
- st.info("💡 您已选择克隆实证段落,必须上传真实的统计分析结果以防数据幻觉。")
90
- uploaded_data_clone = st.file_uploader("4. 上传您的实证数据或统计结果表 (必填,支持 CSV/Excel)", type=["csv", "xlsx", "xls"], key="clone_data", disabled=st.session_state.is_running)
91
 
92
  if st.button("🚀 启动克隆级生成", disabled=st.session_state.is_running, key="btn_clone"):
93
  if not uploaded_reference_clone or not sections_to_clone:
@@ -119,14 +148,11 @@ def render_monitor(agent, phase, current_sec="", details=""):
119
  FLAG_MAP = {"Simplified Chinese (简体中文)": "🇨🇳", "Russian (Русский)": "🇷🇺", "Kazakh (Қазақша)": "🇰🇿"}
120
 
121
  if st.session_state.start_pipeline:
122
- ds_key = os.environ.get("DEEPSEEK_API_KEY", "")
123
- try: s2_key = st.secrets.get("S2_API_KEY", "")
124
- except: s2_key = os.environ.get("S2_API_KEY", "")
125
- if not ds_key:
126
  st.error("❌ DEEPSEEK_API_KEY missing. 请在 Hugging Face 的 Secrets 中配置。")
127
  st.stop()
128
  st.session_state.is_running = True
129
- agent = SSciPipelineAgent(ds_key, s2_key)
130
 
131
  try:
132
  pb = progress_bar.progress(1, text="⏳ 流水线启动...")
@@ -166,7 +192,7 @@ if st.session_state.start_pipeline:
166
  if not refs_text and len(agent.citation_map) == 0:
167
  st.session_state.is_running = False
168
  st.stop()
169
- if len(agent.citation_map) < 80:
170
  st.warning(f"文献数量仅 {len(agent.citation_map)} 篇,可能影响引用数量,继续生成...")
171
 
172
  pb.progress(12, text="🧠 文献综合...")
@@ -297,7 +323,7 @@ if st.session_state.start_pipeline:
297
  Write a natural, smooth transitional opening sentence to bridge this.
298
  """
299
 
300
- if "method" in sec_title.lower() or "result" in sec_title.lower():
301
  base_user += f"""\n[CRITICAL STOP - ZERO DATA HALLUCINATION RULE]
302
  You are writing an empirical section. YOU ARE ABSOLUTELY FORBIDDEN from using any exact numbers (R-square, p-values, sample sizes, beta coefficients) found in the <REFERENCE_TEMPLATE_SNIPPET>.
303
  You MUST ONLY rely on the user's actual statistical data provided below.
@@ -400,7 +426,7 @@ if st.session_state.start_pipeline:
400
  total_words_en = len(full_draft_en.split())
401
  st.success(f"✅ 扩展完成,现总字数: {total_words_en}")
402
 
403
- if len(agent.used_citations) < 80:
404
  unused = [tag for tag in agent.citation_map if tag not in agent.used_citations]
405
  for tag in unused[:80 - len(agent.used_citations)]: agent.used_citations.add(tag)
406
 
 
22
  from core.pipeline_agent import SSciPipelineAgent
23
 
24
  # ==========================================
25
+ # 0. 核心环境与全局变量提取
26
  # ==========================================
27
  os.environ["STREAMLIT_SERVER_ENABLE_CORS"] = "false"
28
  os.environ["STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION"] = "false"
29
 
30
+ # 提前提取全局 API Key 以供克隆模式的文件智能解析使用
31
+ global_ds_key = os.environ.get("DEEPSEEK_API_KEY", "")
32
+ if not global_ds_key:
33
+ try: global_ds_key = st.secrets.get("DEEPSEEK_API_KEY", "")
34
+ except: pass
35
+
36
+ global_s2_key = os.environ.get("S2_API_KEY", "")
37
+ if not global_s2_key:
38
+ try: global_s2_key = st.secrets.get("S2_API_KEY", "")
39
+ except: pass
40
+
41
  WECHAT_BLOCKER_JS = """
42
  <script>
43
  try {
 
90
  with tab_clone:
91
  st.markdown("### 🧬 高级克隆:骨架与风格复刻")
92
  context_differences = st.text_area("1. 研究背景与核心差异(Context & Differences)", placeholder="简述您的新研究与参考论文在数据、案例或背景上的核心区别...", disabled=st.session_state.is_running)
93
+
94
+ # === 自适应目录提取逻辑 ===
95
+ if "extracted_sections" not in st.session_state:
96
+ st.session_state.extracted_sections = ["Introduction", "Literature Review", "Methodology", "Results", "Discussion"]
97
+ if "last_uploaded_clone_ref" not in st.session_state:
98
+ st.session_state.last_uploaded_clone_ref = None
99
+
100
+ uploaded_reference_clone = st.file_uploader("2. 上传范例参考论文 (必填,支持 PDF/DOCX,系统将自动智能解析其目录结构)", type=["pdf", "doc", "docx"], key="clone_ref", disabled=st.session_state.is_running)
101
+
102
+ # 监听上传状态,如果换了新文件且 API Key 存在,则静默读取并提取目录
103
+ if uploaded_reference_clone and global_ds_key:
104
+ if st.session_state.last_uploaded_clone_ref != uploaded_reference_clone.name:
105
+ with st.spinner("🔍 正在智能提取该范例论文的真实目录结构,请稍候..."):
106
+ temp_agent = SSciPipelineAgent(global_ds_key, global_s2_key)
107
+ ref_text_raw = extract_text_from_files([uploaded_reference_clone])
108
+ dynamic_sections = temp_agent.extract_document_structure(ref_text_raw)
109
+
110
+ st.session_state.extracted_sections = dynamic_sections
111
+ st.session_state.last_uploaded_clone_ref = uploaded_reference_clone.name
112
+ st.rerun() # 强制重绘,更新下方的 multiselect
113
+
114
+ sections_to_clone = st.multiselect("3. 🎯 请勾选希望模仿参考论文的哪些部分 (已自适应论文结构):",
115
+ st.session_state.extracted_sections,
116
  disabled=st.session_state.is_running)
117
 
118
+ # 已按照需求修改为常驻可选项
119
+ uploaded_data_clone = st.file_uploader("4. 上传统计分析结果 (可选,支持 CSV/Excel,用于实证章节防幻觉支撑)", type=["csv", "xlsx", "xls"], key="clone_data", disabled=st.session_state.is_running)
 
 
120
 
121
  if st.button("🚀 启动克隆级生成", disabled=st.session_state.is_running, key="btn_clone"):
122
  if not uploaded_reference_clone or not sections_to_clone:
 
148
  FLAG_MAP = {"Simplified Chinese (简体中文)": "🇨🇳", "Russian (Русский)": "🇷🇺", "Kazakh (Қазақша)": "🇰🇿"}
149
 
150
  if st.session_state.start_pipeline:
151
+ if not global_ds_key:
 
 
 
152
  st.error("❌ DEEPSEEK_API_KEY missing. 请在 Hugging Face 的 Secrets 中配置。")
153
  st.stop()
154
  st.session_state.is_running = True
155
+ agent = SSciPipelineAgent(global_ds_key, global_s2_key)
156
 
157
  try:
158
  pb = progress_bar.progress(1, text="⏳ 流水线启动...")
 
192
  if not refs_text and len(agent.citation_map) == 0:
193
  st.session_state.is_running = False
194
  st.stop()
195
+ if len(agent.citation_map) < 80: # 修正了之前的 40
196
  st.warning(f"文献数量仅 {len(agent.citation_map)} 篇,可能影响引用数量,继续生成...")
197
 
198
  pb.progress(12, text="🧠 文献综合...")
 
323
  Write a natural, smooth transitional opening sentence to bridge this.
324
  """
325
 
326
+ if "method" in sec_title.lower() or "result" in sec_title.lower() or "analysis" in sec_title.lower():
327
  base_user += f"""\n[CRITICAL STOP - ZERO DATA HALLUCINATION RULE]
328
  You are writing an empirical section. YOU ARE ABSOLUTELY FORBIDDEN from using any exact numbers (R-square, p-values, sample sizes, beta coefficients) found in the <REFERENCE_TEMPLATE_SNIPPET>.
329
  You MUST ONLY rely on the user's actual statistical data provided below.
 
426
  total_words_en = len(full_draft_en.split())
427
  st.success(f"✅ 扩展完成,现总字数: {total_words_en}")
428
 
429
+ if len(agent.used_citations) < 80: # 同步了这里的 80 逻辑
430
  unused = [tag for tag in agent.citation_map if tag not in agent.used_citations]
431
  for tag in unused[:80 - len(agent.used_citations)]: agent.used_citations.add(tag)
432