chatterer 0.1.25__py3-none-any.whl → 0.1.27__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chatterer/__init__.py +87 -97
- chatterer/common_types/__init__.py +21 -21
- chatterer/common_types/io.py +19 -19
- chatterer/constants.py +5 -0
- chatterer/examples/__main__.py +75 -75
- chatterer/examples/any2md.py +83 -85
- chatterer/examples/pdf2md.py +231 -338
- chatterer/examples/pdf2txt.py +52 -54
- chatterer/examples/ppt.py +487 -486
- chatterer/examples/pw.py +141 -143
- chatterer/examples/snippet.py +54 -56
- chatterer/examples/transcribe.py +192 -192
- chatterer/examples/upstage.py +87 -89
- chatterer/examples/web2md.py +80 -80
- chatterer/interactive.py +422 -354
- chatterer/language_model.py +530 -536
- chatterer/messages.py +21 -21
- chatterer/tools/__init__.py +46 -46
- chatterer/tools/caption_markdown_images.py +388 -384
- chatterer/tools/citation_chunking/__init__.py +3 -3
- chatterer/tools/citation_chunking/chunks.py +51 -53
- chatterer/tools/citation_chunking/citation_chunker.py +117 -118
- chatterer/tools/citation_chunking/citations.py +284 -285
- chatterer/tools/citation_chunking/prompt.py +157 -157
- chatterer/tools/citation_chunking/reference.py +26 -26
- chatterer/tools/citation_chunking/utils.py +138 -138
- chatterer/tools/convert_pdf_to_markdown.py +636 -645
- chatterer/tools/convert_to_text.py +446 -446
- chatterer/tools/upstage_document_parser.py +704 -705
- chatterer/tools/webpage_to_markdown.py +739 -739
- chatterer/tools/youtube.py +146 -147
- chatterer/utils/__init__.py +15 -15
- chatterer/utils/base64_image.py +349 -293
- chatterer/utils/bytesio.py +59 -59
- chatterer/utils/code_agent.py +237 -237
- chatterer/utils/imghdr.py +145 -148
- {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/METADATA +377 -390
- chatterer-0.1.27.dist-info/RECORD +43 -0
- chatterer/strategies/__init__.py +0 -13
- chatterer/strategies/atom_of_thoughts.py +0 -975
- chatterer/strategies/base.py +0 -14
- chatterer-0.1.25.dist-info/RECORD +0 -45
- {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/WHEEL +0 -0
- {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/entry_points.txt +0 -0
- {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/top_level.txt +0 -0
chatterer/examples/pw.py
CHANGED
@@ -1,143 +1,141 @@
|
|
1
|
-
import json
|
2
|
-
import
|
3
|
-
import
|
4
|
-
|
5
|
-
|
6
|
-
from spargear import ArgumentSpec, BaseArguments, RunnableArguments, SubcommandSpec
|
7
|
-
|
8
|
-
from chatterer import PlayWrightBot
|
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
|
-
url
|
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
|
-
url
|
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
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
if __name__ == "__main__":
|
143
|
-
main()
|
1
|
+
import json
|
2
|
+
import sys
|
3
|
+
from pathlib import Path
|
4
|
+
|
5
|
+
from loguru import logger
|
6
|
+
from spargear import ArgumentSpec, BaseArguments, RunnableArguments, SubcommandSpec
|
7
|
+
|
8
|
+
from chatterer import PlayWrightBot
|
9
|
+
|
10
|
+
|
11
|
+
def generate_json_path() -> Path:
|
12
|
+
return Path("session_state.json").resolve()
|
13
|
+
|
14
|
+
|
15
|
+
class ReadArgs(RunnableArguments[None]):
|
16
|
+
"""Arguments for the 'read' subcommand."""
|
17
|
+
|
18
|
+
URL: str
|
19
|
+
"""URL (potentially protected) to navigate to using the saved session."""
|
20
|
+
json: ArgumentSpec[Path] = ArgumentSpec(
|
21
|
+
["--json", "-j"],
|
22
|
+
default_factory=generate_json_path,
|
23
|
+
help="Path to the session state JSON file to load.",
|
24
|
+
)
|
25
|
+
|
26
|
+
def run(self) -> None:
|
27
|
+
"""
|
28
|
+
Loads the session state from the specified JSON file, then navigates
|
29
|
+
to a protected_url that normally requires login. If the stored session
|
30
|
+
is valid, it should open without re-entering credentials.
|
31
|
+
|
32
|
+
Correction: Loads the JSON content into a dict first to satisfy type hints.
|
33
|
+
"""
|
34
|
+
url = self.URL
|
35
|
+
jsonpath = self.json.unwrap()
|
36
|
+
logger.info(f"Loading session from {jsonpath} and navigating to {url} ...")
|
37
|
+
|
38
|
+
if not jsonpath.exists():
|
39
|
+
logger.error(f"Session file not found at {jsonpath}")
|
40
|
+
sys.exit(1)
|
41
|
+
|
42
|
+
# Load the storage state from the JSON file into a dictionary
|
43
|
+
logger.info(f"Reading storage state content from {jsonpath} ...")
|
44
|
+
try:
|
45
|
+
with open(jsonpath, "r", encoding="utf-8") as f:
|
46
|
+
# This dictionary should match the 'StorageState' type expected by Playwright/chatterer
|
47
|
+
storage_state_dict = json.load(f)
|
48
|
+
except json.JSONDecodeError:
|
49
|
+
logger.error(f"Failed to decode JSON from {jsonpath}")
|
50
|
+
sys.exit(1)
|
51
|
+
except Exception as e:
|
52
|
+
logger.error(f"Error reading file {jsonpath}: {e}")
|
53
|
+
sys.exit(1)
|
54
|
+
|
55
|
+
logger.info("Launching browser with loaded session state...")
|
56
|
+
with PlayWrightBot(
|
57
|
+
playwright_launch_options={"headless": False},
|
58
|
+
# Pass the loaded dictionary, which should match the expected 'StorageState' type
|
59
|
+
playwright_persistency_options={"storage_state": storage_state_dict},
|
60
|
+
) as bot:
|
61
|
+
bot.get_page(url)
|
62
|
+
|
63
|
+
logger.info("Press Enter in the console when you're done checking the protected page.")
|
64
|
+
input(" >> Press Enter to exit: ")
|
65
|
+
|
66
|
+
logger.info("Done! Browser is now closed.")
|
67
|
+
|
68
|
+
|
69
|
+
class WriteArgs(RunnableArguments[None]):
|
70
|
+
"""Arguments for the 'write' subcommand."""
|
71
|
+
|
72
|
+
URL: str
|
73
|
+
"""URL to navigate to for manual login."""
|
74
|
+
json: ArgumentSpec[Path] = ArgumentSpec(
|
75
|
+
["--json", "-j"],
|
76
|
+
default_factory=generate_json_path,
|
77
|
+
help="Path to save the session state JSON file.",
|
78
|
+
)
|
79
|
+
|
80
|
+
def run(self) -> None:
|
81
|
+
"""
|
82
|
+
Launches a non-headless browser and navigates to the login_url.
|
83
|
+
The user can manually log in, then press Enter in the console
|
84
|
+
to store the current session state into a JSON file.
|
85
|
+
"""
|
86
|
+
url = self.URL
|
87
|
+
jsonpath = self.json.unwrap()
|
88
|
+
logger.info(f"Launching browser and navigating to {url} ... Please log in manually.")
|
89
|
+
|
90
|
+
# Ensure jsonpath directory exists
|
91
|
+
jsonpath.parent.mkdir(parents=True, exist_ok=True)
|
92
|
+
|
93
|
+
with PlayWrightBot(playwright_launch_options={"headless": False}) as bot:
|
94
|
+
bot.get_page(url)
|
95
|
+
|
96
|
+
logger.info("After completing the login in the browser, press Enter here to save the session.")
|
97
|
+
input(" >> Press Enter when ready: ")
|
98
|
+
|
99
|
+
# get_sync_browser() returns the BrowserContext internally
|
100
|
+
context = bot.get_sync_browser()
|
101
|
+
|
102
|
+
# Save the current session (cookies, localStorage) to a JSON file
|
103
|
+
logger.info(f"Saving storage state to {jsonpath} ...")
|
104
|
+
context.storage_state(path=jsonpath) # Pass Path object directly
|
105
|
+
|
106
|
+
logger.info("Done! Browser is now closed.")
|
107
|
+
|
108
|
+
|
109
|
+
class Arguments(BaseArguments):
|
110
|
+
"""
|
111
|
+
A simple CLI tool for saving and using Playwright sessions via storage_state.
|
112
|
+
Uses spargear for declarative argument parsing.
|
113
|
+
"""
|
114
|
+
|
115
|
+
read: SubcommandSpec[ReadArgs] = SubcommandSpec(
|
116
|
+
name="read",
|
117
|
+
argument_class=ReadArgs,
|
118
|
+
help="Use a saved session to view a protected page.",
|
119
|
+
description="Loads session state from the specified JSON file and navigates to the URL.",
|
120
|
+
)
|
121
|
+
write: SubcommandSpec[WriteArgs] = SubcommandSpec(
|
122
|
+
name="write",
|
123
|
+
argument_class=WriteArgs,
|
124
|
+
help="Save a new session by manually logging in.",
|
125
|
+
description="Launches a browser to the specified URL. Log in manually, then press Enter to save session state.",
|
126
|
+
)
|
127
|
+
|
128
|
+
def run(self) -> None:
|
129
|
+
"""Parses arguments using spargear and executes the corresponding command."""
|
130
|
+
if isinstance(last_subcommand := self.last_command, RunnableArguments):
|
131
|
+
last_subcommand.run()
|
132
|
+
else:
|
133
|
+
self.get_parser().print_help()
|
134
|
+
|
135
|
+
|
136
|
+
def main() -> None:
|
137
|
+
Arguments().run()
|
138
|
+
|
139
|
+
|
140
|
+
if __name__ == "__main__":
|
141
|
+
main()
|
chatterer/examples/snippet.py
CHANGED
@@ -1,56 +1,54 @@
|
|
1
|
-
import
|
2
|
-
from
|
3
|
-
from
|
4
|
-
|
5
|
-
|
6
|
-
from spargear import RunnableArguments
|
7
|
-
|
8
|
-
from chatterer import CodeSnippets
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
"""
|
16
|
-
|
17
|
-
"""
|
18
|
-
|
19
|
-
"""List of
|
20
|
-
|
21
|
-
"""
|
22
|
-
|
23
|
-
"""
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
output = Path(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
if __name__ == "__main__":
|
56
|
-
main()
|
1
|
+
from datetime import datetime
|
2
|
+
from pathlib import Path
|
3
|
+
from typing import Optional
|
4
|
+
|
5
|
+
from loguru import logger
|
6
|
+
from spargear import RunnableArguments
|
7
|
+
|
8
|
+
from chatterer import CodeSnippets
|
9
|
+
|
10
|
+
|
11
|
+
class Arguments(RunnableArguments[CodeSnippets]):
|
12
|
+
PATH_OR_PACKAGE_NAME: str
|
13
|
+
"""Path to the package or file from which to extract code snippets."""
|
14
|
+
output: Optional[str] = None
|
15
|
+
"""Output path for the extracted code snippets. If not provided, defaults to a file with the current timestamp."""
|
16
|
+
ban_file_patterns: list[str] = [".venv/*", Path(__file__).relative_to(Path.cwd()).as_posix()]
|
17
|
+
"""List of file patterns to ignore."""
|
18
|
+
glob_patterns: list[str] = ["*.py"]
|
19
|
+
"""List of glob patterns to include."""
|
20
|
+
case_sensitive: bool = False
|
21
|
+
"""Enable case-sensitive matching for glob patterns."""
|
22
|
+
prevent_save_file: bool = False
|
23
|
+
"""Prevent saving the extracted code snippets to a file."""
|
24
|
+
|
25
|
+
def run(self) -> CodeSnippets:
|
26
|
+
if not self.prevent_save_file:
|
27
|
+
if not self.output:
|
28
|
+
output = Path(datetime.now().strftime("%Y%m%d_%H%M%S") + "_snippets.txt")
|
29
|
+
else:
|
30
|
+
output = Path(self.output)
|
31
|
+
else:
|
32
|
+
output = None
|
33
|
+
|
34
|
+
cs = CodeSnippets.from_path_or_pkgname(
|
35
|
+
path_or_pkgname=self.PATH_OR_PACKAGE_NAME,
|
36
|
+
ban_file_patterns=self.ban_file_patterns,
|
37
|
+
glob_patterns=self.glob_patterns,
|
38
|
+
case_sensitive=self.case_sensitive,
|
39
|
+
)
|
40
|
+
if output is not None:
|
41
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
42
|
+
output.write_text(cs.snippets_text, encoding="utf-8")
|
43
|
+
logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}` and saved to `{output}`.")
|
44
|
+
else:
|
45
|
+
logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}`.")
|
46
|
+
return cs
|
47
|
+
|
48
|
+
|
49
|
+
def main() -> None:
|
50
|
+
Arguments().run()
|
51
|
+
|
52
|
+
|
53
|
+
if __name__ == "__main__":
|
54
|
+
main()
|