J'ai téléchargé l'API de reconnaissance vocale sur Heroku. Comment puis-je publier un fichier wav et obtenir également le destinataire? Puisqu'il y a peu de documents et d'essais et d'erreurs, je vais le laisser comme note de service.
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()))
#Le processus que vous souhaitez mettre ici
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