from flask import Flask, render_template from flask_socketio import SocketIO import speech_recognition as sr app = Flask(__name__) socketio = SocketIO(app) @app.route('/') def index(): return render_template('index.html') @socketio.on('message') def handle_message(message): print(f'Received: {message}') socketio.emit('response', message) @socketio.on('recognize_speech') def recognize_speech(): r = sr.Recognizer() with sr.Microphone() as source: print("Escuchando...") audio = r.listen(source) try: recognized_text = r.recognize_google(audio, language='es-ES') print(f"Texto reconocido: {recognized_text}") socketio.emit('response', recognized_text) except sr.UnknownValueError: print("No se pudo reconocer el audio") except sr.RequestError as e: print(f"Error en la solicitud a Google: {e}") if __name__ == '__main__': socketio.run(app)