Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- main.py +133 -0
- templates/Index.html +27 -0
- templates/Index_old.html +0 -0
main.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template
|
2 |
+
from flask_socketio import SocketIO
|
3 |
+
import speech_recognition as sr
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
socketio = SocketIO(app)
|
7 |
+
|
8 |
+
@app.route('/')
|
9 |
+
def index():
|
10 |
+
return render_template('index.html')
|
11 |
+
|
12 |
+
@socketio.on('message')
|
13 |
+
def handle_message(message):
|
14 |
+
print(f'Received: {message}')
|
15 |
+
socketio.emit('response', message)
|
16 |
+
|
17 |
+
@socketio.on('recognize_speech')
|
18 |
+
def recognize_speech():
|
19 |
+
r = sr.Recognizer()
|
20 |
+
with sr.Microphone() as source:
|
21 |
+
print("Escuchando...")
|
22 |
+
audio = r.listen(source)
|
23 |
+
try:
|
24 |
+
recognized_text = r.recognize_google(audio, language='es-ES')
|
25 |
+
print(f"Texto reconocido: {recognized_text}")
|
26 |
+
socketio.emit('response', recognized_text)
|
27 |
+
except sr.UnknownValueError:
|
28 |
+
print("No se pudo reconocer el audio")
|
29 |
+
except sr.RequestError as e:
|
30 |
+
print(f"Error en la solicitud a Google: {e}")
|
31 |
+
|
32 |
+
if __name__ == '__main__':
|
33 |
+
socketio.run(app)
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
#from flask import Flask, render_template
|
54 |
+
##from deepgram import Deepgram
|
55 |
+
#from dotenv import load_dotenv
|
56 |
+
#import os
|
57 |
+
#import asyncio
|
58 |
+
#from aiohttp import web
|
59 |
+
#from aiohttp_wsgi import WSGIHandler
|
60 |
+
|
61 |
+
#from typing import Dict, Callable
|
62 |
+
|
63 |
+
|
64 |
+
#load_dotenv()
|
65 |
+
|
66 |
+
#app = Flask('aioflask')
|
67 |
+
|
68 |
+
##dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY'))
|
69 |
+
|
70 |
+
#async def process_audio(fast_socket: web.WebSocketResponse):
|
71 |
+
# async def get_transcript(data: Dict) -> None:
|
72 |
+
# if 'channel' in data:
|
73 |
+
# transcript = data['channel']['alternatives'][0]['transcript']
|
74 |
+
|
75 |
+
# if transcript:
|
76 |
+
# await fast_socket.send_str(transcript)
|
77 |
+
|
78 |
+
# deepgram_socket = await connect_to_deepgram(get_transcript)
|
79 |
+
# #########################################
|
80 |
+
# #r = sr.Recognizer()
|
81 |
+
# #with sr.Microphone() as source:
|
82 |
+
# # print("Listening...")
|
83 |
+
# # r.pause_threshold = 1
|
84 |
+
# # audio = r.adjust_for_ambient_noise(source)
|
85 |
+
# # audio = r.listen(source)
|
86 |
+
# #try:
|
87 |
+
# # print("Recognizing...")
|
88 |
+
# # query = r.recognize_google(get_transcript, language='es-ES')
|
89 |
+
# # print(f"User said: {query}\n")
|
90 |
+
# #except Exception as e:
|
91 |
+
# # print(e)
|
92 |
+
# # print("Unable to Recognize your voice.")
|
93 |
+
# # return "none"
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
# ########################################
|
99 |
+
# return deepgram_socket
|
100 |
+
|
101 |
+
#async def connect_to_deepgram(transcript_received_handler: Callable[[Dict], None]) -> str:
|
102 |
+
# try:
|
103 |
+
# socket = await dg_client.transcription.live({'punctuate': True, 'interim_results': False})
|
104 |
+
# socket.registerHandler(socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.'))
|
105 |
+
# socket.registerHandler(socket.event.TRANSCRIPT_RECEIVED, transcript_received_handler)
|
106 |
+
|
107 |
+
# return socket
|
108 |
+
# except Exception as e:
|
109 |
+
# raise Exception(f'Could not open socket: {e}')
|
110 |
+
|
111 |
+
#@app.route('/')
|
112 |
+
#def index():
|
113 |
+
# return render_template('index.html')
|
114 |
+
|
115 |
+
#async def socket(request):
|
116 |
+
# ws = web.WebSocketResponse()
|
117 |
+
# await ws.prepare(request)
|
118 |
+
|
119 |
+
# deepgram_socket = await process_audio(ws)
|
120 |
+
|
121 |
+
# while True:
|
122 |
+
# data = await ws.receive_bytes()
|
123 |
+
# deepgram_socket.send(data)
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
#if __name__ == "__main__":
|
128 |
+
# loop = asyncio.get_event_loop()
|
129 |
+
# aio_app = web.Application()
|
130 |
+
# wsgi = WSGIHandler(app)
|
131 |
+
# aio_app.router.add_route('*', '/{path_info: *}', wsgi.handle_request)
|
132 |
+
# aio_app.router.add_route('GET', '/listen', socket)
|
133 |
+
# web.run_app(aio_app, port=5555)
|
templates/Index.html
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
<head>
|
4 |
+
<title>WebSockets y Reconocimiento de Voz</title>
|
5 |
+
</head>
|
6 |
+
<body>
|
7 |
+
<div id="output"></div>
|
8 |
+
<button onclick="startRecognition()">Comenzar Reconocimiento</button>
|
9 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
|
10 |
+
<script>
|
11 |
+
const socket = io.connect('http://' + document.domain + ':' + location.port);
|
12 |
+
|
13 |
+
socket.on('response', function (message) {
|
14 |
+
const output = document.getElementById('output');
|
15 |
+
output.innerHTML = message;
|
16 |
+
});
|
17 |
+
|
18 |
+
function startRecognition() {
|
19 |
+
socket.emit('recognize_speech');
|
20 |
+
}
|
21 |
+
</script>
|
22 |
+
</body>
|
23 |
+
</html>
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
templates/Index_old.html
ADDED
File without changes
|