JPLTedCas commited on
Commit
240f9c4
1 Parent(s): addb349

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -77
app.py CHANGED
@@ -1,91 +1,50 @@
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, engineio_logger=True, logger=True)#,cors_allowed_origins=['http://0.0.0.0:7860'])
7
-
8
- @app.route('/')
9
- def index():
10
- return render_template('index.html')
11
 
 
 
 
 
12
  r = sr.Recognizer()
 
13
 
14
- @socketio.on('audio_data')
15
- def handle_audio(data):
16
- audio = sr.AudioData(data, 16000, 2)
17
- recognized_text = ""
18
-
19
- #######################################
20
- import pygame
21
- from io import BytesIO
22
- import soundfile as sf
23
- import tempfile
24
- # Configura Pygame
25
- pygame.mixer.init()
26
- # Crea un objeto de BytesIO para reproducir el audio
27
- audio_stream = BytesIO(data)
28
-
29
- #try:
30
- # pygame.mixer.music.load(audio_stream)
31
- # pygame.mixer.music.play()
32
- #except Exception as e:
33
- # return "Error al reproducir audio: {}".format(str(e))
34
-
35
- #return "Audio reproducido con éxito"
36
- # Guarda los datos en un archivo WAV temporal
37
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
38
- sf.write(temp_file.name, data, 44100, format="wav")
39
-
40
-
41
-
42
-
43
-
44
- #######################################
45
- try:
46
- recognized_text = r.recognize_google(audio, language='es-ES')
47
- except sr.UnknownValueError:
48
- recognized_text = "No se pudo reconocer el audio"
49
- except sr.RequestError as e:
50
- recognized_text = "Error en la solicitud: {0}".format(e)
51
-
52
- socketio.emit('transcription', recognized_text)
53
-
54
-
55
-
56
-
57
-
58
-
59
 
60
 
61
- @socketio.on('start_recording')
62
- def start_recording():
63
- # Lógica para comenzar la grabación en el servidor si es necesario
64
- pass
65
 
66
- @socketio.on('stop_recording')
67
- def stop_recording():
68
- # Lógica para detener la grabación en el servidor si es necesario
69
- pass
70
 
71
- @socketio.on('send_audio_to_server')
72
- def send_audio_to_server(audio_data):
73
- # Lógica para procesar y transcribir el audio enviado desde el cliente
74
- audio = sr.AudioData(audio_data, 16000, 2)
75
- print(audio)
76
- recognized_text = ""
77
 
78
- try:
79
- recognized_text = r.recognize_google(audio, language='es-ES')
80
- prin(recognized_text)
81
- except sr.UnknownValueError:
82
- recognized_text = "No se pudo reconocer el audio"
83
- except sr.RequestError as e:
84
- recognized_text = "Error en la solicitud: {0}".format(e)
85
 
86
- socketio.emit('transcription', recognized_text)
87
 
88
 
89
 
90
- if __name__ == '__main__':
91
- socketio.run(app, host='0.0.0.0', port=7860, debug=True)
 
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ from flask import Flask, jsonify
4
+ from flask import request
5
+ from flask import render_template
6
  import speech_recognition as sr
7
+ from werkzeug.datastructures import FileStorage
8
+ import os
9
+ import sys
10
+ from flask_caching import Cache
11
 
 
 
 
 
 
 
12
 
13
+ app = Flask(__name__)
14
+ semaforo=False
15
+ app.config['CACHE_TYPE'] = 'simple'
16
+ cache = Cache(app)
17
  r = sr.Recognizer()
18
+ @app.route("/", methods=['POST', 'GET'])
19
 
20
+ def index():
21
+ recognized_text = "" # Valor predeterminado
22
+
23
+ if request.method == "POST":
24
+ f = request.files['audio_data']
25
+ # Cargar el archivo adjunto directamente
26
+ with sr.AudioFile(f) as source:
27
+ audio_data = r.record(source)
28
+
29
+ # Realizar el reconocimiento de voz
30
+ try:
31
+ recognized_text = r.recognize_google(audio_data, language='es-ES')
32
+ except sr.UnknownValueError:
33
+ recognized_text = "No se pudo reconocer el audio"
34
+ except sr.RequestError as e:
35
+ recognized_text = f"Error en la solicitud: {str(e)}"
36
+ print(recognized_text)
37
+ return render_template('index.html', recognized_text=recognized_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
 
 
 
 
40
 
 
 
 
 
41
 
42
+ if __name__ == "__main__":
43
+ app.run(host='0.0.0.0', port=7860,debug=True)
 
 
 
 
44
 
 
 
 
 
 
 
 
45
 
 
46
 
47
 
48
 
49
+ #if __name__ == '__main__':
50
+ # socketio.run(app, host='0.0.0.0', port=7860, debug=True)