simple-summary/summarize.py

140 lines
3.4 KiB
Python
Raw Permalink Normal View History

2025-01-17 21:37:52 +01:00
import subprocess
import tempfile
from pathlib import Path
from typing import cast
import click
import pytgpt.phind as phind
2025-01-28 08:47:41 +01:00
debug = False
2025-01-17 21:37:52 +01:00
def summarize_text_file(content: str, prompt: str | None) -> str:
prompt = prompt or "Please summarize the following transcript:"
bot = phind.PHIND()
2025-01-28 08:47:41 +01:00
res = bot.chat(f"{prompt} {content}")
return res
2025-01-17 21:37:52 +01:00
2025-01-18 14:34:02 +01:00
def extract_transcript_contents(content: str, keep_newlines: bool = False) -> str:
2025-01-17 21:37:52 +01:00
jq_command = "jq '.events.[].segs | select(. != null).[].utf8'"
result = subprocess.run(
jq_command, shell=True, capture_output=True, text=True, input=content
).stdout
# Replace newlines with spaces
result = result.replace("\n", "").split()
# Join lines back together with newlines
2025-01-18 14:34:02 +01:00
processed = (
" ".join(result).replace('"', "").replace("\\n", "\n" if keep_newlines else " ")
)
2025-01-17 21:37:52 +01:00
return processed
def grab_subtitles(url: str | Path) -> Path:
import yt_dlp
temp_dir = get_temp_dir()
2025-01-18 14:34:21 +01:00
ydl_opts = {
"outtmpl": f"{temp_dir}/subs",
"writeautomaticsub": True,
"subtitlesformat": "json3",
"skip_download": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
2025-01-28 08:47:41 +01:00
dprint(f"Subtitle file saved as: {filename}")
2025-01-18 14:34:21 +01:00
for root, _, files in Path(temp_dir).walk():
for file in files:
if file.endswith(".json3"):
return Path(root).joinpath(file)
raise ValueError("No correct json3 transcript object found.")
2025-01-17 21:37:52 +01:00
INPUT_FORMATS = ["json3", "yt", "txt"]
2025-01-17 21:37:52 +01:00
@click.command()
# TODO: Can I set it so it checks existence *only* when no youtube flag exists?
# TODO: Implement some way to allow content passed in on stdin instead of file (- as arg?)
2025-01-17 21:37:52 +01:00
@click.argument("file_path", type=click.Path(exists=False))
@click.option(
"--from",
"-f",
"get_from",
type=click.Choice(INPUT_FORMATS),
default="txt",
help="Choose format to process between json transcript, yt link or txt file.",
2025-01-17 21:37:52 +01:00
)
@click.option(
"--prompt",
"-p",
2025-01-28 08:47:41 +01:00
default="Please summarize the following transcript:",
2025-01-17 21:37:52 +01:00
type=str,
help="Use custom prompt.",
2025-01-17 21:37:52 +01:00
)
2025-01-28 08:47:41 +01:00
@click.option(
"--log-level",
"-l",
default=0,
help="Set log level to 1 for debug.",
)
def cli(
file_path: Path | str,
prompt: str,
log_level: int,
get_from: str,
2025-01-28 08:47:41 +01:00
):
2025-01-17 21:37:52 +01:00
"""Provide summary for a file at the specified path or a youtube video at the specified url."""
2025-01-28 08:47:41 +01:00
if log_level:
global debug
debug = True
2025-01-17 21:37:52 +01:00
content = ""
# youtube link, dl transcript
if get_from == "yt":
2025-01-17 21:37:52 +01:00
file_path = grab_subtitles(file_path)
file_path = cast(Path, file_path)
2025-01-28 08:47:41 +01:00
dprint(f"file path = {file_path}")
2025-01-17 21:37:52 +01:00
# load local file
with Path(file_path).open() as f:
content = f.read()
if get_from == "json3" or get_from == "yt":
2025-01-17 21:37:52 +01:00
content = extract_transcript_contents(content)
2025-01-28 08:47:41 +01:00
dprint(f"content = {content}")
2025-01-17 21:37:52 +01:00
if not content:
print("Please provide a file with valid content.")
print(summarize_text_file(content, prompt))
2025-01-18 14:34:26 +01:00
2025-01-28 08:47:41 +01:00
cached_dir: Path | None = None
def get_temp_dir() -> Path:
global cached_dir
if cached_dir is None:
cached_dir = Path(tempfile.mkdtemp())
dprint(f"Created and cached temp dir {cached_dir}")
return cached_dir
def dprint(content: str | None):
if debug:
print(f"[DEBUG] {content}")
2025-01-17 21:37:52 +01:00
if __name__ == "__main__":
cli()