Laramie2 commited on
Commit
5643e69
·
verified ·
1 Parent(s): 6846f04

Update gen_ppt.py

Browse files
Files changed (1) hide show
  1. gen_ppt.py +43 -17
gen_ppt.py CHANGED
@@ -4,13 +4,44 @@ from pathlib import Path
4
 
5
  from src import *
6
 
7
- import yaml
8
- import os
9
-
10
- # ============= 读取配置 ===============
11
- def load_config(config_path="config.yaml"):
12
- with open(config_path, "r", encoding="utf-8") as f:
13
- config = yaml.safe_load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return config
15
 
16
  # ========== 读取 prompt 模板 ==========
@@ -19,18 +50,16 @@ def load_prompt(prompt_path="prompt.json", prompt_name="poster_prompt"):
19
  data = json.load(f)
20
  return data.get(prompt_name, "")
21
 
22
- # 应用配置
23
  config = load_config()
24
-
25
  model_name = config['model_settings']['generation_model']
26
 
27
  # ========== 主流程 ==========
28
  def main():
29
- # 输入总文件夹路径(包含多个论文子文件夹)
30
- root_folder = config['path']['root_folder'] # ⚠️ 修改为你的实际路径
31
  prompt_path = "prompt.json"
32
 
33
-
34
  print("📘 Loading prompts...")
35
  # dag prompt:
36
  section_split_prompt = load_prompt(prompt_path, prompt_name="section_split_prompt")
@@ -57,9 +86,6 @@ def main():
57
  add_title_and_hashtag_prompt = load_prompt(prompt_path, prompt_name="add_title_and_hashtag_prompt")
58
  pr_refinement_prompt = load_prompt(prompt_path, prompt_name="pr_refinement_prompt")
59
 
60
-
61
-
62
-
63
  # === 遍历每个子文件夹 ===
64
  for subdir in os.listdir(root_folder):
65
  subdir_path = os.path.join(root_folder, subdir)
@@ -98,7 +124,7 @@ def main():
98
  output_html = os.path.join(auto_path, "poster.html")
99
  graph_json_path = os.path.join(auto_path, "graph.json")
100
 
101
- # # === 清理 markdown === 去除无意义的段落,如relative work,reference,appendix等等
102
  # print("🧹 Cleaning markdown before splitting...")
103
  # cleaned_md_path = clean_paper(md_path, clean_prompt, model="gemini-3-pro-preview", config=config)
104
 
@@ -151,7 +177,7 @@ def main():
151
 
152
  # === 生成最终的PPT ===
153
  ppt_template_path="./ppt_template"
154
- 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 <!-- You need to revise the following parts. X: ... -->. 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 '![](images/abc.jpg)', 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."}
155
  generate_ppt(outline_path,ppt_template_path,generate_ppt_prompt,model=model_name, config=config)
156
 
157
  # === Refiner ===
 
4
 
5
  from src import *
6
 
7
+ # ============= 从环境变量动态生成配置 ===============
8
+ def load_config():
9
+ """
10
+ 不再读取 config.yaml,而是从 Gradio 主程序注入的环境变量中获取专属参数,
11
+ 并组装成原有代码期望的字典结构,实现多用户隔离。
12
+ """
13
+ base_dir = os.path.dirname(os.path.abspath(__file__))
14
+
15
+ # 获取由 app.py 注入的专属输出文件夹,如果没有则回退到本地默认文件夹(方便本地调试)
16
+ user_root_folder = os.environ.get("USER_OUTPUT_DIR", os.path.join(base_dir, "mineru_outputs"))
17
+
18
+ # 获取由 app.py 注入的 API 凭证
19
+ api_key = os.environ.get("GEMINI_API_KEY", "")
20
+ api_base_url = os.environ.get("GEMINI_API_BASE_URL", "")
21
+
22
+ # 可以在这里设置默认使用的模型,或者也改用环境变量传入
23
+ generation_model = os.environ.get("GENERATION_MODEL", "gemini-3-pro-preview")
24
+
25
+ # 拼装成原版 config.yaml 的字典结构,保证后续代码兼容
26
+ config = {
27
+ "model_settings": {
28
+ "generation_model": generation_model
29
+ },
30
+ "path": {
31
+ "root_folder": user_root_folder
32
+ },
33
+ "api_keys": {
34
+ "gemini_api_key": api_key,
35
+ "api_base_url": api_base_url
36
+ }
37
+ }
38
+
39
+ # 如果你的底层 src 代码库也需要直接用到 os.environ,我们在这里显式注入一下
40
+ if api_key:
41
+ os.environ["GEMINI_API_KEY"] = api_key
42
+ if api_base_url:
43
+ os.environ["GEMINI_API_BASE_URL"] = api_base_url
44
+
45
  return config
46
 
47
  # ========== 读取 prompt 模板 ==========
 
50
  data = json.load(f)
51
  return data.get(prompt_name, "")
52
 
53
+ # 应用配置 (此时 config 已经在内存中组装好了)
54
  config = load_config()
 
55
  model_name = config['model_settings']['generation_model']
56
 
57
  # ========== 主流程 ==========
58
  def main():
59
+ # 这里的 root_folder 现在指向的是当前用户的专属 mineru_outputs 文件夹
60
+ root_folder = config['path']['root_folder']
61
  prompt_path = "prompt.json"
62
 
 
63
  print("📘 Loading prompts...")
64
  # dag prompt:
65
  section_split_prompt = load_prompt(prompt_path, prompt_name="section_split_prompt")
 
86
  add_title_and_hashtag_prompt = load_prompt(prompt_path, prompt_name="add_title_and_hashtag_prompt")
87
  pr_refinement_prompt = load_prompt(prompt_path, prompt_name="pr_refinement_prompt")
88
 
 
 
 
89
  # === 遍历每个子文件夹 ===
90
  for subdir in os.listdir(root_folder):
91
  subdir_path = os.path.join(root_folder, subdir)
 
124
  output_html = os.path.join(auto_path, "poster.html")
125
  graph_json_path = os.path.join(auto_path, "graph.json")
126
 
127
+ # # === 清理 markdown === 去除无意义的段落,如relative work,reference,appendix等等
128
  # print("🧹 Cleaning markdown before splitting...")
129
  # cleaned_md_path = clean_paper(md_path, clean_prompt, model="gemini-3-pro-preview", config=config)
130
 
 
177
 
178
  # === 生成最终的PPT ===
179
  ppt_template_path="./ppt_template"
180
+ 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 '![](images/abc.jpg)', 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."}
181
  generate_ppt(outline_path,ppt_template_path,generate_ppt_prompt,model=model_name, config=config)
182
 
183
  # === Refiner ===