I uploaded the voice recognition API on Heroku, How can I post a wav file and get the recipient as well? Since there are few documents and trial and error, I will leave it as a memo.
from flask import Flask, request, abort
import soundfile as sf
from io import BytesIO
import os
app = Flask(__name__)
@app.route("/webapp", methods=['POST'])
def webapp():
webaccesskey = request.headers["KEY"]
webaccesspassword = request.headers["PWD"]
if webaccesskey == "ok" and webaccesspassword == "foapkun":
try:
if request.args.get("TYPE") == "wav_message":
user_id = request.args.get("USERID")
key = request.args.get("KEY")
valid = checker(user_id, key)
if valid == True:
wavdata = request.files["file"].stream
audio_data, samplerate = sf.read(BytesIO(wavdata.read()))
#The process you want to put here
except:
abort(400)
else:
abort(400)
return 'OK'
def checker(user_id, key):
valid = False
if user_id == "user_id":
if key == "key":
valid = True
return valid
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)
import requests
class Message:
def __init__(self):
self.accessURL = "http://localhost:5000/webapp"
self.accesskey = "ok"
self.accessPASSWORD = "qiita"
def put_wav(self, user_id, key):
self.headers = {"KEY": self.accesskey, "PWD": self.accessPASSWORD}
self.payload = {"TYPE": "wav_message", "USERID": user_id, "KEY": key}
fileName = 'test.wav'
files = {'file': open(fileName, 'rb')}
r = requests.post(self.accessURL, headers=self.headers, params=self.payload, files=files)
print(r.text)
if __name__ == "__main__":
message = Message()
message.put_wav(user_id="user_id", key="key")
Recommended Posts