|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline |
|
import time |
|
import os |
|
import numpy as np |
|
import soundfile as sf |
|
import librosa |
|
|
|
|
|
|
|
device = "cuda:0" if torch.cuda.is_available() else "cpu" |
|
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 |
|
print(f"Using device: {device}") |
|
|
|
|
|
stt_model_id = "openai/whisper-tiny" |
|
|
|
|
|
summarizer_model_id = "sshleifer/distilbart-cnn-6-6" |
|
|
|
|
|
SUMMARY_INTERVAL = 30.0 |
|
|
|
|
|
|
|
print("Loading STT model...") |
|
stt_model = AutoModelForSpeechSeq2Seq.from_pretrained( |
|
stt_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True |
|
) |
|
stt_model.to(device) |
|
processor = AutoProcessor.from_pretrained(stt_model_id) |
|
stt_pipeline = pipeline( |
|
"automatic-speech-recognition", |
|
model=stt_model, |
|
tokenizer=processor.tokenizer, |
|
feature_extractor=processor.feature_extractor, |
|
max_new_tokens=128, |
|
chunk_length_s=30, |
|
batch_size=16, |
|
torch_dtype=torch_dtype, |
|
device=device, |
|
) |
|
print("STT model loaded.") |
|
|
|
print("Loading Summarization pipeline...") |
|
summarizer = pipeline( |
|
"summarization", |
|
model=summarizer_model_id, |
|
device=device |
|
) |
|
print("Summarization pipeline loaded.") |
|
|
|
|
|
|
|
|
|
def format_summary_as_bullets(summary_text): |
|
"""Attempts to format a summary text block into bullet points.""" |
|
if not summary_text: |
|
return "" |
|
|
|
|
|
sentences = summary_text.replace(". ", ".\n- ").split('\n') |
|
bullet_summary = "- " + "\n".join(sentences).strip() |
|
|
|
bullet_summary = "\n".join([line for line in bullet_summary.split('\n') if line.strip() not in ['-', '']]) |
|
return bullet_summary |
|
|
|
|
|
|
|
|
|
|
|
def process_audio_stream( |
|
new_chunk_tuple, |
|
accumulated_transcript_state, |
|
last_summary_time_state, |
|
current_summary_state |
|
): |
|
|
|
if new_chunk_tuple is None: |
|
|
|
return accumulated_transcript_state, current_summary_state, accumulated_transcript_state, last_summary_time_state, current_summary_state |
|
|
|
sample_rate, audio_chunk = new_chunk_tuple |
|
|
|
if audio_chunk is None or sample_rate is None or audio_chunk.size == 0: |
|
|
|
return accumulated_transcript_state, current_summary_state, accumulated_transcript_state, last_summary_time_state, current_summary_state |
|
|
|
print(f"Received chunk: {audio_chunk.shape}, Sample Rate: {sample_rate}, Duration: {len(audio_chunk)/sample_rate:.2f}s") |
|
|
|
|
|
if audio_chunk.dtype != np.float32: |
|
|
|
|
|
audio_chunk = audio_chunk.astype(np.float32) / 32768.0 |
|
|
|
|
|
new_text = "" |
|
try: |
|
result = stt_pipeline({"sampling_rate": sample_rate, "raw": audio_chunk.copy()}) |
|
new_text = result["text"].strip() if result["text"] else "" |
|
print(f"Transcription chunk: '{new_text}'") |
|
|
|
except Exception as e: |
|
print(f"Error during transcription chunk: {e}") |
|
new_text = f"[Transcription Error: {e}]" |
|
|
|
|
|
if accumulated_transcript_state and not accumulated_transcript_state.endswith((" ", "\n")) and new_text: |
|
updated_transcript = accumulated_transcript_state + " " + new_text |
|
else: |
|
updated_transcript = accumulated_transcript_state + new_text |
|
|
|
|
|
current_time = time.time() |
|
new_summary = current_summary_state |
|
updated_last_summary_time = last_summary_time_state |
|
|
|
|
|
if updated_transcript and len(updated_transcript) > 50 and (current_time - last_summary_time_state > SUMMARY_INTERVAL): |
|
print(f"Summarizing transcript (length: {len(updated_transcript)})...") |
|
try: |
|
|
|
summary_result = summarizer(updated_transcript, max_length=150, min_length=30, do_sample=False) |
|
if summary_result and isinstance(summary_result, list): |
|
raw_summary = summary_result[0]['summary_text'] |
|
new_summary = format_summary_as_bullets(raw_summary) |
|
updated_last_summary_time = current_time |
|
print("Summary updated.") |
|
else: |
|
print("Summarization did not produce expected output.") |
|
|
|
except Exception as e: |
|
print(f"Error during summarization: {e}") |
|
|
|
|
|
|
|
error_display_summary = f"[Summarization Error]\n\nLast good summary:\n{current_summary_state}" |
|
return updated_transcript, error_display_summary, updated_transcript, last_summary_time_state, current_summary_state |
|
|
|
|
|
|
|
return updated_transcript, new_summary, updated_transcript, updated_last_summary_time, new_summary |
|
|
|
|
|
|
|
print("Creating Gradio interface...") |
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Real-Time Meeting Notes with Webcam View") |
|
gr.Markdown("Speak into your microphone. Transcription appears below. Summary updates periodically.") |
|
|
|
|
|
transcript_state = gr.State("") |
|
last_summary_time = gr.State(0.0) |
|
summary_state = gr.State("") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
|
|
audio_stream = gr.Audio(sources=["microphone"], streaming=True, label="Live Microphone Input", type="numpy") |
|
|
|
|
|
|
|
|
|
webcam_view = gr.Image(sources=["webcam"], label="Your Webcam", streaming=True) |
|
|
|
with gr.Column(scale=2): |
|
transcription_output = gr.Textbox(label="Full Transcription", lines=15, interactive=False) |
|
summary_output = gr.Textbox(label=f"Bullet Point Summary (Updates ~every {SUMMARY_INTERVAL}s)", lines=10, interactive=False) |
|
|
|
|
|
|
|
|
|
audio_stream.stream( |
|
fn=process_audio_stream, |
|
inputs=[audio_stream, transcript_state, last_summary_time, summary_state], |
|
outputs=[transcription_output, summary_output, transcript_state, last_summary_time, summary_state], |
|
) |
|
|
|
|
|
def clear_state_values(): |
|
print("Clearing state.") |
|
return "", "", 0.0, "" |
|
|
|
def clear_state(): |
|
return "", 0.0, "" |
|
|
|
clear_button = gr.Button("Clear Transcript & Summary") |
|
|
|
clear_button.click( |
|
fn=lambda: ("", "", "", 0.0, ""), |
|
inputs=[], |
|
outputs=[transcription_output, summary_output, transcript_state, last_summary_time, summary_state] |
|
) |
|
|
|
|
|
print("Launching Gradio interface...") |
|
demo.queue() |
|
demo.launch(debug=True, share=True) |