Spaces:
Sleeping
Sleeping
travahacker
commited on
Commit
·
802caeb
1
Parent(s):
6c21c8a
fix: corrige internal server error - gr.File, tratamento de erros
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ Transcrição de áudio — ZeroGPU Space
|
|
| 3 |
|
| 4 |
Envie um arquivo de áudio (mp3, wav, m4a, etc.) e transcreva com Whisper.
|
| 5 |
"""
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
-
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
# ZeroGPU: decorator é no-op fora do HF
|
|
@@ -20,36 +20,51 @@ except ImportError:
|
|
| 20 |
|
| 21 |
|
| 22 |
@spaces.GPU(duration=180)
|
| 23 |
-
def transcrever(audio
|
| 24 |
"""Transcreve áudio enviado com Whisper."""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"start": seg.start,
|
| 44 |
-
"end": seg.end,
|
| 45 |
-
"text": seg.text.strip(),
|
| 46 |
-
})
|
| 47 |
|
| 48 |
-
|
| 49 |
-
if not texto:
|
| 50 |
-
return "⚠️ Nenhum texto transcrito (áudio sem fala?)."
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
MODELOS = ["tiny", "base", "small", "medium", "large-v3"]
|
|
@@ -66,10 +81,9 @@ with gr.Blocks(
|
|
| 66 |
)
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
-
audio = gr.
|
| 70 |
label="Áudio",
|
| 71 |
-
|
| 72 |
-
sources=["upload"],
|
| 73 |
)
|
| 74 |
with gr.Row():
|
| 75 |
modelo = gr.Dropdown(
|
|
@@ -104,4 +118,4 @@ with gr.Blocks(
|
|
| 104 |
"A transcrição pode levar 1–2 min para iniciar (fila da GPU)."
|
| 105 |
)
|
| 106 |
|
| 107 |
-
demo.launch()
|
|
|
|
| 3 |
|
| 4 |
Envie um arquivo de áudio (mp3, wav, m4a, etc.) e transcreva com Whisper.
|
| 5 |
"""
|
| 6 |
+
import tempfile
|
| 7 |
from pathlib import Path
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
# ZeroGPU: decorator é no-op fora do HF
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
@spaces.GPU(duration=180)
|
| 23 |
+
def transcrever(audio, modelo: str, idioma: str) -> str:
|
| 24 |
"""Transcreve áudio enviado com Whisper."""
|
| 25 |
+
try:
|
| 26 |
+
from faster_whisper import WhisperModel
|
| 27 |
+
|
| 28 |
+
path = None
|
| 29 |
+
if isinstance(audio, str) and audio and Path(audio).exists():
|
| 30 |
+
path = str(audio)
|
| 31 |
+
elif hasattr(audio, "name"):
|
| 32 |
+
p = getattr(audio, "name", None)
|
| 33 |
+
if p and Path(str(p)).exists():
|
| 34 |
+
path = str(p)
|
| 35 |
+
elif isinstance(audio, bytes) and audio:
|
| 36 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
| 37 |
+
f.write(audio)
|
| 38 |
+
path = f.name
|
| 39 |
+
if not path:
|
| 40 |
+
return "❌ Envie um arquivo de áudio."
|
| 41 |
+
|
| 42 |
+
model = WhisperModel(modelo, device="cuda", compute_type="float16")
|
| 43 |
+
lang = None if idioma == "Auto" else idioma.lower()
|
| 44 |
+
|
| 45 |
+
segments, info = model.transcribe(
|
| 46 |
+
path,
|
| 47 |
+
language=lang,
|
| 48 |
+
beam_size=5,
|
| 49 |
+
vad_filter=True,
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
resultado = []
|
| 53 |
+
for seg in segments:
|
| 54 |
+
resultado.append({
|
| 55 |
+
"start": seg.start,
|
| 56 |
+
"end": seg.end,
|
| 57 |
+
"text": seg.text.strip(),
|
| 58 |
+
})
|
| 59 |
|
| 60 |
+
texto = "\n".join(s["text"] for s in resultado if s["text"])
|
| 61 |
+
if not texto:
|
| 62 |
+
return "⚠️ Nenhum texto transcrito (áudio sem fala?)."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
return f"Idioma detectado: {info.language}\n\n{texto}"
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"❌ Erro: {type(e).__name__}: {e}"
|
| 68 |
|
| 69 |
|
| 70 |
MODELOS = ["tiny", "base", "small", "medium", "large-v3"]
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
+
audio = gr.File(
|
| 85 |
label="Áudio",
|
| 86 |
+
file_types=["audio"],
|
|
|
|
| 87 |
)
|
| 88 |
with gr.Row():
|
| 89 |
modelo = gr.Dropdown(
|
|
|
|
| 118 |
"A transcrição pode levar 1–2 min para iniciar (fila da GPU)."
|
| 119 |
)
|
| 120 |
|
| 121 |
+
demo.launch(show_error=True)
|