Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,22 +8,37 @@ from pydub import AudioSegment
|
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
# Configuration
|
| 11 |
-
MODEL_NAME = "openai/whisper-
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
pipe = pipeline(
|
| 15 |
task="automatic-speech-recognition",
|
| 16 |
model=MODEL_NAME,
|
| 17 |
device=device,
|
| 18 |
model_kwargs={
|
| 19 |
"low_cpu_mem_usage": True,
|
| 20 |
-
"attn_implementation": "eager",
|
|
|
|
| 21 |
},
|
| 22 |
generate_kwargs={
|
| 23 |
-
"language": "french",
|
| 24 |
-
"task": "transcribe",
|
| 25 |
-
"return_timestamps": True,
|
| 26 |
-
"use_cache":
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
)
|
| 29 |
|
|
@@ -37,8 +52,18 @@ def transcribe_audio(audio_path):
|
|
| 37 |
if not audio_path:
|
| 38 |
return "Aucun fichier audio fourni", [], None
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Extraction du texte complet
|
| 44 |
text = result["text"]
|
|
|
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
# Configuration
|
| 11 |
+
MODEL_NAME = "openai/whisper-medium" # Modèle plus léger
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
| 14 |
+
# Configuration de la mémoire CUDA
|
| 15 |
+
if device == "cuda":
|
| 16 |
+
import os
|
| 17 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 18 |
+
|
| 19 |
+
# Libération de la mémoire CUDA
|
| 20 |
+
import gc
|
| 21 |
+
import torch
|
| 22 |
+
gc.collect()
|
| 23 |
+
torch.cuda.empty_cache()
|
| 24 |
+
|
| 25 |
pipe = pipeline(
|
| 26 |
task="automatic-speech-recognition",
|
| 27 |
model=MODEL_NAME,
|
| 28 |
device=device,
|
| 29 |
model_kwargs={
|
| 30 |
"low_cpu_mem_usage": True,
|
| 31 |
+
"attn_implementation": "eager",
|
| 32 |
+
"use_flash_attention_2": True, # Optimisation de la mémoire
|
| 33 |
},
|
| 34 |
generate_kwargs={
|
| 35 |
+
"language": "french",
|
| 36 |
+
"task": "transcribe",
|
| 37 |
+
"return_timestamps": True,
|
| 38 |
+
"use_cache": True,
|
| 39 |
+
"max_new_tokens": 448, # Limite la taille de la sortie
|
| 40 |
+
"chunk_length_s": 30, # Traitement par chunks de 30 secondes
|
| 41 |
+
"batch_size": 1, # Réduit l'utilisation de la mémoire
|
| 42 |
}
|
| 43 |
)
|
| 44 |
|
|
|
|
| 52 |
if not audio_path:
|
| 53 |
return "Aucun fichier audio fourni", [], None
|
| 54 |
|
| 55 |
+
try:
|
| 56 |
+
# Libération de la mémoire avant la transcription
|
| 57 |
+
if device == "cuda":
|
| 58 |
+
torch.cuda.empty_cache()
|
| 59 |
+
|
| 60 |
+
# Utilisation explicite des timestamps au niveau des mots
|
| 61 |
+
result = pipe(
|
| 62 |
+
audio_path,
|
| 63 |
+
return_timestamps="word",
|
| 64 |
+
chunk_length_s=30, # Traitement par chunks
|
| 65 |
+
stride_length_s=5 # Chevauchement pour une meilleure continuité
|
| 66 |
+
)
|
| 67 |
|
| 68 |
# Extraction du texte complet
|
| 69 |
text = result["text"]
|