Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Inicialize o modelo
|
| 5 |
-
transcriber = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Função para transcrever o áudio
|
| 8 |
def transcribe(audio_file):
|
| 9 |
try:
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Interface gráfica com Gradio
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
-
gr.Markdown("# 🎙️ Whisper Transcription -
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
with gr.Column(scale=1):
|
| 22 |
-
gr.Markdown("### 1️⃣ Envie
|
| 23 |
-
audio_input = gr.Audio(type="filepath", label="Envie um arquivo de áudio
|
| 24 |
|
| 25 |
with gr.Column(scale=1):
|
| 26 |
gr.Markdown("### 2️⃣ Resultado da transcrição")
|
| 27 |
-
transcription_output = gr.Textbox(label="Transcrição", lines=
|
| 28 |
|
| 29 |
transcribe_button = gr.Button("🚀 Transcrever")
|
| 30 |
|
|
@@ -33,3 +55,4 @@ with gr.Blocks() as demo:
|
|
| 33 |
|
| 34 |
# Rodar a aplicação
|
| 35 |
demo.launch(share=True)
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
from pydub.utils import make_chunks
|
| 5 |
+
import tempfile
|
| 6 |
|
| 7 |
+
# Inicialize o modelo Whisper com um modelo menor para CPUs
|
| 8 |
+
transcriber = pipeline(
|
| 9 |
+
"automatic-speech-recognition",
|
| 10 |
+
model="openai/whisper-tiny", # Troque para `whisper-base` se necessário
|
| 11 |
+
device="cpu" # Garante que a CPU será usada
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Função para dividir áudios longos em trechos menores (30 segundos)
|
| 15 |
+
def split_audio(audio_path, chunk_length=30_000):
|
| 16 |
+
audio = AudioSegment.from_file(audio_path)
|
| 17 |
+
chunks = make_chunks(audio, chunk_length) # Divide em trechos de 30 segundos
|
| 18 |
+
return chunks
|
| 19 |
|
| 20 |
# Função para transcrever o áudio
|
| 21 |
def transcribe(audio_file):
|
| 22 |
try:
|
| 23 |
+
# Divida o áudio em partes
|
| 24 |
+
chunks = split_audio(audio_file)
|
| 25 |
+
full_transcription = []
|
| 26 |
+
|
| 27 |
+
# Processar cada parte separadamente
|
| 28 |
+
for i, chunk in enumerate(chunks):
|
| 29 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=True) as temp_chunk:
|
| 30 |
+
chunk.export(temp_chunk.name, format="wav")
|
| 31 |
+
transcription = transcriber(temp_chunk.name, return_timestamps=False)["text"]
|
| 32 |
+
full_transcription.append(f"[Parte {i+1}]: {transcription}")
|
| 33 |
+
|
| 34 |
+
return "\n".join(full_transcription)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Erro ao processar áudio: {str(e)}"
|
| 37 |
|
| 38 |
# Interface gráfica com Gradio
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("# 🎙️ Whisper Transcription - Suporte a CPUs e Áudios Longos")
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column(scale=1):
|
| 44 |
+
gr.Markdown("### 1️⃣ Envie seu áudio (máx. 5 minutos)")
|
| 45 |
+
audio_input = gr.Audio(type="filepath", label="Envie um arquivo de áudio")
|
| 46 |
|
| 47 |
with gr.Column(scale=1):
|
| 48 |
gr.Markdown("### 2️⃣ Resultado da transcrição")
|
| 49 |
+
transcription_output = gr.Textbox(label="Transcrição", lines=15, interactive=False)
|
| 50 |
|
| 51 |
transcribe_button = gr.Button("🚀 Transcrever")
|
| 52 |
|
|
|
|
| 55 |
|
| 56 |
# Rodar a aplicação
|
| 57 |
demo.launch(share=True)
|
| 58 |
+
|