Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,16 +16,49 @@ def index():
|
|
| 16 |
|
| 17 |
@app.route('/speech-to-text', methods=['POST'])
|
| 18 |
def speech_to_text_route():
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
@app.route('/process-message', methods=['POST'])
|
| 23 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
response = app.response_class(
|
| 25 |
-
response=json.dumps({"openaiResponseText":
|
| 26 |
status=200,
|
| 27 |
mimetype='application/json'
|
| 28 |
)
|
|
|
|
|
|
|
| 29 |
return response
|
| 30 |
|
| 31 |
|
|
|
|
| 16 |
|
| 17 |
@app.route('/speech-to-text', methods=['POST'])
|
| 18 |
def speech_to_text_route():
|
| 19 |
+
print("processing speech-to-text")
|
| 20 |
+
audio_binary = request.data # Get the user's speech from their request
|
| 21 |
+
text = speech_to_text(audio_binary) # Call speech_to_text function to transcribe the speech
|
| 22 |
+
|
| 23 |
+
# Return the response back to the user in JSON format
|
| 24 |
+
response = app.response_class(
|
| 25 |
+
response=json.dumps({'text': text}),
|
| 26 |
+
status=200,
|
| 27 |
+
mimetype='application/json'
|
| 28 |
+
)
|
| 29 |
+
print(response)
|
| 30 |
+
print(response.data)
|
| 31 |
+
return response
|
| 32 |
|
| 33 |
|
| 34 |
@app.route('/process-message', methods=['POST'])
|
| 35 |
+
def process_message_route():
|
| 36 |
+
user_message = request.json['userMessage'] # Get user's message from their request
|
| 37 |
+
print('user_message', user_message)
|
| 38 |
+
|
| 39 |
+
voice = request.json['voice'] # Get user's preferred voice from their request
|
| 40 |
+
print('voice', voice)
|
| 41 |
+
|
| 42 |
+
# Call openai_process_message function to process the user's message and get a response back
|
| 43 |
+
openai_response_text = openai_process_message(user_message)
|
| 44 |
+
|
| 45 |
+
# Clean the response to remove any emptylines
|
| 46 |
+
openai_response_text = os.linesep.join([s for s in openai_response_text.splitlines() if s])
|
| 47 |
+
|
| 48 |
+
# Call our text_to_speech function to convert OpenAI Api's reponse to speech
|
| 49 |
+
openai_response_speech = text_to_speech(openai_response_text, voice)
|
| 50 |
+
|
| 51 |
+
# convert openai_response_speech to base64 string so it can be sent back in the JSON response
|
| 52 |
+
openai_response_speech = base64.b64encode(openai_response_speech).decode('utf-8')
|
| 53 |
+
|
| 54 |
+
# Send a JSON response back to the user containing their message's response both in text and speech formats
|
| 55 |
response = app.response_class(
|
| 56 |
+
response=json.dumps({"openaiResponseText": openai_response_text, "openaiResponseSpeech": openai_response_speech}),
|
| 57 |
status=200,
|
| 58 |
mimetype='application/json'
|
| 59 |
)
|
| 60 |
+
|
| 61 |
+
print(response)
|
| 62 |
return response
|
| 63 |
|
| 64 |
|