|
|
from pathlib import Path
|
|
|
from typing import Optional, Tuple
|
|
|
from openai import OpenAI, APIError
|
|
|
|
|
|
|
|
|
class ContentAnalyzer:
|
|
|
"""课程内容分析器"""
|
|
|
|
|
|
def __init__(
|
|
|
self,
|
|
|
api_key: str = "sk-01d13a39e09844038322108ecdbd1bbc",
|
|
|
base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
model: str = "deepseek-v3"
|
|
|
):
|
|
|
"""
|
|
|
初始化分析器
|
|
|
|
|
|
:param api_key: 阿里云API密钥
|
|
|
:param base_url: API基础地址
|
|
|
:param model: 使用的模型名称
|
|
|
"""
|
|
|
self.client = OpenAI(api_key=api_key, base_url=base_url)
|
|
|
self.model = model
|
|
|
|
|
|
def analyze_content(
|
|
|
self,
|
|
|
content: str,
|
|
|
prompt_template: str = "帮我梳理:这节课分了几个部分,每部分的名称和开始的时间是多少:{}"
|
|
|
) -> Tuple[bool, str]:
|
|
|
"""
|
|
|
分析课程内容结构
|
|
|
|
|
|
:param content: 需要分析的文本内容
|
|
|
:param prompt_template: 提示词模板,需包含一个{}用于插入内容
|
|
|
:return: (成功标志, 分析结果或错误信息)
|
|
|
"""
|
|
|
try:
|
|
|
full_prompt = prompt_template.format(content)
|
|
|
|
|
|
completion = self.client.chat.completions.create(
|
|
|
model=self.model,
|
|
|
messages=[{'role': 'user', 'content': full_prompt}]
|
|
|
)
|
|
|
|
|
|
if not completion.choices:
|
|
|
return False, "API响应中未包含有效结果"
|
|
|
|
|
|
return True, completion.choices[0].message.content
|
|
|
|
|
|
except APIError as e:
|
|
|
return False, f"API调用失败: {str(e)}"
|
|
|
except Exception as e:
|
|
|
return False, f"未处理的异常: {str(e)}"
|
|
|
|
|
|
def analyze_file(
|
|
|
self,
|
|
|
file_path: Path,
|
|
|
output_path: Optional[Path] = None,
|
|
|
encoding: str = 'utf-8'
|
|
|
) -> Tuple[bool, str]:
|
|
|
"""
|
|
|
从文件读取内容并分析
|
|
|
|
|
|
:param file_path: 输入文件路径
|
|
|
:param output_path: 输出文件路径(可选)
|
|
|
:param encoding: 文件编码格式
|
|
|
:return: (成功标志, 最终结果或错误信息)
|
|
|
"""
|
|
|
try:
|
|
|
if not file_path.exists():
|
|
|
return False, f"文件不存在: {file_path}"
|
|
|
|
|
|
content = file_path.read_text(encoding=encoding)
|
|
|
success, result = self.analyze_content(content)
|
|
|
|
|
|
if not success:
|
|
|
return False, result
|
|
|
|
|
|
# 保存结果
|
|
|
if output_path:
|
|
|
try:
|
|
|
output_path.write_text(result, encoding=encoding)
|
|
|
except IOError as e:
|
|
|
return False, f"结果保存失败: {str(e)}"
|
|
|
|
|
|
return True, result
|
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
return False, f"文件解码失败,请尝试使用正确的编码格式(当前使用 {encoding})"
|
|
|
except Exception as e:
|
|
|
return False, f"文件处理异常: {str(e)}"
|
|
|
|
|
|
|
|
|
def analyzer_action(input_file, output_file):
|
|
|
# 使用示例
|
|
|
analyzer = ContentAnalyzer()
|
|
|
success, result = analyzer.analyze_file(input_file, output_file)
|
|
|
if success:
|
|
|
print("✅ 分析成功!结果如下:\n")
|
|
|
print(result)
|
|
|
else:
|
|
|
print(f"❌ 分析失败: {result}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
input_file = Path(r"D:\dsWork\QingLong\AI\音频文本.txt")
|
|
|
output_file = Path(r"D:\dsWork\QingLong\AI\分析结果.txt")
|
|
|
analyzer_action(input_file, output_file) |