-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
62 lines (47 loc) · 1.68 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
import base64
import json
from flask import Flask, render_template, request
from worker import speech_to_text, text_to_speech, openai_process_message
from flask_cors import CORS
import os
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/speech-to-text', methods=['POST'])
def speech_to_text_route():
print("processing speech to text")
audio_binary = request.data
text = speech_to_text(audio_binary)
response = app.response_class(
response=json.dumps({'text': text}),
status=200,
mimetype='application/json'
)
print(response)
print(response.data)
return response
@app.route('/process-message', methods=['POST'])
def process_prompt_route():
user_message = request.json['userMessage']
print('user_message', user_message)
voice = request.json['voice']
print('voice', voice)
openai_response_text = openai_process_message(user_message)
openai_response_text = os.linesep.join(
[s for s in openai_response_text.splitlines() if s])
openai_response_speech = text_to_speech(openai_response_text, voice)
# convert output_speech to base64 string
openai_response_speech = base64.b64encode(
openai_response_speech).decode('utf-8')
response = app.response_class(
response=json.dumps({"openaiResponseText": openai_response_text,
"openaiResponseSpeech": openai_response_speech}),
status=200,
mimetype='application/json'
)
print(response)
return response
if __name__ == "__main__":
app.run(port=8000, host='0.0.0.0')