File size: 1,906 Bytes
589544d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
%%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.")