Malina commited on
Commit
b1fe369
Β·
1 Parent(s): b0258be

NINA v0.0 - video transcription

Browse files
Files changed (3) hide show
  1. main.py +115 -0
  2. packages.txt +1 -0
  3. requirements.txt +4 -0
main.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ─────────────────────────────────────────────
2
+ # Neural Information & Narrative Abstraction
3
+ # ver 0.00
4
+ # ─────────────────────────────────────────────
5
+
6
+ import os
7
+ import tempfile
8
+
9
+ import ffmpeg # audio extraction
10
+ import whisper # ASR / speech-to-text
11
+ import gradio as gr # UI + HF Spaces runtime
12
+
13
+
14
+ # ── Model load ──────────────────────────────
15
+ print("[NINA] Loading model...")
16
+ asr_model = whisper.load_model("medium") # swap to "small" or "medium" for better accuracy
17
+ print("[NINA] Ready.")
18
+
19
+
20
+ # ── Stage 1: extract audio from video ─────────────────────────────────────────
21
+ def extract_audio(video_path: str) -> str:
22
+ """
23
+ Pull the audio track from an mp4, write it as a
24
+ 16kHz mono WAV to a temp file, return the path.
25
+ """
26
+ tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
27
+ tmp.close()
28
+
29
+ (
30
+ ffmpeg
31
+ .input(video_path)
32
+ .output(
33
+ tmp.name,
34
+ ac=1,
35
+ ar=16000,
36
+ vn=None,
37
+ )
38
+ .run(overwrite_output=True, quiet=True)
39
+ )
40
+
41
+ return tmp.name
42
+
43
+
44
+ # ── Stage 2: transcribe audio β†’ plain text ────────────────────────────────────
45
+ def transcribe_audio(audio_path: str) -> list[dict]:
46
+ """
47
+ Run Whisper on the audio file.
48
+ Returns a list of segment dicts:
49
+ { text, start, end }
50
+ """
51
+ result = asr_model.transcribe(
52
+ audio_path,
53
+ word_timestamps=False,
54
+ verbose=False,
55
+ )
56
+
57
+ segments = [
58
+ {
59
+ "text": seg["text"].strip(),
60
+ "start": round(seg["start"], 2),
61
+ "end": round(seg["end"], 2),
62
+ }
63
+ for seg in result["segments"]
64
+ ]
65
+
66
+ return segments
67
+
68
+
69
+ # ── Formatter: segments β†’ readable transcript string ──────────────────────────
70
+ def format_transcript(segments: list[dict]) -> str:
71
+ lines = []
72
+ for seg in segments:
73
+ timestamp = f"[{seg['start']:>7.2f}s β†’ {seg['end']:>7.2f}s]"
74
+ lines.append(f"{timestamp} {seg['text']}")
75
+ return "\n".join(lines)
76
+
77
+
78
+ # ── Main pipeline (v0) ────────────────────────────────────────────────────────
79
+ def run(video_path: str) -> str:
80
+ if video_path is None:
81
+ return " No video uploaded."
82
+
83
+ print(f"[NINA] Processing: {video_path}")
84
+
85
+ # 1. extract audio
86
+ audio_path = extract_audio(video_path)
87
+ print(f"[NINA] Audio extracted β†’ {audio_path}")
88
+
89
+ # 2. transcribe
90
+ segments = transcribe_audio(audio_path)
91
+ print(f"[NINA] Transcribed {len(segments)} segments.")
92
+
93
+ # 3. cleanup temp audio file
94
+ os.remove(audio_path)
95
+
96
+ # 4. format output
97
+ transcript = format_transcript(segments)
98
+ return transcript
99
+
100
+
101
+ # ── Gradio UI ─────────────────────────────────────────────────────────────────
102
+ demo = gr.Interface(
103
+ fn=run,
104
+ inputs=gr.Video(label="Upload video"),
105
+ outputs=gr.Textbox(
106
+ label="Transcription",
107
+ lines=25,
108
+ ),
109
+ title="Neural Information & Narrative Abstraction v0.00 ",
110
+ description="Upload an MP4. Get a full timestamped transcript.",
111
+ flagging_mode="never",
112
+ )
113
+
114
+ if __name__ == "__main__":
115
+ demo.launch()
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ffmpeg-python
2
+ openai-whisper
3
+ gradio
4
+