.mp3
hoặc .wav
.def lambda_handler(event, context): print("Event:", json.dumps(event)) bucket = event['Records'][0]['s3']['bucket']['name'] audio_key = unquote_plus(event['Records'][0]['s3']['object']['key']) job_name = f"transcription-{int(time.time())}" file_uri = f"s3://{bucket}/{audio_key}"
# Bắt đầu phiên âm transcribe.start_transcription_job( TranscriptionJobName=job_name, Media={'MediaFileUri': file_uri}, MediaFormat=audio_key.split('.')[-1], LanguageCode='en-US', OutputBucketName=bucket )
# Đợi phiên âm hoàn tất while True: status = transcribe.get_transcription_job(TranscriptionJobName=job_name) if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']: break time.sleep(5)
if status['TranscriptionJob']['TranscriptionJobStatus'] == 'FAILED': raise Exception("Transcription failed")
transcript_uri = status['TranscriptionJob']['Transcript']['TranscriptFileUri'] parsed = urlparse(transcript_uri) transcript_bucket = parsed.path.split('/')[1] transcript_key = '/'.join(parsed.path.split('/')[2:]) # Lấy file phiên âm obj = s3.get_object(Bucket=transcript_bucket, Key=transcript_key) transcript_data = json.loads(obj['Body'].read()) transcript_text = transcript_data['results']['transcripts'][0]['transcript']
print("--> Transcribed Text:\n", transcript_text)
# Phân tích cảm xúc với Comprehend sentiment = comprehend.detect_sentiment(Text=transcript_text[:5000], LanguageCode='en')
# Tóm tắt bản ghi với Claude (Bedrock) summary = summarize_with_claude(transcript_text)
return { "statusCode": 200, "body": { "summary": summary, "sentiment": sentiment, "transcription": transcript_text[:500], } }
BEDROCK_MODEL_ID = "eu.anthropic.claude-3-7-sonnet-20250219-v1:0"
def summarize_with_claude(text): body = { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 200, "top_k": 250, "stop_sequences": [], "temperature": 1, "top_p": 0.999, "messages": [ { "role": "user", "content": [ { "type": "text", "text": ( "Summarize this meeting or speech in clear bullet points, " "highlight any action items or topics discussed:\n\n" + text[:4000] ) } ] } ] }
response = bedrock.invoke_model( modelId=BEDROCK_MODEL_ID, body=json.dumps(body), contentType="application/json", accept="application/json", ) response_body = json.loads(response["body"].read()) return response_body.get("content", "No summary generated.")
Bước | Dịch vụ AWS | Mục đích |
---|---|---|
1 | Amazon S3 | Lưu trữ file âm thanh đầu vào .mp3 hoặc .wav |
2 | AWS Lambda | Tự động kích hoạt quy trình phiên âm và xử lý |
3 | Amazon Transcribe | Chuyển đổi âm thanh thành văn bản |
4 | Amazon Comprehend | Phân tích cảm xúc và khai thác chủ đề từ văn bản |
5 | Amazon Bedrock (Claude) | Tóm tắt nội dung, chỉ ra các điểm chính quan trọng |
6 | AWS Lambda | Trả về kết quả dưới dạng JSON qua API |
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "transcribe:StartTranscriptionJob", "transcribe:GetTranscriptionJob", "comprehend:DetectSentiment", "bedrock:InvokeModel" ], "Resource": "*" } ]}