我正在创建一个web应用程序,它在浏览器中录制音频,并将其发送到flask后端进行语音到文本转录和意图分类。 当我在没有检查方法的情况下运行代码时(该方法的代码被注释掉),这就是命令行显示的内容
127.0.0.1 - - [11/Jan/2021 06:50:00] "GET /result HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Admin\Desktop\Voice Assistant web\voice_assistant.py", line 22, in result
file = request.files['audio_data']
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\flask\debughelpers.py", line 96, in __getitem__
return oldcls.__getitem__(self, key)
File "C:\Users\Admin\Desktop\Voice Assistant web\virtualenv\lib\site-packages\werkzeug\datastructures.py", line 442, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'audio_data'
127.0.0.1 - - [11/Jan/2021 06:50:00] "GET /result?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [11/Jan/2021 06:50:00] "GET /result?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [11/Jan/2021 06:50:00] "GET /result?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [11/Jan/2021 06:50:00] "GET /result?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
hello
2021-01-11 06:50:11.906955: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
There is 78 percent chance that you are trying to get the weather!
127.0.0.1 - - [11/Jan/2021 06:50:13] "POST /result HTTP/1.1" 200 -
正如您所看到的,它首先尝试使用GET方法并返回一个错误。之后,它会尝试POST请求,结果会在命令行中返回,但不会显示在浏览器中-错误消息会保留在那里。 当我检查方法是POST还是GET时,它使用GET执行代码。 我怎样才能让它接收POST请求
这是我的python代码
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
import os
from intent_classification import ICModel
from voice_to_text import VTTModel
IC = ICModel()
VTT = VTTModel()
app=Flask(__name__)
app.config['AUDIO_UPLOADS'] = 'audio_file/'
@app.route("/")
def home():
return render_template("index.html")
@app.route("/result", methods=['POST','GET'])
def result():
#if(request.method=='POST'):
print(request.files)
file = request.files['audio_data']
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['AUDIO_UPLOADS'], filename))
actual_file = 'audio_file/' + filename
text = VTT.infer(actual_file)
print(text)
intent = IC.predict(text)
print(intent)
os.remove(actual_file)
return render_template("result.html", pred = intent, text = text)
#else:
#return redirect(url_for('home'))
if __name__ == "__main__":
app.run(debug=True)
这是我的html
{% extends "base.html" %}
{% block content %}
<h1 class="font-bold text-5xl text-white mt-5 mb-10">What is on your mind?</h1>
<div class="flex items-center">
<img id="record" src="/static/images/record.jpg" alt="record"
width="150px" height="150px" class="rounded-xl mx-10"
onClick="startRecording()">
<img id="stop" src="/static/images/stop.jpg" alt="stop"
width="150px" height="150px" class="rounded-xl mx-10"
onClick="stopRecording()">
</div>
<form action="/result" method="POST">
<input type="submit" id="check"
class="rounded-lg bg-green-400 hover:bg-green-600 shadow-lg text-xl text-white font-bold my-6 px-16 py-1"
onclick="upload()" value="Check intent">
</form>
<h2 id="prompt" class="font-bold text-3xl text-white my-10" hidden="true">Listening...</h2>
<div>
<h3 class="text-3xl text-white mt-10 mb-5">
You can request the following:
</h3>
<ul class="text-xl text-white text-center">
<li>adding a song to a playlist</li>
<li>playing music</li>
<li>booking a table at a restaurant</li>
<li>getting the weather</li>
<li>searching for creative work</li>
<li>rating a book</li>
<li>searching for a screening event</li>
</ul>
</div>
<script>
URL = window.URL || window.webkitURL;
var gumStream;
var rec;
var input;
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext;
var constraints = {
audio: true,
video: false
}
var recordButton = document.getElementById("record");
var stopButton = document.getElementById("stop");
var checkButton = document.getElementById("check")
var promptMessage = document.getElementById("prompt");
recordButton.disabled=false;
stopButton.disabled=true;
checkButton.disabled=true;
promptMessage.hidden = true;
function startRecording()
{
console.log("Recording started.")
recordButton.disabled = true;
stopButton.disabled = false;
promptMessage.hidden=false;
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
console.log("getUserMedia() success, stream created, initializing Recorder.js ...");
gumStream = stream;
input = audioContext.createMediaStreamSource(stream);
rec = new Recorder(input, {
numChannels: 1
})
rec.record()
}).catch(function(err) {
recordButton.disabled = false;
stopButton.disabled = true;
promptMessage.hidden=true;
});
}
function stopRecording()
{
console.log("Recording stopped.")
stopButton.disabled = true;
recordButton.disabled = false;
checkButton.disabled = false;
promptMessage.hidden=true;
rec.stop();
gumStream.getAudioTracks()[0].stop();
}
function upload()
{
console.log("Audio being exported.")
checkButton.disabled = true;
var filename = "audio_recording.wav";
rec.exportWAV(function(blob) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
var fd = new FormData();
fd.append('audio_data', blob, filename);
/* try {
let r = await fetch('/result', {method: "POST", body: fd});
console.log('HTTP response code:',r.status);
} catch(e) {
console.log(e);
} */
var xhr = new XMLHttpRequest();
xhr.open('POST', '/result', true);
xhr.send(fd);
});
}
</script>
{% endblock %}
你说得对。
表单在通过fetch或XMLHttpRequest进行传输之前发送。您可以使用表单的提交事件来执行javascript函数。调用
preventDefault
会阻止表单的标准行为您希望如何使用服务器的响应
目前,您在浏览器中使用ajax功能,并以html页面的形式从服务器发送响应。如果您要使用^{}
您可以在客户端使用JSON格式的服务器响应。
我还建议您检查音频文件是否已传输。
下面是一个快速而肮脏的方法
根据您的需求,您应该查看Web Speech API
相关问题 更多 >
编程相关推荐