Initial commit
This commit is contained in:
59
medical_transcriber.py
Normal file
59
medical_transcriber.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from faster_whisper import WhisperModel
|
||||
|
||||
def transcribe_audio(file_path, model_size="large-v3", device="cuda", compute_type="float16"):
|
||||
"""
|
||||
Transcrit un fichier audio avec une précision maximale et optimisation GPU.
|
||||
"""
|
||||
if not os.path.exists(file_path):
|
||||
print(f"Erreur : Le fichier {file_path} n'existe pas.")
|
||||
return None
|
||||
|
||||
print(f"Chargement du modèle {model_size} sur {device}...")
|
||||
# Initialisation du modèle avec optimisation CTranslate2
|
||||
model = WhisperModel(model_size, device=device, compute_type=compute_type)
|
||||
|
||||
print(f"Transcription en cours : {file_path}")
|
||||
start_time = time.time()
|
||||
|
||||
# Transcription avec paramètres optimisés pour la précision médicale
|
||||
# beam_size=5 : meilleure recherche de mots
|
||||
# language="fr" : force le français pour éviter les erreurs de détection
|
||||
segments, info = model.transcribe(
|
||||
file_path,
|
||||
beam_size=5,
|
||||
language="fr",
|
||||
condition_on_previous_text=True,
|
||||
vad_filter=True, # Supprime les silences pour éviter les hallucinations
|
||||
vad_parameters=dict(min_silence_duration_ms=500)
|
||||
)
|
||||
|
||||
print(f"Langue détectée : {info.language} (probabilité: {info.language_probability:.2f})")
|
||||
|
||||
full_text = []
|
||||
for segment in segments:
|
||||
timestamp = f"[{time.strftime('%H:%M:%S', time.gmtime(segment.start))}]"
|
||||
print(f"{timestamp} {segment.text}")
|
||||
full_text.append(f"{timestamp} {segment.text}")
|
||||
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
print(f"
|
||||
Transcription terminée en {duration:.2f} secondes.")
|
||||
|
||||
return "
|
||||
".join(full_text)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python medical_transcriber.py <audio_file>")
|
||||
else:
|
||||
audio_file = sys.argv[1]
|
||||
transcript = transcribe_audio(audio_file)
|
||||
if transcript:
|
||||
output_file = audio_file.rsplit('.', 1)[0] + "_transcript.txt"
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(transcript)
|
||||
print(f"Transcription sauvegardée dans : {output_file}")
|
||||
Reference in New Issue
Block a user