chatterer 0.1.18__py3-none-any.whl → 0.1.20__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 +93 -93
- chatterer/common_types/__init__.py +21 -21
- chatterer/common_types/io.py +19 -19
- chatterer/examples/__init__.py +0 -0
- chatterer/examples/anything_to_markdown.py +85 -91
- chatterer/examples/get_code_snippets.py +55 -62
- chatterer/examples/login_with_playwright.py +156 -167
- chatterer/examples/make_ppt.py +488 -497
- chatterer/examples/pdf_to_markdown.py +100 -107
- chatterer/examples/pdf_to_text.py +54 -56
- chatterer/examples/transcription_api.py +112 -123
- chatterer/examples/upstage_parser.py +89 -100
- chatterer/examples/webpage_to_markdown.py +70 -79
- chatterer/interactive.py +354 -354
- chatterer/language_model.py +533 -533
- chatterer/messages.py +21 -21
- chatterer/strategies/__init__.py +13 -13
- chatterer/strategies/atom_of_thoughts.py +975 -975
- chatterer/strategies/base.py +14 -14
- chatterer/tools/__init__.py +46 -46
- chatterer/tools/caption_markdown_images.py +384 -384
- chatterer/tools/citation_chunking/__init__.py +3 -3
- chatterer/tools/citation_chunking/chunks.py +53 -53
- chatterer/tools/citation_chunking/citation_chunker.py +118 -118
- chatterer/tools/citation_chunking/citations.py +285 -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 +393 -302
- chatterer/tools/convert_to_text.py +446 -447
- chatterer/tools/upstage_document_parser.py +705 -705
- chatterer/tools/webpage_to_markdown.py +739 -739
- chatterer/tools/youtube.py +146 -146
- chatterer/utils/__init__.py +15 -15
- chatterer/utils/base64_image.py +285 -285
- chatterer/utils/bytesio.py +59 -59
- chatterer/utils/code_agent.py +237 -237
- chatterer/utils/imghdr.py +148 -148
- {chatterer-0.1.18.dist-info → chatterer-0.1.20.dist-info}/METADATA +392 -392
- chatterer-0.1.20.dist-info/RECORD +44 -0
- {chatterer-0.1.18.dist-info → chatterer-0.1.20.dist-info}/WHEEL +1 -1
- chatterer-0.1.20.dist-info/entry_points.txt +10 -0
- chatterer-0.1.18.dist-info/RECORD +0 -42
- {chatterer-0.1.18.dist-info → chatterer-0.1.20.dist-info}/top_level.txt +0 -0
@@ -1,107 +1,100 @@
|
|
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
|
-
else:
|
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
|
-
if not indices:
|
102
|
-
raise ValueError
|
103
|
-
return sorted(indices)
|
104
|
-
|
105
|
-
|
106
|
-
if __name__ == "__main__":
|
107
|
-
PdfToMarkdownArgs().run()
|
1
|
+
import logging
|
2
|
+
import sys
|
3
|
+
from pathlib import Path
|
4
|
+
from typing import Optional
|
5
|
+
|
6
|
+
from spargear import ArgumentSpec, BaseArguments
|
7
|
+
|
8
|
+
from chatterer import Chatterer, PdfToMarkdown
|
9
|
+
|
10
|
+
logger = logging.getLogger(__name__)
|
11
|
+
|
12
|
+
|
13
|
+
class PdfToMarkdownArgs(BaseArguments):
|
14
|
+
input: str
|
15
|
+
"""Input PDF file or directory containing PDF files to convert to markdown."""
|
16
|
+
output: Optional[str] = None
|
17
|
+
"""Output path. For a file, path to the output markdown file. For a directory, output directory for .md files."""
|
18
|
+
"""Chatterer instance for communication."""
|
19
|
+
pages: Optional[str] = None
|
20
|
+
"""Page indices to convert (e.g., '1,3,5-9')."""
|
21
|
+
recursive: bool = False
|
22
|
+
"""If input is a directory, search for PDFs recursively."""
|
23
|
+
chatterer: ArgumentSpec[Chatterer] = ArgumentSpec(
|
24
|
+
["--chatterer"],
|
25
|
+
default_factory=lambda: Chatterer.from_provider("google:gemini-2.5-flash-preview-05-20"),
|
26
|
+
help="Chatterer instance for communication.",
|
27
|
+
type=Chatterer.from_provider,
|
28
|
+
)
|
29
|
+
|
30
|
+
def run(self) -> list[dict[str, str]]:
|
31
|
+
input = Path(self.input).resolve()
|
32
|
+
page_indices = parse_page_indices(self.pages) if self.pages else None
|
33
|
+
pdf_files: list[Path] = []
|
34
|
+
is_dir = False
|
35
|
+
if input.is_file():
|
36
|
+
if input.suffix.lower() != ".pdf":
|
37
|
+
sys.exit(1)
|
38
|
+
pdf_files.append(input)
|
39
|
+
elif input.is_dir():
|
40
|
+
is_dir = True
|
41
|
+
pattern = "*.pdf"
|
42
|
+
pdf_files = sorted([
|
43
|
+
f for f in (input.rglob(pattern) if self.recursive else input.glob(pattern)) if f.is_file()
|
44
|
+
])
|
45
|
+
if not pdf_files:
|
46
|
+
sys.exit(0)
|
47
|
+
else:
|
48
|
+
sys.exit(1)
|
49
|
+
if self.output:
|
50
|
+
out_base = Path(self.output).resolve()
|
51
|
+
elif is_dir:
|
52
|
+
out_base = input
|
53
|
+
else:
|
54
|
+
out_base = input.with_suffix(".md")
|
55
|
+
|
56
|
+
if is_dir:
|
57
|
+
out_base.mkdir(parents=True, exist_ok=True)
|
58
|
+
else:
|
59
|
+
out_base.parent.mkdir(parents=True, exist_ok=True)
|
60
|
+
|
61
|
+
converter = PdfToMarkdown(chatterer=self.chatterer.unwrap())
|
62
|
+
results: list[dict[str, str]] = []
|
63
|
+
for pdf in pdf_files:
|
64
|
+
output = (out_base / (pdf.stem + ".md")) if is_dir else out_base
|
65
|
+
md = converter.convert(str(pdf), page_indices)
|
66
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
67
|
+
output.write_text(md, encoding="utf-8")
|
68
|
+
results.append({"input": pdf.as_posix(), "output": output.as_posix(), "result": md})
|
69
|
+
logger.info(f"Converted {len(pdf_files)} PDF(s) to markdown and saved to `{out_base}`.")
|
70
|
+
return results
|
71
|
+
|
72
|
+
|
73
|
+
def parse_page_indices(pages_str: str) -> list[int] | None:
|
74
|
+
if not pages_str:
|
75
|
+
return None
|
76
|
+
indices: set[int] = set()
|
77
|
+
for part in pages_str.split(","):
|
78
|
+
part = part.strip()
|
79
|
+
if not part:
|
80
|
+
continue
|
81
|
+
if "-" in part:
|
82
|
+
start_str, end_str = part.split("-", 1)
|
83
|
+
start = int(start_str.strip())
|
84
|
+
end = int(end_str.strip())
|
85
|
+
if start > end:
|
86
|
+
raise ValueError
|
87
|
+
indices.update(range(start, end + 1))
|
88
|
+
else:
|
89
|
+
indices.add(int(part))
|
90
|
+
if not indices:
|
91
|
+
raise ValueError
|
92
|
+
return sorted(indices)
|
93
|
+
|
94
|
+
|
95
|
+
def main() -> None:
|
96
|
+
PdfToMarkdownArgs().run()
|
97
|
+
|
98
|
+
|
99
|
+
if __name__ == "__main__":
|
100
|
+
main()
|
@@ -1,56 +1,54 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
from
|
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
|
-
if __name__ == "__main__":
|
56
|
-
PdfToTextArgs().run()
|
1
|
+
import logging
|
2
|
+
import sys
|
3
|
+
from pathlib import Path
|
4
|
+
from typing import Optional
|
5
|
+
|
6
|
+
from spargear import BaseArguments
|
7
|
+
|
8
|
+
from chatterer.tools.convert_to_text import pdf_to_text
|
9
|
+
|
10
|
+
logger = logging.getLogger(__name__)
|
11
|
+
|
12
|
+
|
13
|
+
class PdfToTextArgs(BaseArguments):
|
14
|
+
input: Path
|
15
|
+
"""Path to the PDF file to convert to text."""
|
16
|
+
output: Optional[Path]
|
17
|
+
"""Path to the output text file. If not provided, defaults to the input file with a .txt suffix."""
|
18
|
+
pages: Optional[str] = None
|
19
|
+
"""Comma-separated list of page indices to extract from the PDF. Supports ranges, e.g., '1,3,5-9'."""
|
20
|
+
|
21
|
+
def run(self) -> None:
|
22
|
+
input = self.input.resolve()
|
23
|
+
out = self.output or input.with_suffix(".txt")
|
24
|
+
if not input.is_file():
|
25
|
+
sys.exit(1)
|
26
|
+
out.write_text(
|
27
|
+
pdf_to_text(path_or_file=input, page_indices=self.pages),
|
28
|
+
encoding="utf-8",
|
29
|
+
)
|
30
|
+
logger.info(f"Extracted text from `{input}` to `{out}`")
|
31
|
+
|
32
|
+
|
33
|
+
def parse_page_indices(pages_str: str) -> list[int]:
|
34
|
+
indices: set[int] = set()
|
35
|
+
for part in pages_str.split(","):
|
36
|
+
part = part.strip()
|
37
|
+
if "-" in part:
|
38
|
+
start_str, end_str = part.split("-", 1)
|
39
|
+
start = int(start_str)
|
40
|
+
end = int(end_str)
|
41
|
+
if start > end:
|
42
|
+
raise ValueError
|
43
|
+
indices.update(range(start, end + 1))
|
44
|
+
else:
|
45
|
+
indices.add(int(part))
|
46
|
+
return sorted(indices)
|
47
|
+
|
48
|
+
|
49
|
+
def main() -> None:
|
50
|
+
PdfToTextArgs().run()
|
51
|
+
|
52
|
+
|
53
|
+
if __name__ == "__main__":
|
54
|
+
main()
|
@@ -1,123 +1,112 @@
|
|
1
|
-
# pyright: reportUnknownVariableType=false, reportUnknownMemberType=false, reportArgumentType=false, reportMissingTypeStubs=false
|
2
|
-
|
3
|
-
from io import BytesIO
|
4
|
-
from pathlib import Path
|
5
|
-
from typing import cast
|
6
|
-
|
7
|
-
from openai import OpenAI
|
8
|
-
from pydub import AudioSegment
|
9
|
-
from spargear import
|
10
|
-
|
11
|
-
# Maximum chunk length in seconds
|
12
|
-
MAX_CHUNK_DURATION = 600
|
13
|
-
|
14
|
-
|
15
|
-
class TranscriptionApiArguments(BaseArguments):
|
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
|
-
if res.type == "transcript.text.delta":
|
114
|
-
print(res.delta, end="", flush=True)
|
115
|
-
if res.type == "transcript.text.done":
|
116
|
-
print()
|
117
|
-
return res.text
|
118
|
-
else:
|
119
|
-
raise RuntimeError("No transcription result found.")
|
120
|
-
|
121
|
-
|
122
|
-
if __name__ == "__main__":
|
123
|
-
TranscriptionApiArguments().run()
|
1
|
+
# pyright: reportUnknownVariableType=false, reportUnknownMemberType=false, reportArgumentType=false, reportMissingTypeStubs=false
|
2
|
+
|
3
|
+
from io import BytesIO
|
4
|
+
from pathlib import Path
|
5
|
+
from typing import Optional, cast
|
6
|
+
|
7
|
+
from openai import OpenAI
|
8
|
+
from pydub import AudioSegment
|
9
|
+
from spargear import BaseArguments
|
10
|
+
|
11
|
+
# Maximum chunk length in seconds
|
12
|
+
MAX_CHUNK_DURATION = 600
|
13
|
+
|
14
|
+
|
15
|
+
class TranscriptionApiArguments(BaseArguments):
|
16
|
+
input: Path
|
17
|
+
"""The audio file to transcribe."""
|
18
|
+
output: Optional[Path] = None
|
19
|
+
"""Path to save the transcription output."""
|
20
|
+
model: str = "gpt-4o-transcribe"
|
21
|
+
"""The model to use for transcription."""
|
22
|
+
api_key: Optional[str] = None
|
23
|
+
"""The API key for authentication."""
|
24
|
+
base_url: str = "https://api.openai.com/v1"
|
25
|
+
"""The base URL for the API."""
|
26
|
+
prompt: str = "Transcribe whole text from audio."
|
27
|
+
"""The prompt to use for transcription."""
|
28
|
+
|
29
|
+
def run(self) -> None:
|
30
|
+
model = self.model
|
31
|
+
|
32
|
+
client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
33
|
+
|
34
|
+
audio = load_audio_segment(self.input)
|
35
|
+
|
36
|
+
segments = split_audio(audio, MAX_CHUNK_DURATION)
|
37
|
+
print(f"[i] Audio duration: {len(audio) / 1000:.1f}s; splitting into {len(segments)} segment(s)")
|
38
|
+
|
39
|
+
transcripts: list[str] = []
|
40
|
+
for idx, seg in enumerate(segments, start=1):
|
41
|
+
print(f"[i] Transcribing segment {idx}/{len(segments)}...")
|
42
|
+
transcripts.append(transcribe_segment(seg, client, model, self.prompt))
|
43
|
+
|
44
|
+
full_transcript = "\n\n".join(transcripts)
|
45
|
+
output_path: Path = self.output or self.input.with_suffix(".txt")
|
46
|
+
output_path.write_text(full_transcript, encoding="utf-8")
|
47
|
+
print(f"[✓] Transcription saved to: {output_path}")
|
48
|
+
|
49
|
+
|
50
|
+
def load_audio_segment(file_path: Path) -> AudioSegment:
|
51
|
+
"""
|
52
|
+
Load an audio file as an AudioSegment. Convert to mp3 format in-memory if needed.
|
53
|
+
"""
|
54
|
+
ext = file_path.suffix.lower()[1:]
|
55
|
+
audio = AudioSegment.from_file(file_path.as_posix(), format=ext if ext != "mp3" else None)
|
56
|
+
if ext != "mp3":
|
57
|
+
buffer = BytesIO()
|
58
|
+
audio.export(buffer, format="mp3")
|
59
|
+
buffer.seek(0)
|
60
|
+
audio = AudioSegment.from_file(buffer, format="mp3")
|
61
|
+
return audio
|
62
|
+
|
63
|
+
|
64
|
+
def split_audio(audio: AudioSegment, max_duration_s: int) -> list[AudioSegment]:
|
65
|
+
"""
|
66
|
+
Split the AudioSegment into chunks no longer than max_duration_s seconds.
|
67
|
+
"""
|
68
|
+
chunk_length_ms = (max_duration_s - 1) * 1000
|
69
|
+
duration_ms = len(audio)
|
70
|
+
segments: list[AudioSegment] = []
|
71
|
+
segment_idx: int = 0
|
72
|
+
for start_ms in range(0, duration_ms, chunk_length_ms):
|
73
|
+
end_ms = min(start_ms + chunk_length_ms, duration_ms)
|
74
|
+
segment = cast(AudioSegment, audio[start_ms:end_ms])
|
75
|
+
segments.append(segment)
|
76
|
+
# with open(f"segment_{segment_idx}.mp3", "wb") as f:
|
77
|
+
# segment.export(f, format="mp3")
|
78
|
+
segment_idx += 1
|
79
|
+
return segments
|
80
|
+
|
81
|
+
|
82
|
+
def transcribe_segment(segment: AudioSegment, client: OpenAI, model: str, prompt: str) -> str:
|
83
|
+
"""
|
84
|
+
Transcribe a single AudioSegment chunk and return its text.
|
85
|
+
"""
|
86
|
+
buffer = BytesIO()
|
87
|
+
segment.export(buffer, format="mp3")
|
88
|
+
buffer.seek(0)
|
89
|
+
mp3_bytes = buffer.read()
|
90
|
+
response = client.audio.transcriptions.create(
|
91
|
+
model=model,
|
92
|
+
prompt=prompt,
|
93
|
+
file=("audio.mp3", mp3_bytes),
|
94
|
+
response_format="text",
|
95
|
+
stream=True,
|
96
|
+
)
|
97
|
+
for res in response:
|
98
|
+
if res.type == "transcript.text.delta":
|
99
|
+
print(res.delta, end="", flush=True)
|
100
|
+
if res.type == "transcript.text.done":
|
101
|
+
print()
|
102
|
+
return res.text
|
103
|
+
else:
|
104
|
+
raise RuntimeError("No transcription result found.")
|
105
|
+
|
106
|
+
|
107
|
+
def main() -> None:
|
108
|
+
TranscriptionApiArguments().run()
|
109
|
+
|
110
|
+
|
111
|
+
if __name__ == "__main__":
|
112
|
+
main()
|