Spaces:
Running
Running
File size: 13,761 Bytes
d2c6efd 21de594 802b99b 21de594 802b99b 1ee8821 21de594 802b99b 21de594 802b99b 21de594 d2c6efd 21de594 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 21de594 d2c6efd 21de594 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd 1ee8821 d2c6efd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | import json
import os
from pathlib import Path
from src import *
# ============= 从环境变量动态生成配置 ===============
def load_config():
base_dir = os.path.dirname(os.path.abspath(__file__))
user_root_folder = os.environ.get("USER_OUTPUT_DIR", os.path.join(base_dir, "mineru_outputs"))
api_key = os.environ.get("GEMINI_API_KEY", "")
api_base_url = os.environ.get("GEMINI_API_BASE_URL", "")
generation_model = os.environ.get("GENERATION_MODEL", "gemini-3-pro-preview")
# ✅ 修正字典结构:把 api_base_url 移回到外层,和原版 config.yaml 保持绝对一致
config = {
"model_settings": {
"generation_model": generation_model
},
"path": {
"root_folder": user_root_folder
},
"api_keys": {
"gemini_api_key": api_key
},
"api_base_url": api_base_url # <--- ✅ 放在这里才对!
}
if api_key:
os.environ["GEMINI_API_KEY"] = api_key
os.environ["GOOGLE_API_KEY"] = api_key
if api_base_url:
os.environ["GEMINI_API_BASE_URL"] = api_base_url
os.environ["GOOGLE_API_BASE"] = api_base_url
return config
# ========== 读取 prompt 模板 ==========
def load_prompt(prompt_path="prompt.json", prompt_name="poster_prompt"):
with open(prompt_path, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get(prompt_name, "")
# 应用配置
config = load_config()
model_name = config['model_settings']['generation_model']
# ========== 主流程 ==========
def main():
# 输入总文件夹路径(包含多个论文子文件夹)此时指向用户的专属文件夹
root_folder = config['path']['root_folder']
prompt_path = "prompt.json"
print("📘 Loading prompts...")
# dag prompt:
section_split_prompt = load_prompt(prompt_path, prompt_name="section_split_prompt")
clean_prompt = load_prompt(prompt_path, prompt_name="clean_prompt")
initialize_dag_prompt = load_prompt(prompt_path, prompt_name="initialize_dag_prompt")
visual_dag_prompt = load_prompt(prompt_path, prompt_name="visual_dag_prompt")
section_dag_generation_prompt = load_prompt(prompt_path, prompt_name="section_dag_generation_prompt")
# ppt prompt:
outline_initialize_prompt = load_prompt(prompt_path, prompt_name="outline_initialize_prompt")
generate_complete_outline_prompt = load_prompt(prompt_path, prompt_name="generate_complete_outline_prompt")
arrange_template_prompt= load_prompt(prompt_path, prompt_name="arrange_template_prompt")
commenter_prompt= load_prompt(prompt_path, prompt_name="commenter_prompt")
reviser_prompt= load_prompt(prompt_path, prompt_name="reviser_prompt")
# poster prompt:
poster_outline_prompt= load_prompt(prompt_path, prompt_name="poster_outline_prompt")
poster_refinement_prompt= load_prompt(prompt_path, prompt_name="poster_refinement_prompt")
modified_poster_logic_prompt=load_prompt(prompt_path, prompt_name="modified_poster_logic_prompt")
# pr prompt:
extract_basic_information_prompt = load_prompt("prompt.json", "extract_basic_information_prompt")
generate_pr_prompt = load_prompt(prompt_path, prompt_name="generate_pr_prompt")
add_title_and_hashtag_prompt = load_prompt(prompt_path, prompt_name="add_title_and_hashtag_prompt")
pr_refinement_prompt = load_prompt(prompt_path, prompt_name="pr_refinement_prompt")
# === 遍历每个子文件夹 ===
for subdir in os.listdir(root_folder):
subdir_path = os.path.join(root_folder, subdir)
auto_path = os.path.join(subdir_path, "auto") # ✅ 进入 auto 子文件夹
if not os.path.isdir(auto_path):
print(f"⚠️ No 'auto' folder found in {subdir_path}, skipping...")
continue # 只处理存在 auto 文件夹的目录
# ✅ 如果 success.txt 已存在,跳过该目录
success_flag = os.path.join(auto_path, "success_poster.txt")
if os.path.isfile(success_flag):
print(f"✅ success_poster.txt exists in {auto_path}, skipping...")
continue
print(f"\n🚀 Processing paper folder: {auto_path}")
# === 根据子文件夹名精确匹配文件 ===
target_pdf = f"{subdir}_origin.pdf"
target_md = f"{subdir}.md"
pdf_path = os.path.join(auto_path, target_pdf)
md_path = os.path.join(auto_path, target_md)
# === 检查文件是否存在 ===
if not os.path.exists(pdf_path):
print(f"⚠️ Missing expected PDF: {target_pdf} in {auto_path}, skipping...")
continue
if not os.path.exists(md_path):
print(f"⚠️ Missing expected Markdown: {target_md} in {auto_path}, skipping...")
continue
print(f"📄 Matched files:\n PDF: {target_pdf}\n MD: {target_md}")
# === 输出文件路径 ===
output_html = os.path.join(auto_path, "poster.html")
graph_json_path = os.path.join(auto_path, "graph.json")
# # === 清理 markdown === 去除无意义的段落,如relative work,reference,appendix等等
# print("🧹 Cleaning markdown before splitting...")
# cleaned_md_path = clean_paper(md_path, clean_prompt, model="gemini-3-pro-preview", config=config)
# # === 利用gpt将论文分段 ===
# paths = split_paper(cleaned_md_path, section_split_prompt, model="gemini-3-pro-preview" ,config=config)
# # === 利用gpt初始化dag ===
# dag = initialize_dag(markdown_path=cleaned_md_path,initialize_dag_prompt=initialize_dag_prompt,model="gemini-3-pro-preview", config=config)
dag_path = os.path.join(auto_path, "dag.json")
# # === 生成visual_dag ===
# visual_dag_path=os.path.join(auto_path, "visual_dag.json")
# extract_and_generate_visual_dag(markdown_path=cleaned_md_path,prompt_for_gpt=visual_dag_prompt,output_json_path=visual_dag_path,model="gemini-3-pro-preview", config=config)
# add_resolution_to_visual_dag(auto_path, visual_dag_path)
# # === 生成section_dag ===
# section_split_output_path=os.path.join(subdir_path, "section_split_output")
# build_section_dags(folder_path=section_split_output_path,base_prompt=section_dag_generation_prompt,model="gemini-3-pro-preview", config=config)
# # === 向dag.json添加section_dag ===
# section_dag_path=os.path.join(subdir_path, "section_dag")
# merged_path = add_section_dag(section_dag_folder=section_dag_path,main_dag_path=dag_path,output_path=None)
# # === 向dag.json添加visual_dag ===
# add_visual_dag(dag_path=dag_path,visual_dag_path=visual_dag_path)
# # === 完善dag中每一个结点的visual_node ===
# refine_visual_node(dag_path)
# # ============================= PPT部分 ================================
# # === 按照算法选出结点,以便后续生成outline ===
# selected_node_path=os.path.join(auto_path, "selected_node.json")
# generate_selected_nodes(dag_json_path=dag_path, max_len=15,output_path=selected_node_path)
# # === 初始化ouline ===
# outline_path= os.path.join(auto_path, "outline.json")
# outline = outline_initialize(dag_json_path=dag_path,outline_initialize_prompt=outline_initialize_prompt,model=model_name, config=config)
# # === 生成完整ouline ===
# outline_data=generate_complete_outline(selected_node_path,outline_path,generate_complete_outline_prompt,model=model_name, config=config)
# # === 配模板 ===
# arrange_template(outline_path,arrange_template_prompt,model=model_name, config=config)
# # === 生成最终的PPT ===
# ppt_template_path="./ppt_template"
# generate_ppt_prompt = {"role":"system","content":"You are given (1) a slide node (JSON) and (2) an HTML slide template. Your task: revise the HTML template to produce the final slide HTML using the node content. Node fields: text (slide textual content), figure (list of images to display, each has name, caption, resolution), formula (list of formula images to display, each has name, caption, resolution), template (template filename). IMPORTANT RULES: 1) Only modify places in the HTML that are marked by comments like . 2) For 'Subjects' sections: replace the placeholder title with ONE concise summary sentence for this slide. 3) For 'Image' sections (<img ...>): replace src with the relative path extracted from node.figure/formula[i].name; the node image name may be markdown like '', use only 'images/abc.jpg'. 4) For 'Text' sections: replace the placeholder text with the node.text content, formatted cleanly in HTML; keep it readable and you may use <p>, <ul><li>, <br/> appropriately. 5) If the template expects more images/text blocks than provided by the node, leave the missing positions unchanged and do not invent content. 6) If the node provides more images than the template has slots, fill slots in order and ignore the rest. 7) Preserve all other HTML, CSS, and structure exactly. OUTPUT FORMAT: Return ONLY the revised HTML as plain text. Do NOT wrap it in markdown fences. Do NOT add explanations."}
# generate_ppt(outline_path,ppt_template_path,generate_ppt_prompt,model=model_name, config=config)
# # === Refiner ===
# refinement_ppt(input_index=auto_path, prompts=[commenter_prompt, reviser_prompt], model=model_name, max_iterations=3, config=config)
# ============================= Poster部分 ================================
poster_outline_path = os.path.join(auto_path, "poster_outline.txt")
print(f"📝 Generating poster outline at: {poster_outline_path}")
generate_poster_outline_txt(dag_path=dag_path,poster_outline_path=poster_outline_path,poster_outline_prompt=poster_outline_prompt,model=model_name, config=config)
print (f"✅ Poster outline generated.")
poster_path=os.path.join(auto_path, "poster.html")
subdir_name = Path(subdir_path).name
poster_outline_path_modified = os.path.join(auto_path, "poster_outline_modified.txt")
print(f"📝 Modifying poster outline for paper: {subdir_name}")
modify_poster_outline(poster_outline_path=poster_outline_path,poster_paper_name=subdir_name,modified_poster_outline_path=poster_outline_path_modified)
print (f"✅ Poster outline modified.")
modified_poster_logic(poster_outline_path_modified, modified_poster_logic_prompt, model=model_name, config=config)
print(f"🖼️ Building poster HTML at: {poster_path}")
build_poster_from_outline(poster_outline_path=poster_outline_path_modified,poster_template_path="./poster_template/poster_template.html",poster_path=poster_path,)
print (f"✅ Poster HTML built.")
print(f"🖊️ Modifying title and authors in poster HTML...")
modify_title_and_author(dag_path=dag_path,poster_path=poster_path)
print (f"✅ Title and authors modified.")
poster_final_index = os.path.join(auto_path, "final")
os.makedirs(poster_final_index, exist_ok=True)
poster_final_output_path = os.path.join(poster_final_index, "poster_final.html")
print(f"🖊️ Refining poster HTML with Gemini...")
out = inject_img_section_to_poster(figure_path="./poster_template/expore_our_work_in_detail.jpg",auto_path=auto_path,poster_path=poster_path)
refinement_poster(input_html_path=poster_path, prompts=poster_refinement_prompt,output_html_path=poster_final_output_path,model=model_name, config=config)
print (f"✅ Poster HTML refined. Final poster at: {poster_final_output_path}")
# # ============================= PR部分 ================================
# pr_template_path="./pr_template.md"
# basic_information_path = extract_basic_information(dag_path=dag_path, auto_path=auto_path,extract_basic_information_prompt=extract_basic_information_prompt,model=model_name, config=config)
# initialize_pr_markdown(basic_information_path=basic_information_path,auto_path=auto_path,pr_template_path=pr_template_path)
# pr_path=os.path.join(auto_path, "markdown.md")
# generate_pr_from_dag(dag_path=dag_path, pr_path=pr_path, generate_pr_prompt=generate_pr_prompt, model=model_name, config=config)
# print(f"📝 PR generated at: {pr_path}")
# add_title_and_hashtag(pr_path=pr_path, add_title_and_hashtag_prompt=add_title_and_hashtag_prompt, model=model_name, config=config)
# add_institution_tag(pr_path=pr_path)
# dedup_consecutive_markdown_images(pr_path, inplace=True)
# print(f"✅ PR markdown post-processed.")
# print(f"🖊️ Refining PR markdown with LLM...")
# pr_refine_path=os.path.join(auto_path, "markdown_refined.md")
# refinement_pr(pr_path=pr_path, pr_refine_path=pr_refine_path, prompts=pr_refinement_prompt, model=model_name, config=config)
# print (f"✅ PR markdown refined.")
# =============================================================
# 在auto目录下创建success.txt作为标志
success_file_path = os.path.join(auto_path, "success_poster.txt")
open(success_file_path, "w").close()
print(f"✅ Finished processing: {subdir}\n{'-' * 80}")
print("\n🎉 All papers processed successfully!")
if __name__ == "__main__":
main() |