-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
97 lines (79 loc) · 2.9 KB
/
server.py
1
2
3
4
5
6
7
8
9
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from gevent import monkey
monkey.patch_all()
import speech_recognition as sr
import uuid
from flask import Flask, render_template, session, request, url_for, current_app
import wave
import uuid
import os
from gtts import gTTS
from flask_socketio import SocketIO, emit, join_room
dir_path = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'nuttertools'
socketio = SocketIO(app)
@app.route('/')
def chat():
return render_template('chat.html')
@app.route('/login')
def login():
return render_template('login.html')
@socketio.on('message', namespace='/chat')
def chat_message(message):
print("message = ", message)
emit('message', {'data': message['data']}, broadcast=True)
@socketio.on('connect', namespace='/chat')
def test_connect():
emit('my response', {'data': 'Connected', 'count': 0})
@socketio.on('chat-to-aud', namespace='/chat')
def chat_to_aud(message):
print(message)
audio = gTTS(message)
name = str(uuid.uuid1()) + ".wav"
audio.save(os.path.join(dir_path, 'static', '_files', name))
print("done")
emit('add-wavefile', url_for('static',
filename='_files/' + name), broadcast=True)
# os.remove(os.path.join(dir_path, 'static', '_files', 'message.wav'))
@socketio.on('start-recording', namespace='/chat')
def start_recording(options):
"""Start recording audio from the client."""
id = uuid.uuid4().hex # server-side filename
session['wavename'] = id + '.wav'
wf = wave.open(os.path.join(dir_path, 'static', '_files', session['wavename']), 'wb')
wf.setnchannels(options.get('numChannels', 1))
wf.setsampwidth(options.get('bps', 16) // 8)
wf.setframerate(options.get('fps', 44100))
session['wavefile'] = wf
@socketio.on('write-audio', namespace='/chat')
def write_audio(data):
"""Write a chunk of audio from the client."""
session['wavefile'].writeframes(data)
def getTextFromAudio(filepath):
r = sr.Recognizer()
filepath = filepath[1:]
filepath = os.path.join(dir_path, filepath)
print(filepath)
with sr.AudioFile(filepath) as source:
r.adjust_for_ambient_noise(source)
r.pause_threshold = 3
audio = r.listen(source)
# recognize speech using Sphinx
a=r.recognize_google(audio)
return a.lower()
@socketio.on('end-recording', namespace='/chat')
def end_recording():
"""Stop recording audio from the client."""
emit('add-wavefile', url_for('static',
filename='_files/' + session['wavename']))
text = getTextFromAudio(
url_for('static', filename='_files/' + session['wavename']))
print(text)
emit('message', {'data': { 'message': text, 'author': 'by IM4ALL' }}, broadcast=True)
print('here')
session['wavefile'].close()
del session['wavefile']
del session['wavename']
if __name__ == '__main__':
socketio.run(app)