Spaces:
Sleeping
Sleeping
Commit
路
e5cf576
1
Parent(s):
5b590c8
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,137 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
# -*- coding: utf-8 -*-
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from flask import
|
6 |
-
|
7 |
-
from
|
8 |
-
import
|
9 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
#!/usr/bin/env python
|
2 |
# -*- coding: utf-8 -*-
|
3 |
+
#from asyncio.windows_events import NULL
|
4 |
+
#from cgitb import text
|
5 |
+
from flask import Flask, jsonify, session
|
6 |
+
from flask import request
|
7 |
+
from flask import render_template
|
8 |
+
import speech_recognition as sr
|
9 |
+
from werkzeug.datastructures import FileStorage
|
10 |
+
import os
|
11 |
+
import sys
|
12 |
+
from flask_caching import Cache
|
13 |
+
#from flask_socketio import SocketIO
|
14 |
+
|
15 |
+
|
16 |
+
app = Flask(__name__)
|
17 |
+
#socketio = SocketIO(app)
|
18 |
+
semaforo=False
|
19 |
+
app.config['CACHE_TYPE'] = 'simple'
|
20 |
+
cache = Cache(app)
|
21 |
+
r = sr.Recognizer()
|
22 |
+
condition = ""
|
23 |
+
app.secret_key = 'clave'
|
24 |
+
|
25 |
+
|
26 |
+
@app.route("/", methods=['POST', 'GET'])
|
27 |
+
|
28 |
+
def index():
|
29 |
+
r = sr.Recognizer()
|
30 |
+
global condition
|
31 |
+
|
32 |
+
print(condition)
|
33 |
+
print("vacio")
|
34 |
+
#if recognized_text == "No se pudo reconocer el audio":
|
35 |
+
#recognized_text = ""
|
36 |
+
|
37 |
+
recognized_text = "" # Valor predeterminado
|
38 |
+
texto_imprimir = ""
|
39 |
+
activado = False
|
40 |
+
|
41 |
+
#condition = False
|
42 |
+
|
43 |
+
|
44 |
+
if request.method == "POST":
|
45 |
+
print("ondo ondo")
|
46 |
+
if recognized_text == "No se pudo reconocer el audio":
|
47 |
+
#print(recognized_text)
|
48 |
+
|
49 |
+
recognized_text=""
|
50 |
+
|
51 |
+
f = request.files['audio_data']
|
52 |
+
# Cargar el archivo adjunto directamente
|
53 |
+
with sr.AudioFile(f) as source:
|
54 |
+
audio_data = r.record(source)
|
55 |
+
|
56 |
+
# Realizar el reconocimiento de voz
|
57 |
+
try:
|
58 |
+
recognized_text = r.recognize_google(audio_data, language='es-ES')
|
59 |
+
palabras = recognized_text.lower().split()
|
60 |
+
for palabra in palabras:
|
61 |
+
if palabra == "activar":
|
62 |
+
print("activado")
|
63 |
+
condition = 1
|
64 |
+
activado = True
|
65 |
+
#texto_imprimir = recognized_text
|
66 |
+
#return render_template('index.html', recognized_text=recognized_text, condition=condition)
|
67 |
+
|
68 |
+
#condition = True
|
69 |
+
elif palabra == "desactivar":
|
70 |
+
condition = 0
|
71 |
+
activado = False
|
72 |
+
texto_imprimir = ""
|
73 |
+
#condition = False
|
74 |
+
#return render_template('index.html', recognized_text=recognized_text, condition=condition)
|
75 |
+
|
76 |
+
if activado:
|
77 |
+
texto_imprimir += palabra + " "
|
78 |
+
|
79 |
+
except sr.UnknownValueError:
|
80 |
+
recognized_text = "No se pudo reconocer el audio"
|
81 |
+
#print(recognized_text)
|
82 |
+
#condition = False
|
83 |
+
except sr.RequestError as e:
|
84 |
+
recognized_text = f"Error en la solicitud: {str(e)}"
|
85 |
+
#condition = False
|
86 |
+
#print(recognized_text)
|
87 |
+
|
88 |
+
|
89 |
+
if condition is None:
|
90 |
+
condition = 0
|
91 |
+
|
92 |
+
print(condition)
|
93 |
+
session['condition'] = condition
|
94 |
+
print("intento")
|
95 |
+
#else:
|
96 |
+
##########################################################################################
|
97 |
+
query=recognized_text
|
98 |
+
print("TEXTO")
|
99 |
+
#print(query)
|
100 |
+
print (texto_imprimir)
|
101 |
+
return render_template('index.html', condition=condition, texto_imprimir=texto_imprimir)
|
102 |
+
#return render_template('index.html', recognized_text=recognized_text, condition=condition, texto_imprimir=texto_imprimir)
|
103 |
+
|
104 |
+
#@socketio.on('update_condition')
|
105 |
+
#def handle_condition_update(condition):
|
106 |
+
# Procesar y actualizar la condici贸n seg煤n sea necesario
|
107 |
+
# Puedes emitir la nueva condici贸n a todos los clientes conectados
|
108 |
+
# socketio.emit('update_condition', condition, broadcast=True)
|
109 |
+
|
110 |
+
#@app.route('/obtener_contenido',methods=['GET'])
|
111 |
+
#def obtener_contenido():
|
112 |
+
# print("ha entrao bien en def")
|
113 |
+
# condition = session.get('condition','Valor predeterminado si no hay valor en la sesion')
|
114 |
+
# print(condition)
|
115 |
+
# return jsonify({'condition':condition})
|
116 |
+
|
117 |
+
@app.route('/set_condition/<condition>', methods=['GET'])
|
118 |
+
def set_condition(condition):
|
119 |
+
response = jsonify({'condition': condition})
|
120 |
+
response.set_cookie('condition', condition)
|
121 |
+
return response
|
122 |
+
|
123 |
+
@app.route('/get_condition', methods=['GET'])
|
124 |
+
def get_condition():
|
125 |
+
#condition = request.cookies.get('condition', 'Valor predeterminado si no hay valor en la cookie')
|
126 |
+
print(condition)
|
127 |
+
return jsonify({'condition': condition})
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
app.run(host='0.0.0.0', port=7860,debug=True)
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
#f __name__ == '__main__':
|
137 |
+
# socketio.run(app, host='0.0.0.0', port=7860, debug=True)
|