def index(request): """Send back the Bluemix username and password in JSON format.""" return format_access( HttpResponse(json.JSONEncoder().encode({ "username": "******", "password": "******" })))
def forcealign(request): sample = request.GET.get('s', 'None') transcript = request.GET.get('t', 'None') if transcript == 'None' or sample == 'None': return format_access(HttpResponse("One or more arguments was not provided.", status=400)) return_text = "" #Create a temporary directory to house our data tmppath = "./webtmp/" transtmppath = tmppath + "transcript.txt" speechtmppath = tmppath + "audio.wav" if os.path.exists(tmppath): shutil.rmtree(tmppath) os.mkdir(tmppath) try: transfile = urllib2.urlopen(transcript) except Exception as e: return_text = "Error %r loading transcript file" % e return format_access(HttpResponse(return_text)) else: transtext = transfile.read().replace('\xe2\x80\x99', "'") with open(transtmppath, "w") as f: f.write(transtext) print sample, transtext #return_text = "You selected to transcribe the contents of %s with transcription %s: <br/>" % (sample, transtext) transfile.close() try: audio = urllib2.urlopen(sample) except Exception as e: return_text = "Error %r loading speech file" % e return format_access(HttpResponse(return_text)) else: with open(speechtmppath, "wb") as tmpfile: tmpfile.write(audio.read()) audio.close() words = pyforcealign.force_align(P2FA_DIR, speechtmppath, transtmppath) #Create a JSON string to return return_text = json.JSONEncoder().encode([[w.word, w.start, w.end] for w in words]) shutil.rmtree(tmppath) return format_access(HttpResponse(return_text))
def forcealign(request): sample = request.GET.get('s', 'None') transcript = request.GET.get('t', 'None') if transcript == 'None' or sample == 'None': return format_access(HttpResponse("One or more arguments was not provided.", status=400)) return_text = "" #Create a temporary directory to house our data tmppath = "./webtmp/" transtmppath = tmppath + "transcript.txt" speechtmppath = tmppath + "audio.wav" if os.path.exists(tmppath): shutil.rmtree(tmppath) os.mkdir(tmppath) try: transfile = urllib2.urlopen(transcript) except Exception as e: return_text = "Error %r loading transcript file" % e return format_access(HttpResponse(return_text)) else: transtext = transfile.read() with open(transtmppath, "w") as f: f.write(transtext) return_text = "You selected to transcribe the contents of %s with transcription %s: <br/>" % (sample, transtext) transfile.close() try: audio = urllib2.urlopen(sample) except Exception as e: return_text = "Error %r loading speech file" % e return format_access(HttpResponse(return_text)) else: with open(speechtmppath, "wb") as tmpfile: tmpfile.write(audio.read()) audio.close() words = pyforcealign.force_align(P2FA_DIR, speechtmppath, transtmppath) #Create a JSON string to return return_text = json.JSONEncoder().encode([[w.word, w.start, w.end] for w in words]) #pyforcealign.output_string(words) shutil.rmtree(tmppath) return format_access(HttpResponse(return_text))
def token(request): """Send back a username, password, and token string that the client app can use to authenticate a socket for live transcription.""" url = "https://stream.watsonplatform.net/authorization/api/v1/token?url=" + "https://stream.watsonplatform.net/speech-to-text/api" mytoken = requests.Session().request( 'GET', url, auth=("391a148f-6273-4a76-a90c-54a8a67a773c", "huQm9X34vL8b")) return format_access( HttpResponse( json.dumps({ "username": "******", "password": "******", "token": mytoken.text })))
def index(request): return format_access(HttpResponse("Hello, word! You're at the force alignment index."))