Spaces:
Sleeping
Sleeping
Update api/syllabus_utils.py
Browse files14 unmodified lines151617181920212219 unmodified lines4243444546474830 unmodified lines798081828384853 unmodified lines89909192939495969714 unmodified linesfrom pptx import Presentation # python-pptxfrom api.config import DEFAULT_COURSE_TOPICSfrom api.some_module import parse_syllabus_docx, parse_syllabus_pdf, parse_pptx_slidesdef parse_syllabus_docx(path: str) -> List[str]:19 unmodified lines- 返回前若干段作为“课程大纲 topics”"""reader = PdfReader(path)pages_text = []for page in reader.pages:text = page.extract_text() or ""if text.strip():30 unmodified linesdef extract_course_topics_from_file(file_obj, doc_type: str) -> List[str]:"""根据上传文件和 doc_type 提取课程大纲 topics。- 只有 doc_type == "Syllabus" 时才尝试从文件解析;否则用默认大纲。- 支持 .docx / .pdf / .pptx"""if file_obj is None:3 unmodified linesif doc_type_norm != "syllabus":return DEFAULT_COURSE_TOPICS# 兼容两种:file_obj.name (临时文件路径) / UploadFile.filename(只是名字,不是路径)file_path = getattr(file_obj, "name", None) or getattr(file_obj, "filename", None)if not file_path:return DEFAULT_COURSE_TOPICSext = os.path.splitext(file_path)[1].lower()14 unmodified lines15161718192019 unmodified lines404142434445464730 unmodified lines787980818283843 unmodified lines8889909192939495969714 unmodified linesfrom pptx import Presentation # python-pptxfrom api.config import DEFAULT_COURSE_TOPICSdef parse_syllabus_docx(path: str) -> List[str]:19 unmodified lines- 返回前若干段作为“课程大纲 topics”"""reader = PdfReader(path)pages_text: List[str] = []for page in reader.pages:text = page.extract_text() or ""if text.strip():30 unmodified linesdef extract_course_topics_from_file(file_obj, doc_type: str) -> List[str]:"""根据上传文件和 doc_type 提取课程大纲 topics。- 只有 doc_type == "syllabus" 时才尝试从文件解析;否则用默认大纲。- 支持 .docx / .pdf / .pptx"""if file_obj is None:3 unmodified linesif doc_type_norm != "syllabus":return DEFAULT_COURSE_TOPICS# 这里必须是“真实可读路径”,你的 server.py 会传 fo.name = /tmp/xxxfile_path = getattr(file_obj, "name", None)if not file_path or not os.path.exists(file_path):print(f"[Syllabus] file path missing or not found: {file_path!r}")return DEFAULT_COURSE_TOPICSext = os.path.splitext(file_path)[1].lower()
api/syllabus_utils.py+7-7