textflowkit 0.1.3__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.
Files changed (46) hide show
  1. textflowkit/__init__.py +8 -0
  2. textflowkit/adapters/__init__.py +11 -0
  3. textflowkit/adapters/http_server.py +465 -0
  4. textflowkit/adapters/mcp_server.py +554 -0
  5. textflowkit/cli.py +473 -0
  6. textflowkit/core/__init__.py +5 -0
  7. textflowkit/core/batch.py +186 -0
  8. textflowkit/core/bind.py +66 -0
  9. textflowkit/core/cancel.py +19 -0
  10. textflowkit/core/checkpoint.py +389 -0
  11. textflowkit/core/diarize.py +229 -0
  12. textflowkit/core/engine.py +127 -0
  13. textflowkit/core/executor.py +301 -0
  14. textflowkit/core/jobs.py +241 -0
  15. textflowkit/core/model.py +98 -0
  16. textflowkit/core/paths.py +226 -0
  17. textflowkit/core/pipeline.py +368 -0
  18. textflowkit/core/retrieval.py +148 -0
  19. textflowkit/core/runner.py +146 -0
  20. textflowkit/core/service.py +146 -0
  21. textflowkit/core/sqlite_store.py +233 -0
  22. textflowkit/core/submission.py +220 -0
  23. textflowkit/core/timeutil.py +20 -0
  24. textflowkit/core/translate.py +244 -0
  25. textflowkit/render/__init__.py +191 -0
  26. textflowkit/render/docx.py +71 -0
  27. textflowkit/render/fonts/NotoSans.ttf +0 -0
  28. textflowkit/render/fonts/NotoSansArabic.ttf +0 -0
  29. textflowkit/render/fonts/NotoSansSC.ttf +0 -0
  30. textflowkit/render/fonts/OFL-NotoSans.txt +94 -0
  31. textflowkit/render/fonts/OFL-NotoSansSC.txt +93 -0
  32. textflowkit/render/fonts/README.md +19 -0
  33. textflowkit/render/markdown.py +33 -0
  34. textflowkit/render/pdf.py +136 -0
  35. textflowkit/render/srt.py +22 -0
  36. textflowkit/render/txt.py +19 -0
  37. textflowkit/render/vtt.py +20 -0
  38. textflowkit/sources/__init__.py +16 -0
  39. textflowkit/sources/acquire.py +437 -0
  40. textflowkit/sources/detect.py +200 -0
  41. textflowkit/sources/scratch.py +32 -0
  42. textflowkit-0.1.3.dist-info/METADATA +266 -0
  43. textflowkit-0.1.3.dist-info/RECORD +46 -0
  44. textflowkit-0.1.3.dist-info/WHEEL +4 -0
  45. textflowkit-0.1.3.dist-info/entry_points.txt +4 -0
  46. textflowkit-0.1.3.dist-info/licenses/LICENSE +203 -0
@@ -0,0 +1,93 @@
1
+ Copyright 2014-2021 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source'
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ https://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,19 @@
1
+ Bundled PDF fonts (SIL Open Font License 1.1)
2
+ ================================================
3
+
4
+ The fonts are embedded into generated PDFs by ReportLab. They are included in
5
+ the wheel so Unicode export does not depend on host-installed fonts.
6
+
7
+ * `NotoSans.ttf`: Noto Sans Regular, from
8
+ https://github.com/notofonts/noto-fonts/tree/main/hinted/ttf/NotoSans
9
+ SHA-256 `b85c38ecea8a7cfb39c24e395a4007474fa5a4fc864f6ee33309eb4948d232d5`
10
+ * `NotoSansArabic.ttf`: Noto Sans Arabic Regular, from
11
+ https://github.com/notofonts/noto-fonts/tree/main/hinted/ttf/NotoSansArabic
12
+ SHA-256 `ceea25b464a656dc3b26849bab9356740401af62aedf1bfa8b7f0d9b75925b1b`
13
+ * `NotoSansSC.ttf`: Noto Sans SC variable TrueType, from
14
+ https://github.com/google/fonts/tree/main/ofl/notosanssc
15
+ SHA-256 `a3041811a78c361b1de50f953c805e0244951c21c5bd412f7232ef0d899af0da`
16
+
17
+ `OFL-NotoSans.txt` covers the first two fonts. `OFL-NotoSansSC.txt` covers
18
+ Noto Sans SC. These font licenses are separate from textflowkit's Apache-2.0
19
+ license. Do not remove them from redistributed source or wheels.
@@ -0,0 +1,33 @@
1
+ """Markdown renderer - readable transcript with time anchors."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from textflowkit.core.model import Transcript
6
+ from textflowkit.core.timeutil import srt_timestamp
7
+
8
+
9
+ def _hms(seconds: float) -> str:
10
+ return srt_timestamp(seconds).split(",")[0]
11
+
12
+
13
+ def render_markdown(transcript: Transcript, *, title: str | None = None) -> str:
14
+ heading = title or "Transcript"
15
+ out: list[str] = [f"# {heading}", ""]
16
+ if transcript.source:
17
+ out.append(f"**Source:** {transcript.source}")
18
+ if transcript.language:
19
+ out.append(f"**Language:** {transcript.language}")
20
+ if transcript.duration:
21
+ out.append(f"**Duration:** {_hms(transcript.duration)}")
22
+ out += ["", "---", ""]
23
+
24
+ current_speaker: str | None = None
25
+ for seg in transcript.segments:
26
+ if seg.hidden:
27
+ continue
28
+ if seg.speaker and seg.speaker != current_speaker:
29
+ current_speaker = seg.speaker
30
+ out += [f"**{current_speaker}**", ""]
31
+ out.append(f"`{_hms(seg.start)}` {seg.display_text().strip()}")
32
+ out.append("")
33
+ return "\n".join(out)
@@ -0,0 +1,136 @@
1
+ """PDF renderer.
2
+
3
+ Binary output, so it does not go through `render()` - see `render_bytes()`.
4
+
5
+ Like the DOCX renderer this emits the finished data model: speaker labels and
6
+ translated text included.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import io
12
+ import threading
13
+ from pathlib import Path
14
+ from xml.sax.saxutils import escape
15
+
16
+ from textflowkit.core.model import Transcript
17
+ from textflowkit.core.timeutil import srt_timestamp
18
+
19
+ try:
20
+ from reportlab.lib.pagesizes import LETTER
21
+ from reportlab.lib.styles import getSampleStyleSheet
22
+ from reportlab.lib.units import inch
23
+ from reportlab.pdfbase import pdfmetrics
24
+ from reportlab.pdfbase.ttfonts import TTFont
25
+ from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer
26
+ except ImportError as exc: # pragma: no cover - optional extra
27
+ raise ImportError(
28
+ "PDF export requires the 'export' extra. Install with: pip install 'textflowkit[export]'"
29
+ ) from exc
30
+
31
+
32
+ def _hms(seconds: float) -> str:
33
+ return srt_timestamp(seconds).split(",")[0]
34
+
35
+
36
+ _FONT_LOCK = threading.Lock()
37
+ _FONT_DIR = Path(__file__).parent / "fonts"
38
+
39
+
40
+ def _ensure_fonts() -> None:
41
+ with _FONT_LOCK:
42
+ for name in ("NotoSans", "NotoSansArabic", "NotoSansSC"):
43
+ if name not in pdfmetrics.getRegisteredFontNames():
44
+ pdfmetrics.registerFont(TTFont(name, str(_FONT_DIR / f"{name}.ttf")))
45
+ pdfmetrics.registerFontFamily(
46
+ name, normal=name, bold=name, italic=name, boldItalic=name
47
+ )
48
+
49
+
50
+ def _font_for(char: str) -> str:
51
+ code = ord(char)
52
+ if (0x0600 <= code <= 0x06FF or 0x0750 <= code <= 0x077F
53
+ or 0x08A0 <= code <= 0x08FF or 0xFB50 <= code <= 0xFDFF
54
+ or 0xFE70 <= code <= 0xFEFF):
55
+ return "NotoSansArabic"
56
+ if (0x3000 <= code <= 0x303F or 0x3400 <= code <= 0x9FFF
57
+ or 0xF900 <= code <= 0xFAFF):
58
+ return "NotoSansSC"
59
+ return "NotoSans"
60
+
61
+
62
+ def _unicode_markup(text: str) -> str:
63
+ """Escape text and select embedded fonts for CJK and Arabic runs."""
64
+ if not text:
65
+ return ""
66
+ chunks: list[str] = []
67
+ current = _font_for(text[0])
68
+ run: list[str] = []
69
+ for char in text:
70
+ font = _font_for(char)
71
+ if font != current:
72
+ chunks.append(f'<font name="{current}">{escape("".join(run))}</font>')
73
+ run = []
74
+ current = font
75
+ run.append(char)
76
+ chunks.append(f'<font name="{current}">{escape("".join(run))}</font>')
77
+ return "".join(chunks)
78
+
79
+
80
+ def render_pdf(transcript: Transcript, *, title: str = "Transcript") -> bytes:
81
+ """Render a transcript as a PDF."""
82
+ _ensure_fonts()
83
+ buffer = io.BytesIO()
84
+ doc = SimpleDocTemplate(
85
+ buffer,
86
+ pagesize=LETTER,
87
+ title=title,
88
+ leftMargin=0.9 * inch,
89
+ rightMargin=0.9 * inch,
90
+ topMargin=0.9 * inch,
91
+ bottomMargin=0.9 * inch,
92
+ )
93
+ styles = getSampleStyleSheet()
94
+ for name in ("Title", "Normal", "Heading3", "BodyText"):
95
+ styles[name].fontName = "NotoSans"
96
+ styles[name].wordWrap = "LTR"
97
+ styles[name].shaping = 1
98
+ story = [Paragraph(_unicode_markup(title), styles["Title"]), Spacer(1, 10)]
99
+
100
+ if transcript.source:
101
+ story.append(Paragraph(f"<b>Source:</b> {_unicode_markup(transcript.source)}", styles["Normal"]))
102
+ if transcript.language:
103
+ story.append(Paragraph(f"<b>Language:</b> {_unicode_markup(transcript.language)}", styles["Normal"]))
104
+ if transcript.duration:
105
+ story.append(Paragraph(f"<b>Duration:</b> {_hms(transcript.duration)}", styles["Normal"]))
106
+
107
+ diar = (transcript.metadata or {}).get("diarization")
108
+ if diar:
109
+ speakers = ", ".join(diar.get("speakers") or []) or "none"
110
+ story.append(Paragraph(f"<b>Speakers:</b> {_unicode_markup(speakers)}", styles["Normal"]))
111
+ trans = (transcript.metadata or {}).get("translation")
112
+ if trans:
113
+ story.append(
114
+ Paragraph(f"<b>Translated to:</b> {_unicode_markup(str(trans.get('target')))}", styles["Normal"])
115
+ )
116
+
117
+ story.append(Spacer(1, 16))
118
+
119
+ current_speaker: str | None = None
120
+ for segment in transcript.segments:
121
+ if segment.hidden:
122
+ continue
123
+ if segment.speaker and segment.speaker != current_speaker:
124
+ current_speaker = segment.speaker
125
+ story.append(Paragraph(f"<b>{_unicode_markup(current_speaker)}</b>", styles["Heading3"]))
126
+
127
+ stamp = f"[{_hms(segment.start)}] "
128
+ if segment.translated_text:
129
+ body = f"{_unicode_markup(segment.translated_text.strip())}<br/><i>(original: {_unicode_markup(segment.text.strip())})</i>"
130
+ else:
131
+ body = _unicode_markup(segment.text.strip())
132
+ story.append(Paragraph(f"<b>{stamp}</b>{body}", styles["BodyText"]))
133
+ story.append(Spacer(1, 4))
134
+
135
+ doc.build(story)
136
+ return buffer.getvalue()
@@ -0,0 +1,22 @@
1
+ """SRT renderer."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from textflowkit.core.model import Transcript
6
+ from textflowkit.core.timeutil import srt_timestamp
7
+
8
+
9
+ def render_srt(transcript: Transcript, *, include_translation: bool = True) -> str:
10
+ blocks: list[str] = []
11
+ index = 0
12
+ for seg in transcript.segments:
13
+ if seg.hidden:
14
+ continue
15
+ index += 1
16
+ body = seg.display_text().strip() if include_translation else seg.text.strip()
17
+ if seg.speaker:
18
+ body = f"{seg.speaker}: {body}"
19
+ blocks.append(
20
+ f"{index}\n{srt_timestamp(seg.start)} --> {srt_timestamp(seg.end)}\n{body}\n"
21
+ )
22
+ return "\n".join(blocks)
@@ -0,0 +1,19 @@
1
+ """Plain-text renderer."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from textflowkit.core.model import Transcript
6
+
7
+
8
+ def render_txt(transcript: Transcript, *, timestamps: bool = False, speaker: bool = False) -> str:
9
+ lines: list[str] = []
10
+ for seg in transcript.segments:
11
+ if seg.hidden:
12
+ continue
13
+ prefix = ""
14
+ if timestamps:
15
+ prefix = f"[{seg.start:8.2f}] "
16
+ if speaker and seg.speaker:
17
+ prefix += f"{seg.speaker}: "
18
+ lines.append(prefix + seg.display_text().strip())
19
+ return "\n".join(lines) + ("\n" if lines else "")
@@ -0,0 +1,20 @@
1
+ """WebVTT renderer."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from textflowkit.core.model import Transcript
6
+ from textflowkit.core.timeutil import vtt_timestamp
7
+
8
+
9
+ def render_vtt(transcript: Transcript, *, include_translation: bool = True) -> str:
10
+ lines: list[str] = ["WEBVTT", ""]
11
+ for seg in transcript.segments:
12
+ if seg.hidden:
13
+ continue
14
+ body = seg.display_text().strip() if include_translation else seg.text.strip()
15
+ if seg.speaker:
16
+ body = f"<v {seg.speaker}>{body}"
17
+ lines.append(f"{vtt_timestamp(seg.start)} --> {vtt_timestamp(seg.end)}")
18
+ lines.append(body)
19
+ lines.append("")
20
+ return "\n".join(lines)
@@ -0,0 +1,16 @@
1
+ """Source layer: platform detection and media acquisition."""
2
+
3
+ from textflowkit.sources.acquire import AcquisitionError, extract_audio, fetch_media, require_tool
4
+ from textflowkit.sources.detect import PLATFORMS, SourceRef, detect_platform, is_url, resolve_source
5
+
6
+ __all__ = [
7
+ "PLATFORMS",
8
+ "AcquisitionError",
9
+ "SourceRef",
10
+ "detect_platform",
11
+ "extract_audio",
12
+ "fetch_media",
13
+ "is_url",
14
+ "require_tool",
15
+ "resolve_source",
16
+ ]