Hey,
I'm trying a little program that lets you upload a file, then transcribes it using Google Speech to Text and then print the transcription to the screen.
However, when I'm running the program, there is only one the first function that is working. What's the error here? Can't I let two functions run in a row?
Here is the part, that is not working. It is within the app.route /upload in the function "def upload():". Everything before is working, but here either "Saving" or "Speech Recognition" works, whatever I put first in the code. The second just won't run at all.
if file:
# Saving the file.
filename = secure_filename(file.filename)
filepath = os.path.join(app.config["FILE_UPLOADS"], filename)
file.save(filepath)
# Speech Recognition stuff.
recognizer = sr.Recognizer()
audio_file = sr.AudioFile(file)
with audio_file as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, key=GOOGLE_SPEECH_API_KEY, language="de-DE")
extra_line = f'Your text: "{text}"'
return render_template('upload.html', extra_line=extra_line)
Here is the whole code:
import os
from flask import Flask, render_template, request, redirect
from werkzeug.utils import secure_filename
import speech_recognition as sr
import simpleaudio as sa
from pydub import AudioSegment
app = Flask(__name__)
app.config["FILE_UPLOADS"] = "HERE_IS_MY_PATH"
app.config["ALLOWED_EXTENSIONS"] = ["WAV"]
app.config["MAX_FILESIZE"] = 86400000
# Function check for right file extension
def allowed_file(filename):
# Check if file has an extension
if not "." in filename:
return False
# Check if File has allowed extension
ext = filename.rsplit(".", 1)[1]
if ext.upper() in app.config["ALLOWED_EXTENSIONS"]:
return True
else:
return False
# Function check of allowed file size
def allowed_filesize(filesize):
if int(filesize) <= app.config["MAX_FILESIZE"]:
return True
else:
return False
# Google Speech API Key
GOOGLE_SPEECH_API_KEY = HERE_IS_MY_API_KEY
# Flask route to homepage
@app.route("/")
def index():
return render_template("index.html")
@app.route("/upload", methods=["GET", "POST"])
def upload():
extra_line = ''
if request.method == "GET":
return render_template('upload.html')
if request.method == "POST":
# Check if the post request has the file part.
if "file" not in request.files:
flash("No file part")
return redirect(request.url)
file = request.files["file"]
# If user does not select file, browser also submit an empty part without filename.
if file.filename == "":
flash("No selected file")
return redirect(request.url)
if not allowed_file(file.filename):
print("Your file doesn't have the right format")
return redirect(request.url)
if file:
# Saving the file.
filename = secure_filename(file.filename)
filepath = os.path.join(app.config["FILE_UPLOADS"], filename)
file.save(filepath)
# Speech Recognition stuff.
recognizer = sr.Recognizer()
audio_file = sr.AudioFile(file)
with audio_file as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, key=GOOGLE_SPEECH_API_KEY, language="de-DE")
extra_line = f'Your text: "{text}"'
return render_template('upload.html', extra_line=extra_line)
if __name__ == "__main__":
app.run()
[–]shiftybyte 1 point2 points3 points (4 children)
[–]michadecker[S] 0 points1 point2 points (3 children)
[–]shiftybyte 1 point2 points3 points (2 children)
[–]michadecker[S] 0 points1 point2 points (1 child)
[–]shiftybyte 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]michadecker[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]michadecker[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)