-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.py
More file actions
129 lines (107 loc) · 5.16 KB
/
summarizer.py
File metadata and controls
129 lines (107 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Основной модуль для создания саммари документов
"""
from pathlib import Path
from typing import List, Dict, Optional
from llm_providers.factory import LLMProviderFactory
from processors.processor_factory import ProcessorFactory
from processors.image_processor import ImageProcessor
from config import SUPPORTED_FORMATS, MAX_FILE_SIZE_MB, SUPPORTED_IMAGE_FORMATS
class DocumentSummarizer:
def __init__(
self,
provider: Optional[str] = None,
api_key: Optional[str] = None,
model: Optional[str] = None,
vision_model: Optional[str] = None
):
"""
Инициализация саммаризатора
Args:
provider: Имя провайдера LLM ('openrouter', 'openai', и т.д.)
api_key: API ключ (опционально)
model: Модель для использования (опционально)
vision_model: Vision-модель для анализа изображений (опционально)
"""
self.llm_provider = LLMProviderFactory.create_provider(
provider_name=provider,
api_key=api_key,
model=model,
vision_model=vision_model
)
self.processor_factory = ProcessorFactory()
def process_folder(self, folder_path: Path) -> Dict[str, any]:
if not folder_path.exists():
raise ValueError(f"Папка не существует: {folder_path}")
if not folder_path.is_dir():
raise ValueError(f"Указанный путь не является папкой: {folder_path}")
files = self._find_supported_files(folder_path)
if not files:
return {
'total_files': 0,
'processed_files': 0,
'individual_summaries': [],
'combined_summary': 'Не найдено поддерживаемых файлов для обработки.'
}
individual_summaries = []
processed_count = 0
for file_path in files:
try:
result = self._process_file(file_path)
if result:
individual_summaries.append(result)
processed_count += 1
except Exception as e:
print(f"Ошибка при обработке файла {file_path.name}: {str(e)}")
continue
combined_summary = ""
if individual_summaries:
try:
combined_summary = self.llm_provider.generate_combined_summary(individual_summaries)
except Exception as e:
combined_summary = f"Ошибка при создании общего саммари: {str(e)}"
return {
'total_files': len(files),
'processed_files': processed_count,
'individual_summaries': individual_summaries,
'combined_summary': combined_summary
}
def _find_supported_files(self, folder_path: Path) -> List[Path]:
files = []
for file_path in folder_path.iterdir():
if file_path.is_file():
if file_path.suffix.lower() in SUPPORTED_FORMATS:
file_size_mb = file_path.stat().st_size / (1024 * 1024)
if file_size_mb <= MAX_FILE_SIZE_MB:
files.append(file_path)
else:
print(f"Файл {file_path.name} слишком большой ({file_size_mb:.2f} МБ), пропускаем")
return sorted(files)
def _process_file(self, file_path: Path) -> Optional[Dict[str, str]]:
processor = self.processor_factory.get_processor(file_path)
if not processor:
print(f"Не найден процессор для файла: {file_path.name}")
return None
print(f"Обработка файла: {file_path.name}")
is_image = file_path.suffix.lower() in SUPPORTED_IMAGE_FORMATS
if is_image and isinstance(processor, ImageProcessor):
extracted_content = processor.extract_text(file_path, llm_provider=self.llm_provider)
else:
extracted_content = processor.extract_text(file_path)
if not extracted_content or not extracted_content.strip():
print(f"Не удалось извлечь содержимое из файла: {file_path.name}")
return None
try:
if is_image:
summary = self.llm_provider.generate_summary(extracted_content)
else:
summary = self.llm_provider.generate_summary(extracted_content)
return {
'filename': file_path.name,
'summary': summary
}
except Exception as e:
print(f"Ошибка при создании саммари для {file_path.name}: {str(e)}")
return None
def add_processor(self, processor):
self.processor_factory.register_processor(processor)