Skip to content

Instantly share code, notes, and snippets.

@siennathesane
Last active July 22, 2017 07:34
Show Gist options
  • Save siennathesane/f774373471378f8aae81ad87552d83a2 to your computer and use it in GitHub Desktop.
Save siennathesane/f774373471378f8aae81ad87552d83a2 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import os
import subprocess
import sys
import threading
import time
import requests
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 google.cloud.speech import enums
from google.cloud.speech import types
from summa import summarizer
from werkzeug.utils import secure_filename
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
transcribed = ". ".join(extract_transcript(x) for x in res)
try:
print(summarizer.summarize(transcribed), file=sys.stderr)
except ZeroDivisionError:
return
def extract_transcript(x):
# run it through the grammar twice in case there are multiple choices.
return ask_lt(x.alternatives[0].transcript)
def ask_lt(s):
s = s.strip() # Believe this removes all whitespace from left and right
# Need to trim both left and right for elipses
while s[:3] == '...':
s = s[3:]
while s[-3:] == '...':
s = s[:-3]
params = {"text": s, "language": "en-US"}
resp = requests.get("http://127.0.0.1:8081/v2/check", params=params).json()
try:
# basically, if this is empty, it will throw an exception we can catch, and then break the loop.
mistake = resp["matches"][0]
_ = mistake["message"] # throws KeyError if no match
except (KeyError, IndexError):
return s
else:
local_context = mistake["context"]
original_string = s
print(local_context, file=sys.stderr)
fixed_string = ''.join([
original_string[:local_context["offset"]],
mistake["replacements"][0]["value"],
original_string[local_context["length"] + local_context["offset"]:],
])
return ask_lt(fixed_string)
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