Spaces:
Build error
Build error
%%file app.py | |
import streamlit as st | |
import soundfile as sf | |
import vosk | |
import pyaudio | |
import json | |
import threading | |
# Ruta al modelo Vosk | |
model_path = "vosk-model-es-0.42" | |
model = vosk.Model(model_path) | |
# Inicializar Vosk | |
rec = vosk.KaldiRecognizer(model, 16000) | |
# Inicializar PyAudio | |
p = pyaudio.PyAudio() | |
# Variables globales para el streaming | |
stream = None | |
is_recording = False | |
# Funci贸n para iniciar la grabaci贸n | |
def start_recording(): | |
global stream, is_recording | |
is_recording = True | |
stream = p.open(format=pyaudio.paInt16, | |
channels=1, | |
rate=16000, | |
input=True, | |
frames_per_buffer=8192) | |
while is_recording: | |
data = stream.read(4096) | |
if rec.AcceptWaveform(data): | |
result = json.loads(rec.Result()) | |
recognized_text = result['text'] | |
if recognized_text: | |
st.session_state['transcript'] += recognized_text + "\n" | |
st.experimental_rerun() | |
if "terminate" in recognized_text.lower(): | |
stop_recording() | |
# Funci贸n para detener la grabaci贸n | |
def stop_recording(): | |
global stream, is_recording | |
is_recording = False | |
if stream is not None: | |
stream.stop_stream() | |
stream.close() | |
# Interfaz de Streamlit | |
st.title("Reconocimiento de Voz con Vosk") | |
st.write("Haga clic en 'Iniciar Grabaci贸n' y diga 'terminate' para detener la grabaci贸n.") | |
if 'transcript' not in st.session_state: | |
st.session_state['transcript'] = "" | |
if st.button("Iniciar Grabaci贸n"): | |
threading.Thread(target=start_recording).start() | |
if st.button("Detener Grabaci贸n"): | |
stop_recording() | |
st.text_area("Transcripci贸n", st.session_state['transcript'], height=200) | |
# Terminar PyAudio al cerrar la aplicaci贸n | |
def close_pyaudio(): | |
p.terminate() | |
st.write("Cuando termines, cierra la aplicaci贸n.") | |