Spaces:
Build error
Build error
fcernafukuzaki
commited on
Commit
•
589544d
1
Parent(s):
528738b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
%%file app.py
|
2 |
+
import streamlit as st
|
3 |
+
import soundfile as sf
|
4 |
+
import vosk
|
5 |
+
import pyaudio
|
6 |
+
import json
|
7 |
+
import threading
|
8 |
+
|
9 |
+
# Ruta al modelo Vosk
|
10 |
+
model_path = "vosk-model-es-0.42"
|
11 |
+
model = vosk.Model(model_path)
|
12 |
+
|
13 |
+
# Inicializar Vosk
|
14 |
+
rec = vosk.KaldiRecognizer(model, 16000)
|
15 |
+
|
16 |
+
# Inicializar PyAudio
|
17 |
+
p = pyaudio.PyAudio()
|
18 |
+
|
19 |
+
# Variables globales para el streaming
|
20 |
+
stream = None
|
21 |
+
is_recording = False
|
22 |
+
|
23 |
+
# Función para iniciar la grabación
|
24 |
+
def start_recording():
|
25 |
+
global stream, is_recording
|
26 |
+
is_recording = True
|
27 |
+
stream = p.open(format=pyaudio.paInt16,
|
28 |
+
channels=1,
|
29 |
+
rate=16000,
|
30 |
+
input=True,
|
31 |
+
frames_per_buffer=8192)
|
32 |
+
while is_recording:
|
33 |
+
data = stream.read(4096)
|
34 |
+
if rec.AcceptWaveform(data):
|
35 |
+
result = json.loads(rec.Result())
|
36 |
+
recognized_text = result['text']
|
37 |
+
if recognized_text:
|
38 |
+
st.session_state['transcript'] += recognized_text + "\n"
|
39 |
+
st.experimental_rerun()
|
40 |
+
if "terminate" in recognized_text.lower():
|
41 |
+
stop_recording()
|
42 |
+
|
43 |
+
# Función para detener la grabación
|
44 |
+
def stop_recording():
|
45 |
+
global stream, is_recording
|
46 |
+
is_recording = False
|
47 |
+
if stream is not None:
|
48 |
+
stream.stop_stream()
|
49 |
+
stream.close()
|
50 |
+
|
51 |
+
# Interfaz de Streamlit
|
52 |
+
st.title("Reconocimiento de Voz con Vosk")
|
53 |
+
st.write("Haga clic en 'Iniciar Grabación' y diga 'terminate' para detener la grabación.")
|
54 |
+
|
55 |
+
if 'transcript' not in st.session_state:
|
56 |
+
st.session_state['transcript'] = ""
|
57 |
+
|
58 |
+
if st.button("Iniciar Grabación"):
|
59 |
+
threading.Thread(target=start_recording).start()
|
60 |
+
|
61 |
+
if st.button("Detener Grabación"):
|
62 |
+
stop_recording()
|
63 |
+
|
64 |
+
st.text_area("Transcripción", st.session_state['transcript'], height=200)
|
65 |
+
|
66 |
+
# Terminar PyAudio al cerrar la aplicación
|
67 |
+
def close_pyaudio():
|
68 |
+
p.terminate()
|
69 |
+
|
70 |
+
st.write("Cuando termines, cierra la aplicación.")
|