Best Python code snippet using localstack_python
test_transcribe_basics.py
Source:test_transcribe_basics.py
1# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.2# SPDX-License-Identifier: Apache-2.03"""4Unit tests for transcribe_basics.py functions.5"""6import boto37from botocore.exceptions import ClientError8import pytest9import transcribe_basics10def make_test_job(index):11 return {12 'name': f'test-job-{index}',13 'media_uri': 's3://example-bucket/test-media.mp3',14 'media_format': 'mp3',15 'language_code': 'en-US',16 'vocabulary_name': f'test-vocabulary-{index}'17 }18def make_test_vocabulary(index, phrases=False, table_uri=False):19 vocab = {'name': f'test-vocab-{index}', 'language_code': 'en-US'}20 if phrases:21 vocab['phrases'] = ['word', 'other-word', 'yet-another-word']22 if table_uri:23 vocab['table_uri'] = 's3://test-bucket/test-table.txt'24 return vocab25@pytest.mark.parametrize('error_code', [None, 'TestException'])26def test_start_job(make_stubber, error_code):27 transcribe_client = boto3.client('transcribe')28 transcribe_stubber = make_stubber(transcribe_client)29 job = make_test_job(1)30 transcribe_stubber.stub_start_transcription_job(job, error_code=error_code)31 if error_code is None:32 got_job = transcribe_basics.start_job(33 job['name'], job['media_uri'], job['media_format'],34 job['language_code'], transcribe_client, job['vocabulary_name'])35 assert got_job['TranscriptionJobName'] == job['name']36 else:37 with pytest.raises(ClientError) as exc_info:38 transcribe_basics.start_job(39 job['name'], job['media_uri'], job['media_format'],40 job['language_code'], transcribe_client, job['vocabulary_name'])41 assert exc_info.value.response['Error']['Code'] == error_code42@pytest.mark.parametrize('job_slice,error_code', [43 ((0, 10), None),44 ((0, 5), None),45 ((0, 10), 'TestException')])46def test_list_jobs(make_stubber, job_slice, error_code):47 transcribe_client = boto3.client('transcribe')48 transcribe_stubber = make_stubber(transcribe_client)49 job_filter = 'test-filter'50 jobs = [make_test_job(index) for index in range(0, 10)]51 transcribe_stubber.stub_list_transcription_jobs(52 job_filter, jobs, job_slice, error_code=error_code)53 if job_slice[1] < len(jobs):54 transcribe_stubber.stub_list_transcription_jobs(55 job_filter, jobs, [job_slice[1], len(jobs)], next_token='test-token',56 error_code=error_code)57 if error_code is None:58 got_jobs = transcribe_basics.list_jobs(job_filter, transcribe_client)59 assert [got['TranscriptionJobName'] for got in got_jobs] == \60 [had['name'] for had in jobs]61 else:62 with pytest.raises(ClientError) as exc_info:63 transcribe_basics.list_jobs(job_filter, transcribe_client)64 assert exc_info.value.response['Error']['Code'] == error_code65@pytest.mark.parametrize('error_code', [None, 'TestException'])66def test_get_job(make_stubber, error_code):67 transcribe_client = boto3.client('transcribe')68 transcribe_stubber = make_stubber(transcribe_client)69 job = make_test_job(1)70 transcribe_stubber.stub_get_transcription_job(job, error_code=error_code)71 if error_code is None:72 got_job = transcribe_basics.get_job(job['name'], transcribe_client)73 assert got_job['TranscriptionJobName'] == job['name']74 else:75 with pytest.raises(ClientError) as exc_info:76 transcribe_basics.get_job(job['name'], transcribe_client)77 assert exc_info.value.response['Error']['Code'] == error_code78@pytest.mark.parametrize('error_code', [None, 'TestException'])79def test_delete_job(make_stubber, error_code):80 transcribe_client = boto3.client('transcribe')81 transcribe_stubber = make_stubber(transcribe_client)82 job_name = 'test-job'83 transcribe_stubber.stub_delete_transcription_job(job_name, error_code=error_code)84 if error_code is None:85 transcribe_basics.delete_job(job_name, transcribe_client)86 else:87 with pytest.raises(ClientError) as exc_info:88 transcribe_basics.delete_job(job_name, transcribe_client)89 assert exc_info.value.response['Error']['Code'] == error_code90@pytest.mark.parametrize('phrases,error_code', [91 (True, None),92 (False, None),93 (True, 'TestException')])94def test_create_vocabulary(make_stubber, phrases, error_code):95 transcribe_client = boto3.client('transcribe')96 transcribe_stubber = make_stubber(transcribe_client)97 vocab = make_test_vocabulary(1, phrases=phrases, table_uri=not phrases)98 transcribe_stubber.stub_create_vocabulary(vocab, error_code=error_code)99 if error_code is None:100 got_vocab = transcribe_basics.create_vocabulary(101 vocab['name'], vocab['language_code'], transcribe_client,102 phrases=vocab.get('phrases'), table_uri=vocab.get('table_uri'))103 assert got_vocab['VocabularyName'] == vocab['name']104 else:105 with pytest.raises(ClientError) as exc_info:106 transcribe_basics.create_vocabulary(107 vocab['name'], vocab['language_code'], transcribe_client,108 phrases=vocab.get('phrases'), table_uri=vocab.get('table_uri'))109 assert exc_info.value.response['Error']['Code'] == error_code110@pytest.mark.parametrize('vocab_slice,error_code', [111 ((0, 10), None),112 ((0, 5), None),113 ((0, 10), 'TestException')])114def test_list_vocabularies(make_stubber, vocab_slice, error_code):115 transcribe_client = boto3.client('transcribe')116 transcribe_stubber = make_stubber(transcribe_client)117 vocab_filter = 'test-filter'118 vocabs = [make_test_vocabulary(index) for index in range(0, 10)]119 transcribe_stubber.stub_list_vocabularies(120 vocab_filter, vocabs, vocab_slice, error_code=error_code)121 if vocab_slice[1] < len(vocabs):122 transcribe_stubber.stub_list_vocabularies(123 vocab_filter, vocabs, [vocab_slice[1], len(vocabs)],124 next_token='test-token', error_code=error_code)125 if error_code is None:126 got_vocabs = transcribe_basics.list_vocabularies(127 vocab_filter, transcribe_client)128 assert [got['VocabularyName'] for got in got_vocabs] == \129 [had['name'] for had in vocabs]130 else:131 with pytest.raises(ClientError) as exc_info:132 transcribe_basics.list_vocabularies(vocab_filter, transcribe_client)133 assert exc_info.value.response['Error']['Code'] == error_code134@pytest.mark.parametrize('error_code', [None, 'TestException'])135def test_get_vocabulary(make_stubber, error_code):136 transcribe_client = boto3.client('transcribe')137 transcribe_stubber = make_stubber(transcribe_client)138 vocab = make_test_vocabulary(1)139 transcribe_stubber.stub_get_vocabulary(vocab, error_code=error_code)140 if error_code is None:141 transcribe_basics.get_vocabulary(vocab['name'], transcribe_client)142 else:143 with pytest.raises(ClientError) as exc_info:144 transcribe_basics.get_vocabulary(vocab['name'], transcribe_client)145 assert exc_info.value.response['Error']['Code'] == error_code146@pytest.mark.parametrize('phrases,error_code', [147 (True, None),148 (False, None),149 (True, 'TestException')])150def test_update_vocabulary(make_stubber, phrases, error_code):151 transcribe_client = boto3.client('transcribe')152 transcribe_stubber = make_stubber(transcribe_client)153 vocab = make_test_vocabulary(1, phrases=phrases, table_uri=not phrases)154 transcribe_stubber.stub_update_vocabulary(vocab, error_code=error_code)155 if error_code is None:156 transcribe_basics.update_vocabulary(157 vocab['name'], vocab['language_code'], transcribe_client,158 phrases=vocab.get('phrases'), table_uri=vocab.get('table_uri'))159 else:160 with pytest.raises(ClientError) as exc_info:161 transcribe_basics.update_vocabulary(162 vocab['name'], vocab['language_code'], transcribe_client,163 phrases=vocab.get('phrases'), table_uri=vocab.get('table_uri'))164 assert exc_info.value.response['Error']['Code'] == error_code165@pytest.mark.parametrize('error_code', [None, 'TestException'])166def test_delete_vocabulary(make_stubber, error_code):167 transcribe_client = boto3.client('transcribe')168 transcribe_stubber = make_stubber(transcribe_client)169 vocab_name = 'test-vocab'170 transcribe_stubber.stub_delete_vocabulary(vocab_name, error_code=error_code)171 if error_code is None:172 transcribe_basics.delete_vocabulary(vocab_name, transcribe_client)173 else:174 with pytest.raises(ClientError) as exc_info:175 transcribe_basics.delete_vocabulary(vocab_name, transcribe_client)...
views.py
Source:views.py
1from django.shortcuts import render2from django.shortcuts import get_object_or_4043from django.http import HttpResponseRedirect4from django.http import HttpResponse5from django.template import loader6from django.http import Http4047from django.urls import reverse8from django.views import generic9from django.shortcuts import redirect10from django.core.files.storage import FileSystemStorage11from audio.models import File12from .models import Transcribe13from django.utils import timezone14from google.cloud import speech15from google.cloud import storage16import os17# bucket_name = 'kemitraan-telkom-1550985641715.appspot.com' #pak ikhsan18bucket_name = 'quantum-engine-248003.appspot.com' #pak ikhsan19# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "sspk-owner.json" #pak ikhsan20os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials/My_First_Project-2c46ff0c115f.json" #pak ikhsan21def index(request):22 file_list = File.objects.all()23 context = {'file_list': file_list}24 return render(request, 'transcribe/index.html', context)25def edit(request, id):26 file = get_object_or_404(File, id=id)27 28 try:29 transcribe = Transcribe.objects.get(File_id=id)30 except Transcribe.DoesNotExist:31 transcribe = Transcribe.objects.create(File_id=file.id)32 context = {'file': file, 'transcribe': transcribe}33 return render(request, 'transcribe/edit.html', context)34 35def update(request, id):36 file = get_object_or_404(File, id=request.POST['id'])37 file.save()38 transcribe = get_object_or_404(Transcribe, File_id=request.POST['id'])39 # transcribe.action_text = request.POST['action_text']40 # transcribe.enthusiasm_text = request.POST['enthusiasm_text']41 # transcribe.focus_text = request.POST['focus_text']42 # transcribe.imagine_text = request.POST['imagine_text']43 # transcribe.integrity_text = request.POST['integrity_text']44 # transcribe.smart_text = request.POST['smart_text']45 # transcribe.solid_text = request.POST['solid_text']46 # transcribe.speed_text = request.POST['speed_text']47 # transcribe.totality_text = request.POST['totality_text']48 transcribe.verbatim_text = request.POST['verbatim_text']49 transcribe.updated_at = timezone.now()50 transcribe.save()51 return HttpResponseRedirect(reverse('transcribe:edit', args=(file.id,)))52def save(request):53 return redirect('/transcribe/')54def delete(request, id):55 return HttpResponse("You're voting on audio delete %s." % id)56def process(request, id):57 file = get_object_or_404(File, id=id)58 TranscribeThread.run(file)59 return HttpResponseRedirect(reverse('transcribe:edit', args=(id,)))60import threading61class TranscribeThread(threading.Thread):62 def run(file):63 try:64 hasil_transcribe = transcriptingstreo('gs://'+bucket_name+'/'+file.name)65 except:66 hasil_transcribe = transcriptingmono('gs://'+bucket_name+'/'+file.name)67 try:68 transcribe = Transcribe.objects.get(File_id=file.id)69 except Transcribe.DoesNotExist:70 transcribe = Transcribe.objects.create(File_id=file.id)71 transcribe.raw = hasil_transcribe72 transcribe.verbatim_text = hasil_transcribe73 transcribe.save()74def transcriptingstreo(filename):75 ##global hasiltranscript76 hasiltranscript=''77 #client=speech.SpeechClient()78 #audio=speech.types.RecognitionAudio(uri=filename)79 #config=speech.types.RecognitionConfig(80 # encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,language_code='id-ID',audio_channel_count=2,81 #enable_separate_recognition_per_channel=True82 #)83 operation=speech.SpeechClient().long_running_recognize(84 config=speech.types.RecognitionConfig(encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,85 language_code='id-ID',audio_channel_count=2,enable_separate_recognition_per_channel=True)86 ,audio=speech.types.RecognitionAudio(uri=filename))87 print('Waiting for transcriptingstreo to complete...')88 #response = operation.result(timeout=10000)89 for result in operation.result(timeout=10000).results:90 hasiltranscript += str(result.alternatives[0].transcript)91 return hasiltranscript92def transcriptingmono(filename):93 #global hasiltranscript94 #client = speech.SpeechClient()95 #audio = speech.types.RecognitionAudio(uri=filename)96 #config = speech.types.RecognitionConfig(97 # encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16, language_code='id-ID'98 #)99 #operation = client.long_running_recognize(config=config, audio=audio)100 hasiltranscript = ''101 operation = speech.SpeechClient().long_running_recognize(102 config=speech.types.RecognitionConfig(encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,103 language_code='id-ID')104 , audio=speech.types.RecognitionAudio(uri=filename))105 print('Waiting for transcriptingmono to complete...')106 response = operation.result(timeout=10000)107 for result in response.results:108 hasiltranscript += str(result.alternatives[0].transcript)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!