我正在尝试使用 for 循环从音频文件中获取内容来遍历文件的内容,但我得到的是文本的最后一行。我确定问题出在 main.py 的主代码中,我不知道可能出了什么问题。这是一些代码:
from flask import Flask, render_template, request, redirect
from google.cloud import speech
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'PATH'
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
if "file" not in request.files:
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
return redirect(request.url)
if file:
client = speech.SpeechClient()
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code="en-US",
)
content = request.files['file'].read()
audio = speech.RecognitionAudio(content=content)
response = client.recognize(config=config, audio=audio)
for result in response.results:
text = result.alternatives[0].transcript
return render_template('index.html', text=text)
if __name__ == "__main__":
app.run(debug=True)