Created
July 21, 2017 18:52
-
-
Save siennathesane/8f487a6ff3c781689799bb7ce1dec3f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import os | |
| import subprocess | |
| import sys | |
| import threading | |
| import time | |
| from flask import Flask, request, redirect, url_for, flash | |
| from flask_session import Session | |
| from google.cloud import speech | |
| from google.cloud import storage | |
| from werkzeug.utils import secure_filename | |
| from google.cloud.speech import enums | |
| from google.cloud.speech import types | |
| FFMPEG = os.path.join(os.path.dirname(__file__), "ffmpeg.exe") | |
| app = Flask(__name__) | |
| UPLOAD_FOLDER = '.' | |
| ALLOWED_EXTENSIONS = set(['avi', 'mp4', 'mkv']) | |
| SESSION_TYPE = 'filesystem' | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| app.secret_key = os.urandom(8) | |
| sess = Session(app) | |
| sess.init_app(app) | |
| def allowed_file(filename): | |
| return '.' in filename and \ | |
| filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
| @app.route('/', methods=['GET', 'POST']) | |
| def upload_file(): | |
| 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 file.filename == '': | |
| return redirect(request.url) | |
| if file and allowed_file(file.filename): | |
| filename = secure_filename(file.filename) | |
| file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
| process_file(filename) | |
| return redirect(url_for('upload_file', | |
| filename=filename)) | |
| return ''' | |
| <!doctype html> | |
| <title>Upload new File</title> | |
| <h1>Upload new File</h1> | |
| <form method=post enctype=multipart/form-data> | |
| <p><input type=file name=file> | |
| <input type=submit value=Upload> | |
| </form> | |
| ''' | |
| def process_file(filename): | |
| print("Processing file {0}".format(filename), file=sys.stderr) | |
| now = time.time().hex() | |
| local_filename = "audio-{0}.flac".format(now) | |
| command = [FFMPEG, "-i", filename, "-ab", "160k", "-acodec", "flac", "-ac", "1", "-ar", "16000", "-vn", | |
| local_filename] | |
| subprocess.call(" ".join(command), shell=True) | |
| storage_client = storage.Client(project=os.getenv("PROJECT_ID")) | |
| bucket = storage_client.get_bucket(os.getenv("BUCKET_NAME")) | |
| blobber = bucket.blob(local_filename) | |
| blobber.upload_from_filename(local_filename) | |
| os.remove(local_filename) | |
| transcript_thread = threading.Thread(target=get_transcript, args=[local_filename]) | |
| transcript_thread.start() | |
| return | |
| def get_transcript(uri): | |
| """Asynchronously transcribes the audio file specified by the gcs_uri.""" | |
| client = speech.SpeechClient() | |
| audio = types.RecognitionAudio(uri='gs://{0}/{1}'.format(os.getenv("BUCKET_NAME"), uri)) | |
| config = types.RecognitionConfig( | |
| encoding=enums.RecognitionConfig.AudioEncoding.FLAC, | |
| sample_rate_hertz=16000, | |
| language_code='en-US') | |
| operation = client.long_running_recognize(config, audio) | |
| retry_count = 100 | |
| while retry_count > 0 and not operation.done(): | |
| print("Waiting for {0} retries".format(retry_count), file=sys.stderr) | |
| retry_count -= 1 | |
| time.sleep(10) | |
| if not operation.done(): | |
| print('Operation not complete and retry limit reached.') | |
| return | |
| res = operation.result().results | |
| print(res, file=sys.stderr) | |
| print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr) | |
| # for alternative in alternatives: | |
| # print('Transcript: {}'.format(alternative.transcript), file=sys.stderr) | |
| # print('Confidence: {}'.format(alternative.confidence), file=sys.stderr) | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |
| # app.run(debug=False, port=int(os.getenv("PORT")), host="0.0.0.0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment