Ejemplo n.º 1
0
    def polly(self, Text, language, appToken):
        languages = {
            "English" : "Salli",
            "Korean" : "Seoyeon",
            "Japanese" : "Mizuki"
        }

        try:
            user = self.dbClass.getUser(appToken)
            #userId = self.utilClass.getStrUserId(user)
        except:
            Util.error_message("polly.py의 앱토큰 검증부분에서 에러가 발생하였습니다.")
            return HTTP_503_SERVICE_UNAVAILABLE, {
                "statusCode": 503,
                "error": "Bad Request",
                "message": "허용되지 않은 토큰 값입니다."
            }

        if language not in languages:
            Util.error_message("polly.py의 언어코드부분에서 에러가 발생하였습니다.")
            return HTTP_503_SERVICE_UNAVAILABLE, {
                "statusCode": 503,
                "error": "Bad Request",
                "message": "지원하지 않는 언어코드입니다."
            }

        Polly = client("polly", aws_access_key_id=shem_configs['aws_access_key_id'],
                        aws_secret_access_key=shem_configs['aws_secret_access_key'],
                                    region_name="ap-northeast-2")

        try:
            response = Polly.synthesize_speech(
                    Text=Text,
                    OutputFormat="mp3", VoiceId=languages[language])

            stream = response.get("AudioStream")

            with open('test1.mp3', 'wb') as f:
                data = stream.read()
                f.write(data)
            return HTTP_201_CREATED, {
                "statusCode": 201,
                "error": "Bad Request",
                "message": "변환이 완료되었습니다."
            }
        except:
            Util.error_message(tracback.format_exc())
            print(traceback.format_exc())
            return HTTP_500_INTERNAL_SERVER_ERROR, {
                "statusCode": 500,
                "error": "Bad Request",
                "message": "잘못된 접근입니다."
            }
            pass
Ejemplo n.º 2
0
    def transcribe(self, file, filename, token):
        # try:
        #     user = self.dbClass.getUser(token)
        #     userId = self.utilClass.getStrUserId(user)
        # except:
        #     return HTTP_503_SERVICE_UNAVAILABLE, {
        #         "statusCode": 503,
        #         "error": "Bad Request",
        #         "message": "허용되지 않은 토큰 값입니다."
        #     }  => 차후 유저 인증부분 구현

        s3 = boto3.client(
            's3',
            aws_access_key_id=shem_configs['aws_access_key_id'],
            aws_secret_access_key=shem_configs['aws_secret_access_key'],
            region_name='ap-southeast-1')
        s3.upload_file(f'temp/{filename}', 'assetdslab',
                       f'transcribe/{filename}')
        job_uri = f'https://assetdslab.s3.ap-northeast-1.amazonaws.com/transcribe/{filename}'

        Transcribe = boto3.client(
            'transcribe',
            aws_access_key_id=shem_configs['aws_access_key_id'],
            aws_secret_access_key=shem_configs['aws_secret_access_key'],
            region_name='ap-southeast-1')

        try:
            try:
                job_name = token + str(random.randint(0, 1000))
                Transcribe.start_transcription_job(
                    TranscriptionJobName=job_name,
                    Media={'MediaFileUri': job_uri},
                    MediaFormat=filename.split('.')[-1],
                    LanguageCode='ko-KR')  #ko-KR   en-US
            except:
                print(traceback.format_exc())
                Util.error_message(traceback.format_exc())
                try:
                    Transcribe.delete_transcription_job(
                        TranscriptionJobName=job_name)
                except:
                    pass
                return HTTP_503_SERVICE_UNAVAILABLE, {}
                pass

            while True:
                status = Transcribe.get_transcription_job(
                    TranscriptionJobName=job_name)
                if status['TranscriptionJob']['TranscriptionJobStatus'] in [
                        'COMPLETED', 'FAILED'
                ]:
                    break
                time.sleep(1)
            if status['TranscriptionJob'][
                    'TranscriptionJobStatus'] == 'COMPLETED':
                response = urllib.request.urlopen(
                    status['TranscriptionJob']['Transcript']
                    ['TranscriptFileUri'])
                data = json.loads(response.read())
                text = data['results']['transcripts'][0]['transcript']
                Transcribe.delete_transcription_job(
                    TranscriptionJobName=job_name)
            return HTTP_200_OK, {"transcrible-text": text}
        except:
            Transcribe.delete_transcription_job(TranscriptionJobName=job_name)
            print(traceback.format_exc())
            Util.error_message(traceback.format_exc())
            return HTTP_500_INTERNAL_SERVER_ERROR, {
                "statusCode": 500,
                "error": "Bad Request",
                "message": "잘못된 접근입니다."
            }
            pass