Spaces:
Sleeping
Sleeping
Update api/syllabus_utils.py
Browse files- api/syllabus_utils.py +9 -12
api/syllabus_utils.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
# syllabus_utils.py
|
| 2 |
"""
|
| 3 |
工具函数:
|
| 4 |
-
- 解析 Syllabus(.docx / .pdf/ .pptx)
|
| 5 |
- 提取课程大纲 topics
|
| 6 |
"""
|
| 7 |
|
|
@@ -10,15 +9,16 @@ from typing import List
|
|
| 10 |
|
| 11 |
from docx import Document
|
| 12 |
from pypdf import PdfReader
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def parse_syllabus_docx(path: str) -> List[str]:
|
| 18 |
"""
|
| 19 |
从 .docx 文件中提取课程大纲。
|
| 20 |
-
|
| 21 |
-
后面如果你想按 Week 0/1/2 更精细地切,可以再优化这里。
|
| 22 |
"""
|
| 23 |
doc = Document(path)
|
| 24 |
paragraphs = [p.text.strip() for p in doc.paragraphs if p.text and p.text.strip()]
|
|
@@ -56,8 +56,6 @@ def parse_syllabus_pdf(path: str) -> List[str]:
|
|
| 56 |
return chunks[: len(DEFAULT_COURSE_TOPICS)] or DEFAULT_COURSE_TOPICS
|
| 57 |
|
| 58 |
|
| 59 |
-
from pptx import Presentation # python-pptx
|
| 60 |
-
|
| 61 |
def parse_pptx_slides(path: str) -> List[str]:
|
| 62 |
"""
|
| 63 |
从 .pptx 文件中抽取每一页 slide 的文本,返回一个字符串列表。
|
|
@@ -84,16 +82,14 @@ def extract_course_topics_from_file(file_obj, doc_type: str) -> List[str]:
|
|
| 84 |
"""
|
| 85 |
根据上传文件和 doc_type 提取课程大纲 topics。
|
| 86 |
- 只有 doc_type == "Syllabus" 时才尝试从文件解析;否则用默认大纲。
|
| 87 |
-
- 支持 .docx + .pdf
|
| 88 |
"""
|
| 89 |
if file_obj is None:
|
| 90 |
return DEFAULT_COURSE_TOPICS
|
| 91 |
|
| 92 |
if doc_type != "Syllabus":
|
| 93 |
-
# 不是 Syllabus,就不要动课程大纲(可以用默认)
|
| 94 |
return DEFAULT_COURSE_TOPICS
|
| 95 |
|
| 96 |
-
# Gradio File 对象通常有 .name 属性(临时文件路径)
|
| 97 |
file_path = getattr(file_obj, "name", None)
|
| 98 |
if not file_path:
|
| 99 |
return DEFAULT_COURSE_TOPICS
|
|
@@ -105,6 +101,8 @@ def extract_course_topics_from_file(file_obj, doc_type: str) -> List[str]:
|
|
| 105 |
topics = parse_syllabus_docx(file_path)
|
| 106 |
elif ext == ".pdf":
|
| 107 |
topics = parse_syllabus_pdf(file_path)
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
print(f"[Syllabus] Unsupported file type for syllabus: {ext}")
|
| 110 |
topics = DEFAULT_COURSE_TOPICS
|
|
@@ -112,5 +110,4 @@ def extract_course_topics_from_file(file_obj, doc_type: str) -> List[str]:
|
|
| 112 |
print(f"[Syllabus] parse error: {repr(e)}")
|
| 113 |
topics = DEFAULT_COURSE_TOPICS
|
| 114 |
|
| 115 |
-
|
| 116 |
-
return topics or DEFAULT_COURSE_TOPICS
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
工具函数:
|
| 3 |
+
- 解析 Syllabus(.docx / .pdf / .pptx)
|
| 4 |
- 提取课程大纲 topics
|
| 5 |
"""
|
| 6 |
|
|
|
|
| 9 |
|
| 10 |
from docx import Document
|
| 11 |
from pypdf import PdfReader
|
| 12 |
+
from pptx import Presentation # python-pptx
|
| 13 |
|
| 14 |
+
# 关键修复:从 api 包内导入 config
|
| 15 |
+
from .config import DEFAULT_COURSE_TOPICS
|
| 16 |
|
| 17 |
|
| 18 |
def parse_syllabus_docx(path: str) -> List[str]:
|
| 19 |
"""
|
| 20 |
从 .docx 文件中提取课程大纲。
|
| 21 |
+
简单版本:按段落抽取,过滤空行。
|
|
|
|
| 22 |
"""
|
| 23 |
doc = Document(path)
|
| 24 |
paragraphs = [p.text.strip() for p in doc.paragraphs if p.text and p.text.strip()]
|
|
|
|
| 56 |
return chunks[: len(DEFAULT_COURSE_TOPICS)] or DEFAULT_COURSE_TOPICS
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
| 59 |
def parse_pptx_slides(path: str) -> List[str]:
|
| 60 |
"""
|
| 61 |
从 .pptx 文件中抽取每一页 slide 的文本,返回一个字符串列表。
|
|
|
|
| 82 |
"""
|
| 83 |
根据上传文件和 doc_type 提取课程大纲 topics。
|
| 84 |
- 只有 doc_type == "Syllabus" 时才尝试从文件解析;否则用默认大纲。
|
| 85 |
+
- 支持 .docx + .pdf + .pptx
|
| 86 |
"""
|
| 87 |
if file_obj is None:
|
| 88 |
return DEFAULT_COURSE_TOPICS
|
| 89 |
|
| 90 |
if doc_type != "Syllabus":
|
|
|
|
| 91 |
return DEFAULT_COURSE_TOPICS
|
| 92 |
|
|
|
|
| 93 |
file_path = getattr(file_obj, "name", None)
|
| 94 |
if not file_path:
|
| 95 |
return DEFAULT_COURSE_TOPICS
|
|
|
|
| 101 |
topics = parse_syllabus_docx(file_path)
|
| 102 |
elif ext == ".pdf":
|
| 103 |
topics = parse_syllabus_pdf(file_path)
|
| 104 |
+
elif ext == ".pptx":
|
| 105 |
+
topics = parse_pptx_slides(file_path)
|
| 106 |
else:
|
| 107 |
print(f"[Syllabus] Unsupported file type for syllabus: {ext}")
|
| 108 |
topics = DEFAULT_COURSE_TOPICS
|
|
|
|
| 110 |
print(f"[Syllabus] parse error: {repr(e)}")
|
| 111 |
topics = DEFAULT_COURSE_TOPICS
|
| 112 |
|
| 113 |
+
return topics or DEFAULT_COURSE_TOPICS
|
|
|