Python / Flask - 托管服务器上的内部服务器错误
Python / Flask - Internal Server Error on hosting server
在本地服务器上,此 Python 应用程序运行良好,但是,当我在托管我网站的服务器上设置此 Python 应用程序时,index.html
加载,这意味着Python设置好了,但是当我点击Transcribe button
时,在我select the language
和.wav file
之后,出现错误:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
有没有人知道错误在哪里,如果不是错误,如何克服服务器响应。
from flask import Flask, render_template, request, redirect
import speech_recognition as sr
import requests
app = Flask(__name__)
def get_languages():
url = 'https://cloud.google.com/speech-to-text/docs/languages'
resp = requests.get(url)
start_text = ' <tbody class="list">\n'
end_text = ' </tbody>\n'
table = resp.text.split(start_text)[1].split(end_text)[0]
tr_start = ' <tr>\n'
sections = table.split(tr_start)[1:]
languages = []
for section in sections:
short = section.splitlines()[1].split('<td>')[1].split('<')[0]
long = section.splitlines()[0].split('<td>')[1].split('<')[0]
if len(languages) > 0:
# dupe check. For some reason the page has all
# languages twice
if languages[-1] != {'short': short, 'long': long}:
languages.append({'short': short, 'long': long})
else:
languages.append({'short': short, 'long': long})
print(f'FOUND {len(languages)} LANGUAGES')
return languages
language_list = get_languages()
@app.route("/", methods=["GET", "POST"])
def index():
transcript = ""
if request.method == "POST":
print("FORM DATA RECEIVED")
# set the language, use en-US by default
language = request.form.get('lang') or 'en-US'
print(f'SELECTED LANGUAGE: {language}')
if "file" not in request.files:
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
return redirect(request.url)
if file:
recognizer = sr.Recognizer()
audioFile = sr.AudioFile(file)
with audioFile as source:
data = recognizer.record(source)
recognizer.adjust_for_ambient_noise(source)
# change the line below
transcript = recognizer.recognize_google(data, language=language)
return render_template('index.html', transcript=transcript, language_list=language_list)
if __name__ == "__main__":
app.run(debug=True, threaded=True)
日志文件
FOUND 137 LANGUAGES
FORM DATA RECEIVED
SELECTED LANGUAGE: en-US
FOUND 137 LANGUAGES
[2021-08-08 13:37:18,913] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "app.py", line 58, in index
transcript = recognizer.recognize_google(data, language=language)
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 828, in recognize_google
convert_width=2 # audio samples must be 16-bit
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 445, in get_flac_data
flac_converter = get_flac_converter()
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 1183, in get_flac_converter
flac_converter = shutil_which("flac") # check for installed version first
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 1217, in shutil_which
for p in path.split(os.path.pathsep):
AttributeError: 'NoneType' object has no attribute 'split'
以上讨论积分少,无法评论。
不,这不是由于服务器响应时间。
你试过吗
transcript = recognizer.recognize_google(data)
改为
在本地服务器上,此 Python 应用程序运行良好,但是,当我在托管我网站的服务器上设置此 Python 应用程序时,index.html
加载,这意味着Python设置好了,但是当我点击Transcribe button
时,在我select the language
和.wav file
之后,出现错误:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
有没有人知道错误在哪里,如果不是错误,如何克服服务器响应。
from flask import Flask, render_template, request, redirect
import speech_recognition as sr
import requests
app = Flask(__name__)
def get_languages():
url = 'https://cloud.google.com/speech-to-text/docs/languages'
resp = requests.get(url)
start_text = ' <tbody class="list">\n'
end_text = ' </tbody>\n'
table = resp.text.split(start_text)[1].split(end_text)[0]
tr_start = ' <tr>\n'
sections = table.split(tr_start)[1:]
languages = []
for section in sections:
short = section.splitlines()[1].split('<td>')[1].split('<')[0]
long = section.splitlines()[0].split('<td>')[1].split('<')[0]
if len(languages) > 0:
# dupe check. For some reason the page has all
# languages twice
if languages[-1] != {'short': short, 'long': long}:
languages.append({'short': short, 'long': long})
else:
languages.append({'short': short, 'long': long})
print(f'FOUND {len(languages)} LANGUAGES')
return languages
language_list = get_languages()
@app.route("/", methods=["GET", "POST"])
def index():
transcript = ""
if request.method == "POST":
print("FORM DATA RECEIVED")
# set the language, use en-US by default
language = request.form.get('lang') or 'en-US'
print(f'SELECTED LANGUAGE: {language}')
if "file" not in request.files:
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
return redirect(request.url)
if file:
recognizer = sr.Recognizer()
audioFile = sr.AudioFile(file)
with audioFile as source:
data = recognizer.record(source)
recognizer.adjust_for_ambient_noise(source)
# change the line below
transcript = recognizer.recognize_google(data, language=language)
return render_template('index.html', transcript=transcript, language_list=language_list)
if __name__ == "__main__":
app.run(debug=True, threaded=True)
日志文件
FOUND 137 LANGUAGES
FORM DATA RECEIVED
SELECTED LANGUAGE: en-US
FOUND 137 LANGUAGES
[2021-08-08 13:37:18,913] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "app.py", line 58, in index
transcript = recognizer.recognize_google(data, language=language)
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 828, in recognize_google
convert_width=2 # audio samples must be 16-bit
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 445, in get_flac_data
flac_converter = get_flac_converter()
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 1183, in get_flac_converter
flac_converter = shutil_which("flac") # check for installed version first
File "/home/mysrv/virtualenv/transcriber/3.7/lib/python3.7/site-packages/speech_recognition/__init__.py", line 1217, in shutil_which
for p in path.split(os.path.pathsep):
AttributeError: 'NoneType' object has no attribute 'split'
以上讨论积分少,无法评论。 不,这不是由于服务器响应时间。
你试过吗
transcript = recognizer.recognize_google(data)
改为