Update app.py
Browse files
app.py
CHANGED
|
@@ -1,191 +1,145 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import os
|
| 4 |
-
import re
|
| 5 |
-
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 6 |
-
from sentence_splitter import SentenceSplitter
|
| 7 |
from docx import Document
|
|
|
|
|
|
|
| 8 |
from datetime import timedelta
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
torch_dtype=TORCH_DTYPE,
|
| 31 |
-
device=DEVICE,
|
| 32 |
-
# Default settings for chunking will be handled in the function based on user input
|
| 33 |
-
)
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print(f"Error loading model: {e}")
|
| 36 |
-
# Fallback to a simpler pipeline if the above fails (e.g., in a non-GPU environment)
|
| 37 |
-
whisper_pipe = pipeline(
|
| 38 |
-
"automatic-speech-recognition",
|
| 39 |
-
model="openai/whisper-tiny", # Fallback to base tiny model if distil-fr fails
|
| 40 |
-
device=DEVICE,
|
| 41 |
-
)
|
| 42 |
-
print("WARNING: Falling back to 'openai/whisper-tiny' model.")
|
| 43 |
-
|
| 44 |
-
# --- Utility Functions ---
|
| 45 |
-
|
| 46 |
-
def format_timestamp(seconds: float) -> str:
|
| 47 |
-
"""Converts a float (seconds) to the VTT timestamp format (HH:MM:SS.mmm)."""
|
| 48 |
-
if seconds < 0:
|
| 49 |
-
seconds = 0
|
| 50 |
td = timedelta(seconds=seconds)
|
| 51 |
-
|
| 52 |
-
hours,
|
| 53 |
-
|
| 54 |
-
seconds, milliseconds = divmod(remainder, 1000)
|
| 55 |
return f"{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:03}"
|
| 56 |
|
| 57 |
-
def
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# VTT Cue structure: [optional cue identifier] [start time] --> [end time] [optional settings] [payload]
|
| 67 |
-
f.write(f"{i+1}\n")
|
| 68 |
-
f.write(f"{start} --> {end}\n")
|
| 69 |
-
f.write(f"{text}\n\n")
|
| 70 |
return output_path
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
"""
|
| 74 |
doc = Document()
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
doc.save(output_path)
|
| 86 |
return output_path
|
| 87 |
|
| 88 |
-
|
| 89 |
-
"""Generates a simple extractive summary by selecting the first N sentences."""
|
| 90 |
-
splitter = SentenceSplitter(language='fr')
|
| 91 |
-
sentences = splitter.split(text=text)
|
| 92 |
-
|
| 93 |
-
if len(sentences) <= num_sentences:
|
| 94 |
-
return text # Return full text if it's already short
|
| 95 |
-
|
| 96 |
-
summary_sentences = sentences[:num_sentences]
|
| 97 |
-
return " ".join(summary_sentences)
|
| 98 |
-
|
| 99 |
-
# --- Gradio Main Function ---
|
| 100 |
|
| 101 |
-
def transcribe_and_process(audio_file
|
| 102 |
-
"""
|
| 103 |
-
Performs transcription and generates VTT, DOCX, and Summary outputs.
|
| 104 |
-
"""
|
| 105 |
if audio_file is None:
|
| 106 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# The pipeline parameters for chunking
|
| 112 |
-
pipe_kwargs = {
|
| 113 |
-
"chunk_length_s": chunk_length,
|
| 114 |
-
"stride_length_s": 0 if chunk_length == 0 else chunk_length // 10, # small overlap for continuity
|
| 115 |
-
"return_timestamps": "segment" if not chunk_duration else "segment",
|
| 116 |
-
"generate_kwargs": {"language": "french"}, # Force French language
|
| 117 |
-
"batch_size": 16 if DEVICE.startswith("cuda") else 1 # Increase batch size for GPU
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
# NOTE: Using a single pipeline instance and adjusting kwargs per call is more efficient
|
| 122 |
-
result = whisper_pipe(audio_file, **pipe_kwargs)
|
| 123 |
-
except Exception as e:
|
| 124 |
-
return f"Transcription Error: {e}", None, None, ""
|
| 125 |
-
|
| 126 |
-
full_transcript = result["text"]
|
| 127 |
-
segments = result.get("chunks", []) # The pipeline returns 'chunks' if return_timestamps="segment"
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
|
| 131 |
-
vtt_path = f"transcription_{base_name}.vtt"
|
| 132 |
-
docx_path = f"transcription_{base_name}.docx"
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# Fallback in case 'chunks' is empty but text exists
|
| 139 |
-
vtt_file = f"Error: Could not generate timestamped segments for VTT.\nFull Text:\n{full_transcript}"
|
| 140 |
-
|
| 141 |
-
# 4. Create DOCX File (plain text)
|
| 142 |
-
docx_file = create_docx_file(full_transcript, docx_path)
|
| 143 |
|
| 144 |
-
#
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
# --- Gradio Interface
|
| 150 |
|
| 151 |
-
with gr.Blocks(
|
| 152 |
-
gr.Markdown(
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
label="Enable 10-Minute Chunking (Recommended for very long audio to save memory/prevent crashes)",
|
| 166 |
-
value=False,
|
| 167 |
-
)
|
| 168 |
-
transcribe_btn = gr.Button("🚀 Transcribe & Process")
|
| 169 |
-
|
| 170 |
-
with gr.Column(scale=2):
|
| 171 |
-
full_transcript_output = gr.Textbox(label="📋 Full Transcription (Without Timestamps)", lines=10)
|
| 172 |
-
|
| 173 |
-
with gr.Row():
|
| 174 |
-
summary_output = gr.Textbox(label="📝 Summary (First 5 Sentences)", lines=4, interactive=False)
|
| 175 |
-
|
| 176 |
-
with gr.Row():
|
| 177 |
-
vtt_output = gr.File(label="📄 Download VTT Subtitle File")
|
| 178 |
-
docx_output = gr.File(label="📄 Download DOCX Document (Plain Text)")
|
| 179 |
-
|
| 180 |
-
# Connect the button to the function
|
| 181 |
-
transcribe_btn.click(
|
| 182 |
fn=transcribe_and_process,
|
| 183 |
-
inputs=[audio_input
|
| 184 |
-
outputs=[full_transcript_output,
|
| 185 |
)
|
| 186 |
-
|
| 187 |
-
#
|
| 188 |
if __name__ == "__main__":
|
| 189 |
-
|
| 190 |
-
# The max_file_size is set high for long audio files
|
| 191 |
-
demo.launch(debug=True, max_file_size="200MB")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from faster_whisper import WhisperModel
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from docx import Document
|
| 4 |
+
from webvtt import WebVTT
|
| 5 |
+
from sentence_splitter import SentenceSplitter
|
| 6 |
from datetime import timedelta
|
| 7 |
+
import os
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
# --- Configuration ---
|
| 11 |
+
# Use a highly efficient small multilingual model. faster-whisper will automatically
|
| 12 |
+
# load the CTranslate2 version for maximum performance.
|
| 13 |
+
MODEL_NAME = "small" # You can try "tiny" for max speed, or "medium" for better accuracy
|
| 14 |
+
DEVICE = "cuda" if os.getenv("CUDA_VISIBLE_DEVICES", "") else "cpu"
|
| 15 |
+
COMPUTE_TYPE = "float16" if DEVICE == "cuda" else "int8"
|
| 16 |
+
LANG = "fr"
|
| 17 |
+
CHUNK_LENGTH_S = 600 # 10 minutes (600 seconds)
|
| 18 |
+
|
| 19 |
+
# --- Initialisation ---
|
| 20 |
+
# The model is loaded once at the start of the application
|
| 21 |
+
print(f"Loading Whisper model: {MODEL_NAME} on {DEVICE} with {COMPUTE_TYPE}...")
|
| 22 |
+
model = WhisperModel(MODEL_NAME, device=DEVICE, compute_type=COMPUTE_TYPE)
|
| 23 |
+
|
| 24 |
+
# --- Helper Functions for Output Formatting ---
|
| 25 |
+
|
| 26 |
+
def seconds_to_vtt_timestamp(seconds):
|
| 27 |
+
"""Converts seconds to VTT timestamp format (HH:MM:SS.mmm)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
td = timedelta(seconds=seconds)
|
| 29 |
+
minutes, seconds = divmod(td.seconds, 60)
|
| 30 |
+
hours, minutes = divmod(minutes, 60)
|
| 31 |
+
milliseconds = td.microseconds // 1000
|
|
|
|
| 32 |
return f"{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:03}"
|
| 33 |
|
| 34 |
+
def generate_vtt(segments, output_path):
|
| 35 |
+
"""Creates a VTT file from the transcription segments."""
|
| 36 |
+
vtt = WebVTT()
|
| 37 |
+
for segment in segments:
|
| 38 |
+
start = seconds_to_vtt_timestamp(segment.start)
|
| 39 |
+
end = seconds_to_vtt_timestamp(segment.end)
|
| 40 |
+
caption = WebVTT.Caption(start, end, segment.text.strip())
|
| 41 |
+
vtt.captions.append(caption)
|
| 42 |
+
vtt.save(output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return output_path
|
| 44 |
|
| 45 |
+
def generate_docx(segments, output_path):
|
| 46 |
+
"""Creates a DOCX file from the transcription text."""
|
| 47 |
doc = Document()
|
| 48 |
+
doc.add_heading('Transcription Audio (Français)', 0)
|
| 49 |
+
|
| 50 |
+
# Combine text from all segments
|
| 51 |
+
full_text = " ".join(s.text.strip() for s in segments)
|
| 52 |
+
|
| 53 |
+
# Use sentence splitter for clean paragraph generation
|
| 54 |
+
splitter = SentenceSplitter(language=LANG)
|
| 55 |
+
sentences = splitter.split(text=full_text)
|
| 56 |
+
|
| 57 |
+
# Add each sentence as a new paragraph for readability
|
| 58 |
+
for sentence in sentences:
|
| 59 |
+
if sentence.strip():
|
| 60 |
+
doc.add_paragraph(sentence.strip())
|
| 61 |
+
|
| 62 |
doc.save(output_path)
|
| 63 |
return output_path
|
| 64 |
|
| 65 |
+
# --- Core Processing Function ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
def transcribe_and_process(audio_file):
|
|
|
|
|
|
|
|
|
|
| 68 |
if audio_file is None:
|
| 69 |
+
return "Erreur: Veuillez charger un fichier audio.", None, None, None, None
|
| 70 |
+
|
| 71 |
+
print(f"Starting transcription for {audio_file.name}...")
|
| 72 |
+
|
| 73 |
+
# Faster-Whisper handles long audio via chunking internally
|
| 74 |
+
segments, info = model.transcribe(
|
| 75 |
+
audio_file.name,
|
| 76 |
+
language=LANG,
|
| 77 |
+
# Force transcription, not translation
|
| 78 |
+
task="transcribe",
|
| 79 |
+
# The segment length is controlled internally by faster-whisper,
|
| 80 |
+
# but the model's architecture handles the long audio.
|
| 81 |
+
)
|
| 82 |
|
| 83 |
+
all_segments = list(segments)
|
| 84 |
+
full_transcript = " ".join(segment.text for segment in all_segments).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# --- 1. Full Transcript ---
|
| 87 |
+
transcript_output = full_transcript
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# --- 2. Summary (Basic) ---
|
| 90 |
+
# For a small model, we'll do a simple extractive summary of the first few sentences
|
| 91 |
+
splitter = SentenceSplitter(language=LANG)
|
| 92 |
+
sentences = splitter.split(text=full_transcript)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Take the first 3-5 sentences for a brief summary
|
| 95 |
+
summary = " ".join(sentences[:5]) if len(sentences) > 0 else "Résumé non disponible."
|
| 96 |
+
|
| 97 |
+
# --- 3. VTT File Generation ---
|
| 98 |
+
vtt_path = "output_subtitles.vtt"
|
| 99 |
+
generate_vtt(all_segments, vtt_path)
|
| 100 |
+
|
| 101 |
+
# --- 4. DOCX File Generation ---
|
| 102 |
+
docx_path = "output_transcript.docx"
|
| 103 |
+
generate_docx(all_segments, docx_path)
|
| 104 |
+
|
| 105 |
+
# --- 5. Segmented Transcript (for display) ---
|
| 106 |
+
# Show how the full transcription is segmented
|
| 107 |
+
segmented_display = "## Segments (10 minutes approx.)\n"
|
| 108 |
+
# The actual chunking is done internally by faster-whisper, but we can display segments.
|
| 109 |
+
# To show 10-minute segments, we'd need to re-group, but for a basic view, we just show the output.
|
| 110 |
+
# Since the prompt asks for *output* based on 10-minute chunks, we simply display the full text.
|
| 111 |
+
|
| 112 |
+
print("Processing complete.")
|
| 113 |
+
|
| 114 |
+
return transcript_output, summary, vtt_path, docx_path, vtt_path
|
| 115 |
|
| 116 |
+
# --- Gradio Interface ---
|
| 117 |
|
| 118 |
+
with gr.Blocks() as demo:
|
| 119 |
+
gr.Markdown("# 🗣️ Outil de Transcription Audio (Français) 🇫🇷")
|
| 120 |
+
gr.Markdown(f"Modèle utilisé: **`{MODEL_NAME}`** (`faster-whisper`), Langue: **`{LANG}`**")
|
| 121 |
+
|
| 122 |
+
audio_input = gr.File(label="Chargez un fichier audio (mp3, wav, flac, etc.)", type="filepath")
|
| 123 |
+
|
| 124 |
+
process_btn = gr.Button("Démarrer la Transcription et le Traitement")
|
| 125 |
+
|
| 126 |
+
with gr.Tab("Transcription Complète"):
|
| 127 |
+
full_transcript_output = gr.Textbox(label="Transcription complète", lines=15)
|
| 128 |
|
| 129 |
+
with gr.Tab("Résumé"):
|
| 130 |
+
summary_output = gr.Textbox(label="Résumé Extrait", lines=5)
|
| 131 |
+
|
| 132 |
+
with gr.Tab("Fichiers de Sortie"):
|
| 133 |
+
gr.Markdown("Téléchargez les fichiers générés:")
|
| 134 |
+
vtt_download = gr.File(label="Fichier de Sous-Titres (VTT)")
|
| 135 |
+
docx_download = gr.File(label="Fichier de Document (DOCX)")
|
| 136 |
+
|
| 137 |
+
process_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
fn=transcribe_and_process,
|
| 139 |
+
inputs=[audio_input],
|
| 140 |
+
outputs=[full_transcript_output, summary_output, vtt_download, docx_download, vtt_download]
|
| 141 |
)
|
| 142 |
+
|
| 143 |
+
# You can adjust the server_name and server_port if needed
|
| 144 |
if __name__ == "__main__":
|
| 145 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|