Spaces:
Sleeping
Sleeping
知识树切换为markmap交互式思维导图,展示全部内容,移除Mermaid图
Browse files
app.py
CHANGED
|
@@ -234,7 +234,31 @@ def outline_to_mermaid_tree(outline, qa_list=None, max_items=3):
|
|
| 234 |
node_id += 1
|
| 235 |
return 'graph TD\n ' + '\n '.join(nodes + edges)
|
| 236 |
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
def main_func(files, text_input, model_type, max_questions, task_types, api_key=None):
|
| 240 |
outline_result = ""
|
|
@@ -242,7 +266,7 @@ def main_func(files, text_input, model_type, max_questions, task_types, api_key=
|
|
| 242 |
tree_result = ""
|
| 243 |
outline_list = []
|
| 244 |
qa_list = []
|
| 245 |
-
|
| 246 |
if files is not None:
|
| 247 |
if not isinstance(files, list):
|
| 248 |
files = [files]
|
|
@@ -260,7 +284,12 @@ def main_func(files, text_input, model_type, max_questions, task_types, api_key=
|
|
| 260 |
qa_list = call_llm_api(file_text, model_type=model_type, max_questions=max_questions)
|
| 261 |
qa_result += f"【{file.name}】\n{format_qa_output_per_file([(file.name, qa_list)])}\n\n"
|
| 262 |
if "生成知识树" in task_types and (outline_result or qa_result):
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
if text_input.strip():
|
| 265 |
if "生成大纲" in task_types:
|
| 266 |
outline = generate_outline(text_input.strip(), model_type=model_type)
|
|
@@ -271,11 +300,16 @@ def main_func(files, text_input, model_type, max_questions, task_types, api_key=
|
|
| 271 |
qa_list = call_llm_api(text_input.strip(), model_type=model_type, max_questions=max_questions)
|
| 272 |
qa_result += f"【粘贴内容】\n{format_qa_output_per_file([('粘贴内容', qa_list)])}\n\n"
|
| 273 |
if "生成知识树" in task_types and (outline_result or qa_result):
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
return "", "", "请上传文件或输入文本内容 ( •̀ ω •́ )✧"
|
| 277 |
-
return outline_result.strip(), qa_result.strip(),
|
| 278 |
|
|
|
|
| 279 |
with gr.Blocks(css=custom_css) as demo:
|
| 280 |
gr.Markdown("""
|
| 281 |
<div align='center'>
|
|
@@ -303,7 +337,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 303 |
gr.Markdown("### 📚 考题", elem_id="output-title-qa")
|
| 304 |
output_qa = gr.Textbox(label="📚 考题", lines=10, interactive=False, elem_id="output-qa")
|
| 305 |
gr.Markdown("### 🌳 知识树(思维导图)", elem_id="output-title-tree")
|
| 306 |
-
output_tree = gr.
|
| 307 |
gr.Markdown(
|
| 308 |
'''
|
| 309 |
<hr>
|
|
|
|
| 234 |
node_id += 1
|
| 235 |
return 'graph TD\n ' + '\n '.join(nodes + edges)
|
| 236 |
|
| 237 |
+
def outline_to_markmap_md(outline, qa_list=None):
|
| 238 |
+
lines = outline.split('\n') if outline else []
|
| 239 |
+
md = "# 知识树\n"
|
| 240 |
+
if lines:
|
| 241 |
+
md += "## 大纲\n"
|
| 242 |
+
for l in lines:
|
| 243 |
+
l = l.strip()
|
| 244 |
+
if l and not l.startswith('#'):
|
| 245 |
+
md += f"- {l}\n"
|
| 246 |
+
if qa_list:
|
| 247 |
+
md += "## 考题\n"
|
| 248 |
+
for i, qa in enumerate(qa_list):
|
| 249 |
+
if isinstance(qa, dict):
|
| 250 |
+
q = qa.get('question', '')
|
| 251 |
+
a = qa.get('answer', '')
|
| 252 |
+
elif isinstance(qa, str):
|
| 253 |
+
q, a = qa, ''
|
| 254 |
+
else:
|
| 255 |
+
q, a = qa
|
| 256 |
+
md += f"- Q{i+1}: {q}\n"
|
| 257 |
+
if a:
|
| 258 |
+
md += f" - 答案: {a}\n"
|
| 259 |
+
return md
|
| 260 |
+
|
| 261 |
+
# main_func 集成 markmap 输出
|
| 262 |
|
| 263 |
def main_func(files, text_input, model_type, max_questions, task_types, api_key=None):
|
| 264 |
outline_result = ""
|
|
|
|
| 266 |
tree_result = ""
|
| 267 |
outline_list = []
|
| 268 |
qa_list = []
|
| 269 |
+
markmap_html = ""
|
| 270 |
if files is not None:
|
| 271 |
if not isinstance(files, list):
|
| 272 |
files = [files]
|
|
|
|
| 284 |
qa_list = call_llm_api(file_text, model_type=model_type, max_questions=max_questions)
|
| 285 |
qa_result += f"【{file.name}】\n{format_qa_output_per_file([(file.name, qa_list)])}\n\n"
|
| 286 |
if "生成知识树" in task_types and (outline_result or qa_result):
|
| 287 |
+
md = outline_to_markmap_md(locals().get('outline', ''), locals().get('qa_list', []))
|
| 288 |
+
# JS字符串转义
|
| 289 |
+
md_js = md.replace('`', '\\`').replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
|
| 290 |
+
markmap_html += f'''<div id="markmap"></div>
|
| 291 |
+
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
|
| 292 |
+
<script>window.markmap.autoLoader.render('#markmap', `{md_js}`);</script>'''
|
| 293 |
if text_input.strip():
|
| 294 |
if "生成大纲" in task_types:
|
| 295 |
outline = generate_outline(text_input.strip(), model_type=model_type)
|
|
|
|
| 300 |
qa_list = call_llm_api(text_input.strip(), model_type=model_type, max_questions=max_questions)
|
| 301 |
qa_result += f"【粘贴内容】\n{format_qa_output_per_file([('粘贴内容', qa_list)])}\n\n"
|
| 302 |
if "生成知识树" in task_types and (outline_result or qa_result):
|
| 303 |
+
md = outline_to_markmap_md(locals().get('outline', ''), locals().get('qa_list', []))
|
| 304 |
+
md_js = md.replace('`', '\\`').replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
|
| 305 |
+
markmap_html += f'''<div id="markmap"></div>
|
| 306 |
+
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
|
| 307 |
+
<script>window.markmap.autoLoader.render('#markmap', `{md_js}`);</script>'''
|
| 308 |
+
if not (outline_result or qa_result or markmap_html):
|
| 309 |
return "", "", "请上传文件或输入文本内容 ( •̀ ω •́ )✧"
|
| 310 |
+
return outline_result.strip(), qa_result.strip(), markmap_html.strip()
|
| 311 |
|
| 312 |
+
# 前端知识树区域用 gr.HTML 渲染 markmap
|
| 313 |
with gr.Blocks(css=custom_css) as demo:
|
| 314 |
gr.Markdown("""
|
| 315 |
<div align='center'>
|
|
|
|
| 337 |
gr.Markdown("### 📚 考题", elem_id="output-title-qa")
|
| 338 |
output_qa = gr.Textbox(label="📚 考题", lines=10, interactive=False, elem_id="output-qa")
|
| 339 |
gr.Markdown("### 🌳 知识树(思维导图)", elem_id="output-title-tree")
|
| 340 |
+
output_tree = gr.HTML(label="🌳 知识树", elem_id="output-tree")
|
| 341 |
gr.Markdown(
|
| 342 |
'''
|
| 343 |
<hr>
|