-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioToTextNotes.py
More file actions
95 lines (77 loc) · 2.89 KB
/
AudioToTextNotes.py
File metadata and controls
95 lines (77 loc) · 2.89 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
import sounddevice as sd
import numpy as np
import whisper
import queue
import threading
import time
from transformers import pipeline
from datetime import datetime
# Load Whisper model on CPU
model = whisper.load_model("base") # or "medium" for better accuracy
# Load summarization pipeline (CPU only)
summarizer = pipeline(
"summarization",
model="facebook/bart-large-cnn",
device=-1
)
# Audio settings
SAMPLE_RATE = 16000
BLOCK_DURATION = 150 # x seconds
CHUNK_SIZE = int(SAMPLE_RATE * BLOCK_DURATION)
audio_queue = queue.Queue()
# Function to get the current time stamp
def get_current_timestamp():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def audio_callback(indata, frames, time_info, status):
audio_queue.put(indata.copy())
def record_audio():
with sd.InputStream(samplerate=SAMPLE_RATE, channels=1, dtype="float32", callback=audio_callback):
print("🎙️ Recording... (Press Ctrl+C to stop)")
while True:
time.sleep(1)
def save_raw_transcript(text: str):
timestamp = get_current_timestamp()
with open("raw_transcripts.txt", "a", encoding="utf-8") as f:
f.write(f"{timestamp} - {text.strip()}\n\n")
def save_summary_to_file(summary: str):
timestamp = get_current_timestamp()
with open("notes.txt", "a", encoding="utf-8") as f:
f.write(f"{timestamp} - {summary.strip()}\n\n")
def transcribe_and_summarize():
buffer = []
while True:
try:
data = audio_queue.get(timeout=10)
buffer.append(data)
if len(buffer) * data.shape[0] >= CHUNK_SIZE:
audio_chunk = np.concatenate(buffer, axis=0)
buffer = []
print("📝 Transcribing chunk...")
audio_np = audio_chunk.flatten()
result = model.transcribe(audio_np, language="en")
transcription = result["text"].strip()
if len(transcription) > 20:
print("📄 Transcript:", transcription[:200] + "..." if len(transcription) > 200 else transcription)
save_raw_transcript(transcription)
print("✍️ Summarizing...")
summary = summarizer(
transcription,
max_length=150, # 25*x sentences
min_length=75, # 25*x sentences
do_sample=False
)[0]["summary_text"]
print("✅ Summary:\n", summary, "\n")
save_summary_to_file(summary)
except queue.Empty:
continue
# Start threads
record_thread = threading.Thread(target=record_audio, daemon=True)
process_thread = threading.Thread(target=transcribe_and_summarize, daemon=True)
record_thread.start()
process_thread.start()
# Keep the program alive
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
print("🛑 Exiting...")