-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.py
More file actions
executable file
·92 lines (78 loc) · 3.02 KB
/
Copy pathsummarizer.py
File metadata and controls
executable file
·92 lines (78 loc) · 3.02 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
#!/usr/bin/env python
from tg_reader import *
from llm_api import *
import yaml
from typing import Callable
class Summarizer:
def __init__(self, config: dict):
self.config = config
ollama_config: dict = self.config["ollama"]
self.__ollama = OllamaSession(
model=ollama_config["model"],
api_url=ollama_config["api_url"],
timeout=ollama_config.get("timeout", 300),
https_proxy=ollama_config.get("https_proxy")
)
telegram_config: dict = self.config["telegram_acc"]
self.__telegram = TgReader(
api_id=telegram_config["api_id"],
api_hash=telegram_config["api_hash"]
)
self.__consumers = []
def add_consumer(self, callback: Callable[[str], None]):
self.__consumers.append(callback)
def summarize(self):
with self.__telegram:
for chat_info in self.config.get("chats", []):
messages = self.__telegram.get_messages(
chat_id=chat_info["id"],
topic_id=chat_info.get("topic"),
offset_date=chat_info.get("last_read", datetime(2026, 7, 9))
)
if len(messages) < 1:
continue
chat_text: str = "\n".join(str(msg) for msg in messages)
summary = self.__ollama.get_summary(
chat_history=chat_text,
chat_description=chat_info.get("descr")
)
chat_title = self.__telegram.get_chat_name(chat_info['id'])
topic_id = chat_info.get("topic")
if topic_id is not None:
topic_title = self.__telegram.get_topic_name(chat_info['id'], topic_id)
chat_title = f"{chat_title} / {topic_title}"
summary = f"## {chat_title}\n\n{summary}\n\n---\n"
assert isinstance(summary, str)
for consumer in self.__consumers:
consumer(summary)
chat_info["last_read"] = messages[-1].date
self.__telegram.mark_as_read(
chat_info["id"],
messages[-1].id,
topic_id
)
if __name__ == '__main__':
import os
from argparse import ArgumentParser
from pathlib import Path
arg_parser = ArgumentParser(
description="Справка"
)
arg_parser.add_argument(
"-c",
"--config",
default=Path(__file__).resolve().parent / "config.yaml",
help="Путь к конфигурационному YAML-файлу"
)
args = arg_parser.parse_args()
with open(args.config, "r", encoding="utf-8") as file:
config: dict = yaml.safe_load(file.read())
summarizer = Summarizer(config)
summarizer.add_consumer(print)
summarizer.summarize()
with open(args.config, "w", encoding="utf-8") as file:
yaml.safe_dump(
config,
file,
default_flow_style=False
)