-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_slide.py
More file actions
executable file
·359 lines (286 loc) · 10.5 KB
/
generate_slide.py
File metadata and controls
executable file
·359 lines (286 loc) · 10.5 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
"""
End-to-end local pipeline (rerunnable):
- Reads videos from ./downloads
- Extracts audio (skips if exists)
- Transcribes using whisper-cli + ggml CPU model (skips if transcript exists)
- Generates slides + diagrams using Ollama
- Writes Reveal.js HTML slides (skips if final HTML exists)
"""
import os
import sys
import time
import json
import shlex
import subprocess
from pathlib import Path
import requests
from slugify import slugify
# ---------------- CONFIG ----------------
WORKDIR = Path.cwd()
VIDEO_DIR = WORKDIR / "downloads"
AUDIO_DIR = WORKDIR / "audio"
TRANSCRIPT_DIR = WORKDIR / "transcripts"
SLIDES_DIR = WORKDIR / "slides"
DIAGRAMS_DIR = WORKDIR / "diagrams"
# whisper.cpp (CPU ggml model)
WHISPER_CPP_BIN = "/Users/prabhatranjan/whisper.cpp/build/bin/whisper-cli"
WHISPER_MODEL_PATH = "/Users/prabhatranjan/whisper.cpp/models/ggml-base.en.bin"
# LLM (Ollama)
OLLAMA_API_URL = "http://localhost:11434/api/generate"
MODEL_NAME = "llama3" # or "llama3:8b", "mistral", etc.
CHUNK_WORD_TARGET = 450
RATE_LIMIT_SECONDS = 0.4
REVEAL_THEME = "black"
# ----------------------------------------
for d in (AUDIO_DIR, TRANSCRIPT_DIR, SLIDES_DIR, DIAGRAMS_DIR):
d.mkdir(parents=True, exist_ok=True)
# ---------- utilities ----------
def run_cmd(cmd, check=True):
print(f"[CMD] {cmd}")
proc = subprocess.run(shlex.split(cmd), capture_output=True, text=True)
if proc.returncode != 0 and check:
print(proc.stdout)
print(proc.stderr, file=sys.stderr)
raise RuntimeError(f"Command failed: {cmd}")
return proc.stdout
def list_local_videos(video_dir=VIDEO_DIR):
exts = (".mp4", ".mkv", ".webm", ".mov")
videos = [p for p in video_dir.iterdir() if p.suffix.lower() in exts]
print(f"[INFO] Found {len(videos)} videos in {video_dir}")
return videos
def extract_audio(video_path, outdir=AUDIO_DIR):
outpath = outdir / (video_path.stem + ".wav")
if outpath.exists():
print(f"[SKIP] Audio already extracted → {outpath}")
return outpath
print(f"[AUDIO] Extracting audio from {video_path} → {outpath}")
cmd = f"ffmpeg -y -i {shlex.quote(str(video_path))} -ac 1 -ar 16000 {shlex.quote(str(outpath))}"
run_cmd(cmd)
return outpath
def transcribe_audio(wav_path):
print(f"[TRANSCRIBE] Starting transcription for {wav_path}")
expected = TRANSCRIPT_DIR / (wav_path.stem + ".txt")
# If transcript already exists, skip
if expected.exists():
print(f"[SKIP] Transcript already exists → {expected}")
return expected
cmd = f"{WHISPER_CPP_BIN} -m {WHISPER_MODEL_PATH} -f {wav_path} -otxt"
print(f"[TRANSCRIBE] Running:\n {cmd}")
proc = subprocess.run(shlex.split(cmd), capture_output=True, text=True)
print("[TRANSCRIBE] stdout:")
print(proc.stdout.strip())
print("[TRANSCRIBE] stderr:")
print(proc.stderr.strip())
if proc.returncode != 0:
print(f"[TRANSCRIBE] whisper-cli failed with exit code {proc.returncode}")
return None
# Look for transcript in all known locations
candidates = [
Path(str(wav_path).replace(".wav", ".txt")),
Path("transcript.txt"),
Path(str(wav_path) + ".txt"),
]
print("[TRANSCRIBE] Searching for transcript candidates...")
for p in Path(".").rglob(f"{wav_path.stem}*.txt"):
candidates.append(p)
seen = set()
for c in candidates:
if c in seen:
continue
seen.add(c)
print(f"[TRANSCRIBE] Checking: {c}")
if c.exists():
print(f"[TRANSCRIBE] Found transcript → {c}")
expected.parent.mkdir(parents=True, exist_ok=True)
c.rename(expected)
print(f"[TRANSCRIBE] Moved to → {expected}")
return expected
print("[TRANSCRIBE] No transcript found after whisper-cli run")
return None
def read_text_file(p):
with open(p, "r", encoding="utf-8") as fh:
return fh.read()
def clean_text(s):
s = s.replace("\u2019", "'").replace("\u201c", '"').replace("\u201d", '"')
return " ".join(s.split())
def chunk_text_by_words(text, target=CHUNK_WORD_TARGET):
words = text.split()
chunks = []
i = 0
while i < len(words):
j = min(len(words), i + target)
chunks.append(" ".join(words[i:j]))
i = j
return chunks
def build_llm_prompt_for_chunk(chunk_text, video_id, idx, total):
return f"""
You are a concise slide and diagram writer for technical lectures.
Given the transcript chunk below, produce a JSON object with keys:
- title
- bullets (3–5)
- notes (2–4 sentences)
- diagram: {{ "nodes": [...], "edges": [[a,b], ...] }}
Transcript chunk ({idx}/{total}) for lecture {video_id}:
\"\"\"{chunk_text}\"\"\"
Output only valid JSON.
"""
def call_local_llm(prompt, model=MODEL_NAME, max_tokens=512, temperature=0.0):
payload = {
"model": model,
"prompt": prompt,
"max_tokens": max_tokens,
"temperature": temperature,
"stream": True
}
try:
r = requests.post(OLLAMA_API_URL, json=payload, stream=True, timeout=300)
r.raise_for_status()
full_text = ""
for line in r.iter_lines():
if not line:
continue
try:
obj = json.loads(line.decode("utf-8"))
if "response" in obj:
full_text += obj["response"]
except Exception as e:
print(f"[LLM] JSON parse error on line: {line}")
continue
return full_text.strip()
except Exception as e:
print(f"[ERROR] LLM call failed: {e}")
return None
def safe_parse_json(s):
try:
return json.loads(s)
except Exception:
start = s.find("{")
end = s.rfind("}")
if start != -1 and end != -1:
try:
return json.loads(s[start:end + 1])
except Exception:
return None
return None
def render_graphviz_svg(diagram_obj, out_svg_path):
from graphviz import Digraph
g = Digraph(format="svg")
for n in diagram_obj.get("nodes", []):
g.node(slugify(n), label=n)
for a, b in diagram_obj.get("edges", []):
g.edge(slugify(a), slugify(b))
out_svg_path.parent.mkdir(parents=True, exist_ok=True)
g.render(filename=str(out_svg_path.with_suffix("")), cleanup=True)
return out_svg_path
def render_reveal_html(slides, title, outpath):
slide_sections = []
for s in slides:
bullets_li = "\n".join(f"<li>{b}</li>" for b in s.get("bullets", []))
notes_html = f"<aside class='notes'>{s.get('notes','')}</aside>"
diagram_html = ""
if s.get("diagram_svg"):
diagram_html = f"<div><img src='{s['diagram_svg']}' style='max-width:80%;'/></div>"
section = f"""
<section>
<h2>{s.get('title','')}</h2>
<ul>{bullets_li}</ul>
{diagram_html}
{notes_html}
</section>
"""
slide_sections.append(section)
html = f"""<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@4/dist/reveal.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@4/dist/theme/{REVEAL_THEME}.css">
</head>
<body>
<div class="reveal"><div class="slides">{''.join(slide_sections)}</div></div>
<script src="https://cdn.jsdelivr.net/npm/reveal.js@4/dist/reveal.js"></script>
<script>Reveal.initialize({{hash:true}});</script>
</body>
</html>
"""
outpath.parent.mkdir(parents=True, exist_ok=True)
with open(outpath, "w", encoding="utf-8") as fh:
fh.write(html)
# ---------- main processing ----------
def process_video_file(video_path):
video_id = video_path.stem
print(f"\n[INFO] Processing video: {video_id}")
final_html = SLIDES_DIR / f"{slugify(video_id)}.html"
# 1. Skip if slides already exist
if final_html.exists():
print(f"[SKIP] Slides already exist → {final_html}")
return
# 2. Audio (rerunnable)
wav = AUDIO_DIR / f"{video_id}.wav"
if wav.exists():
print(f"[SKIP] Audio already exists → {wav}")
else:
wav = extract_audio(video_path)
# 3. Transcription (rerunnable)
transcript_path = TRANSCRIPT_DIR / f"{video_id}.txt"
if transcript_path.exists():
print(f"[SKIP] Transcript already exists → {transcript_path}")
else:
transcript_path = transcribe_audio(wav)
if transcript_path is None or not transcript_path.exists():
print(f"[ERROR] No transcript produced for {video_id}")
return
print(f"[INFO] Using transcript → {transcript_path}")
# 4. Read + clean transcript
raw = read_text_file(transcript_path)
cleaned = clean_text(raw)
chunks = chunk_text_by_words(cleaned)
# 5. Generate slides + diagrams
slides = []
total_chunks = len(chunks)
print(f"[INFO] Generating slides from {total_chunks} chunks")
for idx, chunk in enumerate(chunks, start=1):
print(f"[INFO] Chunk {idx}/{total_chunks}")
prompt = build_llm_prompt_for_chunk(chunk, video_id, idx, total_chunks)
llm_out = call_local_llm(prompt)
if not llm_out:
print(f"[WARN] Empty LLM output for chunk {idx}")
continue
parsed = safe_parse_json(llm_out)
if not parsed:
print(f"[WARN] JSON parse failed for chunk {idx}")
continue
# Diagram
diagram_svg = None
diagram_obj = parsed.get("diagram") or {}
if diagram_obj.get("nodes"):
svg_path = DIAGRAMS_DIR / f"{video_id}_chunk{idx}.svg"
try:
render_graphviz_svg(diagram_obj, svg_path)
diagram_svg = os.path.relpath(svg_path, SLIDES_DIR)
except Exception as e:
print(f"[WARN] Diagram render failed for chunk {idx}: {e}")
slides.append({
"title": parsed.get("title", ""),
"bullets": parsed.get("bullets", []),
"notes": parsed.get("notes", ""),
"diagram_svg": diagram_svg,
})
time.sleep(RATE_LIMIT_SECONDS)
# 6. Write final slides
if not slides:
print(f"[ERROR] No slides generated for {video_id}")
return
render_reveal_html(slides, f"Lecture {video_id}", final_html)
print(f"[INFO] Slides written → {final_html}")
def main():
videos = list_local_videos()
for video_path in videos:
try:
process_video_file(video_path)
except Exception as e:
print(f"[ERROR] processing {video_path}: {e}")
if __name__ == "__main__":
main()