You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6 KiB

5 months ago
from pathlib import Path
5 months ago
from typing import Optional, Tuple
from openai import OpenAI, APIError
class ContentAnalyzer:
5 months ago
"""课程内容分析器"""
5 months ago
def __init__(
self,
api_key: str = "sk-01d13a39e09844038322108ecdbd1bbc",
base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1",
5 months ago
model: str = "deepseek-v3"
5 months ago
):
"""
初始化分析器
5 months ago
5 months ago
: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]:
"""
分析课程内容结构
5 months ago
5 months ago
:param content: 需要分析的文本内容
5 months ago
:param prompt_template: 提示词模板需包含一个{}用于插入内容
5 months ago
: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]:
"""
从文件读取内容并分析
5 months ago
5 months ago
:param file_path: 输入文件路径
:param output_path: 输出文件路径可选
:param encoding: 文件编码格式
:return: (成功标志, 最终结果或错误信息)
"""
try:
if not file_path.exists():
return False, f"文件不存在: {file_path}"
5 months ago
content = file_path.read_text(encoding=encoding)
5 months ago
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
5 months ago
except UnicodeDecodeError:
return False, f"文件解码失败,请尝试使用正确的编码格式(当前使用 {encoding}"
5 months ago
except Exception as e:
return False, f"文件处理异常: {str(e)}"
5 months ago
def analyzer_action(input_file, output_file):
# 使用示例
5 months ago
analyzer = ContentAnalyzer()
5 months ago
success, result = analyzer.analyze_file(input_file, output_file)
5 months ago
if success:
5 months ago
print("✅ 分析成功!结果如下:\n")
5 months ago
print(result)
else:
5 months ago
print(f"❌ 分析失败: {result}")
5 months ago
5 months ago
if __name__ == '__main__':
input_file = Path(r"D:\dsWork\QingLong\AI\音频文本.txt")
output_file = Path(r"D:\dsWork\QingLong\AI\分析结果.txt")
5 months ago
analyzer_action(input_file, output_file)